1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * RTE Read-Write Locks
13 * This file defines an API for read-write locks. The lock is used to
14 * protect data that allows multiple readers in parallel, but only
15 * one writer. All readers are blocked until the writer is finished
24 #include <rte_common.h>
25 #include <rte_atomic.h>
26 #include <rte_pause.h>
29 * The rte_rwlock_t type.
31 * cnt is -1 when write lock is held, and > 0 when read locks are held.
34 volatile int32_t cnt; /**< -1 when W lock held, > 0 when R locks held. */
38 * A static rwlock initializer.
40 #define RTE_RWLOCK_INITIALIZER { 0 }
43 * Initialize the rwlock to an unlocked state.
46 * A pointer to the rwlock structure.
49 rte_rwlock_init(rte_rwlock_t *rwl)
55 * Take a read lock. Loop until the lock is held.
58 * A pointer to a rwlock structure.
61 rte_rwlock_read_lock(rte_rwlock_t *rwl)
66 while (success == 0) {
67 x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
68 /* write lock is held */
73 success = __atomic_compare_exchange_n(&rwl->cnt, &x, x + 1, 1,
74 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
80 * @b EXPERIMENTAL: this API may change without prior notice.
82 * try to take a read lock.
85 * A pointer to a rwlock structure.
87 * - zero if the lock is successfully taken
88 * - -EBUSY if lock could not be acquired for reading because a
89 * writer holds the lock
93 rte_rwlock_read_trylock(rte_rwlock_t *rwl)
98 while (success == 0) {
99 x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
100 /* write lock is held */
103 success = __atomic_compare_exchange_n(&rwl->cnt, &x, x + 1, 1,
104 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
111 * Release a read lock.
114 * A pointer to the rwlock structure.
117 rte_rwlock_read_unlock(rte_rwlock_t *rwl)
119 __atomic_fetch_sub(&rwl->cnt, 1, __ATOMIC_RELEASE);
124 * @b EXPERIMENTAL: this API may change without prior notice.
126 * try to take a write lock.
129 * A pointer to a rwlock structure.
131 * - zero if the lock is successfully taken
132 * - -EBUSY if lock could not be acquired for writing because
133 * it was already locked for reading or writing
137 rte_rwlock_write_trylock(rte_rwlock_t *rwl)
141 x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
142 if (x != 0 || __atomic_compare_exchange_n(&rwl->cnt, &x, -1, 1,
143 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) == 0)
150 * Take a write lock. Loop until the lock is held.
153 * A pointer to a rwlock structure.
156 rte_rwlock_write_lock(rte_rwlock_t *rwl)
161 while (success == 0) {
162 x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
168 success = __atomic_compare_exchange_n(&rwl->cnt, &x, -1, 1,
169 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
174 * Release a write lock.
177 * A pointer to a rwlock structure.
180 rte_rwlock_write_unlock(rte_rwlock_t *rwl)
182 __atomic_store_n(&rwl->cnt, 0, __ATOMIC_RELEASE);
186 * Try to execute critical section in a hardware memory transaction, if it
187 * fails or not available take a read lock
189 * NOTE: An attempt to perform a HW I/O operation inside a hardware memory
190 * transaction always aborts the transaction since the CPU is not able to
191 * roll-back should the transaction fail. Therefore, hardware transactional
192 * locks are not advised to be used around rte_eth_rx_burst() and
193 * rte_eth_tx_burst() calls.
196 * A pointer to a rwlock structure.
199 rte_rwlock_read_lock_tm(rte_rwlock_t *rwl);
202 * Commit hardware memory transaction or release the read lock if the lock is used as a fall-back
205 * A pointer to the rwlock structure.
208 rte_rwlock_read_unlock_tm(rte_rwlock_t *rwl);
211 * Try to execute critical section in a hardware memory transaction, if it
212 * fails or not available take a write lock
214 * NOTE: An attempt to perform a HW I/O operation inside a hardware memory
215 * transaction always aborts the transaction since the CPU is not able to
216 * roll-back should the transaction fail. Therefore, hardware transactional
217 * locks are not advised to be used around rte_eth_rx_burst() and
218 * rte_eth_tx_burst() calls.
221 * A pointer to a rwlock structure.
224 rte_rwlock_write_lock_tm(rte_rwlock_t *rwl);
227 * Commit hardware memory transaction or release the write lock if the lock is used as a fall-back
230 * A pointer to a rwlock structure.
233 rte_rwlock_write_unlock_tm(rte_rwlock_t *rwl);
239 #endif /* _RTE_RWLOCK_H_ */