| 1 |
/*=========================================================================== |
| 2 |
* |
| 3 |
* PUBLIC DOMAIN NOTICE |
| 4 |
* National Center for Biotechnology Information |
| 5 |
* |
| 6 |
* This software/database is a "United States Government Work" under the |
| 7 |
* terms of the United States Copyright Act. It was written as part of |
| 8 |
* the author's official duties as a United States Government employee and |
| 9 |
* thus cannot be copyrighted. This software/database is freely available |
| 10 |
* to the public for use. The National Library of Medicine and the U.S. |
| 11 |
* Government have not placed any restriction on its use or reproduction. |
| 12 |
* |
| 13 |
* Although all reasonable efforts have been taken to ensure the accuracy |
| 14 |
* and reliability of the software and data, the NLM and the U.S. |
| 15 |
* Government do not and cannot warrant the performance or results that |
| 16 |
* may be obtained by using this software or data. The NLM and the U.S. |
| 17 |
* Government disclaim all warranties, express or implied, including |
| 18 |
* warranties of performance, merchantability or fitness for any particular |
| 19 |
* purpose. |
| 20 |
* |
| 21 |
* Please cite the author in any work or product based on this material. |
| 22 |
* |
| 23 |
* =========================================================================== |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifndef _h_atomic32_ |
| 28 |
#define _h_atomic32_ |
| 29 |
|
| 30 |
#ifdef __cplusplus |
| 31 |
extern "C" { |
| 32 |
#endif |
| 33 |
|
| 34 |
typedef int atomic32_t; |
| 35 |
|
| 36 |
/* int atomic32_read ( const atomic32_t *v ); */ |
| 37 |
#define atomic32_read( v ) \ |
| 38 |
( * ( v ) ) |
| 39 |
|
| 40 |
/* void atomic32_set ( atomic32_t *v, int i ); */ |
| 41 |
#define atomic32_set( v, i ) \ |
| 42 |
( ( void ) ( * ( v ) = ( i ) ) ) |
| 43 |
|
| 44 |
/* add to v -> counter and return the prior value */ |
| 45 |
static __inline__ int atomic32_read_and_add ( atomic32_t *v, int i ) |
| 46 |
{ |
| 47 |
int rtn = * v; |
| 48 |
* v += i; |
| 49 |
return rtn; |
| 50 |
} |
| 51 |
|
| 52 |
/* if no read is needed, define the least expensive atomic add */ |
| 53 |
#define atomic32_add( v, i ) \ |
| 54 |
atomic32_read_and_add ( v, i ) |
| 55 |
|
| 56 |
/* add to v -> counter and return the result */ |
| 57 |
static __inline__ int atomic32_add_and_read ( atomic32_t *v, int i ) |
| 58 |
{ |
| 59 |
int rtn = * v += i; |
| 60 |
return rtn; |
| 61 |
} |
| 62 |
|
| 63 |
/* just don't try to find out what the result was */ |
| 64 |
static __inline__ void atomic32_inc ( atomic32_t *v ) |
| 65 |
{ |
| 66 |
++ v [ 0 ]; |
| 67 |
} |
| 68 |
|
| 69 |
static __inline__ void atomic32_dec ( atomic32_t *v ) |
| 70 |
{ |
| 71 |
-- v [ 0 ]; |
| 72 |
} |
| 73 |
|
| 74 |
/* decrement by one and test result for 0 */ |
| 75 |
static __inline__ int atomic32_dec_and_test ( atomic32_t *v ) |
| 76 |
{ |
| 77 |
int rtn = -- v [ 0 ] == 0; |
| 78 |
return rtn; |
| 79 |
} |
| 80 |
|
| 81 |
/* when atomic32_dec_and_test uses predecrement, you want |
| 82 |
postincrement to this function. so it isn't very useful */ |
| 83 |
static __inline__ int atomic32_inc_and_test ( atomic32_t *v ) |
| 84 |
{ |
| 85 |
int rtn = ++ v [ 0 ] == 0; |
| 86 |
return rtn; |
| 87 |
} |
| 88 |
|
| 89 |
/* HERE's useful */ |
| 90 |
#define atomic32_test_and_inc( v ) \ |
| 91 |
( atomic32_read_and_add ( v, 1 ) == 0 ) |
| 92 |
|
| 93 |
static __inline__ int atomic32_test_and_set ( atomic32_t *v, int s, int t ) |
| 94 |
{ |
| 95 |
int rtn = * v; |
| 96 |
if ( rtn == t ) |
| 97 |
* v = s; |
| 98 |
return rtn; |
| 99 |
} |
| 100 |
|
| 101 |
/* conditional modifications */ |
| 102 |
static __inline__ |
| 103 |
int atomic32_read_and_add_lt ( atomic32_t *v, int i, int t ) |
| 104 |
{ |
| 105 |
int rtn = * v; |
| 106 |
if ( rtn < t ) |
| 107 |
* v = rtn + i; |
| 108 |
return rtn; |
| 109 |
} |
| 110 |
|
| 111 |
#define atomic32_add_if_lt( v, i, t ) \ |
| 112 |
( atomic32_read_and_add_lt ( v, i, t ) < ( t ) ) |
| 113 |
|
| 114 |
static __inline__ |
| 115 |
int atomic32_read_and_add_le ( atomic32_t *v, int i, int t ) |
| 116 |
{ |
| 117 |
int rtn = * v; |
| 118 |
if ( rtn <= t ) |
| 119 |
* v = rtn + i; |
| 120 |
return rtn; |
| 121 |
} |
| 122 |
|
| 123 |
#define atomic32_add_if_le( v, i, t ) \ |
| 124 |
( atomic32_read_and_add_le ( v, i, t ) <= ( t ) ) |
| 125 |
|
| 126 |
static __inline__ |
| 127 |
int atomic32_read_and_add_eq ( atomic32_t *v, int i, int t ) |
| 128 |
{ |
| 129 |
int rtn = * v; |
| 130 |
if ( rtn == t ) |
| 131 |
* v = rtn + i; |
| 132 |
return rtn; |
| 133 |
} |
| 134 |
|
| 135 |
#define atomic32_add_if_eq( v, i, t ) \ |
| 136 |
( atomic32_read_and_add_eq ( v, i, t ) == ( t ) ) |
| 137 |
|
| 138 |
static __inline__ |
| 139 |
int atomic32_read_and_add_ne ( atomic32_t *v, int i, int t ) |
| 140 |
{ |
| 141 |
int rtn = * v; |
| 142 |
if ( rtn != t ) |
| 143 |
* v = rtn + i; |
| 144 |
return rtn; |
| 145 |
} |
| 146 |
|
| 147 |
#define atomic32_add_if_ne( v, i, t ) \ |
| 148 |
( atomic32_read_and_add_ne ( v, i, t ) != ( t ) ) |
| 149 |
|
| 150 |
static __inline__ |
| 151 |
int atomic32_read_and_add_ge ( atomic32_t *v, int i, int t ) |
| 152 |
{ |
| 153 |
int rtn = * v; |
| 154 |
if ( rtn >= t ) |
| 155 |
* v = rtn + i; |
| 156 |
return rtn; |
| 157 |
} |
| 158 |
|
| 159 |
#define atomic32_add_if_ge( v, i, t ) \ |
| 160 |
( atomic32_read_and_add_ge ( v, i, t ) >= ( t ) ) |
| 161 |
|
| 162 |
static __inline__ |
| 163 |
int atomic32_read_and_add_gt ( atomic32_t *v, int i, int t ) |
| 164 |
{ |
| 165 |
int rtn = * v; |
| 166 |
if ( rtn > t ) |
| 167 |
* v = rtn + i; |
| 168 |
return rtn; |
| 169 |
} |
| 170 |
|
| 171 |
#define atomic32_add_if_gt( v, i, t ) \ |
| 172 |
( atomic32_read_and_add_gt ( v, i, t ) > ( t ) ) |
| 173 |
|
| 174 |
static __inline__ |
| 175 |
int atomic32_read_and_add_odd ( atomic32_t *v, int i ) |
| 176 |
{ |
| 177 |
int rtn = * v; |
| 178 |
if ( ( rtn & 1 ) != 0 ) |
| 179 |
* v = rtn + i; |
| 180 |
return rtn; |
| 181 |
} |
| 182 |
|
| 183 |
static __inline__ |
| 184 |
int atomic32_read_and_add_even ( atomic32_t *v, int i ) |
| 185 |
{ |
| 186 |
int rtn = * v; |
| 187 |
if ( ( rtn & 1 ) == 0 ) |
| 188 |
* v = rtn + i; |
| 189 |
return rtn; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
#ifdef __cplusplus |
| 194 |
} |
| 195 |
#endif |
| 196 |
|
| 197 |
#endif /* _h_atomic32_ */ |