26 #define OPAL_MUTEX_H 1
28 #include "opal_config.h"
30 #if OPAL_ENABLE_MULTI_THREADS
50 OPAL_DECLSPEC
extern bool opal_uses_threads;
53 OPAL_DECLSPEC
extern bool opal_mutex_check_locks;
157 #define opal_using_threads() opal_uses_threads
177 #if OPAL_ENABLE_MULTI_THREADS
178 opal_uses_threads = have;
181 opal_uses_threads =
false;
183 return opal_uses_threads;
201 #if OPAL_ENABLE_MULTI_THREADS
202 #define OPAL_THREAD_LOCK(mutex) \
204 if (opal_using_threads()) { \
205 opal_mutex_lock(mutex); \
208 #elif OPAL_ENABLE_DEBUG
209 #define OPAL_THREAD_LOCK(mutex) \
211 (mutex)->m_lock_debug++; \
212 if (opal_mutex_check_locks && 1 != (mutex)->m_lock_debug) { \
213 opal_output(0, "Warning -- mutex already locked at %s:%d," \
215 (mutex)->m_lock_file, \
216 (mutex)->m_lock_line, \
217 __FILE__, __LINE__); \
219 (mutex)->m_lock_file = __FILE__; \
220 (mutex)->m_lock_line = __LINE__; \
223 #define OPAL_THREAD_LOCK(mutex)
242 #if OPAL_ENABLE_MULTI_THREADS
243 #define OPAL_THREAD_TRYLOCK(mutex) (opal_using_threads() ? opal_mutex_trylock(mutex) : 0)
244 #elif OPAL_ENABLE_DEBUG
246 opal_thread_debug_trylock(
opal_mutex_t *mutex,
const char *file,
int line)
250 if (0 == (mutex)->m_lock_debug) {
251 (mutex)->m_lock_debug++;
252 (mutex)->m_lock_file = file;
253 (mutex)->m_lock_line = line;
256 if (opal_mutex_check_locks) {
257 opal_output(0,
"Warning -- during trylock, mutex already locked at %s:%d "
260 (mutex)->m_lock_file,
261 (mutex)->m_lock_line);
267 #define OPAL_THREAD_TRYLOCK(mutex) opal_thread_debug_trylock(mutex, __FILE__, __LINE__)
269 #define OPAL_THREAD_TRYLOCK(mutex) 0
286 #if OPAL_ENABLE_MULTI_THREADS
287 #define OPAL_THREAD_UNLOCK(mutex) \
289 if (opal_using_threads()) { \
290 opal_mutex_unlock(mutex); \
293 #elif OPAL_ENABLE_DEBUG
294 #define OPAL_THREAD_UNLOCK(mutex) \
296 (mutex)->m_lock_debug--; \
297 if (opal_mutex_check_locks && 0 > (mutex)->m_lock_debug) { \
298 opal_output(0, "Warning -- mutex was double locked from %s:%d", \
299 __FILE__, __LINE__); \
300 } else if (opal_mutex_check_locks && 0 > (mutex)->m_lock_debug) { \
301 opal_output(0, "Warning -- mutex not locked from %s:%d", \
302 __FILE__, __LINE__); \
304 (mutex)->m_lock_file = NULL; \
305 (mutex)->m_lock_line = 0; \
309 #define OPAL_THREAD_UNLOCK(mutex)
329 #if OPAL_ENABLE_MULTI_THREADS
330 #define OPAL_THREAD_SCOPED_LOCK(mutex, action) \
332 if(opal_using_threads()) { \
333 opal_mutex_lock(mutex); \
335 opal_mutex_unlock(mutex); \
340 #elif OPAL_ENABLE_DEBUG
341 #define OPAL_THREAD_SCOPED_LOCK(mutex, action) \
343 if (0 != (mutex)->m_lock_debug) { \
344 opal_output(0, "scoped_lock: Warning -- mutex already " \
345 "locked at %s:%d, now at %s:%d", \
346 __FILE__, __LINE__, \
347 (mutex)->m_lock_file, \
348 (mutex)->m_lock_line); \
350 (mutex)->m_lock_debug--; \
352 (mutex)->m_lock_debug++; \
355 #define OPAL_THREAD_SCOPED_LOCK(mutex, action) (action)
363 #if OPAL_ENABLE_MULTI_THREADS
364 #define OPAL_THREAD_ADD32(x,y) \
365 (opal_using_threads() ? opal_atomic_add_32(x,y) : (*x += y))
367 #define OPAL_THREAD_ADD32(x,y) (*x += y)
370 #if OPAL_ENABLE_MULTI_THREADS
371 #define OPAL_THREAD_ADD64(x,y) \
372 (opal_using_threads() ? opal_atomic_add_64(x,y) : (*x += y))
374 #define OPAL_THREAD_ADD64(x,y) (*x += y)
377 #if OPAL_ENABLE_MULTI_THREADS
378 #define OPAL_THREAD_ADD_SIZE_T(x,y) \
379 (opal_using_threads() ? opal_atomic_add_size_t(x,y) : (*x += y))
381 #define OPAL_THREAD_ADD_SIZE_T(x,y) (*x += y)
384 #define OPAL_CMPSET(x, y, z) ((*(x) == (y)) ? ((*(x) = (z)), 1) : 0)
386 #if OPAL_ENABLE_MULTI_THREADS
387 # if OPAL_HAVE_ATOMIC_CMPSET_32
388 # define OPAL_ATOMIC_CMPSET_32(x, y, z) \
389 (opal_using_threads() ? opal_atomic_cmpset_32(x, y, z) : OPAL_CMPSET(x, y, z))
391 # if OPAL_HAVE_ATOMIC_CMPSET_64
392 # define OPAL_ATOMIC_CMPSET_64(x, y, z) \
393 (opal_using_threads() ? opal_atomic_cmpset_64(x, y, z) : OPAL_CMPSET(x, y, z))
395 # if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
396 # define OPAL_ATOMIC_CMPSET(x, y, z) \
397 (opal_using_threads() ? opal_atomic_cmpset(x, y, z) : OPAL_CMPSET(x, y, z))
400 # if OPAL_HAVE_ATOMIC_CMPSET_32
401 # define OPAL_ATOMIC_CMPSET_32(x, y, z) OPAL_CMPSET(x, y, z)
403 # if OPAL_HAVE_ATOMIC_CMPSET_64
404 # define OPAL_ATOMIC_CMPSET_64(x, y, z) OPAL_CMPSET(x, y, z)
406 # if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
407 # define OPAL_ATOMIC_CMPSET(x, y, z) OPAL_CMPSET(x, y, z)
static void opal_mutex_lock(opal_mutex_t *mutex)
Acquire a mutex.
OPAL output stream facility.
static int opal_mutex_atomic_trylock(opal_mutex_t *mutex)
Try to acquire a mutex using atomic operations.
static void opal_mutex_atomic_unlock(opal_mutex_t *mutex)
Release a mutex using atomic operations.
Definition: mutex_unix.h:53
static int opal_mutex_trylock(opal_mutex_t *mutex)
Try to acquire a mutex.
Mutual exclusion functions: Unix implementation.
Mutual exclusion functions: Windows implementation.
static void opal_mutex_unlock(opal_mutex_t *mutex)
Release a mutex.
OPAL_DECLSPEC void opal_output(int output_id, const char *format,...) __opal_attribute_format__(__printf__
Main function to send output to a stream.
static bool opal_set_using_threads(bool have)
Set whether the process is using multiple threads or not.
Definition: mutex.h:175
static void opal_mutex_atomic_lock(opal_mutex_t *mutex)
Acquire a mutex using atomic operations.