ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
abtd_spinlock.h
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #ifndef ABTD_SPINLOCK_H_INCLUDED
7 #define ABTD_SPINLOCK_H_INCLUDED
8 
9 typedef struct ABTD_spinlock {
10  ABTD_atomic_bool val;
11 } ABTD_spinlock;
12 
13 #define ABTD_SPINLOCK_STATIC_INITIALIZER() \
14  { \
15  ABTD_ATOMIC_BOOL_STATIC_INITIALIZER(0) \
16  }
17 
18 static inline ABT_bool ABTD_spinlock_is_locked(const ABTD_spinlock *p_lock)
19 {
20  return ABTD_atomic_acquire_load_bool(&p_lock->val);
21 }
22 
23 static inline void ABTD_spinlock_clear(ABTD_spinlock *p_lock)
24 {
25  ABTD_atomic_relaxed_clear_bool(&p_lock->val);
26 }
27 
28 static inline void ABTD_spinlock_acquire(ABTD_spinlock *p_lock)
29 {
30  while (ABTD_atomic_test_and_set_bool(&p_lock->val)) {
31  while (ABTD_spinlock_is_locked(p_lock) != ABT_FALSE)
32  ;
33  }
34 }
35 
36 /* Return ABT_FALSE if the lock is acquired. */
37 static inline ABT_bool ABTD_spinlock_try_acquire(ABTD_spinlock *p_lock)
38 {
39  return ABTD_atomic_test_and_set_bool(&p_lock->val) ? ABT_TRUE : ABT_FALSE;
40 }
41 
42 static inline void ABTD_spinlock_release(ABTD_spinlock *p_lock)
43 {
44  ABTD_atomic_release_clear_bool(&p_lock->val);
45 }
46 
47 #endif /* ABTD_SPINLOCK_H_INCLUDED */
ABT_bool
int ABT_bool
Boolean type.
Definition: abt.h:1043
ABT_TRUE
#define ABT_TRUE
True constant for ABT_bool.
Definition: abt.h:784
ABT_FALSE
#define ABT_FALSE
False constant for ABT_bool.
Definition: abt.h:786