2014-07-03 22:56:46 +04:00
|
|
|
/*
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2014-2015 Intel, Inc. All rights reserved.
|
2015-08-07 17:42:16 +03:00
|
|
|
* Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
|
2014-07-03 22:56:46 +04:00
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2014-07-03 22:56:46 +04:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2014-07-03 22:56:46 +04:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opal_config.h"
|
|
|
|
#include "opal/constants.h"
|
|
|
|
|
2015-02-10 18:22:10 +03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
#include "opal/class/opal_list.h"
|
|
|
|
#include "opal/mca/event/event.h"
|
|
|
|
#include "opal/threads/threads.h"
|
|
|
|
#include "opal/util/error.h"
|
|
|
|
#include "opal/util/fd.h"
|
|
|
|
|
|
|
|
#include "opal/runtime/opal_progress_threads.h"
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
/* create a tracking object for progress threads */
|
|
|
|
typedef struct {
|
|
|
|
opal_list_item_t super;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
2015-07-12 18:23:34 +03:00
|
|
|
int refcount;
|
2014-07-03 22:56:46 +04:00
|
|
|
char *name;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
opal_event_base_t *ev_base;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
/* This will be set to false when it is time for the progress
|
|
|
|
thread to exit */
|
2014-07-22 20:55:23 +04:00
|
|
|
volatile bool ev_active;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
/* This event will always be set on the ev_base (so that the
|
|
|
|
ev_base is not empty!) */
|
2014-07-03 22:56:46 +04:00
|
|
|
opal_event_t block;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
bool engine_constructed;
|
2014-07-03 22:56:46 +04:00
|
|
|
opal_thread_t engine;
|
|
|
|
} opal_progress_tracker_t;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
static void tracker_constructor(opal_progress_tracker_t *p)
|
2014-07-03 22:56:46 +04:00
|
|
|
{
|
2015-07-12 18:23:34 +03:00
|
|
|
p->refcount = 1; // start at one since someone created it
|
2014-07-03 22:56:46 +04:00
|
|
|
p->name = NULL;
|
|
|
|
p->ev_base = NULL;
|
2015-08-07 17:42:16 +03:00
|
|
|
p->ev_active = false;
|
|
|
|
p->engine_constructed = false;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
static void tracker_destructor(opal_progress_tracker_t *p)
|
2014-07-03 22:56:46 +04:00
|
|
|
{
|
2015-08-07 17:42:16 +03:00
|
|
|
opal_event_del(&p->block);
|
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
if (NULL != p->name) {
|
|
|
|
free(p->name);
|
|
|
|
}
|
|
|
|
if (NULL != p->ev_base) {
|
|
|
|
opal_event_base_free(p->ev_base);
|
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
if (p->engine_constructed) {
|
2014-07-03 22:56:46 +04:00
|
|
|
OBJ_DESTRUCT(&p->engine);
|
|
|
|
}
|
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
static OBJ_CLASS_INSTANCE(opal_progress_tracker_t,
|
|
|
|
opal_list_item_t,
|
2015-08-07 17:42:16 +03:00
|
|
|
tracker_constructor,
|
|
|
|
tracker_destructor);
|
2014-07-03 22:56:46 +04:00
|
|
|
|
|
|
|
static bool inited = false;
|
2015-08-07 17:42:16 +03:00
|
|
|
static opal_list_t tracking;
|
|
|
|
static struct timeval long_timeout = {
|
|
|
|
.tv_sec = 3600,
|
|
|
|
.tv_usec = 0
|
|
|
|
};
|
|
|
|
static const char *shared_thread_name = "OPAL-wide async progress thread";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this event is fired, just restart it so that this event base
|
|
|
|
* continues to have something to block on.
|
|
|
|
*/
|
|
|
|
static void dummy_timeout_cb(int fd, short args, void *cbdata)
|
2014-07-03 22:56:46 +04:00
|
|
|
{
|
2014-07-04 18:55:51 +04:00
|
|
|
opal_progress_tracker_t *trk = (opal_progress_tracker_t*)cbdata;
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
opal_event_add(&trk->block, &long_timeout);
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Main for the progress thread
|
|
|
|
*/
|
2014-07-22 20:55:23 +04:00
|
|
|
static void* progress_engine(opal_object_t *obj)
|
|
|
|
{
|
2014-07-26 21:01:27 +04:00
|
|
|
opal_thread_t *t = (opal_thread_t*)obj;
|
|
|
|
opal_progress_tracker_t *trk = (opal_progress_tracker_t*)t->t_arg;
|
2014-07-03 22:56:46 +04:00
|
|
|
|
2014-07-22 20:55:23 +04:00
|
|
|
while (trk->ev_active) {
|
|
|
|
opal_event_loop(trk->ev_base, OPAL_EVLOOP_ONCE);
|
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
2014-07-22 20:55:23 +04:00
|
|
|
return OPAL_THREAD_CANCELLED;
|
|
|
|
}
|
2014-07-03 22:56:46 +04:00
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
static void stop_progress_engine(opal_progress_tracker_t *trk)
|
|
|
|
{
|
|
|
|
assert(trk->ev_active);
|
|
|
|
trk->ev_active = false;
|
|
|
|
|
|
|
|
/* break the event loop - this will cause the loop to exit upon
|
|
|
|
completion of any current event */
|
|
|
|
opal_event_base_loopbreak(trk->ev_base);
|
|
|
|
|
|
|
|
opal_thread_join(&trk->engine, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int start_progress_engine(opal_progress_tracker_t *trk)
|
|
|
|
{
|
|
|
|
assert(!trk->ev_active);
|
|
|
|
trk->ev_active = true;
|
|
|
|
|
|
|
|
/* fork off a thread to progress it */
|
|
|
|
trk->engine.t_run = progress_engine;
|
|
|
|
trk->engine.t_arg = trk;
|
|
|
|
|
|
|
|
int rc = opal_thread_start(&trk->engine);
|
|
|
|
if (OPAL_SUCCESS != rc) {
|
|
|
|
OPAL_ERROR_LOG(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
opal_event_base_t *opal_progress_thread_init(const char *name)
|
2014-07-03 22:56:46 +04:00
|
|
|
{
|
|
|
|
opal_progress_tracker_t *trk;
|
|
|
|
int rc;
|
|
|
|
|
2015-07-12 18:23:34 +03:00
|
|
|
if (!inited) {
|
|
|
|
OBJ_CONSTRUCT(&tracking, opal_list_t);
|
|
|
|
inited = true;
|
|
|
|
}
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
if (NULL == name) {
|
|
|
|
name = shared_thread_name;
|
|
|
|
}
|
|
|
|
|
2015-07-12 18:23:34 +03:00
|
|
|
/* check if we already have this thread */
|
|
|
|
OPAL_LIST_FOREACH(trk, &tracking, opal_progress_tracker_t) {
|
|
|
|
if (0 == strcmp(name, trk->name)) {
|
|
|
|
/* we do, so up the refcount on it */
|
|
|
|
++trk->refcount;
|
|
|
|
/* return the existing base */
|
|
|
|
return trk->ev_base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
trk = OBJ_NEW(opal_progress_tracker_t);
|
2015-08-07 17:42:16 +03:00
|
|
|
if (NULL == trk) {
|
|
|
|
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
trk->name = strdup(name);
|
2015-08-07 17:42:16 +03:00
|
|
|
if (NULL == trk->name) {
|
2014-07-03 22:56:46 +04:00
|
|
|
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
|
|
|
|
OBJ_RELEASE(trk);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
if (NULL == (trk->ev_base = opal_event_base_create())) {
|
|
|
|
OPAL_ERROR_LOG(OPAL_ERR_OUT_OF_RESOURCE);
|
|
|
|
OBJ_RELEASE(trk);
|
|
|
|
return NULL;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
/* add an event to the new event base (if there are no events,
|
|
|
|
opal_event_loop() will return immediately) */
|
|
|
|
opal_event_set(trk->ev_base, &trk->block, -1, OPAL_EV_PERSIST,
|
|
|
|
dummy_timeout_cb, trk);
|
|
|
|
opal_event_add(&trk->block, &long_timeout);
|
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
/* construct the thread object */
|
|
|
|
OBJ_CONSTRUCT(&trk->engine, opal_thread_t);
|
2015-08-07 17:42:16 +03:00
|
|
|
trk->engine_constructed = true;
|
|
|
|
if (OPAL_SUCCESS != (rc = start_progress_engine(trk))) {
|
2014-07-03 22:56:46 +04:00
|
|
|
OPAL_ERROR_LOG(rc);
|
|
|
|
OBJ_RELEASE(trk);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
opal_list_append(&tracking, &trk->super);
|
2015-08-07 17:42:16 +03:00
|
|
|
|
2014-07-03 22:56:46 +04:00
|
|
|
return trk->ev_base;
|
|
|
|
}
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
int opal_progress_thread_finalize(const char *name)
|
2014-07-03 22:56:46 +04:00
|
|
|
{
|
|
|
|
opal_progress_tracker_t *trk;
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
/* nothing we can do */
|
2015-08-07 17:42:16 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == name) {
|
|
|
|
name = shared_thread_name;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find the specified engine */
|
|
|
|
OPAL_LIST_FOREACH(trk, &tracking, opal_progress_tracker_t) {
|
|
|
|
if (0 == strcmp(name, trk->name)) {
|
2015-07-12 18:23:34 +03:00
|
|
|
/* decrement the refcount */
|
|
|
|
--trk->refcount;
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
/* If the refcount is still above 0, we're done here */
|
|
|
|
if (trk->refcount > 0) {
|
|
|
|
return OPAL_SUCCESS;
|
2014-07-04 18:55:51 +04:00
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
/* If the progress thread is active, stop it */
|
|
|
|
if (trk->ev_active) {
|
|
|
|
stop_progress_engine(trk);
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
opal_list_remove_item(&tracking, &trk->super);
|
|
|
|
OBJ_RELEASE(trk);
|
|
|
|
return OPAL_SUCCESS;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
return OPAL_ERR_NOT_FOUND;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
|
|
|
|
2015-08-07 17:42:16 +03:00
|
|
|
/*
|
|
|
|
* Stop the progress thread, but don't delete the tracker (or event base)
|
|
|
|
*/
|
|
|
|
int opal_progress_thread_pause(const char *name)
|
2014-07-03 22:56:46 +04:00
|
|
|
{
|
|
|
|
opal_progress_tracker_t *trk;
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
/* nothing we can do */
|
2015-08-07 17:42:16 +03:00
|
|
|
return OPAL_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == name) {
|
|
|
|
name = shared_thread_name;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find the specified engine */
|
|
|
|
OPAL_LIST_FOREACH(trk, &tracking, opal_progress_tracker_t) {
|
|
|
|
if (0 == strcmp(name, trk->name)) {
|
2015-08-07 17:42:16 +03:00
|
|
|
if (trk->ev_active) {
|
|
|
|
stop_progress_engine(trk);
|
2014-07-04 18:55:51 +04:00
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
int opal_progress_thread_resume(const char *name)
|
|
|
|
{
|
|
|
|
opal_progress_tracker_t *trk;
|
|
|
|
|
|
|
|
if (!inited) {
|
|
|
|
/* nothing we can do */
|
|
|
|
return OPAL_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == name) {
|
|
|
|
name = shared_thread_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the specified engine */
|
|
|
|
OPAL_LIST_FOREACH(trk, &tracking, opal_progress_tracker_t) {
|
|
|
|
if (0 == strcmp(name, trk->name)) {
|
|
|
|
if (trk->ev_active) {
|
|
|
|
return OPAL_ERR_RESOURCE_BUSY;
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
2015-08-07 17:42:16 +03:00
|
|
|
|
|
|
|
return start_progress_engine(trk);
|
2014-07-03 22:56:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OPAL_ERR_NOT_FOUND;
|
|
|
|
}
|