Skip to content

Commit

Permalink
Merge pull request open-mpi#11 from shintaro-iwasaki/thread-framework…
Browse files Browse the repository at this point in the history
…2-cleanup

/opal/mca/threads: clean up 3
  • Loading branch information
hppritcha authored Oct 31, 2019
2 parents 80503e5 + cbf5ba0 commit a5e87d6
Show file tree
Hide file tree
Showing 32 changed files with 425 additions and 429 deletions.
12 changes: 7 additions & 5 deletions opal/mca/threads/argobots/threads_argobots.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
* $HEADER$
*/

#ifndef OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_H
#define OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_H 1
#ifndef OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_H
#define OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_H

#include <abt.h>

static inline void ensure_init_argobots(void) {
if (ABT_initialized() != 0)
ABT_init(0, 0);
static inline void opal_threads_argobots_ensure_init(void)
{
if (ABT_initialized() != 0) {
ABT_init(0, 0);
}
}

#endif /* OPAL_MCA_THREADS_ARGOBOTS_THREADS_ARGOBOTS_H */
4 changes: 2 additions & 2 deletions opal/mca/threads/argobots/threads_argobots_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int opal_threads_argobots_open(void);

const opal_threads_base_component_1_0_0_t mca_threads_argobots_component = {
/* First, the mca_component_t struct containing meta information
about the component itself */
* about the component itself */
.threadsc_version = {
OPAL_THREADS_BASE_VERSION_1_0_0,

Expand All @@ -52,6 +52,6 @@ const opal_threads_base_component_1_0_0_t mca_threads_argobots_component = {

int opal_threads_argobots_open(void)
{
ensure_init_argobots();
opal_threads_argobots_ensure_init();
return OPAL_SUCCESS;
}
3 changes: 1 addition & 2 deletions opal/mca/threads/argobots/threads_argobots_condition.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
Expand All @@ -22,14 +23,12 @@

#include "opal/mca/threads/condition.h"


static void opal_condition_construct(opal_condition_t *c)
{
c->c_waiting = 0;
c->c_signaled = 0;
}


static void opal_condition_destruct(opal_condition_t *c)
{
}
Expand Down
45 changes: 20 additions & 25 deletions opal/mca/threads/argobots/threads_argobots_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

#include <abt.h>

static void *
evthread_argobots_lock_alloc(unsigned locktype)
static void *evthread_argobots_lock_alloc(unsigned locktype)
{
ABT_mutex lock;
if (locktype & EVTHREAD_LOCKTYPE_RECURSIVE) {
Expand All @@ -33,15 +32,13 @@ evthread_argobots_lock_alloc(unsigned locktype)
return lock;
}

static void
evthread_argobots_lock_free(void *_lock, unsigned locktype)
static void evthread_argobots_lock_free(void *_lock, unsigned locktype)
{
ABT_mutex lock = _lock;
ABT_mutex_free(&lock);
}

static int
evthread_argobots_lock(unsigned mode, void *_lock)
static int evthread_argobots_lock(unsigned mode, void *_lock)
{
int ret;
ABT_mutex lock = _lock;
Expand All @@ -53,8 +50,7 @@ evthread_argobots_lock(unsigned mode, void *_lock)
return ret;
}

static int
evthread_argobots_unlock(unsigned mode, void *_lock)
static int evthread_argobots_unlock(unsigned mode, void *_lock)
{
ABT_mutex lock = _lock;
int ret = ABT_mutex_unlock(lock);
Expand All @@ -63,43 +59,40 @@ evthread_argobots_unlock(unsigned mode, void *_lock)
return ret;
}

static unsigned long
evthread_argobots_get_id(void)
static unsigned long evthread_argobots_get_id(void)
{
ABT_thread thr;
ABT_thread_self(&thr);
return (unsigned long)((intptr_t)thr);
}

static void *
evthread_argobots_cond_alloc(unsigned condflags)
static void *evthread_argobots_cond_alloc(unsigned condflags)
{
ABT_cond cond;
ABT_cond_create(&cond);
return cond;
}

static void
evthread_argobots_cond_free(void *_cond)
static void evthread_argobots_cond_free(void *_cond)
{
ABT_cond cond = _cond;
ABT_cond_free(&cond);
}

static int
evthread_argobots_cond_signal(void *_cond, int broadcast)
static int evthread_argobots_cond_signal(void *_cond, int broadcast)
{
ABT_cond cond = _cond;
int r;
if (broadcast)
if (broadcast) {
r = ABT_cond_broadcast(cond);
else
} else {
r = ABT_cond_signal(cond);
}
return r ? -1 : 0;
}

static int
evthread_argobots_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
static int evthread_argobots_cond_wait(void *_cond, void *_lock,
const struct timeval *tv)
{
int r;
ABT_cond cond = _cond;
Expand All @@ -111,19 +104,21 @@ evthread_argobots_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
evutil_gettimeofday(&now, NULL);
evutil_timeradd(&now, tv, &abstime);
ts.tv_sec = abstime.tv_sec;
ts.tv_nsec = abstime.tv_usec*1000;
ts.tv_nsec = abstime.tv_usec * 1000;
r = ABT_cond_timedwait(cond, lock, &ts);
if (r != 0)
if (r != 0) {
return 1;
else
} else {
return 0;
}
} else {
r = ABT_cond_wait(cond, lock);
return r ? -1 : 0;
}
}

void opal_event_use_threads(void) {
void opal_event_use_threads(void)
{
struct evthread_lock_callbacks cbs = {
EVTHREAD_LOCK_API_VERSION,
EVTHREAD_LOCKTYPE_RECURSIVE,
Expand All @@ -139,7 +134,7 @@ void opal_event_use_threads(void) {
evthread_argobots_cond_signal,
evthread_argobots_cond_wait
};
ensure_init_argobots();
opal_threads_argobots_ensure_init();
evthread_set_lock_callbacks(&cbs);
evthread_set_condition_callbacks(&cond_cbs);
evthread_set_id_callback(evthread_argobots_get_id);
Expand Down
57 changes: 34 additions & 23 deletions opal/mca/threads/argobots/threads_argobots_module.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
Expand All @@ -17,6 +18,7 @@
*
* $HEADER$
*/

#include <unistd.h>

#include "opal/mca/threads/argobots/threads_argobots.h"
Expand Down Expand Up @@ -51,47 +53,53 @@ OBJ_CLASS_INSTANCE(opal_thread_t,
opal_object_t,
opal_thread_construct, NULL);

static inline ABT_thread opal_thread_get_argobots_self(void) {
static inline ABT_thread opal_thread_get_argobots_self(void)
{
ABT_thread self;
ABT_thread_self(&self);
return self;
}

static void opal_thread_argobots_wrapper(void *arg) {
opal_thread_t *t = (opal_thread_t *) arg;
t->t_ret = ((void*(*)(void*)) t->t_run)(t);
static void opal_thread_argobots_wrapper(void *arg)
{
opal_thread_t *t = (opal_thread_t *)arg;
t->t_ret = ((void *(*)(void *))t->t_run)(t);
}

opal_thread_t *opal_thread_get_self(void)
{
ensure_init_argobots();
opal_threads_argobots_ensure_init();
opal_thread_t *t = OBJ_NEW(opal_thread_t);
t->t_handle = opal_thread_get_argobots_self();
return t;
}

bool opal_thread_self_compare(opal_thread_t *t)
{
ensure_init_argobots();
return t->t_handle == opal_thread_get_argobots_self();
opal_threads_argobots_ensure_init();
return opal_thread_get_argobots_self() == t->t_handle;
}

int opal_thread_join(opal_thread_t *t, void **thr_return) {
ensure_init_argobots();
int opal_thread_join(opal_thread_t *t, void **thr_return)
{
opal_threads_argobots_ensure_init();
int rc = ABT_thread_free(&t->t_handle);
if (thr_return)
if (thr_return) {
*thr_return = t->t_ret;
}
t->t_handle = ABT_THREAD_NULL;
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
return (0 == rc) ? OPAL_SUCCESS : OPAL_ERROR;
}

void opal_thread_set_main() {
ensure_init_argobots();
void opal_thread_set_main()
{
opal_threads_argobots_ensure_init();
opal_main_thread = opal_thread_get_argobots_self();
}

int opal_thread_start(opal_thread_t *t) {
ensure_init_argobots();
int opal_thread_start(opal_thread_t *t)
{
opal_threads_argobots_ensure_init();
int rc;
if (OPAL_ENABLE_DEBUG) {
if (NULL == t->t_run || t->t_handle != ABT_THREAD_NULL) {
Expand All @@ -105,32 +113,35 @@ int opal_thread_start(opal_thread_t *t) {
opal_thread_argobots_wrapper, t,
ABT_THREAD_ATTR_NULL, &t->t_handle);

return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
return (0 == rc) ? OPAL_SUCCESS : OPAL_ERROR;
}

opal_class_t opal_thread_t_class;

int opal_tsd_key_create(opal_tsd_key_t *key, opal_tsd_destructor_t destructor)
{
ensure_init_argobots();
opal_threads_argobots_ensure_init();
int rc;
rc = ABT_key_create(destructor, key);
if ((0 == rc) && (opal_thread_get_argobots_self() == opal_main_thread)) {
opal_tsd_key_values = (struct opal_tsd_key_value *)realloc(opal_tsd_key_values, (opal_tsd_key_values_count+1) * sizeof(struct opal_tsd_key_value));
opal_tsd_key_values = (struct opal_tsd_key_value *)
realloc(opal_tsd_key_values, (opal_tsd_key_values_count + 1) *
sizeof(struct opal_tsd_key_value));
opal_tsd_key_values[opal_tsd_key_values_count].key = *key;
opal_tsd_key_values[opal_tsd_key_values_count].destructor = destructor;
opal_tsd_key_values_count ++;
opal_tsd_key_values_count++;
}
return rc;
}

int opal_tsd_keys_destruct(void)
{
ensure_init_argobots();
opal_threads_argobots_ensure_init();
int i;
void * ptr;
for (i=0; i<opal_tsd_key_values_count; i++) {
if(OPAL_SUCCESS == opal_tsd_getspecific(opal_tsd_key_values[i].key, &ptr)) {
void *ptr;
for (i = 0; i < opal_tsd_key_values_count; i++) {
if (OPAL_SUCCESS ==
opal_tsd_getspecific(opal_tsd_key_values[i].key, &ptr)) {
if (NULL != opal_tsd_key_values[i].destructor) {
opal_tsd_key_values[i].destructor(ptr);
opal_tsd_setspecific(opal_tsd_key_values[i].key, NULL);
Expand Down
26 changes: 16 additions & 10 deletions opal/mca/threads/argobots/threads_argobots_mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
*/
bool opal_uses_threads = false;

static void mca_threads_argobots_mutex_constructor(opal_mutex_t *p_mutex) {
ensure_init_argobots();
static void mca_threads_argobots_mutex_constructor(opal_mutex_t *p_mutex)
{
opal_threads_argobots_ensure_init();
p_mutex->m_lock_argobots = OPAL_ABT_MUTEX_NULL;
p_mutex->m_recursive = 0;
#if OPAL_ENABLE_DEBUG
Expand All @@ -49,15 +50,18 @@ static void mca_threads_argobots_mutex_constructor(opal_mutex_t *p_mutex) {
opal_atomic_lock_init(&p_mutex->m_lock_atomic, 0);
}

static void mca_threads_argobots_mutex_desctructor(opal_mutex_t *p_mutex) {
ensure_init_argobots();
if (OPAL_ABT_MUTX_NULL != p_mutex->m_lock_argobots)
static void mca_threads_argobots_mutex_desctructor(opal_mutex_t *p_mutex)
{
opal_threads_argobots_ensure_init();
if (OPAL_ABT_MUTEX_NULL != p_mutex->m_lock_argobots) {
ABT_mutex_free(&p_mutex->m_lock_argobots);
}
}

static void mca_threads_argobots_recursive_mutex_constructor
(opal_recursive_mutex_t *p_mutex) {
ensure_init_argobots();
(opal_recursive_mutex_t *p_mutex)
{
opal_threads_argobots_ensure_init();
p_mutex->m_lock_argobots = OPAL_ABT_MUTEX_NULL;
p_mutex->m_recursive = 1;
#if OPAL_ENABLE_DEBUG
Expand All @@ -69,10 +73,12 @@ static void mca_threads_argobots_recursive_mutex_constructor
}

static void mca_threads_argobots_recursive_mutex_desctructor
(opal_recursive_mutex_t *p_mutex) {
ensure_init_argobots();
if (OPAL_ABT_MUTEX_NULL != p_mutex->m_lock_argobots)
(opal_recursive_mutex_t *p_mutex)
{
opal_threads_argobots_ensure_init();
if (OPAL_ABT_MUTEX_NULL != p_mutex->m_lock_argobots) {
ABT_mutex_free(&p_mutex->m_lock_argobots);
}
}

OBJ_CLASS_INSTANCE(opal_mutex_t,
Expand Down
Loading

0 comments on commit a5e87d6

Please sign in to comment.