1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*
* 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.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2015-2017 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include "opal/threads/threads.h"
#include "opal/threads/tsd.h"
#include "opal/constants.h"
bool opal_debug_threads = false;
static void opal_thread_construct(opal_thread_t *t);
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;
OBJ_CLASS_INSTANCE(opal_thread_t,
opal_object_t,
opal_thread_construct, NULL);
/*
* Constructor
*/
static void opal_thread_construct(opal_thread_t *t)
{
t->t_run = 0;
t->t_handle = (pthread_t) -1;
}
int opal_thread_start(opal_thread_t *t)
{
int rc;
if (OPAL_ENABLE_DEBUG) {
if (NULL == t->t_run || t->t_handle != (pthread_t) -1) {
return OPAL_ERR_BAD_PARAM;
}
}
rc = pthread_create(&t->t_handle, NULL, (void*(*)(void*)) t->t_run, t);
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
}
int opal_thread_join(opal_thread_t *t, void **thr_return)
{
int rc = pthread_join(t->t_handle, thr_return);
t->t_handle = (pthread_t) -1;
return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
}
bool opal_thread_self_compare(opal_thread_t *t)
{
return t->t_handle == pthread_self();
}
opal_thread_t *opal_thread_get_self(void)
{
opal_thread_t *t = OBJ_NEW(opal_thread_t);
t->t_handle = pthread_self();
return t;
}
void opal_thread_kill(opal_thread_t *t, int sig)
{
pthread_kill(t->t_handle, sig);
}
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);
opal_tsd_setspecific(opal_tsd_key_values[i].key, NULL);
}
}
}
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();
}
|