2015-08-21 19:36:49 +03:00
|
|
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
|
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.
|
2016-05-25 02:20:51 +03:00
|
|
|
* Copyright (c) 2004-2016 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2004-11-28 23:09:25 +03:00
|
|
|
* 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.
|
2009-01-11 05:30:00 +03:00
|
|
|
* Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
|
2016-02-23 19:58:32 +03:00
|
|
|
* Copyright (c) 2007-2016 Los Alamos National Security, LLC. All rights
|
2015-06-24 06:59:57 +03:00
|
|
|
* reserved.
|
2007-08-01 16:13:34 +04:00
|
|
|
* Copyright (c) 2007 Voltaire. All rights reserved.
|
2010-10-28 00:47:42 +04:00
|
|
|
* Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
|
2007-08-01 16:13:34 +04:00
|
|
|
*
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
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"
|
2008-02-19 03:39:48 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/sys/atomic.h"
|
2016-02-23 19:58:32 +03:00
|
|
|
#include "opal/prefetch.h"
|
2006-08-20 19:54:04 +04:00
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
BEGIN_C_DECLS
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
/**
|
|
|
|
* @file:
|
|
|
|
*
|
|
|
|
* Mutual exclusion functions.
|
|
|
|
*
|
|
|
|
* Functions for locking of critical sections.
|
|
|
|
*/
|
2007-06-12 20:25:26 +04:00
|
|
|
|
2004-10-22 20:06:05 +04:00
|
|
|
/*
|
|
|
|
* declaring this here so that CL does not complain
|
2015-06-24 06:59:57 +03:00
|
|
|
*/
|
2006-08-20 19:54:04 +04:00
|
|
|
OPAL_DECLSPEC extern bool opal_uses_threads;
|
2007-06-12 20:25:26 +04:00
|
|
|
|
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;
|
2015-08-24 22:26:47 +03:00
|
|
|
typedef struct opal_mutex_t opal_recursive_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
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
END_C_DECLS
|
|
|
|
|
|
|
|
#include "mutex_unix.h"
|
|
|
|
|
|
|
|
BEGIN_C_DECLS
|
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.
|
|
|
|
*/
|
2007-04-12 08:29:43 +04:00
|
|
|
#define opal_using_threads() opal_uses_threads
|
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-07-04 02:45:48 +04:00
|
|
|
opal_uses_threads = have;
|
2013-12-13 23:40:12 +04:00
|
|
|
return opal_using_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-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_LOCK(mutex) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2016-02-23 19:58:32 +03:00
|
|
|
if (OPAL_UNLIKELY(opal_using_threads())) { \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2004-01-11 01:22:50 +03:00
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to lock a mutex if opal_using_threads() says that multiple
|
|
|
|
* threads may be active in the process.
|
|
|
|
*
|
|
|
|
* @param mutex Pointer to a opal_mutex_t to trylock
|
|
|
|
*
|
|
|
|
* If there is a possibility that multiple threads are running in the
|
|
|
|
* process (as determined by opal_using_threads()), this function will
|
|
|
|
* trylock the mutex.
|
|
|
|
*
|
|
|
|
* If there is no possibility that multiple threads are running in the
|
|
|
|
* process, return immediately without modifying the mutex.
|
|
|
|
*
|
|
|
|
* Returns 0 if mutex was locked, non-zero otherwise.
|
|
|
|
*/
|
2013-12-13 23:40:12 +04:00
|
|
|
#define OPAL_THREAD_TRYLOCK(mutex) \
|
2016-02-23 19:58:32 +03:00
|
|
|
(OPAL_UNLIKELY(opal_using_threads()) ? opal_mutex_trylock(mutex) : 0)
|
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-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_UNLOCK(mutex) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2016-02-23 19:58:32 +03:00
|
|
|
if (OPAL_UNLIKELY(opal_using_threads())) { \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2005-04-20 00:50:44 +04:00
|
|
|
|
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-07-04 02:45:48 +04:00
|
|
|
#define OPAL_THREAD_SCOPED_LOCK(mutex, action) \
|
2004-08-19 03:24:27 +04:00
|
|
|
do { \
|
2016-02-23 19:58:32 +03:00
|
|
|
if(OPAL_UNLIKELY(opal_using_threads())) { \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(mutex); \
|
2015-08-21 19:36:49 +03:00
|
|
|
action; \
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(mutex); \
|
2004-08-19 03:24:27 +04:00
|
|
|
} else { \
|
2015-08-21 19:36:49 +03:00
|
|
|
action; \
|
2004-08-19 03:24:27 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2013-12-13 23:40:12 +04:00
|
|
|
static inline int32_t
|
|
|
|
OPAL_THREAD_ADD32(volatile int32_t *addr, int delta)
|
|
|
|
{
|
|
|
|
int32_t ret;
|
2004-11-11 18:41:07 +03:00
|
|
|
|
2016-02-23 19:58:32 +03:00
|
|
|
if (OPAL_UNLIKELY(opal_using_threads())) {
|
2013-12-13 23:40:12 +04:00
|
|
|
ret = opal_atomic_add_32(addr, delta);
|
|
|
|
} else {
|
|
|
|
ret = (*addr += delta);
|
|
|
|
}
|
2005-04-20 00:50:44 +04:00
|
|
|
|
2013-12-13 23:40:12 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-01-21 00:22:38 +04:00
|
|
|
#if OPAL_HAVE_ATOMIC_MATH_64
|
2013-12-13 23:40:12 +04:00
|
|
|
static inline int64_t
|
|
|
|
OPAL_THREAD_ADD64(volatile int64_t *addr, int delta)
|
|
|
|
{
|
|
|
|
int64_t ret;
|
|
|
|
|
2016-02-23 19:58:32 +03:00
|
|
|
if (OPAL_UNLIKELY(opal_using_threads())) {
|
2013-12-13 23:40:12 +04:00
|
|
|
ret = opal_atomic_add_64(addr, delta);
|
|
|
|
} else {
|
|
|
|
ret = (*addr += delta);
|
|
|
|
}
|
2005-06-29 00:15:01 +04:00
|
|
|
|
2013-12-13 23:40:12 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2014-01-21 00:22:38 +04:00
|
|
|
#endif
|
2013-12-13 23:40:12 +04:00
|
|
|
|
|
|
|
static inline size_t
|
|
|
|
OPAL_THREAD_ADD_SIZE_T(volatile size_t *addr, int delta)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
2016-02-23 19:58:32 +03:00
|
|
|
if (OPAL_UNLIKELY(opal_using_threads())) {
|
2013-12-13 23:40:12 +04:00
|
|
|
ret = opal_atomic_add_size_t(addr, delta);
|
|
|
|
} else {
|
|
|
|
ret = (*addr += delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BWB: FIX ME: remove if possible */
|
2007-09-09 12:00:16 +04:00
|
|
|
#define OPAL_CMPSET(x, y, z) ((*(x) == (y)) ? ((*(x) = (z)), 1) : 0)
|
|
|
|
|
2013-12-13 23:40:12 +04:00
|
|
|
#if OPAL_HAVE_ATOMIC_CMPSET_32
|
|
|
|
#define OPAL_ATOMIC_CMPSET_32(x, y, z) \
|
2016-02-23 19:58:32 +03:00
|
|
|
(OPAL_UNLIKELY(opal_using_threads()) ? opal_atomic_cmpset_32(x, y, z) : OPAL_CMPSET(x, y, z))
|
2013-12-13 23:40:12 +04:00
|
|
|
#endif
|
|
|
|
#if OPAL_HAVE_ATOMIC_CMPSET_64
|
|
|
|
#define OPAL_ATOMIC_CMPSET_64(x, y, z) \
|
2016-02-23 19:58:32 +03:00
|
|
|
(OPAL_UNLIKELY(opal_using_threads()) ? opal_atomic_cmpset_64(x, y, z) : OPAL_CMPSET(x, y, z))
|
2013-12-13 23:40:12 +04:00
|
|
|
#endif
|
|
|
|
#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
|
|
|
|
#define OPAL_ATOMIC_CMPSET(x, y, z) \
|
2016-02-23 19:58:32 +03:00
|
|
|
(OPAL_UNLIKELY(opal_using_threads()) ? opal_atomic_cmpset(x, y, z) : OPAL_CMPSET(x, y, z))
|
2007-08-01 16:13:34 +04:00
|
|
|
#endif
|
2016-05-25 02:20:51 +03:00
|
|
|
#if OPAL_HAVE_ATOMIC_CMPSET_32 || OPAL_HAVE_ATOMIC_CMPSET_64
|
|
|
|
#define OPAL_ATOMIC_CMPSET_PTR(x, y, z) \
|
|
|
|
(opal_using_threads() ? opal_atomic_cmpset_ptr(x, y, z) : OPAL_CMPSET(x, y, z))
|
|
|
|
#endif
|
|
|
|
|
2007-08-01 16:13:34 +04:00
|
|
|
|
2016-06-22 18:47:36 +03:00
|
|
|
static inline void *opal_thread_swap_ptr (volatile void *ptr, void *newvalue)
|
|
|
|
{
|
|
|
|
if (opal_using_threads ()) {
|
|
|
|
return opal_atomic_swap_ptr (ptr, newvalue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *old = ((void **) ptr)[0];
|
|
|
|
((void **) ptr)[0] = newvalue;
|
|
|
|
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPAL_ATOMIC_SWAP_PTR(x, y) opal_thread_swap_ptr (x, y)
|
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
END_C_DECLS
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#endif /* OPAL_MUTEX_H */
|