Commit | Line | Data |
---|---|---|
ea0efc06 | 1 | #ifdef USE_THREADS |
12ca11f6 | 2 | |
ea0efc06 | 3 | #ifdef WIN32 |
d55594ae GS |
4 | # include <win32thread.h> |
5 | #else | |
12ca11f6 | 6 | |
9731c6ca | 7 | #ifndef DJGPP |
12ca11f6 | 8 | /* POSIXish threads */ |
11343788 | 9 | #ifdef OLD_PTHREADS_API |
ea0efc06 MB |
10 | # define pthread_mutexattr_init(a) pthread_mutexattr_create(a) |
11 | # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t) | |
12 | # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d)) | |
13 | # define YIELD pthread_yield() | |
14 | # define DETACH(t) \ | |
15 | STMT_START { \ | |
46930d8f | 16 | if (pthread_detach(&(t)->self)) { \ |
ea0efc06 MB |
17 | MUTEX_UNLOCK(&(t)->mutex); \ |
18 | croak("panic: DETACH"); \ | |
19 | } \ | |
20 | } STMT_END | |
11343788 | 21 | #else |
ea0efc06 MB |
22 | # define pthread_mutexattr_default NULL |
23 | # define pthread_condattr_default NULL | |
11343788 | 24 | #endif /* OLD_PTHREADS_API */ |
d55594ae | 25 | #endif |
9731c6ca | 26 | #endif |
11343788 | 27 | |
940cb80d MB |
28 | #ifdef PTHREADS_CREATED_JOINABLE |
29 | # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE | |
30 | #else | |
31 | # ifdef PTHREAD_CREATE_UNDETACHED | |
32 | # define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED | |
33 | # else | |
34 | # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE | |
35 | # endif | |
36 | #endif | |
37 | ||
ea0efc06 | 38 | #ifndef YIELD |
52e1cb5e JH |
39 | # ifdef HAS_PTHREAD_YIELD |
40 | # define YIELD pthread_yield() | |
41 | # else | |
42 | # define YIELD sched_yield() | |
43 | # endif | |
ea0efc06 MB |
44 | #endif |
45 | ||
46 | #ifndef MUTEX_INIT | |
47 | #define MUTEX_INIT(m) \ | |
48 | STMT_START { \ | |
49 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ | |
50 | croak("panic: MUTEX_INIT"); \ | |
51 | } STMT_END | |
52 | #define MUTEX_LOCK(m) \ | |
53 | STMT_START { \ | |
54 | if (pthread_mutex_lock((m))) \ | |
55 | croak("panic: MUTEX_LOCK"); \ | |
56 | } STMT_END | |
57 | #define MUTEX_UNLOCK(m) \ | |
58 | STMT_START { \ | |
59 | if (pthread_mutex_unlock((m))) \ | |
60 | croak("panic: MUTEX_UNLOCK"); \ | |
61 | } STMT_END | |
62 | #define MUTEX_DESTROY(m) \ | |
63 | STMT_START { \ | |
64 | if (pthread_mutex_destroy((m))) \ | |
65 | croak("panic: MUTEX_DESTROY"); \ | |
66 | } STMT_END | |
67 | #endif /* MUTEX_INIT */ | |
68 | ||
69 | #ifndef COND_INIT | |
70 | #define COND_INIT(c) \ | |
71 | STMT_START { \ | |
72 | if (pthread_cond_init((c), pthread_condattr_default)) \ | |
73 | croak("panic: COND_INIT"); \ | |
74 | } STMT_END | |
75 | #define COND_SIGNAL(c) \ | |
76 | STMT_START { \ | |
77 | if (pthread_cond_signal((c))) \ | |
78 | croak("panic: COND_SIGNAL"); \ | |
79 | } STMT_END | |
80 | #define COND_BROADCAST(c) \ | |
81 | STMT_START { \ | |
82 | if (pthread_cond_broadcast((c))) \ | |
83 | croak("panic: COND_BROADCAST"); \ | |
84 | } STMT_END | |
85 | #define COND_WAIT(c, m) \ | |
86 | STMT_START { \ | |
87 | if (pthread_cond_wait((c), (m))) \ | |
88 | croak("panic: COND_WAIT"); \ | |
89 | } STMT_END | |
90 | #define COND_DESTROY(c) \ | |
91 | STMT_START { \ | |
92 | if (pthread_cond_destroy((c))) \ | |
93 | croak("panic: COND_DESTROY"); \ | |
94 | } STMT_END | |
95 | #endif /* COND_INIT */ | |
33f46ff6 | 96 | |
f826a10b | 97 | /* DETACH(t) must only be called while holding t->mutex */ |
ea0efc06 MB |
98 | #ifndef DETACH |
99 | #define DETACH(t) \ | |
100 | STMT_START { \ | |
46930d8f | 101 | if (pthread_detach((t)->self)) { \ |
ea0efc06 MB |
102 | MUTEX_UNLOCK(&(t)->mutex); \ |
103 | croak("panic: DETACH"); \ | |
104 | } \ | |
105 | } STMT_END | |
106 | #endif /* DETACH */ | |
33f46ff6 | 107 | |
ea0efc06 MB |
108 | #ifndef JOIN |
109 | #define JOIN(t, avp) \ | |
110 | STMT_START { \ | |
46930d8f | 111 | if (pthread_join((t)->self, (void**)(avp))) \ |
ea0efc06 MB |
112 | croak("panic: pthread_join"); \ |
113 | } STMT_END | |
114 | #endif /* JOIN */ | |
115 | ||
116 | #ifndef SET_THR | |
117 | #define SET_THR(t) \ | |
118 | STMT_START { \ | |
119 | if (pthread_setspecific(thr_key, (void *) (t))) \ | |
120 | croak("panic: pthread_setspecific"); \ | |
121 | } STMT_END | |
122 | #endif /* SET_THR */ | |
123 | ||
124 | #ifndef THR | |
125 | # ifdef OLD_PTHREADS_API | |
52e1cb5e | 126 | struct perl_thread *getTHR _((void)); |
ea0efc06 MB |
127 | # define THR getTHR() |
128 | # else | |
52e1cb5e | 129 | # define THR ((struct perl_thread *) pthread_getspecific(thr_key)) |
ea0efc06 MB |
130 | # endif /* OLD_PTHREADS_API */ |
131 | #endif /* THR */ | |
132 | ||
940cb80d MB |
133 | /* |
134 | * dTHR is performance-critical. Here, we only do the pthread_get_specific | |
135 | * if there may be more than one thread in existence, otherwise we get thr | |
136 | * from thrsv which is cached in the per-interpreter structure. | |
137 | * Systems with very fast pthread_get_specific (which should be all systems | |
138 | * but unfortunately isn't) may wish to simplify to "...*thr = THR". | |
139 | */ | |
ea0efc06 | 140 | #ifndef dTHR |
940cb80d MB |
141 | # define dTHR \ |
142 | struct perl_thread *thr = threadnum? THR : (struct perl_thread*)SvPVX(thrsv) | |
ea0efc06 | 143 | #endif /* dTHR */ |
11343788 | 144 | |
33f46ff6 MB |
145 | #ifndef INIT_THREADS |
146 | # ifdef NEED_PTHREAD_INIT | |
147 | # define INIT_THREADS pthread_init() | |
148 | # else | |
149 | # define INIT_THREADS NOOP | |
150 | # endif | |
151 | #endif | |
11343788 | 152 | |
940cb80d MB |
153 | /* Accessor for per-thread SVs */ |
154 | #define THREADSV(i) (thr->threadsvp[i]) | |
155 | ||
156 | /* | |
157 | * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we | |
158 | * try only locking them if there may be more than one thread in existence. | |
159 | * Systems with very fast mutexes (and/or slow conditionals) may wish to | |
160 | * remove the "if (threadnum) ..." test. | |
161 | */ | |
162 | #define LOCK_SV_MUTEX \ | |
163 | STMT_START { \ | |
164 | if (threadnum) \ | |
165 | MUTEX_LOCK(&sv_mutex); \ | |
166 | } STMT_END | |
167 | ||
168 | #define UNLOCK_SV_MUTEX \ | |
169 | STMT_START { \ | |
170 | if (threadnum) \ | |
171 | MUTEX_UNLOCK(&sv_mutex); \ | |
172 | } STMT_END | |
d55594ae | 173 | |
ea0efc06 MB |
174 | #ifndef THREAD_RET_TYPE |
175 | # define THREAD_RET_TYPE void * | |
176 | # define THREAD_RET_CAST(p) ((void *)(p)) | |
177 | #endif /* THREAD_RET */ | |
178 | ||
11343788 | 179 | |
33f46ff6 | 180 | /* Values and macros for thr->flags */ |
605e5515 MB |
181 | #define THRf_STATE_MASK 7 |
182 | #define THRf_R_JOINABLE 0 | |
183 | #define THRf_R_JOINED 1 | |
184 | #define THRf_R_DETACHED 2 | |
185 | #define THRf_ZOMBIE 3 | |
186 | #define THRf_DEAD 4 | |
f93b4edd | 187 | |
458fb581 | 188 | #define THRf_DID_DIE 8 |
07b73707 | 189 | |
f826a10b | 190 | /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */ |
458fb581 | 191 | #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK) |
f93b4edd | 192 | #define ThrSETSTATE(t, s) STMT_START { \ |
605e5515 | 193 | (t)->flags &= ~THRf_STATE_MASK; \ |
33f46ff6 | 194 | (t)->flags |= (s); \ |
605e5515 MB |
195 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), \ |
196 | "thread %p set to state %d\n", (t), (s))); \ | |
f93b4edd MB |
197 | } STMT_END |
198 | ||
199 | typedef struct condpair { | |
f826a10b MB |
200 | perl_mutex mutex; /* Protects all other fields */ |
201 | perl_cond owner_cond; /* For when owner changes at all */ | |
202 | perl_cond cond; /* For cond_signal and cond_broadcast */ | |
203 | Thread owner; /* Currently owning thread */ | |
f93b4edd MB |
204 | } condpair_t; |
205 | ||
206 | #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex) | |
207 | #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond) | |
208 | #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond) | |
209 | #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner | |
210 | ||
ea0efc06 MB |
211 | #else |
212 | /* USE_THREADS is not defined */ | |
213 | #define MUTEX_LOCK(m) | |
214 | #define MUTEX_UNLOCK(m) | |
215 | #define MUTEX_INIT(m) | |
216 | #define MUTEX_DESTROY(m) | |
217 | #define COND_INIT(c) | |
218 | #define COND_SIGNAL(c) | |
219 | #define COND_BROADCAST(c) | |
220 | #define COND_WAIT(c, m) | |
221 | #define COND_DESTROY(c) | |
93bce2dc GS |
222 | #define LOCK_SV_MUTEX |
223 | #define UNLOCK_SV_MUTEX | |
ea0efc06 MB |
224 | |
225 | #define THR | |
226 | /* Rats: if dTHR is just blank then the subsequent ";" throws an error */ | |
227 | #define dTHR extern int errno | |
11343788 | 228 | #endif /* USE_THREADS */ |