1 #ifndef HALIDE_SCOPED_SPIN_LOCK_H
2 #define HALIDE_SCOPED_SPIN_LOCK_H
3 
4 namespace Halide {
5 namespace Runtime {
6 namespace Internal {
7 
8 // An RAII spin lock.
9 struct ScopedSpinLock {
10     // Note that __atomic_test_and_set() requires use of a char (or bool)
11     typedef char AtomicFlag;
12 
13     volatile AtomicFlag *const flag;
14 
ScopedSpinLockScopedSpinLock15     ALWAYS_INLINE ScopedSpinLock(volatile AtomicFlag *flag)
16         : flag(flag) {
17         while (__atomic_test_and_set(flag, __ATOMIC_ACQUIRE)) {
18             // nothing
19         }
20     }
21 
~ScopedSpinLockScopedSpinLock22     ALWAYS_INLINE ~ScopedSpinLock() {
23         __atomic_clear(flag, __ATOMIC_RELEASE);
24     }
25 };
26 
27 }  // namespace Internal
28 }  // namespace Runtime
29 }  // namespace Halide
30 
31 #endif
32