2015-10-14 15:43:13 -06:00
|
|
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
|
2004-08-18 23:24:27 +00:00
|
|
|
/*
|
2005-11-05 19:57:48 +00:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2006-08-23 00:29:35 +00:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 19:57:48 +00:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2015-06-23 20:59:57 -07:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2004-11-28 20:09:25 +00:00
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 12:43:37 +00:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2015-10-14 15:43:13 -06:00
|
|
|
* Copyright (c) 2007-2015 Los Alamos National Security, LLC. All rights
|
2015-06-23 20:59:57 -07:00
|
|
|
* reserved.
|
2014-10-21 19:49:58 +09:00
|
|
|
* Copyright (c) 2015 Research Organization for Information Science
|
|
|
|
* and Technology (RIST). All rights reserved.
|
2004-11-22 01:38:40 +00:00
|
|
|
* $COPYRIGHT$
|
2015-06-23 20:59:57 -07:00
|
|
|
*
|
2004-11-22 01:38:40 +00:00
|
|
|
* Additional copyrights may follow
|
2015-06-23 20:59:57 -07:00
|
|
|
*
|
2004-08-18 23:24:27 +00:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
#ifndef OPAL_MUTEX_UNIX_H
|
|
|
|
#define OPAL_MUTEX_UNIX_H 1
|
2004-08-18 23:24:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file:
|
|
|
|
*
|
2006-01-11 04:34:29 +00:00
|
|
|
* Mutual exclusion functions: Unix implementation.
|
2004-08-18 23:24:27 +00:00
|
|
|
*
|
|
|
|
* Functions for locking of critical sections.
|
|
|
|
*
|
|
|
|
* On unix, use pthreads or our own atomic operations as
|
|
|
|
* available.
|
|
|
|
*/
|
|
|
|
|
2009-03-04 15:35:54 +00:00
|
|
|
#include "opal_config.h"
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2004-10-20 01:03:09 +00:00
|
|
|
#ifdef HAVE_PTHREAD_H
|
2004-08-18 23:24:27 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2006-01-11 04:34:29 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2005-07-03 16:06:07 +00:00
|
|
|
#include "opal/class/opal_object.h"
|
2006-02-12 01:33:29 +00:00
|
|
|
#include "opal/sys/atomic.h"
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2007-06-12 16:25:26 +00:00
|
|
|
BEGIN_C_DECLS
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
struct opal_mutex_t {
|
2005-07-03 16:06:07 +00:00
|
|
|
opal_object_t super;
|
2007-06-12 16:25:26 +00:00
|
|
|
|
2004-08-19 19:29:01 +00:00
|
|
|
pthread_mutex_t m_lock_pthread;
|
2007-06-12 16:25:26 +00:00
|
|
|
|
2013-12-13 19:40:12 +00:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2007-06-12 16:25:26 +00:00
|
|
|
int m_lock_debug;
|
2007-11-05 13:03:35 +00:00
|
|
|
const char *m_lock_file;
|
2007-06-12 16:25:26 +00:00
|
|
|
int m_lock_line;
|
|
|
|
#endif
|
|
|
|
|
2005-07-03 21:38:51 +00:00
|
|
|
opal_atomic_lock_t m_lock_atomic;
|
2004-08-18 23:24:27 +00:00
|
|
|
};
|
2006-08-20 15:54:04 +00:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
|
2015-08-24 13:26:47 -06:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_recursive_mutex_t);
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2015-10-14 15:43:13 -06:00
|
|
|
#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
|
|
|
|
#define OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
|
|
|
|
#elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
|
|
|
|
#define OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if OPAL_ENABLE_DEBUG
|
|
|
|
#define OPAL_MUTEX_STATIC_INIT \
|
|
|
|
{ \
|
|
|
|
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
|
|
|
|
.m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, \
|
|
|
|
.m_lock_debug = 0, \
|
|
|
|
.m_lock_file = NULL, \
|
|
|
|
.m_lock_line = 0, \
|
|
|
|
.m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define OPAL_MUTEX_STATIC_INIT \
|
|
|
|
{ \
|
|
|
|
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
|
|
|
|
.m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, \
|
|
|
|
.m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
|
|
|
|
|
|
|
|
#if OPAL_ENABLE_DEBUG
|
|
|
|
#define OPAL_RECURSIVE_MUTEX_STATIC_INIT \
|
|
|
|
{ \
|
|
|
|
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
|
|
|
|
.m_lock_pthread = OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \
|
|
|
|
.m_lock_debug = 0, \
|
|
|
|
.m_lock_file = NULL, \
|
|
|
|
.m_lock_line = 0, \
|
|
|
|
.m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define OPAL_RECURSIVE_MUTEX_STATIC_INIT \
|
|
|
|
{ \
|
|
|
|
.super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \
|
|
|
|
.m_lock_pthread = OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \
|
|
|
|
.m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
/************************************************************************
|
|
|
|
*
|
|
|
|
* mutex operations (non-atomic versions)
|
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline int opal_mutex_trylock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2009-05-06 20:11:28 +00:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2006-01-11 04:34:29 +00:00
|
|
|
int ret = pthread_mutex_trylock(&m->m_lock_pthread);
|
|
|
|
if (ret == EDEADLK) {
|
|
|
|
errno = ret;
|
|
|
|
perror("opal_mutex_trylock()");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
#else
|
2004-08-19 19:29:01 +00:00
|
|
|
return pthread_mutex_trylock(&m->m_lock_pthread);
|
2006-01-11 04:34:29 +00:00
|
|
|
#endif
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline void opal_mutex_lock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2009-05-06 20:11:28 +00:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2006-01-11 20:56:22 +00:00
|
|
|
int ret = pthread_mutex_lock(&m->m_lock_pthread);
|
2006-01-11 04:34:29 +00:00
|
|
|
if (ret == EDEADLK) {
|
|
|
|
errno = ret;
|
|
|
|
perror("opal_mutex_lock()");
|
|
|
|
abort();
|
|
|
|
}
|
2006-01-11 20:56:22 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_lock(&m->m_lock_pthread);
|
2006-01-11 04:34:29 +00:00
|
|
|
#endif
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline void opal_mutex_unlock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2009-05-06 20:11:28 +00:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2006-01-11 20:56:22 +00:00
|
|
|
int ret = pthread_mutex_unlock(&m->m_lock_pthread);
|
2006-01-11 04:34:29 +00:00
|
|
|
if (ret == EPERM) {
|
|
|
|
errno = ret;
|
|
|
|
perror("opal_mutex_unlock");
|
|
|
|
abort();
|
|
|
|
}
|
2006-01-11 20:56:22 +00:00
|
|
|
#else
|
|
|
|
pthread_mutex_unlock(&m->m_lock_pthread);
|
2006-01-11 04:34:29 +00:00
|
|
|
#endif
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
/************************************************************************
|
|
|
|
*
|
|
|
|
* mutex operations (atomic versions)
|
|
|
|
*
|
|
|
|
************************************************************************/
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
#if OPAL_HAVE_ATOMIC_SPINLOCKS
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
/************************************************************************
|
|
|
|
* Spin Locks
|
|
|
|
************************************************************************/
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2005-07-03 21:38:51 +00:00
|
|
|
return opal_atomic_trylock(&m->m_lock_atomic);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2005-07-03 21:38:51 +00:00
|
|
|
opal_atomic_lock(&m->m_lock_atomic);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2005-07-03 21:38:51 +00:00
|
|
|
opal_atomic_unlock(&m->m_lock_atomic);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
/************************************************************************
|
2006-08-23 00:29:35 +00:00
|
|
|
* Standard locking
|
2006-01-11 04:34:29 +00:00
|
|
|
************************************************************************/
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2005-07-03 22:45:48 +00:00
|
|
|
return opal_mutex_trylock(m);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_lock(m);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_unlock(m);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-06-12 16:25:26 +00:00
|
|
|
END_C_DECLS
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
#endif /* OPAL_MUTEX_UNIX_H */
|