aboutsummaryrefslogtreecommitdiff
path: root/src/cas_spinlock.h
blob: 1f25ebf7f7e2a2d2e32eebf53ab14c85a6a9d3e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//#define CAS_LOCK_USLEEP 4
#define CAS_LOCK_USLEEP 200



#define USE_UNLIKELY
#ifdef USE_UNLIKELY
#include "unlikely.h"
static inline void lock(volatile int *aexclusion) {
  if (likely(__sync_lock_test_and_set(aexclusion, 1) == 0)) {
      return; 
  }

  while (__sync_lock_test_and_set(aexclusion, 1)) {
    while (*aexclusion) {
      usleep(CAS_LOCK_USLEEP);
    }
  }
}

#else

static inline void lock(volatile int *aexclusion) {
  while (__sync_lock_test_and_set(aexclusion, 1))
    while (*aexclusion)
      usleep(CAS_LOCK_USLEEP);
}

#endif


static inline void unlock(volatile int *aexclusion) {
  __sync_lock_release(aexclusion); 
}