EPICS Base  7.0.8.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
epicsAtomicDefault.h
1 
2 /*************************************************************************\
3 * Copyright (c) 2011 LANS LLC, as Operator of
4 * Los Alamos National Laboratory.
5 * Copyright (c) 2011 UChicago Argonne LLC, as Operator of Argonne
6 * National Laboratory.
7 * SPDX-License-Identifier: EPICS
8 * EPICS BASE is distributed subject to a Software License Agreement found
9 * in file LICENSE that is included with this distribution.
10 \*************************************************************************/
11 
12 /*
13  * Author Jeffrey O. Hill
15  */
16 
17 #ifndef epicsAtomicDefault_h
18 #define epicsAtomicDefault_h
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /*
25  * struct EpicsAtomicLockKey;
26  * LIBCOM_API void epicsAtomicReadMemoryBarrier ();
27  * LIBCOM_API void epicsAtomicWriteMemoryBarrier ();
28  * LIBCOM_API void epicsAtomicLock ( struct EpicsAtomicLockKey * );
29  * LIBCOM_API void epicsAtomicUnock ( struct EpicsAtomicLockKey * );
30  */
31 
32 /*
33  * incr
34  */
35 #ifndef EPICS_ATOMIC_INCR_INTT
36 EPICS_ATOMIC_INLINE int epicsAtomicIncrIntT ( int * pTarget )
37 {
39  int result;
40 
41  epicsAtomicLock ( & key );
42  result = ++(*pTarget);
43  epicsAtomicUnlock ( & key );
44  return result;
45 }
46 #endif
47 
48 #ifndef EPICS_ATOMIC_INCR_SIZET
49 EPICS_ATOMIC_INLINE size_t epicsAtomicIncrSizeT ( size_t * pTarget )
50 {
52  size_t result;
53 
54  epicsAtomicLock ( & key );
55  result = ++(*pTarget);
56  epicsAtomicUnlock ( & key );
57  return result;
58 }
59 #endif
60 
61 /*
62  * decr
63  */
64 #ifndef EPICS_ATOMIC_DECR_INTT
65 EPICS_ATOMIC_INLINE int epicsAtomicDecrIntT ( int * pTarget )
66 {
68  int result;
69 
70  epicsAtomicLock ( & key );
71  result = --(*pTarget);
72  epicsAtomicUnlock ( & key );
73  return result;
74 }
75 #endif
76 
77 #ifndef EPICS_ATOMIC_DECR_SIZET
78 EPICS_ATOMIC_INLINE size_t epicsAtomicDecrSizeT ( size_t * pTarget )
79 {
81  size_t result;
82 
83  epicsAtomicLock ( & key );
84  result = --(*pTarget);
85  epicsAtomicUnlock ( & key );
86  return result;
87 }
88 #endif
89 
90 /*
91  * add/sub
92  */
93 #ifndef EPICS_ATOMIC_ADD_INTT
94 EPICS_ATOMIC_INLINE int epicsAtomicAddIntT ( int * pTarget, int delta )
95 {
97  int result;
98 
99  epicsAtomicLock ( & key );
100  result = *pTarget += delta;
101  epicsAtomicUnlock ( & key );
102  return result;
103 }
104 #endif
105 
106 #ifndef EPICS_ATOMIC_ADD_SIZET
107 EPICS_ATOMIC_INLINE size_t epicsAtomicAddSizeT ( size_t * pTarget, size_t delta )
108 {
109  EpicsAtomicLockKey key;
110  size_t result;
111 
112  epicsAtomicLock ( & key );
113  result = *pTarget += delta;
114  epicsAtomicUnlock ( & key );
115  return result;
116 }
117 #endif
118 
119 #ifndef EPICS_ATOMIC_SUB_SIZET
120 EPICS_ATOMIC_INLINE size_t epicsAtomicSubSizeT ( size_t * pTarget, size_t delta )
121 {
122  EpicsAtomicLockKey key;
123  size_t result;
124 
125  epicsAtomicLock ( & key );
126  result = *pTarget -= delta;
127  epicsAtomicUnlock ( & key );
128  return result;
129 }
130 #endif
131 
132 /*
133  * set
134  */
135 #ifndef EPICS_ATOMIC_SET_INTT
136 EPICS_ATOMIC_INLINE void epicsAtomicSetIntT ( int * pTarget, int newVal )
137 {
138  *pTarget = newVal;
140 }
141 #endif
142 
143 #ifndef EPICS_ATOMIC_SET_SIZET
144 EPICS_ATOMIC_INLINE void epicsAtomicSetSizeT ( size_t * pTarget, size_t newVal )
145 {
146  *pTarget = newVal;
148 }
149 #endif
150 
151 #ifndef EPICS_ATOMIC_SET_PTRT
152 EPICS_ATOMIC_INLINE void epicsAtomicSetPtrT ( EpicsAtomicPtrT * pTarget,
153  EpicsAtomicPtrT newVal )
154 {
155  *pTarget = newVal;
157 }
158 #endif
159 
160 /*
161  * get
162  */
163 #ifndef EPICS_ATOMIC_GET_INTT
164 EPICS_ATOMIC_INLINE int epicsAtomicGetIntT ( const int * pTarget )
165 {
167  return *pTarget;
168 }
169 #endif
170 
171 #ifndef EPICS_ATOMIC_GET_SIZET
172 EPICS_ATOMIC_INLINE size_t epicsAtomicGetSizeT ( const size_t * pTarget )
173 {
175  return *pTarget;
176 }
177 #endif
178 
179 #ifndef EPICS_ATOMIC_GET_PTRT
180 EPICS_ATOMIC_INLINE EpicsAtomicPtrT
182 {
184  return *pTarget;
185 }
186 #endif
187 
188 /*
189  * cmp and swap
190  */
191 #ifndef EPICS_ATOMIC_CAS_INTT
192 EPICS_ATOMIC_INLINE int epicsAtomicCmpAndSwapIntT ( int * pTarget, int oldval, int newval )
193 {
194  EpicsAtomicLockKey key;
195  int cur;
196 
197  epicsAtomicLock ( & key );
198  cur = *pTarget;
199  if ( cur == oldval ) {
200  *pTarget = newval;
201  }
202  epicsAtomicUnlock ( & key );
203  return cur;
204 }
205 #endif
206 
207 #ifndef EPICS_ATOMIC_CAS_SIZET
208 EPICS_ATOMIC_INLINE size_t epicsAtomicCmpAndSwapSizeT ( size_t * pTarget,
209  size_t oldval, size_t newval )
210 {
211  EpicsAtomicLockKey key;
212  size_t cur;
213 
214  epicsAtomicLock ( & key );
215  cur = *pTarget;
216  if ( cur == oldval ) {
217  *pTarget = newval;
218  }
219  epicsAtomicUnlock ( & key );
220  return cur;
221 }
222 #endif
223 
224 #ifndef EPICS_ATOMIC_CAS_PTRT
226  EpicsAtomicPtrT * pTarget,
227  EpicsAtomicPtrT oldval, EpicsAtomicPtrT newval )
228 {
229  EpicsAtomicLockKey key;
230  EpicsAtomicPtrT cur;
231 
232  epicsAtomicLock ( & key );
233  cur = *pTarget;
234  if ( cur == oldval ) {
235  *pTarget = newval;
236  }
237  epicsAtomicUnlock ( & key );
238  return cur;
239 }
240 #endif
241 
242 #ifdef __cplusplus
243 } /* end of extern "C" */
244 #endif
245 
246 #endif /* epicsAtomicDefault_h */
247 
248 
void * EpicsAtomicPtrT
Definition: epicsAtomic.h:49
EPICS_ATOMIC_INLINE EpicsAtomicPtrT epicsAtomicGetPtrT(const EpicsAtomicPtrT *pTarget)
atomically load and return pointer value
EPICS_ATOMIC_INLINE int epicsAtomicIncrIntT(int *pTarget)
atomic increment on int value
EPICS_ATOMIC_INLINE int epicsAtomicDecrIntT(int *pTarget)
atomic decrement on int value
EPICS_ATOMIC_INLINE int epicsAtomicGetIntT(const int *pTarget)
atomically load and return int value
EPICS_ATOMIC_INLINE size_t epicsAtomicAddSizeT(size_t *pTarget, size_t delta)
atomic addition on size_t value
EPICS_ATOMIC_INLINE void epicsAtomicSetIntT(int *pTarget, int newValue)
atomically assign int value to variable
EPICS_ATOMIC_INLINE int epicsAtomicAddIntT(int *pTarget, int delta)
atomic addition on int value
EPICS_ATOMIC_INLINE int epicsAtomicCmpAndSwapIntT(int *pTarget, int oldVal, int newVal)
atomically compare int value with expected and if equal swap with new value
EPICS_ATOMIC_INLINE size_t epicsAtomicSubSizeT(size_t *pTarget, size_t delta)
atomic subtraction on size_t value
EPICS_ATOMIC_INLINE void epicsAtomicSetSizeT(size_t *pTarget, size_t newValue)
atomically assign size_t value to variable
EPICS_ATOMIC_INLINE void epicsAtomicSetPtrT(EpicsAtomicPtrT *pTarget, EpicsAtomicPtrT newValue)
atomically assign pointer value to variable
EPICS_ATOMIC_INLINE void epicsAtomicReadMemoryBarrier(void)
load target into cache
EPICS_ATOMIC_INLINE size_t epicsAtomicCmpAndSwapSizeT(size_t *pTarget, size_t oldVal, size_t newVal)
atomically compare size_t value with expected and if equal swap with new value
EPICS_ATOMIC_INLINE size_t epicsAtomicGetSizeT(const size_t *pTarget)
atomically load and return size_t value
EPICS_ATOMIC_INLINE void epicsAtomicWriteMemoryBarrier(void)
push cache version of target into target
EPICS_ATOMIC_INLINE size_t epicsAtomicIncrSizeT(size_t *pTarget)
atomic increment on size_t value
EPICS_ATOMIC_INLINE EpicsAtomicPtrT epicsAtomicCmpAndSwapPtrT(EpicsAtomicPtrT *pTarget, EpicsAtomicPtrT oldVal, EpicsAtomicPtrT newVal)
atomically compare int value with expected and if equal swap with new value
EPICS_ATOMIC_INLINE size_t epicsAtomicDecrSizeT(size_t *pTarget)
atomic decrement on size_t value