2003-12-22 19:29:21 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-07 21:39:35 +03:00
|
|
|
* $HEADER$
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-01-07 21:39:35 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#ifndef OPAL_MUTEX_H
|
|
|
|
#define OPAL_MUTEX_H 1
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
|
|
|
#include "opal/sys/atomic.h"
|
2004-10-21 02:31:03 +04:00
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
/**
|
|
|
|
* @file:
|
|
|
|
*
|
|
|
|
* Mutual exclusion functions.
|
|
|
|
*
|
|
|
|
* Functions for locking of critical sections.
|
|
|
|
*/
|
2004-10-22 20:06:05 +04:00
|
|
|
/*
|
|
|
|
* declaring this here so that CL does not complain
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
OMPI_DECLSPEC extern bool opal_uses_threads;
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Opaque mutex object
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
typedef struct opal_mutex_t opal_mutex_t;
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to acquire a mutex.
|
|
|
|
*
|
|
|
|
* @param mutex Address of the mutex.
|
|
|
|
* @return 0 if the mutex was acquired, 1 otherwise.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_trylock(opal_mutex_t *mutex);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Acquire a mutex.
|
|
|
|
*
|
|
|
|
* @param mutex Address of the mutex.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_lock(opal_mutex_t *mutex);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release a mutex.
|
|
|
|
*
|
|
|
|
* @param mutex Address of the mutex.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_unlock(opal_mutex_t *mutex);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to acquire a mutex using atomic operations.
|
|
|
|
*
|
|
|
|
* @param mutex Address of the mutex.
|
|
|
|
* @return 0 if the mutex was acquired, 1 otherwise.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_atomic_trylock(opal_mutex_t *mutex);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Acquire a mutex using atomic operations.
|
|
|
|
*
|
|
|
|
* @param mutex Address of the mutex.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_atomic_lock(opal_mutex_t *mutex);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release a mutex using atomic operations.
|
|
|
|
*
|
|
|
|
* @param mutex Address of the mutex.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_atomic_unlock(opal_mutex_t *mutex);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
2004-01-14 02:32:19 +03:00
|
|
|
/**
|
|
|
|
* Check and see if the process is using multiple threads.
|
|
|
|
*
|
|
|
|
* @retval true If the process may have more than one thread.
|
|
|
|
* @retval false If the process only has a single thread.
|
|
|
|
*
|
|
|
|
* The value that this function returns is influenced by:
|
|
|
|
*
|
2004-08-19 03:24:27 +04:00
|
|
|
* - how MPI_INIT or MPI_INIT_THREAD was invoked,
|
2004-01-14 02:32:19 +03:00
|
|
|
* - what the final MPI thread level was determined to be,
|
2004-06-07 19:33:53 +04:00
|
|
|
* - whether the OMPI or MPI libraries are multi-threaded (Jan 2003:
|
2004-01-14 02:32:19 +03:00
|
|
|
* they're not),
|
|
|
|
* - whether configure determined if we have thread support or not
|
|
|
|
*
|
2004-06-07 19:33:53 +04:00
|
|
|
* MPI_INIT and MPI_INIT_THREAD (specifically, back-end OMPI startup
|
2005-07-04 02:45:48 +04:00
|
|
|
* functions) invoke opal_set_using_threads() to influence the value of
|
2004-01-14 02:32:19 +03:00
|
|
|
* this function, depending on their situation. Some examples:
|
|
|
|
*
|
|
|
|
* - if configure determined that we do not have threads, then this
|
|
|
|
* value will always be false.
|
|
|
|
*
|
2004-08-06 18:30:18 +04:00
|
|
|
* - if MPI_INIT is invoked, and the ompi libraries are [still]
|
2004-01-14 02:32:19 +03:00
|
|
|
* single-threaded, this value will be false.
|
|
|
|
*
|
|
|
|
* - if MPI_INIT_THREAD is invoked with MPI_THREAD_MULTIPLE, we have
|
|
|
|
* thread support, and the final thread level is determined to be
|
|
|
|
* MPI_THREAD_MULTIPLE, this value will be true.
|
|
|
|
*
|
2004-06-07 19:33:53 +04:00
|
|
|
* - if the process is a single-threaded OMPI executable (e.g., mpicc),
|
2004-01-14 02:32:19 +03:00
|
|
|
* this value will be false.
|
|
|
|
*
|
|
|
|
* Hence, this function will return false if there is guaranteed to
|
|
|
|
* only be one thread in the process. If there is even the
|
|
|
|
* possibility that we may have multiple threads, true will be
|
|
|
|
* returned.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline bool opal_using_threads(void)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
return opal_uses_threads;
|
2004-01-12 21:17:29 +03:00
|
|
|
}
|
2004-01-11 01:22:50 +03:00
|
|
|
|
2004-01-14 07:00:15 +03:00
|
|
|
|
2004-01-14 02:32:19 +03:00
|
|
|
/**
|
|
|
|
* Set whether the process is using multiple threads or not.
|
|
|
|
*
|
|
|
|
* @param have Boolean indicating whether the process is using
|
|
|
|
* multiple threads or not.
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @retval opal_using_threads The new return value from
|
|
|
|
* opal_using_threads().
|
2004-01-14 02:32:19 +03:00
|
|
|
*
|
|
|
|
* This function is used to influence the return value of
|
2005-07-04 02:45:48 +04:00
|
|
|
* opal_using_threads(). If configure detected that we have thread
|
2004-01-14 02:32:19 +03:00
|
|
|
* support, the return value of future invocations of
|
2005-07-04 02:45:48 +04:00
|
|
|
* opal_using_threads() will be the parameter's value. If configure
|
2004-01-14 02:32:19 +03:00
|
|
|
* detected that we have no thread support, then the retuen from
|
2005-07-04 02:45:48 +04:00
|
|
|
* opal_using_threads() will always be false.
|
2004-01-11 01:22:50 +03:00
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline bool opal_set_using_threads(bool have)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-02-16 20:42:07 +03:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_uses_threads = have;
|
2004-01-14 02:32:19 +03:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_uses_threads = false;
|
2004-01-14 02:32:19 +03:00
|
|
|
#endif
|
2005-07-04 02:45:48 +04:00
|
|
|
return opal_uses_threads;
|
2004-01-14 02:32:19 +03:00
|
|
|
}
|
2004-01-11 01:22:50 +03:00
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-01-14 02:32:19 +03:00
|
|
|
/**
|
2005-07-04 02:45:48 +04:00
|
|
|
* Lock a mutex if opal_using_threads() says that multiple threads may
|
2004-01-14 02:32:19 +03:00
|
|
|
* be active in the process.
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @param mutex Pointer to a opal_mutex_t to lock.
|
2004-01-14 02:32:19 +03:00
|
|
|
*
|
|
|
|
* If there is a possibility that multiple threads are running in the
|
2005-07-04 02:45:48 +04:00
|
|
|
* process (as determined by opal_using_threads()), this function will
|
2004-01-14 02:32:19 +03:00
|
|
|
* block waiting to lock the mutex.
|
|
|
|
*
|
|
|
|
* If there is no possibility that multiple threads are running in the
|
|
|
|
* process, return immediately.
|
|
|
|
*/
|
2005-04-20 00:50:44 +04:00
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_LOCK(mutex) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2005-07-04 02:45:48 +04:00
|
|
|
if (opal_using_threads()) { \
|
|
|
|
opal_mutex_lock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2005-04-20 00:50:44 +04:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_LOCK(mutex)
|
2005-04-20 00:50:44 +04:00
|
|
|
#endif
|
2004-01-11 01:22:50 +03:00
|
|
|
|
2006-01-23 21:35:40 +03:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
|
|
|
#define OPAL_THREAD_TRYLOCK(mutex) (opal_using_threads() ? opal_mutex_trylock(mutex) : 1)
|
|
|
|
#else
|
|
|
|
#define OPAL_THREAD_TRYLOCK(mutex) 1
|
|
|
|
#endif
|
|
|
|
|
2005-04-12 23:51:29 +04:00
|
|
|
|
|
|
|
/**
|
2005-07-04 02:45:48 +04:00
|
|
|
* Unlock a mutex if opal_using_threads() says that multiple threads
|
2004-01-14 02:32:19 +03:00
|
|
|
* may be active in the process.
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @param mutex Pointer to a opal_mutex_t to unlock.
|
2004-01-14 02:32:19 +03:00
|
|
|
*
|
|
|
|
* If there is a possibility that multiple threads are running in the
|
2005-07-04 02:45:48 +04:00
|
|
|
* process (as determined by opal_using_threads()), this function will
|
2004-01-14 02:32:19 +03:00
|
|
|
* unlock the mutex.
|
|
|
|
*
|
|
|
|
* If there is no possibility that multiple threads are running in the
|
|
|
|
* process, return immediately without modifying the mutex.
|
2004-01-11 01:22:50 +03:00
|
|
|
*/
|
2005-04-20 00:50:44 +04:00
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_UNLOCK(mutex) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2005-07-04 02:45:48 +04:00
|
|
|
if (opal_using_threads()) { \
|
|
|
|
opal_mutex_unlock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2005-04-20 00:50:44 +04:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_UNLOCK(mutex)
|
2005-04-20 00:50:44 +04:00
|
|
|
#endif
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/**
|
2005-07-04 02:45:48 +04:00
|
|
|
* Lock a mutex if opal_using_threads() says that multiple threads may
|
2004-08-19 03:24:27 +04:00
|
|
|
* be active in the process for the duration of the specified action.
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @param mutex Pointer to a opal_mutex_t to lock.
|
2004-08-19 03:24:27 +04:00
|
|
|
* @param action A scope over which the lock is held.
|
|
|
|
*
|
|
|
|
* If there is a possibility that multiple threads are running in the
|
2005-07-04 02:45:48 +04:00
|
|
|
* process (as determined by opal_using_threads()), this function will
|
2004-08-19 03:24:27 +04:00
|
|
|
* acquire the lock before invoking the specified action and release
|
|
|
|
* it on return.
|
|
|
|
*
|
|
|
|
* If there is no possibility that multiple threads are running in the
|
|
|
|
* process, invoke the action without acquiring the lock.
|
|
|
|
*/
|
2005-04-20 00:50:44 +04:00
|
|
|
|
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_SCOPED_LOCK(mutex, action) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2005-07-04 02:45:48 +04:00
|
|
|
if(opal_using_threads()) { \
|
|
|
|
opal_mutex_lock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
(action); \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} else { \
|
|
|
|
(action); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2005-04-20 00:50:44 +04:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_SCOPED_LOCK(mutex,action) (action)
|
2005-04-20 00:50:44 +04:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-11-11 18:41:07 +03:00
|
|
|
/**
|
2005-07-04 02:45:48 +04:00
|
|
|
* Use an atomic operation for increment/decrement if opal_using_threads()
|
2004-11-11 18:41:07 +03:00
|
|
|
* indicates that threads are in use by the application or library.
|
|
|
|
*/
|
|
|
|
|
2005-04-20 00:50:44 +04:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_ADD32(x,y) \
|
|
|
|
((OMPI_HAVE_THREAD_SUPPORT && opal_using_threads()) ? \
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_add_32(x,y) : (*x += y))
|
2005-04-20 00:50:44 +04:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_ADD32(x,y) (*x += y)
|
2005-04-20 00:50:44 +04:00
|
|
|
#endif
|
2004-11-11 18:41:07 +03:00
|
|
|
|
2005-04-20 00:50:44 +04:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_ADD64(x,y) \
|
|
|
|
((OMPI_HAVE_THREAD_SUPPORT && opal_using_threads()) ? \
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_add_64(x,y) : (*x += y))
|
2005-04-20 00:50:44 +04:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_ADD64(x,y) (*x += y)
|
2005-04-20 00:50:44 +04:00
|
|
|
#endif
|
|
|
|
|
2005-06-29 00:15:01 +04:00
|
|
|
#if OMPI_HAVE_THREAD_SUPPORT
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_ADD_SIZE_T(x,y) \
|
2005-08-02 21:36:01 +04:00
|
|
|
((OMPI_HAVE_THREAD_SUPPORT && opal_using_threads()) ? \
|
|
|
|
opal_atomic_add_size_t(x,y) : (*x += y))
|
2005-06-29 00:15:01 +04:00
|
|
|
#else
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_ADD_SIZE_T(x,y) (*x += y)
|
2005-06-29 00:15:01 +04:00
|
|
|
#endif
|
|
|
|
|
2004-11-11 18:41:07 +03:00
|
|
|
|
2004-01-14 02:32:19 +03:00
|
|
|
/**
|
|
|
|
* Always locks a mutex (never compile- or run-time removed)
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @param mutex A pointer to a opal_mutex_t.
|
2004-01-14 02:32:19 +03:00
|
|
|
*
|
|
|
|
* Locks the mutex. This is the macro that you should use for mutexes
|
|
|
|
* that should always be locked, regardless of whether the process has
|
|
|
|
* multiple threads or not. This is useful, for example, with shared
|
|
|
|
* memory.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_LOCK(mutex) opal_mutex_atomic_lock(mutex)
|
2004-01-14 02:32:19 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Always unlocks a mutex (never compile- or run-time removed)
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @param mutex A pointer to a opal_mutex_t.
|
2004-01-14 02:32:19 +03:00
|
|
|
*
|
|
|
|
* Unlocks the mutex. This is the macro that you should use for
|
|
|
|
* mutexes that should always be unlocked, regardless of whether the
|
|
|
|
* process has multiple threads or not. This is useful, for example,
|
|
|
|
* with shared memory.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_UNLOCK(mutex) opal_mutex_atomic_unlock(mutex)
|
2004-01-11 01:22:50 +03:00
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Lock a mutex for the duration of the specified action.
|
|
|
|
*
|
2005-07-04 02:45:48 +04:00
|
|
|
* @param mutex Pointer to a opal_mutex_t to lock.
|
2004-08-19 03:24:27 +04:00
|
|
|
* @param action A scope over which the lock is held.
|
|
|
|
*
|
|
|
|
* This is the macro that you should use for mutexes that should
|
|
|
|
* always be locked, regardless of whether the process has multiple
|
|
|
|
* threads or not. This is useful, for example, with shared memory.
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
#define OPAL_SCOPED_LOCK(mutex, action) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
(action); \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} while (0)
|
|
|
|
|
2004-10-21 02:31:03 +04:00
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-12-13 01:02:42 +03:00
|
|
|
#ifdef __WINDOWS__
|
2004-08-19 03:24:27 +04:00
|
|
|
#include "mutex_windows.h"
|
|
|
|
#else
|
|
|
|
#include "mutex_unix.h"
|
2003-12-22 19:29:21 +03:00
|
|
|
#endif
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#endif /* OPAL_MUTEX_H */
|