ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
sync.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <stdatomic.h>
14 #include <stdbool.h>
15 #include <stdint.h>
16 
17 #if defined(__x86_64__) || defined(__i386__)
18 #include <immintrin.h>
19 #endif
20 
22 typedef atomic_flag spinlock_t;
23 
28 #define spin_init(lck_p) spin_unlock(lck_p)
29 
34 #if defined(__x86_64__) || defined(__i386__)
35 #define spin_pause() _mm_pause()
36 #else
37 #define spin_pause()
38 #endif
39 
44 #define spin_lock(lck_p) \
45  __extension__({ \
46  while(atomic_flag_test_and_set_explicit((lck_p), memory_order_acquire)) \
47  spin_pause(); \
48  })
49 
55 #define spin_trylock(lck_p) !atomic_flag_test_and_set_explicit((lck_p), memory_order_acquire)
56 
61 #define spin_unlock(lck_p) atomic_flag_clear_explicit((lck_p), memory_order_release)
62 
63 extern bool sync_thread_barrier(void);
bool sync_thread_barrier(void)
Synchronizes threads on a barrier.
Definition: sync.c:19
atomic_flag spinlock_t
The type of a spinlock, an efficient lock primitive in contended scenarios.
Definition: sync.h:22