xrootd
XrdSysAtomics.hh
Go to the documentation of this file.
1 #ifndef _XRDSYSATOMICS_
2 #define _XRDSYSATOMICS_
3 /******************************************************************************/
4 /* */
5 /* X r d S y s A t o m i c s . h h */
6 /* */
7 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /******************************************************************************/
12 
13 /* The following instruction acronyms are used:
14  AtomicCAS() -> Compare And [if equal] Set
15  AtomicDTZ() -> Decrease To Zero
16  AtomicFAZ() -> Fetch And Zero
17  AtomicISM() -> Increase and Set Maximum
18 */
19 
20 #ifdef HAVE_ATOMICS
21 #define AtomicBeg(Mtx)
22 #define AtomicEnd(Mtx)
23 #define AtomicAdd(x, y) __sync_fetch_and_add(&x, y)
24 #define AtomicCAS(x, y, z) __sync_bool_compare_and_swap(&x, y, z)
25 #define AtomicDec(x) __sync_fetch_and_sub(&x, 1)
26 #define AtomicDTZ(x) if (!(__sync_fetch_and_sub(&x, 1))) AtomicFAZ(x)
27 #define AtomicFAZ(x) __sync_fetch_and_and(&x, 0)
28 #define AtomicGet(x) __sync_fetch_and_or(&x, 0)
29 #define AtomicInc(x) __sync_fetch_and_add(&x, 1)
30 #define AtomicISM(x, y) if (AtomicGet(y) <= AtomicInc(x)) AtomicCAS(y, y, x)
31 #define AtomicSub(x, y) __sync_fetch_and_sub(&x, y)
32 #else
33 #define AtomicBeg(Mtx) Mtx.Lock()
34 #define AtomicEnd(Mtx) Mtx.UnLock()
35 #define AtomicAdd(x, y) x += y
36 #define AtomicCAS(x, y, z) if (x == y) x = z
37 #define AtomicDTZ(x) if (!(x--)) x = 0
38 #define AtomicDec(x) x--
39 #define AtomicFAZ(x) x; x = 0
40 #define AtomicGet(x) x
41 #define AtomicInc(x) x++
42 #define AtomicISM(x, y) if (y == x++) y = x
43 #define AtomicSub(x, y) x -= y
44 #endif
45 #endif