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
|
|
|
|
2015-01-07 18:45:54 +03:00
|
|
|
#include "opal/threads/thread_usage.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-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
|
|
|
/**
|
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)
|
|
|
|
|
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 */
|