This source file includes following definitions.
- opal_thread_construct
- opal_thread_start
- opal_thread_join
- opal_thread_self_compare
- opal_thread_get_self
- opal_thread_kill
- opal_tsd_key_create
- opal_tsd_keys_destruct
- opal_thread_set_main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include "opal_config.h"
23
24 #include "opal/threads/threads.h"
25 #include "opal/threads/tsd.h"
26 #include "opal/constants.h"
27
28 bool opal_debug_threads = false;
29
30 static void opal_thread_construct(opal_thread_t *t);
31
32 static pthread_t opal_main_thread;
33
34 struct opal_tsd_key_value {
35 opal_tsd_key_t key;
36 opal_tsd_destructor_t destructor;
37 };
38
39 static struct opal_tsd_key_value *opal_tsd_key_values = NULL;
40 static int opal_tsd_key_values_count = 0;
41
42 OBJ_CLASS_INSTANCE(opal_thread_t,
43 opal_object_t,
44 opal_thread_construct, NULL);
45
46
47
48
49
50 static void opal_thread_construct(opal_thread_t *t)
51 {
52 t->t_run = 0;
53 t->t_handle = (pthread_t) -1;
54 }
55
56 int opal_thread_start(opal_thread_t *t)
57 {
58 int rc;
59
60 if (OPAL_ENABLE_DEBUG) {
61 if (NULL == t->t_run || t->t_handle != (pthread_t) -1) {
62 return OPAL_ERR_BAD_PARAM;
63 }
64 }
65
66 rc = pthread_create(&t->t_handle, NULL, (void*(*)(void*)) t->t_run, t);
67
68 return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
69 }
70
71
72 int opal_thread_join(opal_thread_t *t, void **thr_return)
73 {
74 int rc = pthread_join(t->t_handle, thr_return);
75 t->t_handle = (pthread_t) -1;
76 return (rc == 0) ? OPAL_SUCCESS : OPAL_ERROR;
77 }
78
79
80 bool opal_thread_self_compare(opal_thread_t *t)
81 {
82 return t->t_handle == pthread_self();
83 }
84
85
86 opal_thread_t *opal_thread_get_self(void)
87 {
88 opal_thread_t *t = OBJ_NEW(opal_thread_t);
89 t->t_handle = pthread_self();
90 return t;
91 }
92
93 void opal_thread_kill(opal_thread_t *t, int sig)
94 {
95 pthread_kill(t->t_handle, sig);
96 }
97
98 int opal_tsd_key_create(opal_tsd_key_t *key,
99 opal_tsd_destructor_t destructor)
100 {
101 int rc;
102 rc = pthread_key_create(key, destructor);
103 if ((0 == rc) && (pthread_self() == opal_main_thread)) {
104 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));
105 opal_tsd_key_values[opal_tsd_key_values_count].key = *key;
106 opal_tsd_key_values[opal_tsd_key_values_count].destructor = destructor;
107 opal_tsd_key_values_count ++;
108 }
109 return rc;
110 }
111
112 int opal_tsd_keys_destruct()
113 {
114 int i;
115 void * ptr;
116 for (i=0; i<opal_tsd_key_values_count; i++) {
117 if(OPAL_SUCCESS == opal_tsd_getspecific(opal_tsd_key_values[i].key, &ptr)) {
118 if (NULL != opal_tsd_key_values[i].destructor) {
119 opal_tsd_key_values[i].destructor(ptr);
120 opal_tsd_setspecific(opal_tsd_key_values[i].key, NULL);
121 }
122 }
123 }
124 if (0 < opal_tsd_key_values_count) {
125 free(opal_tsd_key_values);
126 opal_tsd_key_values_count = 0;
127 }
128 return OPAL_SUCCESS;
129 }
130
131 void opal_thread_set_main() {
132 opal_main_thread = pthread_self();
133 }