2004-08-19 03:24:27 +04: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.
|
2006-08-23 04:29:35 +04:00
|
|
|
* Copyright (c) 2004-2006 The University of Tennessee and The University
|
2005-11-05 22:57:48 +03:00
|
|
|
* 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.
|
2007-06-12 20:25:26 +04:00
|
|
|
* Copyright (c) 2007 Los Alamos National Security, LLC. All rights
|
|
|
|
* reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-08-19 03:24:27 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#ifndef OPAL_MUTEX_UNIX_H
|
|
|
|
#define OPAL_MUTEX_UNIX_H 1
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @file:
|
|
|
|
*
|
2006-01-11 07:34:29 +03:00
|
|
|
* Mutual exclusion functions: Unix implementation.
|
2004-08-19 03:24:27 +04:00
|
|
|
*
|
|
|
|
* Functions for locking of critical sections.
|
|
|
|
*
|
|
|
|
* On unix, use pthreads or our own atomic operations as
|
|
|
|
* available.
|
|
|
|
*/
|
|
|
|
|
2009-03-04 18:35:54 +03:00
|
|
|
#include "opal_config.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_HAVE_POSIX_THREADS
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_PTHREAD_H
|
2004-08-19 03:24:27 +04:00
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2006-01-11 07:34:29 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/sys/atomic.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
BEGIN_C_DECLS
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
struct opal_mutex_t {
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t super;
|
2007-06-12 20:25:26 +04:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_HAVE_POSIX_THREADS
|
2004-08-19 23:29:01 +04:00
|
|
|
pthread_mutex_t m_lock_pthread;
|
2004-08-25 18:24:16 +04:00
|
|
|
#endif
|
2007-06-12 20:25:26 +04:00
|
|
|
|
2013-12-13 23:40:12 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2007-06-12 20:25:26 +04:00
|
|
|
int m_lock_debug;
|
2007-11-05 16:03:35 +03:00
|
|
|
const char *m_lock_file;
|
2007-06-12 20:25:26 +04:00
|
|
|
int m_lock_line;
|
|
|
|
#endif
|
|
|
|
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_lock_t m_lock_atomic;
|
2004-08-19 03:24:27 +04:00
|
|
|
};
|
2006-08-20 19:54:04 +04:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
/************************************************************************
|
|
|
|
*
|
|
|
|
* mutex operations (non-atomic versions)
|
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_HAVE_POSIX_THREADS
|
2006-01-11 07:34:29 +03:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* POSIX threads
|
|
|
|
************************************************************************/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_trylock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2006-01-11 07:34:29 +03: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 23:29:01 +04:00
|
|
|
return pthread_mutex_trylock(&m->m_lock_pthread);
|
2006-01-11 07:34:29 +03:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_lock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2006-01-11 23:56:22 +03:00
|
|
|
int ret = pthread_mutex_lock(&m->m_lock_pthread);
|
2006-01-11 07:34:29 +03:00
|
|
|
if (ret == EDEADLK) {
|
|
|
|
errno = ret;
|
|
|
|
perror("opal_mutex_lock()");
|
|
|
|
abort();
|
|
|
|
}
|
2006-01-11 23:56:22 +03:00
|
|
|
#else
|
|
|
|
pthread_mutex_lock(&m->m_lock_pthread);
|
2006-01-11 07:34:29 +03:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_unlock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2009-05-07 00:11:28 +04:00
|
|
|
#if OPAL_ENABLE_DEBUG
|
2006-01-11 23:56:22 +03:00
|
|
|
int ret = pthread_mutex_unlock(&m->m_lock_pthread);
|
2006-01-11 07:34:29 +03:00
|
|
|
if (ret == EPERM) {
|
|
|
|
errno = ret;
|
|
|
|
perror("opal_mutex_unlock");
|
|
|
|
abort();
|
|
|
|
}
|
2006-01-11 23:56:22 +03:00
|
|
|
#else
|
|
|
|
pthread_mutex_unlock(&m->m_lock_pthread);
|
2006-01-11 07:34:29 +03:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
#elif OPAL_HAVE_ATOMIC_SPINLOCKS
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
/************************************************************************
|
|
|
|
* Spin Locks
|
|
|
|
************************************************************************/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_trylock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2006-01-11 07:34:29 +03:00
|
|
|
return opal_atomic_trylock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_lock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2006-01-11 07:34:29 +03:00
|
|
|
opal_atomic_lock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_unlock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2006-01-11 07:34:29 +03:00
|
|
|
opal_atomic_unlock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
#else
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
#error No mutex definition
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
#endif
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
/************************************************************************
|
|
|
|
*
|
|
|
|
* mutex operations (atomic versions)
|
|
|
|
*
|
|
|
|
************************************************************************/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
#if OPAL_HAVE_ATOMIC_SPINLOCKS
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
/************************************************************************
|
|
|
|
* Spin Locks
|
|
|
|
************************************************************************/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 01:38:51 +04:00
|
|
|
return opal_atomic_trylock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_lock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 01:38:51 +04:00
|
|
|
opal_atomic_unlock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2006-01-11 07:34:29 +03:00
|
|
|
#else
|
|
|
|
|
|
|
|
/************************************************************************
|
2006-08-23 04:29:35 +04:00
|
|
|
* Standard locking
|
2006-01-11 07:34:29 +03:00
|
|
|
************************************************************************/
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_mutex_atomic_trylock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
return opal_mutex_trylock(m);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_atomic_lock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(m);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(m);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
END_C_DECLS
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#endif /* OPAL_MUTEX_UNIX_H */
|