1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #ifndef OPAL_THREAD_H
25 #define OPAL_THREAD_H 1
26
27 #include "opal_config.h"
28
29 #include <pthread.h>
30 #include <signal.h>
31
32 #include "opal/class/opal_object.h"
33 #if OPAL_ENABLE_DEBUG
34 #include "opal/util/output.h"
35 #endif
36
37 #include "mutex.h"
38 #include "condition.h"
39
40 BEGIN_C_DECLS
41
42 typedef void *(*opal_thread_fn_t) (opal_object_t *);
43
44 #define OPAL_THREAD_CANCELLED ((void*)1);
45
46 struct opal_thread_t {
47 opal_object_t super;
48 opal_thread_fn_t t_run;
49 void* t_arg;
50 pthread_t t_handle;
51 };
52
53 typedef struct opal_thread_t opal_thread_t;
54
55 #if OPAL_ENABLE_DEBUG
56 OPAL_DECLSPEC extern bool opal_debug_threads;
57 #endif
58
59
60 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_thread_t);
61
62 #if OPAL_ENABLE_DEBUG
63 #define OPAL_ACQUIRE_THREAD(lck, cnd, act) \
64 do { \
65 OPAL_THREAD_LOCK((lck)); \
66 if (opal_debug_threads) { \
67 opal_output(0, "Waiting for thread %s:%d", \
68 __FILE__, __LINE__); \
69 } \
70 while (*(act)) { \
71 opal_condition_wait((cnd), (lck)); \
72 } \
73 if (opal_debug_threads) { \
74 opal_output(0, "Thread obtained %s:%d", \
75 __FILE__, __LINE__); \
76 } \
77 *(act) = true; \
78 } while(0);
79 #else
80 #define OPAL_ACQUIRE_THREAD(lck, cnd, act) \
81 do { \
82 OPAL_THREAD_LOCK((lck)); \
83 while (*(act)) { \
84 opal_condition_wait((cnd), (lck)); \
85 } \
86 *(act) = true; \
87 } while(0);
88 #endif
89
90
91 #if OPAL_ENABLE_DEBUG
92 #define OPAL_RELEASE_THREAD(lck, cnd, act) \
93 do { \
94 if (opal_debug_threads) { \
95 opal_output(0, "Releasing thread %s:%d", \
96 __FILE__, __LINE__); \
97 } \
98 *(act) = false; \
99 opal_condition_broadcast((cnd)); \
100 OPAL_THREAD_UNLOCK((lck)); \
101 } while(0);
102 #else
103 #define OPAL_RELEASE_THREAD(lck, cnd, act) \
104 do { \
105 *(act) = false; \
106 opal_condition_broadcast((cnd)); \
107 OPAL_THREAD_UNLOCK((lck)); \
108 } while(0);
109 #endif
110
111
112 #define OPAL_WAKEUP_THREAD(cnd, act) \
113 do { \
114 *(act) = false; \
115 opal_condition_broadcast((cnd)); \
116 } while(0);
117
118
119
120
121
122
123
124 #define OPAL_POST_OBJECT(o) opal_atomic_wmb()
125
126
127
128 #define OPAL_ACQUIRE_OBJECT(o) opal_atomic_rmb()
129
130
131
132 OPAL_DECLSPEC int opal_thread_start(opal_thread_t *);
133 OPAL_DECLSPEC int opal_thread_join(opal_thread_t *, void **thread_return);
134 OPAL_DECLSPEC bool opal_thread_self_compare(opal_thread_t*);
135 OPAL_DECLSPEC opal_thread_t *opal_thread_get_self(void);
136 OPAL_DECLSPEC void opal_thread_kill(opal_thread_t *, int sig);
137 OPAL_DECLSPEC void opal_thread_set_main(void);
138
139 END_C_DECLS
140
141 #endif