Debian Codegraph @

interfaces/cc/clang/x86_64/atomic.h

raw
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_atomic_
28 #define _h_atomic_
29
30 #ifndef _h_atomic32_
31 #include "atomic32.h"
32 #endif
33
34 #ifndef _h_atomic64_
35 #include "atomic64.h"
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #if DFLT_ATOMIC_BITS == 32
43 #define ATOMIC_NAME( suffix ) \
44 atomic32_ ## suffix
45 typedef int atomic_int;
46 #else
47 #define ATOMIC_NAME( suffix ) \
48 atomic64_ ## suffix
49 typedef long int atomic_int;
50 #endif
51
52 typedef struct ATOMIC_NAME ( t ) atomic_t;
53
54 typedef struct atomic_ptr_t atomic_ptr_t;
55 struct atomic_ptr_t
56 {
57 void * volatile ptr;
58 };
59
60 /* ( * v ) */
61 #define atomic_read( v ) \
62 ATOMIC_NAME ( read ) ( v )
63
64 /* ( * v ) = i */
65 #define atomic_set( v, i ) \
66 ATOMIC_NAME ( set ) ( v, i )
67
68 /* prior = ( * v ), ( * v ) += i, prior */
69 #define atomic_read_and_add( v, i ) \
70 ATOMIC_NAME ( read_and_add ) ( v, i )
71
72 /* ( * v ) += i */
73 #define atomic_add( v, i ) \
74 ATOMIC_NAME ( add ) ( v, i )
75
76 /* ( * v ) += i */
77 #define atomic_add_and_read( v, i ) \
78 ATOMIC_NAME ( add_and_read ) ( v, i )
79
80 /* ( void ) ++ ( * v ) */
81 #define atomic_inc( v ) \
82 ATOMIC_NAME ( inc ) ( v )
83
84 /* ( void ) -- ( * v ) */
85 #define atomic_dec( v ) \
86 ATOMIC_NAME ( dec ) ( v )
87
88 /* -- ( * v ) == 0 */
89 #define atomic_dec_and_test( v ) \
90 ATOMIC_NAME ( dec_and_test ) ( v )
91
92 /* ++ ( * v ) == 0
93 when atomic_dec_and_test uses predecrement, you want
94 postincrement to this function. so it isn't very useful */
95 #define atomic_inc_and_test( v ) \
96 ATOMIC_NAME ( inc_and_test ) ( v )
97
98 /* ( * v ) -- == 0
99 HERE's useful */
100 #define atomic_test_and_inc( v ) \
101 ATOMIC_NAME ( test_and_inc ) ( v )
102
103 /* prior = ( * v ), ( * v ) = ( prior == t ? s : prior ), prior */
104 #define atomic_test_and_set( v, s, t ) \
105 ATOMIC_NAME ( test_and_set ) ( v, s, t )
106
107 /* N.B. - THESE FUNCTIONS ARE FOR 64 BIT PTRS ONLY */
108
109 /* int atomic_read_ptr ( const atomic_ptr_t *v ); */
110 #define atomic_read_ptr( v ) \
111 ( ( v ) -> ptr )
112
113 static __inline__
114 void *atomic_test_and_set_ptr ( atomic_ptr_t *v, void *s, void *t )
115 {
116 void *rtn;
117 __asm__ __volatile__
118 (
119 "lock;"
120 "cmpxchg %%rsi,(%%rdi)"
121 : "=a" ( rtn )
122 : "D" ( v ), "S" ( s ), "a" ( t )
123 );
124 return rtn;
125 }
126
127 /* val = ( * v ), ( ( * v ) = ( val < t ) ? val + i : val ), val */
128 #define atomic_read_and_add_lt( v, i, t ) \
129 ATOMIC_NAME ( read_and_add_lt ) ( v, i, t )
130
131 /* val = ( * v ), ( ( * v ) = ( val <= t ) ? val + i : val ), val */
132 #define atomic_read_and_add_le( v, i, t ) \
133 ATOMIC_NAME ( read_and_add_le ) ( v, i, t )
134
135 /* val = ( * v ), ( ( * v ) = ( val == t ) ? val + i : val ), val */
136 #define atomic_read_and_add_eq( v, i, t ) \
137 ATOMIC_NAME ( read_and_add_eq ) ( v, i, t )
138
139 /* val = ( * v ), ( ( * v ) = ( val != t ) ? val + i : val ), val */
140 #define atomic_read_and_add_ne( v, i, t ) \
141 ATOMIC_NAME ( read_and_add_ne ) ( v, i, t )
142
143 /* val = ( * v ), ( ( * v ) = ( val >= t ) ? val + i : val ), val */
144 #define atomic_read_and_add_ge( v, i, t ) \
145 ATOMIC_NAME ( read_and_add_ge ) ( v, i, t )
146
147 /* val = ( * v ), ( ( * v ) = ( val > t ) ? val + i : val ), val */
148 #define atomic_read_and_add_gt( v, i, t ) \
149 ATOMIC_NAME ( read_and_add_gt ) ( v, i, t )
150
151 /* val = ( * v ), ( ( * v ) = ( ( val & 1 ) == 1 ) ? val + i : val ), val */
152 #define atomic_read_and_add_odd( v, i ) \
153 ATOMIC_NAME ( read_and_add_odd ) ( v, i )
154
155 /* val = ( * v ), ( ( * v ) = ( ( val & 1 ) == 0 ) ? val + i : val ), val */
156 #define atomic_read_and_add_even( v, i ) \
157 ATOMIC_NAME ( read_and_add_even ) ( v, i )
158
159 /* DEPRECATED */
160
161 /* val = ( * v ), ( * v ) = ( val < t ? val + i : val ), ( val < t ? 1 : 0 ) */
162 #define atomic_add_if_lt( v, i, t ) \
163 ATOMIC_NAME ( add_if_lt ) ( v, i, t )
164
165 /* val = ( * v ), ( * v ) = ( val <= t ? val + i : val ), ( val <= t ? 1 : 0 ) */
166 #define atomic_add_if_le( v, i, t ) \
167 ATOMIC_NAME ( add_if_le ) ( v, i, t )
168
169 /* val = ( * v ), ( * v ) = ( val == t ? val + i : val ), ( val == t ? 1 : 0 ) */
170 #define atomic_add_if_eq( v, i, t ) \
171 ATOMIC_NAME ( add_if_eq ) ( v, i, t )
172
173 /* val = ( * v ), ( * v ) = ( val >= t ? val + i : val ), ( val >= t ? 1 : 0 ) */
174 #define atomic_add_if_ge( v, i, t ) \
175 ATOMIC_NAME ( add_if_ge ) ( v, i, t )
176
177 /* val = ( * v ), ( * v ) = ( val > t ? val + i : val ), ( val > t ? 1 : 0 ) */
178 #define atomic_add_if_gt( v, i, t ) \
179 ATOMIC_NAME ( add_if_gt ) ( v, i, t )
180
181 #undef LOCK
182
183
184 #ifdef __cplusplus
185 }
186 #endif
187
188 #endif /* _h_atomic_ */