2004-08-19 03:24:27 +04:00
|
|
|
/*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OMPI_MUTEX_UNIX_H
|
|
|
|
#define OMPI_MUTEX_UNIX_H 1
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file:
|
|
|
|
*
|
|
|
|
* Mutual exclusion functions: Windows implementation.
|
|
|
|
*
|
|
|
|
* Functions for locking of critical sections.
|
|
|
|
*
|
|
|
|
* On unix, use pthreads or our own atomic operations as
|
|
|
|
* available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "class/ompi_object.h"
|
|
|
|
#include "include/sys/atomic.h"
|
|
|
|
|
|
|
|
struct ompi_mutex_t {
|
|
|
|
ompi_object_t super;
|
2004-08-25 18:55:35 +04:00
|
|
|
#if OMPI_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
|
2004-08-19 23:29:01 +04:00
|
|
|
ompi_lock_t m_lock_atomic;
|
2004-08-19 03:24:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
OBJ_CLASS_DECLARATION(ompi_mutex_t);
|
|
|
|
|
|
|
|
|
2004-08-25 18:24:16 +04:00
|
|
|
#if OMPI_HAVE_ATOMIC && OMPI_HAVE_POSIX_THREADS
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ompi_mutex_* implemented using pthreads
|
|
|
|
* ompi_mutex_atomic_* implemented using atomic operations
|
|
|
|
*/
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline int ompi_mutex_trylock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
return pthread_mutex_trylock(&m->m_lock_pthread);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_lock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
pthread_mutex_lock(&m->m_lock_pthread);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_unlock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
pthread_mutex_unlock(&m->m_lock_pthread);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_atomic_lock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
ompi_atomic_lock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline int ompi_mutex_atomic_trylock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
return ompi_atomic_trylock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_atomic_unlock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
ompi_atomic_unlock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-25 18:24:16 +04:00
|
|
|
#elif OMPI_HAVE_POSIX_THREADS
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ompi_mutex_* and ompi_mutex_atomic_* implemented using pthreads
|
|
|
|
*/
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline int ompi_mutex_trylock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
return pthread_mutex_trylock(&m->m_lock_pthread);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_lock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
pthread_mutex_lock(&m->m_lock_pthread);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_unlock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
pthread_mutex_unlock(&m->m_lock_pthread);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline int ompi_mutex_atomic_trylock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
return ompi_mutex_trylock(m);
|
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_atomic_lock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
ompi_mutex_lock(m);
|
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_atomic_unlock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
ompi_mutex_unlock(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-25 18:24:16 +04:00
|
|
|
#elif OMPI_HAVE_ATOMIC
|
2004-08-19 03:24:27 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ompi_mutex_* and ompi_mutex_atomic_* implemented using atomic
|
|
|
|
* operations
|
|
|
|
*/
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline int ompi_mutex_trylock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
return ompi_atomic_trylock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_lock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
ompi_atomic_lock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_unlock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
2004-08-19 23:29:01 +04:00
|
|
|
ompi_atomic_unlock(&m->m_lock_atomic);
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline int ompi_mutex_atomic_trylock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
return ompi_mutex_trylock(m);
|
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_atomic_lock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
ompi_mutex_lock(m);
|
|
|
|
}
|
|
|
|
|
2004-08-19 23:29:01 +04:00
|
|
|
static inline void ompi_mutex_atomic_unlock(ompi_mutex_t *m)
|
2004-08-19 03:24:27 +04:00
|
|
|
{
|
|
|
|
ompi_mutex_unlock(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#error No mutex definition
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* OMPI_MUTEX_UNIX_H */
|