move qa/ to tools/
[archipelago] / xseg / xq / xq_lock.h
1 #ifndef _XQ_LOCK_H
2 #define _XQ_LOCK_H
3
4 #define MFENCE() __sync_synchronize()
5 #define BARRIER() __asm__ __volatile__ ("" : "memory")
6 #define __pause() __asm__ __volatile__ ("pause\n");
7 #undef __pause
8 #define __pause()
9
10 struct xq_lock {
11         long lock;
12         unsigned long serial;
13 } __attribute__ ((aligned (32))); /* support up to 128bit longs */
14
15 static inline unsigned long xq_acquire(struct xq_lock *lock, unsigned long nr)
16 {
17         unsigned long __serial;
18         for (;;) {
19                 for (; *(volatile unsigned long *)(&lock->lock); )
20                         __pause();
21
22                 if (!__sync_fetch_and_sub(&lock->lock, 1))
23                         break;
24         }
25
26         __serial = lock->serial;
27         lock->serial += nr;
28         return __serial;
29 }
30
31 static inline void xq_release(struct xq_lock *lock)
32 {
33         lock->lock = 0L;
34 }
35
36 #endif