2005-03-14 23:57:21 +03:00
|
|
|
/*
|
2007-03-17 02:11:45 +03:00
|
|
|
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
|
2005-11-05 22:57:48 +03:00
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* 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-03-03 19:10:26 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
#ifndef OPAL_CONDITION_SPINLOCK_H
|
|
|
|
#define OPAL_CONDITION_SPINLOCK_H
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2005-04-05 22:56:32 +04:00
|
|
|
#ifdef HAVE_TIME_H
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
|
|
|
#include <pthread.h>
|
2007-06-27 20:48:30 +04:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
|
|
|
#include <thread.h>
|
|
|
|
#include <synch.h>
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
#include "opal/threads/condition.h"
|
|
|
|
#include "opal/threads/mutex.h"
|
2005-07-04 01:57:43 +04:00
|
|
|
#include "opal/runtime/opal_progress.h"
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2007-03-17 02:11:45 +03:00
|
|
|
#include "opal/runtime/opal_cr.h"
|
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
BEGIN_C_DECLS
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Combine pthread support w/ polled progress to allow run-time selection
|
|
|
|
* of threading vs. non-threading progress.
|
|
|
|
*/
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
struct opal_condition_t {
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t super;
|
2005-03-14 23:57:21 +03:00
|
|
|
volatile int c_waiting;
|
|
|
|
volatile int c_signaled;
|
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
|
|
|
pthread_cond_t c_cond;
|
2007-06-27 20:48:30 +04:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS
|
|
|
|
cond_t c_cond;
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
};
|
2005-07-04 02:45:48 +04:00
|
|
|
typedef struct opal_condition_t opal_condition_t;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2006-08-20 19:54:04 +04:00
|
|
|
OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_condition_t);
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_condition_wait(opal_condition_t *c, opal_mutex_t *m)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
c->c_waiting++;
|
2007-06-12 20:25:26 +04:00
|
|
|
|
|
|
|
#if OMPI_ENABLE_DEBUG && !OMPI_HAVE_THREAD_SUPPORT
|
2007-06-22 19:28:12 +04:00
|
|
|
if (opal_mutex_check_locks && 0 == m->m_lock_debug) { \
|
2007-06-12 20:25:26 +04:00
|
|
|
opal_output(0, "Warning -- mutex not locked in condition_wait"); \
|
|
|
|
} \
|
|
|
|
m->m_lock_debug--;
|
|
|
|
#endif
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
if (opal_using_threads()) {
|
2005-03-15 00:57:34 +03:00
|
|
|
#if OMPI_HAVE_POSIX_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
2005-03-14 23:57:21 +03:00
|
|
|
rc = pthread_cond_wait(&c->c_cond, &m->m_lock_pthread);
|
2007-06-27 20:48:30 +04:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
|
|
|
rc = cond_wait(&c->c_cond, &m->m_lock_solaris);
|
2005-03-14 23:57:21 +03:00
|
|
|
#else
|
2006-03-29 18:54:58 +04:00
|
|
|
if (c->c_signaled) {
|
|
|
|
c->c_waiting--;
|
|
|
|
opal_mutex_unlock(m);
|
|
|
|
opal_progress();
|
|
|
|
opal_mutex_lock(m);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
while (c->c_signaled == 0) {
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(m);
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress();
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(m);
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
while (c->c_signaled == 0) {
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress();
|
2007-03-17 02:11:45 +03:00
|
|
|
OPAL_CR_TEST_CHECKPOINT_READY_STALL();
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 20:25:26 +04:00
|
|
|
|
|
|
|
#if OMPI_ENABLE_DEBUG && !OMPI_HAVE_THREAD_SUPPORT
|
|
|
|
m->m_lock_debug++;
|
|
|
|
#endif
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_condition_timedwait(opal_condition_t *c,
|
|
|
|
opal_mutex_t *m,
|
2005-03-14 23:57:21 +03:00
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2007-01-19 22:48:06 +03:00
|
|
|
struct timeval absolute;
|
2005-03-14 23:57:21 +03:00
|
|
|
int rc = 0;
|
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
#if OMPI_ENABLE_DEBUG && !OMPI_HAVE_THREAD_SUPPORT
|
2007-06-22 19:28:12 +04:00
|
|
|
if (opal_mutex_check_locks && 0 == m->m_lock_debug) { \
|
2007-06-12 20:25:26 +04:00
|
|
|
opal_output(0, "Warning -- mutex not locked in condition_wait"); \
|
|
|
|
} \
|
|
|
|
m->m_lock_debug--;
|
|
|
|
#endif
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
c->c_waiting++;
|
2005-07-04 02:45:48 +04:00
|
|
|
if (opal_using_threads()) {
|
2005-03-15 00:57:34 +03:00
|
|
|
#if OMPI_HAVE_POSIX_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
2005-03-14 23:57:21 +03:00
|
|
|
rc = pthread_cond_timedwait(&c->c_cond, &m->m_lock_pthread, abstime);
|
2007-06-27 20:48:30 +04:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
|
|
|
/* deal with const-ness */
|
|
|
|
timestruc_t to;
|
|
|
|
to.tv_sec = abstime->tv_sec;
|
|
|
|
to.tv_nsec = abstime->tv_nsec;
|
|
|
|
rc = cond_timedwait(&c->c_cond, &m->m_lock_solaris, &to);
|
2004-03-03 19:10:26 +03:00
|
|
|
#else
|
2007-01-19 22:48:06 +03:00
|
|
|
absolute.tv_sec = abstime->tv_sec;
|
|
|
|
absolute.tv_usec = abstime->tv_nsec * 1000;
|
2005-03-14 23:57:21 +03:00
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
while (c->c_signaled == 0 &&
|
2007-01-19 22:48:06 +03:00
|
|
|
(tv.tv_sec <= absolute.tv_sec ||
|
|
|
|
(tv.tv_sec == absolute.tv_sec && tv.tv_usec < absolute.tv_usec))) {
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_unlock(m);
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress();
|
2005-03-14 23:57:21 +03:00
|
|
|
gettimeofday(&tv,NULL);
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(m);
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
2004-03-03 19:10:26 +03:00
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
} else {
|
2007-01-19 22:48:06 +03:00
|
|
|
absolute.tv_sec = abstime->tv_sec;
|
|
|
|
absolute.tv_usec = abstime->tv_nsec * 1000;
|
2005-03-14 23:57:21 +03:00
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
while (c->c_signaled == 0 &&
|
2007-01-19 22:48:06 +03:00
|
|
|
(tv.tv_sec <= absolute.tv_sec ||
|
|
|
|
(tv.tv_sec == absolute.tv_sec && tv.tv_usec < absolute.tv_usec))) {
|
2005-07-04 01:57:43 +04:00
|
|
|
opal_progress();
|
2005-03-14 23:57:21 +03:00
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
}
|
|
|
|
}
|
2007-06-12 20:25:26 +04:00
|
|
|
|
|
|
|
#if OMPI_ENABLE_DEBUG && !OMPI_HAVE_THREAD_SUPPORT
|
|
|
|
m->m_lock_debug++;
|
|
|
|
#endif
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
|
|
|
return rc;
|
|
|
|
}
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_condition_signal(opal_condition_t *c)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
|
|
|
if (c->c_waiting) {
|
|
|
|
c->c_signaled++;
|
2005-03-15 00:57:34 +03:00
|
|
|
#if OMPI_HAVE_POSIX_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
2005-07-04 02:45:48 +04:00
|
|
|
if(opal_using_threads()) {
|
2005-03-14 23:57:21 +03:00
|
|
|
pthread_cond_signal(&c->c_cond);
|
|
|
|
}
|
2007-06-27 20:48:30 +04:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
|
|
|
if(opal_using_threads()) {
|
|
|
|
cond_signal(&c->c_cond);
|
|
|
|
}
|
2004-03-03 19:10:26 +03:00
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static inline int opal_condition_broadcast(opal_condition_t *c)
|
2005-03-14 23:57:21 +03:00
|
|
|
{
|
2006-03-29 18:54:58 +04:00
|
|
|
c->c_signaled = c->c_waiting;
|
2005-03-15 00:57:34 +03:00
|
|
|
#if OMPI_HAVE_POSIX_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
2005-07-04 02:45:48 +04:00
|
|
|
if(opal_using_threads()) {
|
2006-02-13 00:36:07 +03:00
|
|
|
if( 1 == c->c_waiting ) {
|
|
|
|
pthread_cond_signal(&c->c_cond);
|
|
|
|
} else {
|
|
|
|
pthread_cond_broadcast(&c->c_cond);
|
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
2007-06-27 20:48:30 +04:00
|
|
|
#elif OMPI_HAVE_SOLARIS_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
|
|
|
if(opal_using_threads()) {
|
|
|
|
cond_broadcast(&c->c_cond);
|
|
|
|
}
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-06-12 20:25:26 +04:00
|
|
|
END_C_DECLS
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
#endif
|
|
|
|
|