Various types of locks for thread synchronization.
Reader / writer lock (odp_rwlock_t)
A reader/writer lock allows multiple simultaneous readers but only one writer at a time. A thread that wants write access will have to wait until there are no threads that want read access. This causes a risk for starvation. The trylock variants can be used to avoid blocking when the lock is not immediately available.
Recursive reader/writer lock (odp_rwlock_recursive_t)
This is recursive version of the reader/writer lock. A thread can read- or write-acquire a recursive read-write lock multiple times without a deadlock. To release the lock, the thread must unlock it the same number of times. Recursion is supported only for a pure series of read or write lock calls. Read and write lock calls must not be mixed when recursing.
For example, these are supported...
- read_lock(); read_lock(); read_unlock(); read_unlock();
- write_lock(); write_lock(); write_unlock(); write_unlock();
... but this is not supported.
- read_lock(); write_lock(); write_unlock(); read_unlock();
The trylock variants can be used to avoid blocking when the lock is not immediately available.
Spin lock (odp_spinlock_t)
Spinlock simply re-tries to acquire the lock as long as takes to succeed. Spinlock is not fair since some threads may succeed more often than others.
Recursive spin lock (odp_spinlock_recursive_t)
This is recursive version of the spin lock. A thread can acquire the lock multiple times without a deadlock. To release the lock, the thread must unlock it the same number of times.
Ticket lock (odp_ticketlock_t)
Acquiring a ticket lock happens in two phases. First the threads takes a ticket. Second it waits (spins) until it is its turn. Ticket locks are believed to be more fair than spin locks. Ticket locks shall not be used if a thread may be preempted, since other threads cannot acquire the lock while the thread in turn is stalled.
◆ odp_rwlock_t
◆ odp_rwlock_recursive_t
◆ odp_spinlock_t
◆ odp_spinlock_recursive_t
◆ odp_ticketlock_t
◆ odp_rwlock_init()
Initialize a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_read_lock()
Acquire read permission on a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_read_trylock()
Try to acquire read permission to a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Return values
-
0 | Lock was not available for read access |
!0 | Read access to lock acquired |
◆ odp_rwlock_read_unlock()
Release read permission on a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_write_lock()
Acquire write permission on a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_write_trylock()
Try to acquire write permission to a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Return values
-
0 | Lock was not available for write access |
!0 | Write access to lock acquired |
◆ odp_rwlock_write_unlock()
Release write permission on a reader/writer lock.
- Parameters
-
rwlock | Pointer to a reader/writer lock |
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_recursive_init()
◆ odp_rwlock_recursive_read_lock()
Acquire recursive rwlock for reading.
This call allows the thread to acquire the same lock multiple times for reading. The lock cannot be acquired for writing while holding it for reading.
- Parameters
-
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_recursive_read_trylock()
Try to acquire recursive rwlock for reading.
- Parameters
-
- Return values
-
0 | Lock was not available for read access |
!0 | Read access to lock acquired |
◆ odp_rwlock_recursive_read_unlock()
Release recursive rwlock after reading.
- Parameters
-
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_recursive_write_lock()
Acquire recursive rwlock for writing.
This call allows the thread to acquire the same lock multiple times for writing. The lock cannot be acquired for reading while holding it for writing.
- Parameters
-
- Examples
- odp_lock_perf.c.
◆ odp_rwlock_recursive_write_trylock()
Try to acquire recursive rwlock for writing.
- Parameters
-
- Return values
-
0 | Lock was not available for write access |
!0 | Write access to lock acquired |
◆ odp_rwlock_recursive_write_unlock()
Release recursive rwlock after writing.
- Parameters
-
- Examples
- odp_lock_perf.c.
◆ odp_spinlock_init()
◆ odp_spinlock_lock()
◆ odp_spinlock_trylock()
Try to acquire spin lock.
- Parameters
-
splock | Pointer to a spin lock |
- Return values
-
0 | lock not acquired |
!0 | lock acquired |
◆ odp_spinlock_unlock()
◆ odp_spinlock_is_locked()
Check if spin lock is busy (locked).
- Parameters
-
splock | Pointer to a spin lock |
- Return values
-
1 | lock busy (locked) |
0 | lock not busy. |
◆ odp_spinlock_recursive_init()
◆ odp_spinlock_recursive_lock()
◆ odp_spinlock_recursive_trylock()
Try to acquire recursive spinlock.
- Parameters
-
- Return values
-
1 | lock acquired |
0 | lock not acquired |
◆ odp_spinlock_recursive_unlock()
◆ odp_spinlock_recursive_is_locked()
Check if recursive spinlock is locked.
- Parameters
-
- Return values
-
1 | lock is locked |
0 | lock is not locked |
◆ odp_ticketlock_init()
◆ odp_ticketlock_lock()
◆ odp_ticketlock_trylock()
Try to acquire ticket lock.
- Parameters
-
tklock | Pointer to a ticket lock |
- Return values
-
1 | lock acquired |
0 | lock not acquired |
- Examples
- odp_dma_perf.c.
◆ odp_ticketlock_unlock()
◆ odp_ticketlock_is_locked()
Check if ticket lock is locked.
- Parameters
-
tklock | Pointer to a ticket lock |
- Return values
-
1 | the lock is busy (locked) |
0 | the lock is available (unlocked) |