summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCosta Tsaousis <costa@netdata.cloud>2022-11-30 14:23:21 +0200
committerGitHub <noreply@github.com>2022-11-30 14:23:21 +0200
commit904b3264eec749e74d3fa3bbf6bda46763503b4e (patch)
tree653259366e4e2189db78d263273de952c5be32f0
parent3772a02457e15792817770ba112603905674b429 (diff)
fix spinlock (#14068)
-rw-r--r--libnetdata/locks/locks.c11
1 files changed, 4 insertions, 7 deletions
diff --git a/libnetdata/locks/locks.c b/libnetdata/locks/locks.c
index 9bf56e9f58..f7191be52c 100644
--- a/libnetdata/locks/locks.c
+++ b/libnetdata/locks/locks.c
@@ -287,17 +287,15 @@ void netdata_spinlock_init(SPINLOCK *spinlock) {
}
void netdata_spinlock_lock(SPINLOCK *spinlock) {
- netdata_thread_disable_cancelability();
-
static const struct timespec ns = { .tv_sec = 0, .tv_nsec = 1 };
- bool expected = false, desired = true;
+
+ netdata_thread_disable_cancelability();
for(int i = 1;
__atomic_load_n(&spinlock->locked, __ATOMIC_RELAXED) ||
- !__atomic_compare_exchange_n(&spinlock->locked, &expected, desired, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)
+ __atomic_test_and_set(&spinlock->locked, __ATOMIC_ACQUIRE)
; i++
) {
-
if(unlikely(i == 8)) {
i = 0;
nanosleep(&ns, NULL);
@@ -307,8 +305,7 @@ void netdata_spinlock_lock(SPINLOCK *spinlock) {
}
void netdata_spinlock_unlock(SPINLOCK *spinlock) {
- __atomic_store_n(&spinlock->locked, false, __ATOMIC_RELEASE);
-
+ __atomic_clear(&spinlock->locked, __ATOMIC_RELEASE);
netdata_thread_enable_cancelability();
}