/
thread.h
324 lines (273 loc) · 10.6 KB
/
thread.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* thread.h
* Copyright (C) 2001-2007, Parrot Foundation.
* SVN Info
* $Id$
* Overview:
* This is the api header for the thread primitives
* Data Structure and Algorithms:
* History:
* Notes:
* References:
*/
#ifndef PARROT_THREAD_H_GUARD
#define PARROT_THREAD_H_GUARD
# include "parrot/parrot.h"
# include "parrot/atomic.h"
#ifndef PARROT_HAS_THREADS
# define LOCK(m)
# define UNLOCK(m)
# define COND_WAIT(c, m)
# define COND_TIMED_WAIT(c, m, t)
# define COND_SIGNAL(c)
# define COND_BROADCAST(c)
# define MUTEX_INIT(m)
# define MUTEX_DESTROY(m)
# define COND_INIT(c)
# define COND_DESTROY(c)
# define THREAD_CREATE_DETACHED(t, func, arg)
# define THREAD_CREATE_JOINABLE(t, func, arg)
# define JOIN(t, ret)
# define DETACH(t)
# define CLEANUP_PUSH(f, a)
# define CLEANUP_POP(a)
# define Parrot_mutex int
# define Parrot_cond int
# define Parrot_thread int
typedef void (*Cleanup_Handler)(void *);
# ifndef __timespec_defined
# define __timespec_defined
struct timespec {
time_t tv_sec;
long tv_nsec;
};
# endif /* __timespec_defined */
#endif /* PARROT_HAS_THREADS */
#ifndef YIELD
# define YIELD
#endif /* YIELD */
typedef enum {
THREAD_STATE_JOINABLE, /* default */
THREAD_STATE_DETACHED = 0x01, /* i.e. non-joinable */
THREAD_STATE_JOINED = 0x02, /* JOIN was issued */
THREAD_STATE_FINISHED = 0x04, /* the thread function has ended */
THREAD_STATE_NOT_STARTED = 0x08, /* the thread wasn't started */
THREAD_STATE_SUSPENDED_GC = 0x10, /* suspended for GC on request */
THREAD_STATE_GC_WAKEUP = 0x20, /* the thread is waiting on its condition
variable, and will do a GC run if
it is woken up and marked as suspended
for GC */
THREAD_STATE_SUSPEND_GC_REQUESTED = 0x40 /* the thread's event queue
contains a suspend-for-GC event */
} thread_state_enum;
/*
* per interpreter thread data structure
*/
typedef struct _Thread_data {
Parrot_thread thread; /* pthread_t or such */
INTVAL state;
int wants_shared_gc; /* therad is trying to
do a shared GC run */
UINTVAL tid; /* 0.. n-1 idx in interp array */
Parrot_Interp joiner; /* thread that is trying to join this */
/* for wr access to interpreter e.g. for GC
* if only used for GC the lock could be in the arena
* instead here, or in the interpreter, with negative size impact
* for the non-threaded case
*/
Parrot_mutex interp_lock;
/* for waking up the interpreter from various sorts
* of sleeping
*/
Parrot_cond interp_cond;
/* COW'd constant tables */
Hash *const_tables;
} Thread_data;
# define LOCK_INTERPRETER(interp) \
if ((interp)->thread_data) \
LOCK((interp)->thread_data->interp_lock)
# define UNLOCK_INTERPRETER(interp) \
if ((interp)->thread_data) \
UNLOCK((interp)->thread_data->interp_lock)
# define INTERPRETER_LOCK_INIT(interp) \
do { \
MUTEX_INIT((interp)->thread_data->interp_lock); \
COND_INIT((interp)->thread_data->interp_cond); \
} while (0)
# define INTERPRETER_LOCK_DESTROY(interp) \
do { \
MUTEX_DESTROY((interp)->thread_data->interp_lock); \
COND_DESTROY((interp)->thread_data->interp_cond); \
} while (0)
/*
* this global mutex protects the list of interpreters
*/
VAR_SCOPE Parrot_mutex interpreter_array_mutex;
VAR_SCOPE Interp ** interpreter_array;
VAR_SCOPE size_t n_interpreters;
typedef enum {
THREAD_GC_STAGE_NONE,
THREAD_GC_STAGE_MARK,
THREAD_GC_STAGE_SWEEP = THREAD_GC_STAGE_NONE
} thread_gc_stage_enum;
typedef struct _Shared_gc_info {
thread_gc_stage_enum gc_stage;
Parrot_cond gc_cond;
int num_reached;
Parrot_atomic_integer gc_block_level;
} Shared_gc_info;
/* TODO use thread pools instead */
VAR_SCOPE Shared_gc_info *shared_gc_info;
typedef struct _Sync {
Parrot_Interp owner; /* that interpreter, that owns
the arena, where the PMC is in */
Parrot_mutex pmc_lock; /* for wr access to PMCs content */
} Sync;
/* HEADERIZER BEGIN: src/thread.c */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_EXPORT
void Parrot_shared_gc_block(PARROT_INTERP)
__attribute__nonnull__(1);
PARROT_EXPORT
void Parrot_shared_gc_unblock(PARROT_INTERP)
__attribute__nonnull__(1);
void pt_add_to_interpreters(PARROT_INTERP,
ARGIN_NULLOK(Parrot_Interp new_interp))
__attribute__nonnull__(1);
void pt_clone_code(Parrot_Interp d, Parrot_Interp s);
void pt_clone_globals(Parrot_Interp d, Parrot_Interp s);
void pt_free_pool(PARROT_INTERP)
__attribute__nonnull__(1);
void pt_gc_mark_root_finished(PARROT_INTERP)
__attribute__nonnull__(1);
void pt_gc_start_mark(PARROT_INTERP)
__attribute__nonnull__(1);
void pt_gc_stop_mark(PARROT_INTERP)
__attribute__nonnull__(1);
void pt_join_threads(PARROT_INTERP)
__attribute__nonnull__(1);
PARROT_CAN_RETURN_NULL
PMC * pt_shared_fixup(PARROT_INTERP, ARGMOD(PMC *pmc))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
FUNC_MODIFIES(*pmc);
void pt_suspend_self_for_gc(PARROT_INTERP)
__attribute__nonnull__(1);
void pt_thread_detach(UINTVAL tid);
PARROT_CAN_RETURN_NULL
PMC* pt_thread_join(NOTNULL(Parrot_Interp parent), UINTVAL tid)
__attribute__nonnull__(1);
void pt_thread_kill(UINTVAL tid);
void pt_thread_prepare_for_run(Parrot_Interp d, NULLOK(Parrot_Interp s));
int pt_thread_run(PARROT_INTERP,
ARGOUT(PMC *dest_interp),
ARGIN(PMC *sub),
ARGIN_NULLOK(PMC *arg))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(*dest_interp);
int pt_thread_run_1(PARROT_INTERP,
ARGOUT(PMC* dest_interp),
ARGIN(PMC* sub),
ARGIN(PMC *arg))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(* dest_interp);
int pt_thread_run_2(PARROT_INTERP,
ARGOUT(PMC* dest_interp),
ARGIN(PMC* sub),
ARGIN(PMC *arg))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(* dest_interp);
int pt_thread_run_3(PARROT_INTERP,
ARGOUT(PMC* dest_interp),
ARGIN(PMC* sub),
ARGIN(PMC *arg))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4)
FUNC_MODIFIES(* dest_interp);
void pt_thread_wait_with(PARROT_INTERP, ARGMOD(Parrot_mutex *mutex))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
FUNC_MODIFIES(*mutex);
void pt_thread_yield(void);
PARROT_CAN_RETURN_NULL
PMC * pt_transfer_sub(
ARGOUT(Parrot_Interp d),
ARGIN(Parrot_Interp s),
ARGIN(PMC *sub))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(d);
#define ASSERT_ARGS_Parrot_shared_gc_block __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_Parrot_shared_gc_unblock __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_add_to_interpreters __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_clone_code __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pt_clone_globals __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pt_free_pool __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_gc_mark_root_finished __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_gc_start_mark __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_gc_stop_mark __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_join_threads __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_shared_fixup __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(pmc))
#define ASSERT_ARGS_pt_suspend_self_for_gc __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
#define ASSERT_ARGS_pt_thread_detach __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pt_thread_join __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(parent))
#define ASSERT_ARGS_pt_thread_kill __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pt_thread_prepare_for_run __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pt_thread_run __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(dest_interp) \
, PARROT_ASSERT_ARG(sub))
#define ASSERT_ARGS_pt_thread_run_1 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(dest_interp) \
, PARROT_ASSERT_ARG(sub) \
, PARROT_ASSERT_ARG(arg))
#define ASSERT_ARGS_pt_thread_run_2 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(dest_interp) \
, PARROT_ASSERT_ARG(sub) \
, PARROT_ASSERT_ARG(arg))
#define ASSERT_ARGS_pt_thread_run_3 __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(dest_interp) \
, PARROT_ASSERT_ARG(sub) \
, PARROT_ASSERT_ARG(arg))
#define ASSERT_ARGS_pt_thread_wait_with __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(mutex))
#define ASSERT_ARGS_pt_thread_yield __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_pt_transfer_sub __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(d) \
, PARROT_ASSERT_ARG(s) \
, PARROT_ASSERT_ARG(sub))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: src/thread.c */
#endif /* PARROT_THREAD_H_GUARD */
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/