Skip to content

Commit 7fd130b

Browse files
Fix non-atomic load used in trylock and remove some redundant fences.
1 parent b1171cb commit 7fd130b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

include/qt_atomics.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,19 @@ extern pthread_mutexattr_t _fastlock_attr;
149149
# define QTHREAD_TRYLOCK_INIT(x) { (x).u = 0; }
150150
# define QTHREAD_TRYLOCK_INIT_PTR(x) { (x)->u = 0; }
151151
# define QTHREAD_TRYLOCK_LOCK(x) { uint32_t val = qthread_incr(&(x)->s.users, 1); \
152-
while (val != (x)->s.ticket) SPINLOCK_BODY(); \
153-
THREAD_FENCE_MEM_ACQUIRE; /* spin waiting for my turn */ }
152+
while (val != atomic_load_explicit((_Atomic uint32_t *)&(x)->s.ticket, memory_order_acquire)) SPINLOCK_BODY();}
154153
# define QTHREAD_TRYLOCK_UNLOCK(x) do { COMPILER_FENCE; \
155-
THREAD_FENCE_MEM_RELEASE; \
154+
THREAD_FENCE_MEM_RELEASE; \
156155
qthread_incr(&(x)->s.ticket, 1); /* allow next guy's turn */ \
157156
} while (0)
158157
# define QTHREAD_TRYLOCK_DESTROY(x)
159158
# define QTHREAD_TRYLOCK_DESTROY_PTR(x)
160159

161160
static inline int QTHREAD_TRYLOCK_TRY(qt_spin_trylock_t *x)
162161
{
163-
qt_spin_trylock_t newcmp, cmp = *(x);
162+
qt_spin_trylock_t newcmp, cmp;
163+
uint64_t tmp = atomic_load_explicit((_Atomic uint64_t*)x, memory_order_relaxed);
164+
cmp = *(qt_spin_trylock_t*)&tmp;
164165

165166
if (cmp.s.users != cmp.s.ticket) {
166167
return 0;
@@ -170,7 +171,6 @@ static inline int QTHREAD_TRYLOCK_TRY(qt_spin_trylock_t *x)
170171
newcmp.s.users = newcmp.s.ticket + 1;
171172

172173
if(qthread_cas(&(x->u), cmp.u, newcmp.u) == cmp.u) {
173-
THREAD_FENCE_MEM_ACQUIRE;
174174
return 1;
175175
}
176176
return 0;

0 commit comments

Comments
 (0)