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.
|
2004-11-28 20:09:25 +00:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* 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.
|
2007-06-12 16:25:26 +00:00
|
|
|
* Copyright (c) 2007 Los Alamos National Security, LLC. All rights
|
|
|
|
* reserved.
|
2004-11-22 01:38:40 +00:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
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>
|
2007-06-12 16:25:26 +00:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
2006-02-26 16:56:38 +00:00
|
|
|
#include <thread.h>
|
2006-02-26 17:08:47 +00:00
|
|
|
#include <synch.h>
|
2004-10-20 01:03:09 +00:00
|
|
|
#endif
|
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-25 14:55:35 +00:00
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
2004-08-19 19:29:01 +00:00
|
|
|
pthread_mutex_t m_lock_pthread;
|
2007-06-12 16:25:26 +00:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
2006-01-11 04:34:29 +00:00
|
|
|
mutex_t m_lock_solaris;
|
2004-08-25 14:24:16 +00:00
|
|
|
#endif
|
2007-06-12 16:25:26 +00:00
|
|
|
|
|
|
|
#if !OMPI_HAVE_THREAD_SUPPORT && OMPI_ENABLE_DEBUG
|
|
|
|
int m_lock_debug;
|
|
|
|
char *m_lock_file;
|
|
|
|
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);
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
/************************************************************************
|
|
|
|
*
|
|
|
|
* mutex operations (non-atomic versions)
|
|
|
|
*
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* POSIX threads
|
|
|
|
************************************************************************/
|
2004-08-18 23:24:27 +00:00
|
|
|
|
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
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
#if OMPI_ENABLE_DEBUG
|
|
|
|
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
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
#if OMPI_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
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
#if OMPI_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
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
/************************************************************************
|
|
|
|
* Solaris threads
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
static inline int opal_mutex_trylock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
return mutex_trylock(&m->m_lock_solaris);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
static inline void opal_mutex_lock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
mutex_lock(&m->m_lock_solaris);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
static inline void opal_mutex_unlock(opal_mutex_t *m)
|
2004-08-18 23:24:27 +00:00
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
mutex_unlock(&m->m_lock_solaris);
|
2004-08-18 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
#elif 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
|
|
|
|
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
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
return opal_atomic_trylock(&m->m_lock_atomic);
|
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
|
|
|
{
|
2006-01-11 04:34:29 +00:00
|
|
|
opal_atomic_lock(&m->m_lock_atomic);
|
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
|
|
|
{
|
2006-01-11 04:34:29 +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
|
2004-08-18 23:24:27 +00:00
|
|
|
|
2006-01-11 04:34:29 +00:00
|
|
|
#error No mutex definition
|
2004-08-18 23:24:27 +00:00
|
|
|
|
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 */
|