fix spinlock (#14068)
Costa Tsaousis committed
Nov 30, 2022 at 14:23 UTC
904b3264eec749e74d3fa3bbf6bda46763503b4e
1 file changed
+4
-7
libnetdata/locks/locks.c
+4
-7
@@ -287,17 +287,15 @@ void netdata_spinlock_init(SPINLOCK *spinlock) {
287
}
288
289
void netdata_spinlock_lock(SPINLOCK *spinlock) {
290
- netdata_thread_disable_cancelability();
291
-
290
static const struct timespec ns = { .tv_sec = 0, .tv_nsec = 1 };
293
- bool expected = false, desired = true;
291
+
292
+ netdata_thread_disable_cancelability();
293
294
for(int i = 1;
295
__atomic_load_n(&spinlock->locked, __ATOMIC_RELAXED) ||
297
- !__atomic_compare_exchange_n(&spinlock->locked, &expected, desired, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)
296
+ __atomic_test_and_set(&spinlock->locked, __ATOMIC_ACQUIRE)
297
; i++
298
) {
300
-
299
if(unlikely(i == 8)) {
300
i = 0;
301
nanosleep(&ns, NULL);
@@ -307,8 +305,7 @@ void netdata_spinlock_lock(SPINLOCK *spinlock) {
305
}
306
307
void netdata_spinlock_unlock(SPINLOCK *spinlock) {
310
- __atomic_store_n(&spinlock->locked, false, __ATOMIC_RELEASE);
311
-
308
+ __atomic_clear(&spinlock->locked, __ATOMIC_RELEASE);
309
netdata_thread_enable_cancelability();
310
}
311