---
name: "rte_seqlock.h"
section: "3"
platform: "linux"
package: "dpdk-doc"
version: "24.11.4-0+deb13u1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/rte_seqlock.h"
---

# rte\_seqlock.h(3)

## NAME

rte\_seqlock.h

## SYNOPSIS

`#include <stdbool.h>`\
 `#include <stdint.h>`\
 `#include <rte_atomic.h>`\
 `#include <rte_branch_prediction.h>`\
 `#include <rte_seqcount.h>`\
 `#include <rte_spinlock.h>`\

### Data Structures

struct **rte\_seqlock\_t**\

### Macros

\#define **RTE\_SEQLOCK\_INITIALIZER**\

### Functions

static void **rte\_seqlock\_init** (**rte\_seqlock\_t** \*seqlock)\
 static uint32\_t **rte\_seqlock\_read\_begin** (const **rte\_seqlock\_t** \*seqlock)\
 static bool **rte\_seqlock\_read\_retry** (const **rte\_seqlock\_t** \*seqlock, uint32\_t begin\_sn)\
 static void **rte\_seqlock\_write\_lock** (**rte\_seqlock\_t** \*seqlock)\
 static void **rte\_seqlock\_write\_unlock** (**rte\_seqlock\_t** \*seqlock)\

## Detailed Description

RTE Seqlock

A sequence lock (seqlock) is a synchronization primitive allowing multiple, parallel, readers to efficiently and safely (i.e., in a data-race free manner) access lock-protected data. The RTE seqlock permits multiple writers as well. A spinlock is used for writer-writer synchronization.

A reader never blocks a writer. Very high frequency writes may prevent readers from making progress.

A seqlock is not preemption-safe on the writer side. If a writer is preempted, it may block readers until the writer thread is allowed to continue. Heavy computations should be kept out of the writer-side critical section, to avoid delaying readers.

Seqlocks are useful for data which are read by many cores, at a high frequency, and relatively infrequently written to.

One way to think about seqlocks is that they provide means to perform atomic operations on objects larger than what the native machine instructions allow for.

To avoid resource reclamation issues, the data protected by a seqlock should typically be kept self-contained (e.g., no pointers to mutable, dynamically allocated data).

Example usage:

```
#define MAX_Y_LEN 16
// Application-defined example data structure, protected by a seqlock.
struct config {
        rte_seqlock_t lock;
        int param_x;
        char param_y[MAX_Y_LEN];
};
// Accessor function for reading config fields.
void
config_read(const struct config *config, int *param_x, char *param_y)
{
        uint32_t sn;
        do {
                sn = rte_seqlock_read_begin(&config->lock);
                // Loads may be atomic or non-atomic, as in this example.
                *param_x = config->param_x;
                strcpy(param_y, config->param_y);
                // An alternative to an immediate retry is to abort and
                // try again at some later time, assuming progress is
                // possible without the data.
        } while (rte_seqlock_read_retry(&config->lock, sn));
}
// Accessor function for writing config fields.
void
config_update(struct config *config, int param_x, const char *param_y)
{
        rte_seqlock_write_lock(&config->lock);
        // Stores may be atomic or non-atomic, as in this example.
        config->param_x = param_x;
        strcpy(config->param_y, param_y);
        rte_seqlock_write_unlock(&config->lock);
}
```

In case there is only a single writer, or writer-writer serialization is provided by other means, the use of sequence lock (i.e., **rte\_seqlock\_t**) can be replaced with the use of the 'raw' **rte\_seqcount\_t** type instead.

**See also**

https://en.wikipedia.org/wiki/Seqlock.

Definition in file **rte\_seqlock.h**.

## Macro Definition Documentation

### #define RTE\_SEQLOCK\_INITIALIZER

**Value:**.PP

```
    {                           \
        .count = RTE_SEQCOUNT_INITIALIZER,       \
        .lock = RTE_SPINLOCK_INITIALIZER     \
    }
```

A static seqlock initializer.

Definition at line **109** of file **rte\_seqlock.h**.

## Function Documentation

### static void rte\_seqlock\_init (rte\_seqlock\_t \* seqlock)` [inline]`, ` [static]`

Initialize the seqlock.

This function initializes the seqlock, and leaves the writer-side spinlock unlocked.

**Parameters**

*seqlock* A pointer to the seqlock.

Definition at line **125** of file **rte\_seqlock.h**.

### static uint32\_t rte\_seqlock\_read\_begin (const rte\_seqlock\_t \* seqlock)` [inline]`, ` [static]`

Begin a read-side critical section.

See **rte\_seqcount\_read\_retry()** for details.

**Parameters**

*seqlock* A pointer to the seqlock.

**Returns**

The seqlock sequence number for this critical section, to later be passed to **rte\_seqlock\_read\_retry()**.

**See also**

**rte\_seqlock\_read\_retry()**

**rte\_seqcount\_read\_retry()**

Definition at line **146** of file **rte\_seqlock.h**.

### static bool rte\_seqlock\_read\_retry (const rte\_seqlock\_t \* seqlock, uint32\_t begin\_sn)` [inline]`, ` [static]`

End a read-side critical section.

See **rte\_seqcount\_read\_retry()** for details.

**Parameters**

*seqlock* A pointer to the seqlock. \
 *begin\_sn* The seqlock sequence number returned by **rte\_seqlock\_read\_begin()**.

**Returns**

true or false, if the just-read seqlock-protected data was inconsistent or consistent, respectively, at the time it was read.

**See also**

**rte\_seqlock\_read\_begin()**

Definition at line **168** of file **rte\_seqlock.h**.

### static void rte\_seqlock\_write\_lock (rte\_seqlock\_t \* seqlock)` [inline]`, ` [static]`

Begin a write-side critical section.

A call to this function acquires the write lock associated `seqlock`, and marks the beginning of a write-side critical section.

After having called this function, the caller may go on to modify (both read and write) the protected data, in an atomic or non-atomic manner.

After the necessary updates have been performed, the application calls **rte\_seqlock\_write\_unlock()**.

This function is not preemption-safe in the sense that preemption of the calling thread may block reader progress until the writer thread is rescheduled.

Unlike **rte\_seqlock\_read\_begin()**, each call made to **rte\_seqlock\_write\_lock()** must be matched with an unlock call.

**Parameters**

*seqlock* A pointer to the seqlock.

**See also**

**rte\_seqlock\_write\_unlock()**

Definition at line **199** of file **rte\_seqlock.h**.

### static void rte\_seqlock\_write\_unlock (rte\_seqlock\_t \* seqlock)` [inline]`, ` [static]`

End a write-side critical section.

A call to this function marks the end of the write-side critical section, for `seqlock`. After this call has been made, the protected data may no longer be modified.

**Parameters**

*seqlock* A pointer to the seqlock.

**See also**

**rte\_seqlock\_write\_lock()**

Definition at line **221** of file **rte\_seqlock.h**.

## Author

Generated automatically by Doxygen for DPDK from the source code.
