2005-03-14 23:57:21 +03:00
|
|
|
/*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University.
|
|
|
|
* All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of the University of Tennessee.
|
|
|
|
* 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.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-03-03 19:10:26 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
2005-03-14 23:57:21 +03:00
|
|
|
#ifndef OMPI_CONDITION_SPINLOCK_H
|
|
|
|
#define OMPI_CONDITION_SPINLOCK_H
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2004-06-07 19:33:53 +04:00
|
|
|
#include "ompi_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>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "threads/condition.h"
|
|
|
|
#include "threads/mutex.h"
|
|
|
|
#include "runtime/ompi_progress.h"
|
|
|
|
|
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Combine pthread support w/ polled progress to allow run-time selection
|
|
|
|
* of threading vs. non-threading progress.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ompi_condition_t {
|
|
|
|
ompi_object_t super;
|
|
|
|
volatile int c_waiting;
|
|
|
|
volatile int c_signaled;
|
|
|
|
#if OMPI_HAVE_POSIX_THREADS
|
|
|
|
pthread_cond_t c_cond;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
typedef struct ompi_condition_t ompi_condition_t;
|
|
|
|
|
|
|
|
OMPI_DECLSPEC OBJ_CLASS_DECLARATION(ompi_condition_t);
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
|
|
|
|
static inline int ompi_condition_wait(ompi_condition_t *c, ompi_mutex_t *m)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
c->c_waiting++;
|
|
|
|
if (ompi_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);
|
|
|
|
#else
|
|
|
|
while (c->c_signaled == 0) {
|
|
|
|
ompi_mutex_unlock(m);
|
|
|
|
ompi_progress();
|
|
|
|
ompi_mutex_lock(m);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
while (c->c_signaled == 0) {
|
|
|
|
ompi_progress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ompi_condition_timedwait(ompi_condition_t *c,
|
|
|
|
ompi_mutex_t *m,
|
|
|
|
const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct timeval abs;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
c->c_waiting++;
|
|
|
|
if (ompi_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);
|
2004-03-03 19:10:26 +03:00
|
|
|
#else
|
2005-03-14 23:57:21 +03:00
|
|
|
abs.tv_sec = abstime->tv_sec;
|
|
|
|
abs.tv_usec = abstime->tv_nsec * 1000;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
while (c->c_signaled == 0 &&
|
|
|
|
(tv.tv_sec <= abs.tv_sec ||
|
|
|
|
(tv.tv_sec == abs.tv_sec && tv.tv_usec < abs.tv_usec))) {
|
|
|
|
ompi_mutex_unlock(m);
|
|
|
|
ompi_progress();
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
ompi_mutex_lock(m);
|
|
|
|
}
|
2004-03-03 19:10:26 +03:00
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
} else {
|
|
|
|
abs.tv_sec = abstime->tv_sec;
|
|
|
|
abs.tv_usec = abstime->tv_nsec * 1000;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
while (c->c_signaled == 0 &&
|
|
|
|
(tv.tv_sec <= abs.tv_sec ||
|
|
|
|
(tv.tv_sec == abs.tv_sec && tv.tv_usec < abs.tv_usec))) {
|
|
|
|
ompi_progress();
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
|
|
|
return rc;
|
|
|
|
}
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
static inline int ompi_condition_signal(ompi_condition_t *c)
|
|
|
|
{
|
|
|
|
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-03-14 23:57:21 +03:00
|
|
|
if(ompi_using_threads()) {
|
|
|
|
pthread_cond_signal(&c->c_cond);
|
|
|
|
}
|
2004-03-03 19:10:26 +03:00
|
|
|
#endif
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ompi_condition_broadcast(ompi_condition_t *c)
|
|
|
|
{
|
|
|
|
c->c_signaled += c->c_waiting;
|
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
|
|
|
if(ompi_using_threads()) {
|
|
|
|
pthread_cond_broadcast(&c->c_cond);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|