@cryptotaxi247 / netdata-1 / commits / 33e74ca6f

add fast path to waitq (#20198)

Costa Tsaousis committed Apr 28, 2025 at 16:14 UTC 33e74ca6f03cbb2ff79431c2804f79b2515bdfeb
1 file changed +16
src/libnetdata/locks/waitq.c
+16
@@ -58,6 +58,14 @@ static ALWAYS_INLINE bool clear_our_priority(WAITQ *waitq, uint64_t our_order) {
58 }
59
60 ALWAYS_INLINE bool waitq_try_acquire_with_trace(WAITQ *waitq, WAITQ_PRIORITY priority, const char *func __maybe_unused) {
61 + // Fast path for no contention - try to get the lock immediately without a sequence number
62 + if (__atomic_load_n(&waitq->current_priority, __ATOMIC_RELAXED) == NO_PRIORITY &&
63 + spinlock_trylock(&waitq->spinlock)) {
64 + waitq->writer = gettid_cached();
65 + return true;
66 + }
67 +
68 + // Normal path with queuing if contention exists
69 uint64_t our_order = get_our_order(waitq, priority);
70
71 bool rc = write_our_priority(waitq, our_order) && spinlock_trylock(&waitq->spinlock);
@@ -70,6 +78,14 @@ ALWAYS_INLINE bool waitq_try_acquire_with_trace(WAITQ *waitq, WAITQ_PRIORITY pri
78 }
79
80 ALWAYS_INLINE void waitq_acquire_with_trace(WAITQ *waitq, WAITQ_PRIORITY priority, const char *func) {
81 + // Fast path for no contention - try to get the lock immediately without a sequence number
82 + if (__atomic_load_n(&waitq->current_priority, __ATOMIC_RELAXED) == NO_PRIORITY &&
83 + spinlock_trylock(&waitq->spinlock)) {
84 + waitq->writer = gettid_cached();
85 + return;
86 + }
87 +
88 + // Normal path with queuing if contention exists
89 uint64_t our_order = get_our_order(waitq, priority);
90
91 size_t spins = 0;