@@ -93,82 +93,87 @@ static inline bool opal_set_using_threads(bool have)
9393 * indicates that threads are in use by the application or library.
9494 */
9595
96- static inline int32_t
97- OPAL_THREAD_ADD32 (volatile int32_t * addr , int delta )
98- {
99- int32_t ret ;
96+ #define OPAL_THREAD_DEFINE_ATOMIC_ADD (type , suffix ) \
97+ static inline type opal_thread_add_ ## suffix (volatile type *addr, type delta) \
98+ { \
99+ if (OPAL_UNLIKELY(opal_using_threads())) { \
100+ return opal_atomic_add_ ## suffix (addr, delta); \
101+ } \
102+ \
103+ return (*addr += delta); \
104+ }
100105
101- if (OPAL_UNLIKELY (opal_using_threads ())) {
102- ret = opal_atomic_add_32 (addr , delta );
103- } else {
104- ret = (* addr += delta );
105- }
106+ #define OPAL_THREAD_DEFINE_ATOMIC_CMPSET (type , addr_type , suffix ) \
107+ static inline bool opal_thread_cmpset_bool_ ## suffix (volatile addr_type *addr, type compare, type value) \
108+ { \
109+ type ret; \
110+ \
111+ if (OPAL_UNLIKELY(opal_using_threads())) { \
112+ return opal_atomic_cmpset_ ## suffix ((volatile type *) addr, compare, value); \
113+ } \
114+ \
115+ if ((type) *addr == compare) { \
116+ ((type *) addr)[0] = value; \
117+ return true; \
118+ } \
119+ \
120+ return false; \
121+ }
106122
107- return ret ;
123+ #define OPAL_THREAD_DEFINE_ATOMIC_SWAP (type , addr_type , suffix ) \
124+ static inline type opal_thread_swap_ ## suffix (volatile addr_type *ptr, type newvalue) \
125+ { \
126+ if (opal_using_threads ()) { \
127+ return opal_atomic_swap_ ## suffix ((volatile type *) ptr, newvalue); \
128+ } \
129+ \
130+ type old = ((type *) ptr)[0]; \
131+ ((type *) ptr)[0] = newvalue; \
132+ \
133+ return old; \
108134}
109135
110- #if OPAL_HAVE_ATOMIC_MATH_64
111- static inline int64_t
112- OPAL_THREAD_ADD64 (volatile int64_t * addr , int delta )
113- {
114- int64_t ret ;
136+ OPAL_THREAD_DEFINE_ATOMIC_ADD (int32_t , 32 )
137+ OPAL_THREAD_DEFINE_ATOMIC_ADD (size_t , size_t )
138+ OPAL_THREAD_DEFINE_ATOMIC_CMPSET (int32_t , int32_t , 32 )
139+ OPAL_THREAD_DEFINE_ATOMIC_CMPSET (void * , intptr_t , ptr )
140+ OPAL_THREAD_DEFINE_ATOMIC_SWAP (int32_t , int32_t , 32 )
141+ OPAL_THREAD_DEFINE_ATOMIC_SWAP (void * , intptr_t , ptr )
115142
116- if (OPAL_UNLIKELY (opal_using_threads ())) {
117- ret = opal_atomic_add_64 (addr , delta );
118- } else {
119- ret = (* addr += delta );
120- }
143+ #define OPAL_THREAD_ADD32 opal_thread_add_32
144+ #define OPAL_ATOMIC_ADD32 opal_thread_add_32
121145
122- return ret ;
123- }
124- #endif
146+ #define OPAL_THREAD_ADD_SIZE_T opal_thread_add_size_t
147+ #define OPAL_ATOMIC_ADD_SIZE_T opal_thread_add_size_t
125148
126- static inline size_t
127- OPAL_THREAD_ADD_SIZE_T (volatile size_t * addr , int delta )
128- {
129- size_t ret ;
149+ #define OPAL_THREAD_CMPSET_32 opal_thread_cmpset_bool_32
150+ #define OPAL_ATOMIC_CMPSET_32 opal_thread_cmpset_bool_32
130151
131- if (OPAL_UNLIKELY (opal_using_threads ())) {
132- ret = opal_atomic_add_size_t (addr , delta );
133- } else {
134- ret = (* addr += delta );
135- }
152+ #define OPAL_THREAD_CMPSET_PTR (x , y , z ) opal_thread_cmpset_bool_ptr ((volatile intptr_t *) x, (void *) y, (void *) z)
153+ #define OPAL_ATOMIC_CMPSET_PTR OPAL_THREAD_CMPSET_PTR
136154
137- return ret ;
138- }
155+ #define OPAL_THREAD_SWAP_32 opal_thread_swap_32
156+ #define OPAL_ATOMIC_SWAP_32 opal_thread_swap_32
139157
140- /* BWB: FIX ME: remove if possible */
141- #define OPAL_CMPSET ( x , y , z ) ((*(x) == (y)) ? ((*(x) = (z)), 1) : 0)
158+ #define OPAL_THREAD_SWAP_PTR ( x , y ) opal_thread_swap_ptr ((volatile intptr_t *) x, (void *) y)
159+ #define OPAL_ATOMIC_SWAP_PTR OPAL_THREAD_SWAP_PTR
142160
143- #if OPAL_HAVE_ATOMIC_CMPSET_32
144- #define OPAL_ATOMIC_CMPSET_32 (x , y , z ) \
145- (opal_using_threads() ? opal_atomic_cmpset_32(x, y, z) : OPAL_CMPSET(x, y, z))
146- #endif
147- #if OPAL_HAVE_ATOMIC_CMPSET_64
148- #define OPAL_ATOMIC_CMPSET_64 (x , y , z ) \
149- (opal_using_threads() ? opal_atomic_cmpset_64(x, y, z) : OPAL_CMPSET(x, y, z))
150- #endif
151- #if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
152- #define OPAL_ATOMIC_CMPSET (x , y , z ) \
153- (opal_using_threads() ? opal_atomic_cmpset(x, y, z) : OPAL_CMPSET(x, y, z))
154- #endif
155- #if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
156- #define OPAL_ATOMIC_CMPSET_PTR (x , y , z ) \
157- (opal_using_threads() ? opal_atomic_cmpset_ptr(x, y, z) : OPAL_CMPSET(x, y, z))
158- #endif
161+ /* define 64-bit macros is 64-bit atomic math is available */
162+ #if OPAL_HAVE_ATOMIC_MATH_64
159163
160- static inline void * opal_thread_swap_ptr (volatile void * ptr , void * newvalue )
161- {
162- if (opal_using_threads ()) {
163- return opal_atomic_swap_ptr (ptr , newvalue );
164- }
164+ OPAL_THREAD_DEFINE_ATOMIC_ADD (int64_t , 64 )
165+ OPAL_THREAD_DEFINE_ATOMIC_CMPSET (int64_t , int64_t , 64 )
166+ OPAL_THREAD_DEFINE_ATOMIC_SWAP (int64_t , int64_t , 64 )
165167
166- void * old = (( void * * ) ptr )[ 0 ];
167- (( void * * ) ptr )[ 0 ] = newvalue ;
168+ #define OPAL_THREAD_ADD64 opal_thread_add_64
169+ #define OPAL_ATOMIC_ADD64 opal_thread_add_64
168170
169- return old ;
170- }
171+ #define OPAL_THREAD_CMPSET_64 opal_thread_cmpset_bool_64
172+ #define OPAL_ATOMIC_CMPSET_64 opal_thread_cmpset_bool_64
171173
172- #define OPAL_ATOMIC_SWAP_PTR (x , y ) opal_thread_swap_ptr (x, y)
174+ #define OPAL_THREAD_SWAP_64 opal_thread_swap_64
175+ #define OPAL_ATOMIC_SWAP_64 opal_thread_swap_64
176+
177+ #endif
173178
174179#endif /* !defined(OPAL_THREAD_USAGE_H) */
0 commit comments