2004-03-03 16:10:26 +00:00
|
|
|
/*
|
2004-11-22 01:38:40 +00: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 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
|
2004-03-17 18:45:16 +00:00
|
|
|
#include "threads/condition.h"
|
2004-05-18 21:06:11 +00:00
|
|
|
#include "threads/mutex.h"
|
2004-06-07 15:33:53 +00:00
|
|
|
#include "runtime/ompi_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
|
|
|
|
|
|
|
|
2004-06-07 15:33:53 +00:00
|
|
|
struct ompi_condition_t {
|
2004-08-28 01:15:19 +00:00
|
|
|
ompi_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
|
|
|
};
|
2004-06-07 15:33:53 +00:00
|
|
|
typedef struct ompi_condition_t ompi_condition_t;
|
2004-03-03 16:10:26 +00:00
|
|
|
|
2004-10-22 16:06:05 +00:00
|
|
|
OMPI_DECLSPEC OBJ_CLASS_DECLARATION(ompi_condition_t);
|
2004-03-03 16:10:26 +00:00
|
|
|
|
|
|
|
|
2004-08-19 19:29:01 +00:00
|
|
|
static inline int ompi_condition_wait(ompi_condition_t *c, ompi_mutex_t *m)
|
2004-03-03 16:10:26 +00:00
|
|
|
{
|
2004-04-06 16:13:17 +00:00
|
|
|
c->c_waiting++;
|
2004-08-19 19:29:01 +00:00
|
|
|
if (ompi_using_threads()) {
|
|
|
|
while (c->c_signaled == 0) {
|
2004-06-07 15:33:53 +00:00
|
|
|
ompi_mutex_unlock(m);
|
|
|
|
ompi_progress();
|
|
|
|
ompi_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) {
|
2004-06-07 15:33:53 +00:00
|
|
|
ompi_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;
|
|
|
|
}
|
|
|
|
|
2004-08-19 19:29:01 +00:00
|
|
|
static inline int ompi_condition_timedwait(ompi_condition_t *c,
|
|
|
|
ompi_mutex_t *m,
|
|
|
|
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++;
|
|
|
|
if (ompi_using_threads()) {
|
|
|
|
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))) {
|
2004-09-01 23:08:58 +00:00
|
|
|
ompi_mutex_unlock(m);
|
|
|
|
ompi_progress();
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
ompi_mutex_lock(m);
|
|
|
|
}
|
|
|
|
} 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))) {
|
2004-09-01 23:08:58 +00:00
|
|
|
ompi_progress();
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c->c_signaled--;
|
|
|
|
c->c_waiting--;
|
2004-03-03 16:10:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-19 19:29:01 +00:00
|
|
|
static inline int ompi_condition_signal(ompi_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;
|
|
|
|
}
|
|
|
|
|
2004-08-19 19:29:01 +00:00
|
|
|
static inline int ompi_condition_broadcast(ompi_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
|
|
|
|
|