2004-03-03 16:10:26 +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.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* 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.
|
2004-11-22 01:38:40 +00:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-03-03 16:10:26 +00:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
2004-06-24 21:09:55 +00:00
|
|
|
#ifndef OMPI_CONDITION_SPINLOCK_H
|
|
|
|
#define OMPI_CONDITION_SPINLOCK_H
|
2004-03-03 16:10:26 +00:00
|
|
|
|
2004-09-01 23:08:58 +00:00
|
|
|
#include "ompi_config.h"
|
2004-10-20 01:03:09 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2004-09-01 23:08:58 +00:00
|
|
|
#include <sys/time.h>
|
2004-10-20 01:03:09 +00:00
|
|
|
#endif
|
2005-07-03 22:45:48 +00:00
|
|
|
#include "opal/threads/condition.h"
|
|
|
|
#include "opal/threads/mutex.h"
|
2005-07-03 21:57:43 +00:00
|
|
|
#include "opal/runtime/opal_progress.h"
|
2004-03-03 16:10:26 +00:00
|
|
|
|
2004-10-20 22:31:03 +00:00
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2005-03-14 20:57:21 +00:00
|
|
|
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
struct opal_condition_t {
|
2005-07-03 16:06:07 +00:00
|
|
|
opal_object_t super;
|
2004-03-03 16:10:26 +00:00
|
|
|
volatile int c_waiting;
|
2004-04-06 16:13:17 +00:00
|
|
|
volatile int c_signaled;
|
2004-03-03 16:10:26 +00:00
|
|
|
};
|
2005-07-03 22:45:48 +00:00
|
|
|
typedef struct opal_condition_t opal_condition_t;
|
2004-03-03 16:10:26 +00:00
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
OMPI_DECLSPEC OBJ_CLASS_DECLARATION(opal_condition_t);
|
2004-03-03 16:10:26 +00:00
|
|
|
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline int opal_condition_wait(opal_condition_t *c, opal_mutex_t *m)
|
2004-03-03 16:10:26 +00:00
|
|
|
{
|
2004-04-06 16:13:17 +00:00
|
|
|
c->c_waiting++;
|
2005-07-03 22:45:48 +00:00
|
|
|
if (opal_using_threads()) {
|
2004-08-19 19:29:01 +00:00
|
|
|
while (c->c_signaled == 0) {
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_unlock(m);
|
2005-07-03 21:57:43 +00:00
|
|
|
opal_progress();
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_lock(m);
|
2004-05-18 21:06:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-08-19 19:29:01 +00:00
|
|
|
while (c->c_signaled == 0) {
|
2005-07-03 21:57:43 +00:00
|
|
|
opal_progress();
|
2004-05-18 21:06:11 +00:00
|
|
|
}
|
2004-04-06 16:13:17 +00:00
|
|
|
}
|
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
2004-03-03 16:10:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline int opal_condition_timedwait(opal_condition_t *c,
|
|
|
|
opal_mutex_t *m,
|
2004-08-19 19:29:01 +00:00
|
|
|
const struct timespec *abstime)
|
2004-03-03 16:10:26 +00:00
|
|
|
{
|
2004-09-01 23:08:58 +00:00
|
|
|
struct timeval tv;
|
2004-10-12 15:50:01 +00:00
|
|
|
struct timeval abs;
|
|
|
|
abs.tv_sec = abstime->tv_sec;
|
2005-02-08 18:57:07 +00:00
|
|
|
abs.tv_usec = abstime->tv_nsec / 1000;
|
2004-09-01 23:08:58 +00:00
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
|
|
|
|
c->c_waiting++;
|
2005-07-03 22:45:48 +00:00
|
|
|
if (opal_using_threads()) {
|
2004-09-01 23:08:58 +00:00
|
|
|
while (c->c_signaled == 0 &&
|
2004-10-12 15:50:01 +00:00
|
|
|
(tv.tv_sec <= abs.tv_sec ||
|
|
|
|
(tv.tv_sec == abs.tv_sec && tv.tv_usec < abs.tv_usec))) {
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_unlock(m);
|
2005-07-03 21:57:43 +00:00
|
|
|
opal_progress();
|
2004-09-01 23:08:58 +00:00
|
|
|
gettimeofday(&tv,NULL);
|
2005-07-03 22:45:48 +00:00
|
|
|
opal_mutex_lock(m);
|
2004-09-01 23:08:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (c->c_signaled == 0 &&
|
2004-10-12 15:50:01 +00:00
|
|
|
(tv.tv_sec <= abs.tv_sec ||
|
|
|
|
(tv.tv_sec == abs.tv_sec && tv.tv_usec < abs.tv_usec))) {
|
2005-07-03 21:57:43 +00:00
|
|
|
opal_progress();
|
2004-09-01 23:08:58 +00:00
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
2004-03-03 16:10:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline int opal_condition_signal(opal_condition_t *c)
|
2004-03-03 16:10:26 +00:00
|
|
|
{
|
2004-08-19 19:29:01 +00:00
|
|
|
if (c->c_waiting) {
|
|
|
|
c->c_signaled++;
|
2004-04-06 16:13:17 +00:00
|
|
|
}
|
2004-03-03 16:10:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-03 22:45:48 +00:00
|
|
|
static inline int opal_condition_broadcast(opal_condition_t *c)
|
2004-03-03 16:10:26 +00:00
|
|
|
{
|
2004-04-06 16:13:17 +00:00
|
|
|
c->c_signaled += c->c_waiting;
|
2004-03-03 16:10:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-20 22:31:03 +00:00
|
|
|
#if defined(c_plusplus) || defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
2004-03-03 16:10:26 +00:00
|
|
|
#endif
|
|
|
|
|