2003-12-22 19:29:21 +03: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.
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2004-11-28 23:09:25 +03:00
|
|
|
* 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.
|
2010-08-05 20:25:32 +04:00
|
|
|
* Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
|
2016-10-26 09:38:45 +03:00
|
|
|
* Copyright (c) 2015-2017 Research Organization for Information Science
|
2014-10-21 14:49:58 +04:00
|
|
|
* and Technology (RIST). All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2004-11-22 04:38:40 +03:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2004-01-07 21:39:35 +03:00
|
|
|
* $HEADER$
|
2003-12-22 19:29:21 +03:00
|
|
|
*/
|
2004-01-07 21:39:35 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-08-15 15:02:01 +04:00
|
|
|
#include "opal/threads/threads.h"
|
2016-10-26 09:38:45 +03:00
|
|
|
#include "opal/threads/tsd.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2003-12-22 19:29:21 +03:00
|
|
|
|
2010-08-05 20:25:32 +04:00
|
|
|
bool opal_debug_threads = false;
|
2004-03-03 19:10:26 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
static void opal_thread_construct(opal_thread_t *t);
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2016-10-26 09:38:45 +03:00
|
|
|
static pthread_t opal_main_thread;
|
|
|
|
|
|
|
|
struct opal_tsd_key_value {
|
|
|
|
opal_tsd_key_t key;
|
|
|
|
opal_tsd_destructor_t destructor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct opal_tsd_key_value *opal_tsd_key_values = NULL;
|
|
|
|
static int opal_tsd_key_values_count = 0;
|
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
OBJ_CLASS_INSTANCE(opal_thread_t,
|
2005-07-03 20:06:07 +04:00
|
|
|
opal_object_t,
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_construct, NULL);
|
2004-11-15 23:03:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
2005-07-04 02:45:48 +04:00
|
|
|
static void opal_thread_construct(opal_thread_t *t)
|
2004-03-03 19:10:26 +03:00
|
|
|
{
|
|
|
|
t->t_run = 0;
|
2004-03-18 17:17:23 +03:00
|
|
|
t->t_handle = (pthread_t) -1;
|
2004-03-03 19:10:26 +03:00
|
|
|
}
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_start(opal_thread_t *t)
|
2004-03-03 19:10:26 +03:00
|
|
|
{
|
|
|
|
int rc;
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2009-05-07 00:11:28 +04:00
|
|
|
if (OPAL_ENABLE_DEBUG) {
|
2004-08-19 03:24:27 +04:00
|
|
|
if (NULL == t->t_run || t->t_handle != (pthread_t) -1) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_BAD_PARAM;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-18 19:41:40 +04:00
|
|
|
rc = pthread_create(&t->t_handle, NULL, (void*(*)(void*)) t->t_run, t);
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
|
2004-03-03 19:10:26 +03:00
|
|
|
}
|
|
|
|
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
int opal_thread_join(opal_thread_t *t, void **thr_return)
|
2004-03-03 19:10:26 +03:00
|
|
|
{
|
|
|
|
int rc = pthread_join(t->t_handle, thr_return);
|
2007-05-21 20:06:14 +04:00
|
|
|
t->t_handle = (pthread_t) -1;
|
2006-02-12 04:33:29 +03:00
|
|
|
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
|
2004-08-19 03:24:27 +04:00
|
|
|
}
|
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
bool opal_thread_self_compare(opal_thread_t *t)
|
2004-08-28 05:15:19 +04:00
|
|
|
{
|
|
|
|
return t->t_handle == pthread_self();
|
|
|
|
}
|
2004-08-19 03:24:27 +04:00
|
|
|
|
2004-11-15 23:03:14 +03:00
|
|
|
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *opal_thread_get_self(void)
|
2004-11-15 23:03:14 +03:00
|
|
|
{
|
2005-07-04 02:45:48 +04:00
|
|
|
opal_thread_t *t = OBJ_NEW(opal_thread_t);
|
2004-11-15 23:03:14 +03:00
|
|
|
t->t_handle = pthread_self();
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2010-11-08 22:06:10 +03:00
|
|
|
void opal_thread_kill(opal_thread_t *t, int sig)
|
|
|
|
{
|
|
|
|
pthread_kill(t->t_handle, sig);
|
|
|
|
}
|
2016-10-26 09:38:45 +03:00
|
|
|
|
|
|
|
int opal_tsd_key_create(opal_tsd_key_t *key,
|
|
|
|
opal_tsd_destructor_t destructor)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
rc = pthread_key_create(key, destructor);
|
|
|
|
if ((0 == rc) && (pthread_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[opal_tsd_key_values_count].key = *key;
|
|
|
|
opal_tsd_key_values[opal_tsd_key_values_count].destructor = destructor;
|
|
|
|
opal_tsd_key_values_count ++;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int opal_tsd_keys_destruct()
|
|
|
|
{
|
|
|
|
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)) {
|
|
|
|
if (NULL != opal_tsd_key_values[i].destructor) {
|
|
|
|
opal_tsd_key_values[i].destructor(ptr);
|
2017-01-25 04:57:14 +03:00
|
|
|
opal_tsd_setspecific(opal_tsd_key_values[i].key, NULL);
|
2016-10-26 09:38:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (0 < opal_tsd_key_values_count) {
|
|
|
|
free(opal_tsd_key_values);
|
|
|
|
opal_tsd_key_values_count = 0;
|
|
|
|
}
|
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void opal_thread_set_main() {
|
|
|
|
opal_main_thread = pthread_self();
|
|
|
|
}
|