This source file includes following definitions.
- pmix_thread_construct
- pmix_thread_start
- pmix_thread_join
- pmix_thread_self_compare
- pmix_thread_get_self
- pmix_thread_kill
- pmix_tsd_key_create
- pmix_tsd_keys_destruct
- pmix_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
23 #include "pmix_config.h"
24
25 #include "src/threads/threads.h"
26 #include "src/threads/tsd.h"
27 #include "pmix_common.h"
28
29 bool pmix_debug_threads = false;
30
31 static void pmix_thread_construct(pmix_thread_t *t);
32
33 static pthread_t pmix_main_thread;
34
35 struct pmix_tsd_key_value {
36 pmix_tsd_key_t key;
37 pmix_tsd_destructor_t destructor;
38 };
39
40 static struct pmix_tsd_key_value *pmix_tsd_key_values = NULL;
41 static int pmix_tsd_key_values_count = 0;
42
43 PMIX_EXPORT PMIX_CLASS_INSTANCE(pmix_thread_t,
44 pmix_object_t,
45 pmix_thread_construct, NULL);
46
47
48
49
50
51 static void pmix_thread_construct(pmix_thread_t *t)
52 {
53 t->t_run = 0;
54 t->t_handle = (pthread_t) -1;
55 }
56
57 int pmix_thread_start(pmix_thread_t *t)
58 {
59 int rc;
60
61 if (PMIX_ENABLE_DEBUG) {
62 if (NULL == t->t_run || t->t_handle != (pthread_t) -1) {
63 return PMIX_ERR_BAD_PARAM;
64 }
65 }
66
67 rc = pthread_create(&t->t_handle, NULL, (void*(*)(void*)) t->t_run, t);
68
69 return (rc == 0) ? PMIX_SUCCESS : PMIX_ERROR;
70 }
71
72
73 int pmix_thread_join(pmix_thread_t *t, void **thr_return)
74 {
75 int rc = pthread_join(t->t_handle, thr_return);
76 t->t_handle = (pthread_t) -1;
77 return (rc == 0) ? PMIX_SUCCESS : PMIX_ERROR;
78 }
79
80
81 bool pmix_thread_self_compare(pmix_thread_t *t)
82 {
83 return t->t_handle == pthread_self();
84 }
85
86
87 pmix_thread_t *pmix_thread_get_self(void)
88 {
89 pmix_thread_t *t = PMIX_NEW(pmix_thread_t);
90 t->t_handle = pthread_self();
91 return t;
92 }
93
94 void pmix_thread_kill(pmix_thread_t *t, int sig)
95 {
96 pthread_kill(t->t_handle, sig);
97 }
98
99 int pmix_tsd_key_create(pmix_tsd_key_t *key,
100 pmix_tsd_destructor_t destructor)
101 {
102 int rc;
103 rc = pthread_key_create(key, destructor);
104 if ((0 == rc) && (pthread_self() == pmix_main_thread)) {
105 pmix_tsd_key_values = (struct pmix_tsd_key_value *)realloc(pmix_tsd_key_values, (pmix_tsd_key_values_count+1) * sizeof(struct pmix_tsd_key_value));
106 pmix_tsd_key_values[pmix_tsd_key_values_count].key = *key;
107 pmix_tsd_key_values[pmix_tsd_key_values_count].destructor = destructor;
108 pmix_tsd_key_values_count ++;
109 }
110 return rc;
111 }
112
113 int pmix_tsd_keys_destruct()
114 {
115 int i;
116 void * ptr;
117 for (i=0; i<pmix_tsd_key_values_count; i++) {
118 if(PMIX_SUCCESS == pmix_tsd_getspecific(pmix_tsd_key_values[i].key, &ptr)) {
119 if (NULL != pmix_tsd_key_values[i].destructor) {
120 pmix_tsd_key_values[i].destructor(ptr);
121 pmix_tsd_setspecific(pmix_tsd_key_values[i].key, NULL);
122 }
123 }
124 }
125 if (0 < pmix_tsd_key_values_count) {
126 free(pmix_tsd_key_values);
127 pmix_tsd_key_values_count = 0;
128 }
129 return PMIX_SUCCESS;
130 }
131
132 void pmix_thread_set_main() {
133 pmix_main_thread = pthread_self();
134 }