2004-09-26 21:43:35 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03: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 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-09-26 21:43:35 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-10-22 20:06:05 +04:00
|
|
|
#include "ompi_config.h"
|
2004-09-26 21:43:35 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2004-09-26 21:43:35 +04:00
|
|
|
#include <sys/time.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2004-09-26 21:43:35 +04:00
|
|
|
#include <sys/types.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-09-29 22:40:46 +04:00
|
|
|
#include <signal.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2004-09-26 21:43:35 +04:00
|
|
|
#include <sys/wait.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-04 03:31:27 +04:00
|
|
|
#include "opal/util/output.h"
|
2005-07-03 20:06:07 +04:00
|
|
|
#include "opal/class/opal_object.h"
|
2005-07-03 20:22:16 +04:00
|
|
|
#include "opal/class/opal_list.h"
|
2005-07-04 03:09:55 +04:00
|
|
|
#include "opal/event/event.h"
|
2005-07-04 02:45:48 +04:00
|
|
|
#include "opal/threads/mutex.h"
|
|
|
|
#include "opal/threads/condition.h"
|
2005-08-15 22:25:35 +04:00
|
|
|
#include "orte/include/orte_constants.h"
|
|
|
|
#include "orte/runtime/orte_wait.h"
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-14 22:05:30 +04:00
|
|
|
#ifdef HAVE_WAITPID
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Local Class Declarations
|
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
struct blk_waitpid_data_t {
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t super;
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_condition_t *cond;
|
2004-09-26 21:43:35 +04:00
|
|
|
volatile int done;
|
|
|
|
volatile int status;
|
2004-10-27 02:11:03 +04:00
|
|
|
volatile int free;
|
2004-09-26 21:43:35 +04:00
|
|
|
};
|
|
|
|
typedef struct blk_waitpid_data_t blk_waitpid_data_t;
|
|
|
|
|
|
|
|
struct pending_pids_item_t {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-09-26 21:43:35 +04:00
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
};
|
|
|
|
typedef struct pending_pids_item_t pending_pids_item_t;
|
|
|
|
|
|
|
|
struct registered_cb_item_t {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t super;
|
2004-09-26 21:43:35 +04:00
|
|
|
pid_t pid;
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_fn_t callback;
|
2004-09-26 21:43:35 +04:00
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
typedef struct registered_cb_item_t registered_cb_item_t;
|
|
|
|
|
2004-12-14 18:47:31 +03:00
|
|
|
struct waitpid_callback_data_t {
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
int options;
|
|
|
|
pid_t ret;
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_t mutex;
|
|
|
|
opal_condition_t cond;
|
2004-12-14 18:47:31 +03:00
|
|
|
volatile bool done;
|
|
|
|
};
|
|
|
|
typedef struct waitpid_callback_data_t waitpid_callback_data_t;
|
|
|
|
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Local Class Definitions
|
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
static void
|
2005-07-03 20:06:07 +04:00
|
|
|
blk_waitpid_data_construct(opal_object_t *obj)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
|
|
|
blk_waitpid_data_t *data = (blk_waitpid_data_t*) obj;
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
data->cond = OBJ_NEW(opal_condition_t);
|
2004-09-26 21:43:35 +04:00
|
|
|
data->done = 0;
|
|
|
|
data->status = 0;
|
2004-10-27 02:11:03 +04:00
|
|
|
data->free = 0;
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2005-07-03 20:06:07 +04:00
|
|
|
blk_waitpid_data_destruct(opal_object_t *obj)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
|
|
|
blk_waitpid_data_t *data = (blk_waitpid_data_t*) obj;
|
|
|
|
|
|
|
|
if (NULL != data->cond) OBJ_RELEASE(data->cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-03 20:06:07 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(blk_waitpid_data_t, opal_object_t,
|
2004-09-26 21:43:35 +04:00
|
|
|
blk_waitpid_data_construct,
|
|
|
|
blk_waitpid_data_destruct);
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(pending_pids_item_t, opal_list_item_t, NULL, NULL);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(registered_cb_item_t, opal_list_item_t, NULL, NULL);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Local Variables
|
|
|
|
*
|
|
|
|
********************************************************************/
|
2004-10-27 02:11:03 +04:00
|
|
|
static volatile int cb_enabled = true;
|
2005-07-04 02:45:48 +04:00
|
|
|
static opal_mutex_t mutex;
|
2005-07-03 20:22:16 +04:00
|
|
|
static opal_list_t pending_pids;
|
|
|
|
static opal_list_t registered_cb;
|
2005-07-04 03:09:55 +04:00
|
|
|
static struct opal_event handler;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Local Function Prototypes
|
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
static void blk_waitpid_cb(pid_t wpid, int status, void *data);
|
|
|
|
static pending_pids_item_t* find_pending_pid(pid_t pid, bool create);
|
|
|
|
static registered_cb_item_t* find_waiting_cb(pid_t pid, bool create);
|
|
|
|
static void do_waitall(int options);
|
2004-10-27 02:11:03 +04:00
|
|
|
static void trigger_callback(registered_cb_item_t *cb,
|
|
|
|
pending_pids_item_t *pending);
|
2005-03-14 23:57:21 +03:00
|
|
|
static int register_callback(pid_t pid, orte_wait_fn_t callback,
|
2004-09-26 21:43:35 +04:00
|
|
|
void *data);
|
2004-10-27 02:11:03 +04:00
|
|
|
static int unregister_callback(pid_t pid);
|
2005-03-14 23:57:21 +03:00
|
|
|
void orte_wait_signal_callback(int fd, short event, void *arg);
|
2004-12-14 18:47:31 +03:00
|
|
|
static pid_t internal_waitpid(pid_t pid, int *status, int options);
|
2005-03-14 23:57:21 +03:00
|
|
|
#if OMPI_THREADS_HAVE_DIFFERENT_PIDS
|
2004-12-14 18:47:31 +03:00
|
|
|
static void internal_waitpid_callback(int fd, short event, void *arg);
|
|
|
|
#endif
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Interface Functions
|
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
int
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_init(void)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CONSTRUCT(&mutex, opal_mutex_t);
|
2005-07-03 20:22:16 +04:00
|
|
|
OBJ_CONSTRUCT(&pending_pids, opal_list_t);
|
|
|
|
OBJ_CONSTRUCT(®istered_cb, opal_list_t);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-04 03:09:55 +04:00
|
|
|
opal_event_set(&handler, SIGCHLD, OPAL_EV_SIGNAL|OPAL_EV_PERSIST,
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_signal_callback,
|
|
|
|
&handler);
|
|
|
|
|
2005-07-04 03:09:55 +04:00
|
|
|
opal_event_add(&handler, NULL);
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_finalize(void)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2005-07-04 03:09:55 +04:00
|
|
|
opal_event_del(&handler);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
/* clear out the lists */
|
2005-07-03 20:22:16 +04:00
|
|
|
while (NULL != (item = opal_list_remove_first(&pending_pids))) {
|
2004-09-26 21:43:35 +04:00
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
2005-07-03 20:22:16 +04:00
|
|
|
while (NULL != (item = opal_list_remove_first(®istered_cb))) {
|
2004-09-26 21:43:35 +04:00
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
OBJ_DESTRUCT(&mutex);
|
|
|
|
OBJ_DESTRUCT(&pending_pids);
|
|
|
|
OBJ_DESTRUCT(®istered_cb);
|
|
|
|
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
int
|
|
|
|
orte_wait_kill(int sig)
|
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_t children;
|
|
|
|
opal_list_item_t* item;
|
2005-03-14 23:57:21 +03:00
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
OBJ_CONSTRUCT(&children, opal_list_t);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2005-03-14 23:57:21 +03:00
|
|
|
do_waitall(0);
|
2005-07-03 20:22:16 +04:00
|
|
|
while (NULL != (item = opal_list_remove_first(®istered_cb))) {
|
2005-03-14 23:57:21 +03:00
|
|
|
registered_cb_item_t *cb = (registered_cb_item_t*)item;
|
|
|
|
pending_pids_item_t *pending = find_pending_pid(cb->pid,false);
|
|
|
|
if(NULL == pending) {
|
|
|
|
int status;
|
|
|
|
kill(cb->pid, sig);
|
|
|
|
waitpid(cb->pid,&status,0);
|
|
|
|
} else {
|
|
|
|
OBJ_RELEASE(pending);
|
|
|
|
}
|
|
|
|
OBJ_RELEASE(item);
|
|
|
|
}
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2005-03-14 23:57:21 +03:00
|
|
|
}
|
|
|
|
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
pid_t
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_waitpid(pid_t wpid, int *status, int options)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
|
|
|
pending_pids_item_t *pending = NULL;
|
|
|
|
blk_waitpid_data_t *data = NULL;
|
|
|
|
struct timespec spintime;
|
|
|
|
pid_t ret;
|
|
|
|
|
|
|
|
if ((wpid <= 0) || (0 != (options & WUNTRACED))) {
|
2005-08-15 22:25:35 +04:00
|
|
|
errno = ORTE_ERR_NOT_IMPLEMENTED;
|
2004-09-26 21:43:35 +04:00
|
|
|
return (pid_t) -1;
|
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
do_waitall(options);
|
|
|
|
pending = find_pending_pid(wpid, false);
|
|
|
|
if (NULL != pending) {
|
|
|
|
*status = pending->status;
|
|
|
|
ret = pending->pid;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(&pending_pids, (opal_list_item_t*) pending);
|
2004-09-26 21:43:35 +04:00
|
|
|
OBJ_RELEASE(pending);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == (options & WNOHANG)) {
|
|
|
|
/* blocking - create a blk_waitpid_data_t, register the
|
|
|
|
callback with it, and wait for the trigger. Hold mutex
|
|
|
|
until after we register so that waitpid isn't called before
|
|
|
|
the callback is registered. There is a race condition
|
|
|
|
between starting to sit in the condition_wait and the
|
|
|
|
callback being triggered, so poll for completion on the
|
2005-03-28 19:57:11 +04:00
|
|
|
event just in case. */
|
2004-09-26 21:43:35 +04:00
|
|
|
data = OBJ_NEW(blk_waitpid_data_t);
|
|
|
|
if (NULL == data) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
register_callback(wpid, blk_waitpid_cb, data);
|
|
|
|
|
|
|
|
while (0 == data->done) {
|
|
|
|
spintime.tv_sec = 0;
|
|
|
|
spintime.tv_nsec = 1 * 1000 * 1000; /* 1 milliseconds */
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_condition_timedwait(data->cond,
|
2005-03-14 23:57:21 +03:00
|
|
|
&mutex,
|
2004-09-26 21:43:35 +04:00
|
|
|
&spintime);
|
2005-03-28 19:57:11 +04:00
|
|
|
|
|
|
|
/* if we have pthreads and progress threads and we are the
|
2005-07-04 02:45:48 +04:00
|
|
|
event thread, opal_condition_timedwait won't progress
|
2005-03-28 19:57:11 +04:00
|
|
|
anything, so we need to do it. */
|
|
|
|
#if OMPI_HAVE_POSIX_THREADS && OMPI_ENABLE_PROGRESS_THREADS
|
2005-07-04 02:45:48 +04:00
|
|
|
if (opal_using_threads()) {
|
|
|
|
opal_mutex_unlock(&mutex);
|
2005-07-04 03:09:55 +04:00
|
|
|
opal_event_loop(OPAL_EVLOOP_NONBLOCK);
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_mutex_lock(&mutex);
|
2004-10-14 03:08:47 +04:00
|
|
|
}
|
2005-03-28 19:57:11 +04:00
|
|
|
#endif
|
2004-10-27 02:11:03 +04:00
|
|
|
do_waitall(0);
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = wpid;
|
|
|
|
*status = data->status;
|
|
|
|
|
2005-03-28 19:57:11 +04:00
|
|
|
/* Unlock the mutex first, so as to not cause any deadlocks.
|
|
|
|
We aren't going to touch any variables that could cause
|
|
|
|
problems with thread badness, so it's ok to be here without
|
|
|
|
the thread locked. Wich is also the reason we go to done
|
|
|
|
instead of cleanup. */
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2005-03-28 19:57:11 +04:00
|
|
|
|
2004-10-27 02:11:03 +04:00
|
|
|
while (0 == data->free) {
|
|
|
|
/* don't free the condition variable until we are positive
|
|
|
|
that the broadcast is done being sent. Otherwise,
|
|
|
|
pthreads gets really unhappy when we pull the rug out
|
|
|
|
from under it. Yes, it's spinning. No, we won't spin
|
2005-03-28 19:57:11 +04:00
|
|
|
for long. */
|
|
|
|
|
2005-07-04 03:09:55 +04:00
|
|
|
if (!OMPI_HAVE_THREAD_SUPPORT || opal_event_progress_thread()) {
|
|
|
|
opal_event_loop(OPAL_EVLOOP_NONBLOCK);
|
2004-10-27 02:11:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-26 21:43:35 +04:00
|
|
|
OBJ_RELEASE(data);
|
2005-03-28 19:57:11 +04:00
|
|
|
/* see note above while loop for why we jump to done */
|
2004-10-27 02:11:03 +04:00
|
|
|
goto done;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* non-blocking - return what waitpid would */
|
2004-12-14 18:47:31 +03:00
|
|
|
ret = internal_waitpid(wpid, status, options);
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
|
|
|
|
done:
|
2004-09-26 21:43:35 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_cb(pid_t wpid, orte_wait_fn_t callback, void *data)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2005-08-15 22:25:35 +04:00
|
|
|
if (wpid <= 0) return ORTE_ERR_NOT_IMPLEMENTED;
|
|
|
|
if (NULL == callback) return ORTE_ERR_BAD_PARAM;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2004-09-26 21:43:35 +04:00
|
|
|
ret = register_callback(wpid, callback, data);
|
2005-03-14 23:57:21 +03:00
|
|
|
do_waitall(0);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-27 02:11:03 +04:00
|
|
|
int
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_cb_cancel(pid_t wpid)
|
2004-10-27 02:11:03 +04:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2005-08-15 22:25:35 +04:00
|
|
|
if (wpid <= 0) return ORTE_ERR_BAD_PARAM;
|
2004-10-27 02:11:03 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
do_waitall(0);
|
|
|
|
ret = unregister_callback(wpid);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* callback from the event library whenever a SIGCHLD is received */
|
2004-09-26 21:43:35 +04:00
|
|
|
void
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_signal_callback(int fd, short event, void *arg)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
2005-07-04 03:09:55 +04:00
|
|
|
struct opal_event *signal = (struct opal_event*) arg;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-04 03:09:55 +04:00
|
|
|
if (SIGCHLD != OPAL_EVENT_SIGNAL(signal)) return;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2004-09-26 21:43:35 +04:00
|
|
|
do_waitall(0);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-27 02:11:03 +04:00
|
|
|
int
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_cb_disable()
|
2004-10-27 02:11:03 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
do_waitall(0);
|
|
|
|
cb_enabled = false;
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2004-10-27 02:11:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2005-03-14 23:57:21 +03:00
|
|
|
orte_wait_cb_enable()
|
2004-10-27 02:11:03 +04:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
cb_enabled = true;
|
|
|
|
do_waitall(0);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2004-10-27 02:11:03 +04:00
|
|
|
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2004-10-27 02:11:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-26 21:43:35 +04:00
|
|
|
/*********************************************************************
|
|
|
|
*
|
|
|
|
* Local Functions
|
|
|
|
*
|
|
|
|
* None of these functions should lock mutex. All but blk_waitpid_cb
|
|
|
|
* should only be called if the mutex is already locked.
|
|
|
|
*
|
|
|
|
********************************************************************/
|
|
|
|
static void
|
|
|
|
blk_waitpid_cb(pid_t wpid, int status, void *data)
|
|
|
|
{
|
|
|
|
blk_waitpid_data_t *wp_data = (blk_waitpid_data_t*) data;
|
|
|
|
|
|
|
|
wp_data->status = status;
|
|
|
|
wp_data->done = 1;
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_condition_signal(wp_data->cond);
|
2004-10-27 02:11:03 +04:00
|
|
|
wp_data->free = 1;
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -1 will return the first available pid */
|
|
|
|
static pending_pids_item_t *
|
|
|
|
find_pending_pid(pid_t pid, bool create)
|
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item;
|
2004-09-26 21:43:35 +04:00
|
|
|
pending_pids_item_t *pending;
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_get_first(&pending_pids) ;
|
|
|
|
item != opal_list_get_end(&pending_pids) ;
|
|
|
|
item = opal_list_get_next(item)) {
|
2004-09-26 21:43:35 +04:00
|
|
|
pending = (pending_pids_item_t*) item;
|
|
|
|
|
|
|
|
if (pending->pid == pid || -1 == pid) {
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create) {
|
|
|
|
pending = OBJ_NEW(pending_pids_item_t);
|
|
|
|
if (NULL == pending) return NULL;
|
|
|
|
|
|
|
|
pending->pid = pid;
|
|
|
|
pending->status = 0;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(&pending_pids, (opal_list_item_t*) pending);
|
2004-09-26 21:43:35 +04:00
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* pid must be positive */
|
|
|
|
static registered_cb_item_t *
|
|
|
|
find_waiting_cb(pid_t pid, bool create)
|
|
|
|
{
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_item_t *item = NULL;
|
2004-09-26 21:43:35 +04:00
|
|
|
registered_cb_item_t *reg_cb = NULL;
|
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
for (item = opal_list_get_first(®istered_cb) ;
|
|
|
|
item != opal_list_get_end(®istered_cb) ;
|
|
|
|
item = opal_list_get_next(item)) {
|
2004-09-26 21:43:35 +04:00
|
|
|
reg_cb = (registered_cb_item_t*) item;
|
|
|
|
|
|
|
|
if (reg_cb->pid == pid) {
|
|
|
|
return reg_cb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create) {
|
|
|
|
reg_cb = OBJ_NEW(registered_cb_item_t);
|
|
|
|
if (NULL == reg_cb) return NULL;
|
|
|
|
|
|
|
|
reg_cb->pid = pid;
|
|
|
|
reg_cb->callback = NULL;
|
|
|
|
reg_cb->data = NULL;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(®istered_cb, (opal_list_item_t*) reg_cb);
|
2004-09-26 21:43:35 +04:00
|
|
|
return reg_cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_waitall(int options)
|
|
|
|
{
|
2004-10-27 02:11:03 +04:00
|
|
|
if (!cb_enabled) return;
|
2004-09-26 21:43:35 +04:00
|
|
|
while (1) {
|
2005-03-14 23:57:21 +03:00
|
|
|
int status;
|
|
|
|
pid_t ret = internal_waitpid(-1, &status, WNOHANG);
|
|
|
|
pending_pids_item_t *pending;
|
|
|
|
registered_cb_item_t *cb;
|
|
|
|
|
2004-09-26 21:43:35 +04:00
|
|
|
if (-1 == ret && EINTR == errno) continue;
|
|
|
|
if (ret <= 0) break;
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
cb = find_waiting_cb(ret, false);
|
|
|
|
if (NULL == cb) {
|
|
|
|
pending = OBJ_NEW(pending_pids_item_t);
|
|
|
|
pending->pid = ret;
|
|
|
|
pending->status = status;
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_append(&pending_pids, &pending->super);
|
2005-03-14 23:57:21 +03:00
|
|
|
} else {
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(®istered_cb, &cb->super);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&mutex);
|
2005-03-14 23:57:21 +03:00
|
|
|
cb->callback(cb->pid, status, cb->data);
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&mutex);
|
2005-03-14 23:57:21 +03:00
|
|
|
OBJ_RELEASE(cb);
|
|
|
|
}
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
trigger_callback(registered_cb_item_t *cb, pending_pids_item_t *pending)
|
|
|
|
{
|
|
|
|
assert(cb->pid == pending->pid);
|
|
|
|
|
|
|
|
cb->callback(cb->pid, pending->status, cb->data);
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(&pending_pids, (opal_list_item_t*) pending);
|
|
|
|
opal_list_remove_item(®istered_cb, (opal_list_item_t*) cb);
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2005-03-14 23:57:21 +03:00
|
|
|
register_callback(pid_t pid, orte_wait_fn_t callback, void *data)
|
2004-09-26 21:43:35 +04:00
|
|
|
{
|
|
|
|
registered_cb_item_t *reg_cb;
|
|
|
|
pending_pids_item_t *pending;
|
|
|
|
|
|
|
|
/* register the callback */
|
|
|
|
reg_cb = find_waiting_cb(pid, true);
|
2005-08-15 22:25:35 +04:00
|
|
|
if (NULL == reg_cb) return ORTE_ERROR;
|
|
|
|
if (NULL != reg_cb->callback) return ORTE_EXISTS;
|
2004-09-26 21:43:35 +04:00
|
|
|
|
|
|
|
reg_cb->pid = pid;
|
|
|
|
reg_cb->callback = callback;
|
|
|
|
reg_cb->data = data;
|
|
|
|
|
|
|
|
/* make sure we shouldn't trigger right now */
|
|
|
|
pending = find_pending_pid(pid, false);
|
|
|
|
if (NULL != pending) {
|
|
|
|
trigger_callback(reg_cb, pending);
|
|
|
|
}
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-27 02:11:03 +04:00
|
|
|
static int
|
|
|
|
unregister_callback(pid_t pid)
|
|
|
|
{
|
|
|
|
registered_cb_item_t *reg_cb;
|
|
|
|
|
|
|
|
/* register the callback */
|
|
|
|
reg_cb = find_waiting_cb(pid, false);
|
2005-08-15 22:25:35 +04:00
|
|
|
if (NULL == reg_cb) return ORTE_ERR_BAD_PARAM;
|
2004-10-27 02:11:03 +04:00
|
|
|
|
2005-07-03 20:22:16 +04:00
|
|
|
opal_list_remove_item(®istered_cb, (opal_list_item_t*) reg_cb);
|
2004-10-27 02:11:03 +04:00
|
|
|
|
2005-08-15 22:25:35 +04:00
|
|
|
return ORTE_SUCCESS;
|
2004-10-27 02:11:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-14 18:47:31 +03:00
|
|
|
|
|
|
|
static pid_t
|
|
|
|
internal_waitpid(pid_t pid, int *status, int options)
|
|
|
|
{
|
2005-03-14 23:57:21 +03:00
|
|
|
#if OMPI_THREADS_HAVE_DIFFERENT_PIDS
|
2004-12-14 18:47:31 +03:00
|
|
|
waitpid_callback_data_t data;
|
|
|
|
struct timeval tv;
|
2005-07-04 03:09:55 +04:00
|
|
|
struct opal_event ev;
|
2004-12-14 18:47:31 +03:00
|
|
|
|
2005-07-04 03:09:55 +04:00
|
|
|
if (opal_event_progress_thread()) {
|
2004-12-14 18:47:31 +03:00
|
|
|
/* I already am the progress thread. no need to event me */
|
|
|
|
return waitpid(pid, status, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
data.done = false;
|
|
|
|
data.pid = pid;
|
|
|
|
data.options = options;
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CONSTRUCT(&(data.mutex), opal_mutex_t);
|
|
|
|
OBJ_CONSTRUCT(&(data.cond), opal_condition_t);
|
2004-12-14 18:47:31 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_LOCK(&(data.mutex));
|
2004-12-14 18:47:31 +03:00
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
2005-07-04 03:09:55 +04:00
|
|
|
opal_evtimer_set(&ev, internal_waitpid_callback, &data);
|
|
|
|
opal_evtimer_add(&ev, &tv);
|
2004-12-14 18:47:31 +03:00
|
|
|
|
|
|
|
while (data.done == false) {
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_condition_wait(&(data.cond), &(data.mutex));
|
2004-12-14 18:47:31 +03:00
|
|
|
}
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OPAL_THREAD_UNLOCK(&(data.mutex));
|
2004-12-14 18:47:31 +03:00
|
|
|
|
|
|
|
OBJ_DESTRUCT(&(data.cond));
|
|
|
|
OBJ_DESTRUCT(&(data.mutex));
|
|
|
|
|
|
|
|
*status = data.status;
|
|
|
|
return data.ret;
|
|
|
|
|
2004-10-22 20:06:05 +04:00
|
|
|
#else
|
2004-12-14 18:47:31 +03:00
|
|
|
return waitpid(pid, status, options);
|
2004-10-22 20:06:05 +04:00
|
|
|
#endif
|
2004-09-26 21:43:35 +04:00
|
|
|
}
|
2004-12-14 18:47:31 +03:00
|
|
|
|
|
|
|
|
2005-03-14 23:57:21 +03:00
|
|
|
#if OMPI_THREADS_HAVE_DIFFERENT_PIDS
|
2004-12-14 18:47:31 +03:00
|
|
|
static void
|
|
|
|
internal_waitpid_callback(int fd, short event, void *arg)
|
|
|
|
{
|
|
|
|
waitpid_callback_data_t *data = (waitpid_callback_data_t*) arg;
|
|
|
|
|
|
|
|
data->ret = waitpid(data->pid, &(data->status), data->options);
|
|
|
|
|
|
|
|
data->done = true;
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_condition_signal(&(data->cond));
|
2004-12-14 18:47:31 +03:00
|
|
|
}
|
|
|
|
#endif
|
2005-07-14 22:05:30 +04:00
|
|
|
|
|
|
|
#else /* HAVE_WAITPID */
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_init(void) {
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_SUCCESS;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_finalize(void)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_SUCCESS;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
orte_waitpid(pid_t wpid, int *status, int options)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_cb(pid_t wpid, orte_wait_fn_t callback, void *data)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_cb_cancel(pid_t wpid)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_cb_disable(void)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_cb_enable(void)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
orte_wait_kill(int sig)
|
|
|
|
{
|
2005-09-13 01:45:58 +04:00
|
|
|
return ORTE_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|