|
| 1 | +/* |
| 2 | +* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2025. ALL RIGHTS RESERVED. |
| 3 | +* |
| 4 | +* See file LICENSE for terms. |
| 5 | +*/ |
| 6 | + |
| 7 | +#ifndef UCS_RWLOCK_H |
| 8 | +#define UCS_RWLOCK_H |
| 9 | + |
| 10 | +#include <ucs/arch/cpu.h> |
| 11 | +#include <ucs/debug/assert.h> |
| 12 | +#include <ucs/sys/compiler_def.h> |
| 13 | + |
| 14 | +/** |
| 15 | + * The ucs_rw_spinlock_t type. |
| 16 | + * |
| 17 | + * Readers increment the counter by UCS_RWLOCK_READ (4) |
| 18 | + * Writers set the UCS_RWLOCK_WRITE bit when lock is held |
| 19 | + * and set the UCS_RWLOCK_WAIT bit while waiting. |
| 20 | + * UCS_RWLOCK_WAIT bit is meant for all subsequent reader |
| 21 | + * to let any writer go first to avoid write starvation. |
| 22 | + * |
| 23 | + * 31 2 1 0 |
| 24 | + * +-------------------+-+-+ |
| 25 | + * | readers | | | |
| 26 | + * +-------------------+-+-+ |
| 27 | + * ^ ^ |
| 28 | + * | | |
| 29 | + * WRITE: lock held ----/ | |
| 30 | + * WAIT: writer pending --/ |
| 31 | + */ |
| 32 | + |
| 33 | +#define UCS_RWLOCK_WAIT UCS_BIT(0) /* Writer is waiting */ |
| 34 | +#define UCS_RWLOCK_WRITE UCS_BIT(1) /* Writer has the lock */ |
| 35 | +#define UCS_RWLOCK_MASK (UCS_RWLOCK_WAIT | UCS_RWLOCK_WRITE) |
| 36 | +#define UCS_RWLOCK_READ UCS_BIT(2) /* Reader increment */ |
| 37 | + |
| 38 | +#define UCS_RWLOCK_STATIC_INITIALIZER {0} |
| 39 | + |
| 40 | + |
| 41 | +#define ucs_rw_spinlock_assert(_lock, _cond, _desc) \ |
| 42 | + ucs_assertv((_lock)->state _cond, "lock=%p " _desc " state=0x%x%s%s", \ |
| 43 | + (_lock), (_lock)->state, \ |
| 44 | + (_lock)->state & UCS_RWLOCK_WAIT ? " WAIT" : "", \ |
| 45 | + (_lock)->state & UCS_RWLOCK_WRITE ? " WRITE" : "") |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * Reader-writer spin lock. |
| 50 | + */ |
| 51 | +typedef struct { |
| 52 | + uint32_t state; |
| 53 | +} ucs_rw_spinlock_t; |
| 54 | + |
| 55 | + |
| 56 | +static UCS_F_ALWAYS_INLINE void |
| 57 | +ucs_rw_spinlock_read_lock(ucs_rw_spinlock_t *lock) |
| 58 | +{ |
| 59 | + uint32_t x; |
| 60 | + |
| 61 | + for (;;) { |
| 62 | + while (__atomic_load_n(&lock->state, __ATOMIC_RELAXED) & |
| 63 | + UCS_RWLOCK_MASK) { |
| 64 | + ucs_cpu_relax(); |
| 65 | + } |
| 66 | + |
| 67 | + x = __atomic_fetch_add(&lock->state, UCS_RWLOCK_READ, __ATOMIC_ACQUIRE); |
| 68 | + if (!(x & UCS_RWLOCK_MASK)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + __atomic_fetch_sub(&lock->state, UCS_RWLOCK_READ, __ATOMIC_RELAXED); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +static UCS_F_ALWAYS_INLINE void |
| 78 | +ucs_rw_spinlock_read_unlock(ucs_rw_spinlock_t *lock) |
| 79 | +{ |
| 80 | + ucs_rw_spinlock_assert(lock, >= UCS_RWLOCK_READ, "read underrun"); |
| 81 | + __atomic_fetch_sub(&lock->state, UCS_RWLOCK_READ, __ATOMIC_RELEASE); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +static UCS_F_ALWAYS_INLINE void |
| 86 | +ucs_rw_spinlock_write_lock(ucs_rw_spinlock_t *lock) |
| 87 | +{ |
| 88 | + uint32_t x; |
| 89 | + |
| 90 | + x = __atomic_load_n(&lock->state, __ATOMIC_RELAXED); |
| 91 | + if (ucs_unlikely(x > UCS_RWLOCK_WAIT)) { |
| 92 | + goto wait; |
| 93 | + } |
| 94 | + |
| 95 | + for (;;) { |
| 96 | + if (__atomic_compare_exchange_n(&lock->state, &x, UCS_RWLOCK_WRITE, 0, |
| 97 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | +wait: |
| 102 | + if (ucs_likely(!(x & UCS_RWLOCK_WAIT))) { |
| 103 | + __atomic_fetch_or(&lock->state, UCS_RWLOCK_WAIT, __ATOMIC_RELAXED); |
| 104 | + } |
| 105 | + |
| 106 | + while ((x = __atomic_load_n(&lock->state, __ATOMIC_RELAXED)) > |
| 107 | + UCS_RWLOCK_WAIT) { |
| 108 | + ucs_cpu_relax(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +static UCS_F_ALWAYS_INLINE int |
| 115 | +ucs_rw_spinlock_write_trylock(ucs_rw_spinlock_t *lock) |
| 116 | +{ |
| 117 | + uint32_t x; |
| 118 | + |
| 119 | + x = __atomic_load_n(&lock->state, __ATOMIC_RELAXED); |
| 120 | + if ((x < UCS_RWLOCK_WRITE) && |
| 121 | + (__atomic_compare_exchange_n(&lock->state, &x, x | UCS_RWLOCK_WRITE, 1, |
| 122 | + __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))) { |
| 123 | + return 1; |
| 124 | + } |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +static UCS_F_ALWAYS_INLINE void |
| 131 | +ucs_rw_spinlock_write_unlock(ucs_rw_spinlock_t *lock) |
| 132 | +{ |
| 133 | + ucs_rw_spinlock_assert(lock, >= UCS_RWLOCK_WRITE, "write underrun"); |
| 134 | + __atomic_fetch_sub(&lock->state, UCS_RWLOCK_WRITE, __ATOMIC_RELEASE); |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +static UCS_F_ALWAYS_INLINE void ucs_rw_spinlock_init(ucs_rw_spinlock_t *lock) |
| 139 | +{ |
| 140 | + lock->state = 0; |
| 141 | +} |
| 142 | + |
| 143 | + |
| 144 | +static UCS_F_ALWAYS_INLINE void ucs_rw_spinlock_cleanup(ucs_rw_spinlock_t *lock) |
| 145 | +{ |
| 146 | + ucs_rw_spinlock_assert(lock, == 0, "not released"); |
| 147 | +} |
| 148 | + |
| 149 | +#endif |
0 commit comments