forked from parrot/parrot
/
thread.c
490 lines (353 loc) · 11.9 KB
/
thread.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
Copyright (C) 2011, Parrot Foundation.
=head1 NAME
src/thread.c - Thread handling stuff
=head1 DESCRIPTION
Threads are created by creating new C<ParrotInterpreter> objects.
=head2 Functions
=over 4
=cut
*/
#include "parrot/parrot.h"
#include "parrot/extend.h"
#include "parrot/thread.h"
#include "parrot/atomic.h"
#include "parrot/runcore_api.h"
#include "pmc/pmc_scheduler.h"
#include "pmc/pmc_sub.h"
#include "pmc/pmc_parrotinterpreter.h"
/* HEADERIZER HFILE: include/parrot/thread.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_CAN_RETURN_NULL
static PMC * Parrot_thread_make_local_args_copy(PARROT_INTERP,
ARGIN(Parrot_Interp source),
ARGIN_NULLOK(PMC *args))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_CAN_RETURN_NULL
static PMC * Parrot_thread_make_local_copy(PARROT_INTERP,
ARGIN(Parrot_Interp from),
ARGIN(PMC *arg))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
PARROT_CAN_RETURN_NULL
static void* Parrot_thread_outer_runloop(ARGIN_NULLOK(void *arg));
#define ASSERT_ARGS_Parrot_thread_make_local_args_copy \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(source))
#define ASSERT_ARGS_Parrot_thread_make_local_copy __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(from) \
, PARROT_ASSERT_ARG(arg))
#define ASSERT_ARGS_Parrot_thread_outer_runloop __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
static Interp * threads_array[MAX_THREADS];
/*
=item C<PMC * Parrot_thread_create(PARROT_INTERP, INTVAL type, INTVAL
clone_flags)>
Create a new thread, cloning the current interpreter
=cut
*/
PARROT_CANNOT_RETURN_NULL
PMC *
Parrot_thread_create(PARROT_INTERP, INTVAL type, INTVAL clone_flags)
{
ASSERT_ARGS(Parrot_thread_create)
PMC * const new_interp_pmc = Parrot_pmc_new(interp, type);
Interp * const new_interp = (Interp *)VTABLE_get_pointer(interp, new_interp_pmc);
/* Parrot_pmc_new sets parent_interpreter which would confuse the GC */
new_interp->parent_interpreter = NULL;
clone_interpreter(new_interp, interp, clone_flags);
new_interp->thread_data = mem_internal_allocate_zeroed_typed(Thread_data);
new_interp->thread_data->tid = 0;
#ifdef _WIN32
#else
pipe(new_interp->thread_data->notifierfd);
#endif
PARROT_GC_WRITE_BARRIER(interp, new_interp);
return new_interp_pmc;
}
/*
=item C<int Parrot_thread_run(PARROT_INTERP, PMC *thread_interp_pmc, PMC *sub,
PMC *arg)>
Run a Parrot_thread
=cut
*/
int
Parrot_thread_run(PARROT_INTERP, ARGMOD(PMC *thread_interp_pmc), ARGIN(PMC *sub),
ARGIN_NULLOK(PMC *arg))
{
ASSERT_ARGS(Parrot_thread_run)
Interp * const thread_interp = (Interp *)VTABLE_get_pointer(interp, thread_interp_pmc);
/*SETATTR_ParrotInterpreter_sub(interp, thread_interp_pmc,
Parrot_thread_transfer_sub(thread_interp, interp, sub)); */
VTABLE_set_pmc(interp, thread_interp_pmc,
Parrot_thread_make_local_args_copy(thread_interp, interp, arg));
thread_interp->thread_data->state = THREAD_STATE_JOINABLE;
THREAD_CREATE_JOINABLE(thread_interp->thread_data->thread,
Parrot_thread_outer_runloop, thread_interp_pmc);
return thread_interp->thread_data->tid;
}
/*
=item C<void Parrot_thread_schedule_task(PARROT_INTERP, PMC *thread, PMC *task)>
Schedule a task with the thread's scheduler.
=cut
*/
void
Parrot_thread_schedule_task(PARROT_INTERP, ARGIN(PMC *thread), ARGIN(PMC *task))
{
ASSERT_ARGS(Parrot_thread_schedule_task)
PMC * const self = (PMC*) thread;
Parrot_Interp const thread_interp =
(Parrot_Interp)((Parrot_ParrotInterpreter_attributes *)PMC_data(self))->interp;
PMC * const local_task = Parrot_thread_make_local_copy(thread_interp, interp, task);
VTABLE_push_pmc(thread_interp, thread_interp->scheduler, local_task);
}
/*
=item C<static void* Parrot_thread_outer_runloop(void *arg)>
Run a Parrot_thread
=cut
*/
PARROT_CAN_RETURN_NULL
static void*
Parrot_thread_outer_runloop(ARGIN_NULLOK(void *arg))
{
ASSERT_ARGS(Parrot_thread_outer_runloop)
int lo_var_ptr;
PMC * const self = (PMC*) arg;
PMC *ret_val = PMCNULL;
Parrot_Interp interp =
(Parrot_Interp)((Parrot_ParrotInterpreter_attributes *)PMC_data(self))->interp;
PMC * const scheduler = interp->scheduler;
Parrot_Scheduler_attributes * const sched = PARROT_SCHEDULER(scheduler);
INTVAL alarm_count;
char dummy;
/* need to set it here because argument passing can trigger GC */
interp->lo_var_ptr = &lo_var_ptr;
do {
while (VTABLE_get_integer(interp, scheduler) > 0) {
/* there can be no active runloops at this point, so it should be save
* to start counting at 0 again. This way the continuation in the next
* task will find a runloop with id 1 when encountering an exception */
interp->current_runloop_level = 0;
reset_runloop_id_counter(interp);
Parrot_cx_next_task(interp, scheduler);
/* add expired alarms to the task queue */
Parrot_cx_check_alarms(interp, interp->scheduler);
}
alarm_count = VTABLE_get_integer(interp, sched->alarms);
if (alarm_count > 0) {
#ifdef _WIN32
/* TODO: Implement on Windows */
#else
/* Nothing to do except to wait for the next alarm to expire */
read(interp->thread_data->notifierfd[0], &dummy, 1);
#endif
Parrot_cx_check_alarms(interp, interp->scheduler);
}
} while (alarm_count);
return ret_val;
}
/*
=item C<void Parrot_thread_notify_threads(PARROT_INTERP)>
Give all threads a chance to check their alarms.
=cut
*/
void
Parrot_thread_notify_threads(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_thread_notify_threads)
#ifdef _WIN32
#else
int i;
char dummy = 0;
Interp ** const threads_array = Parrot_thread_get_threads_array(interp);
for (i = 1; i < MAX_THREADS; i++)
if (threads_array[i]) {
write(threads_array[i]->thread_data->notifierfd[1], &dummy, 1);
}
#endif
}
/*
=back
=head2 Helper functions used also for running plain interpreters
=over 4
=item C<void Parrot_clone_code(Parrot_Interp d, Parrot_Interp s)>
Copies/clones the packfile/code from interpreter C<s> to C<d>. All
resources are created in C<d>.
=cut
*/
void
Parrot_clone_code(Parrot_Interp d, Parrot_Interp s)
{
ASSERT_ARGS(Parrot_clone_code)
Parrot_block_GC_mark(d);
Interp_flags_SET(d, PARROT_EXTERN_CODE_FLAG);
d->code = NULL;
Parrot_switch_to_cs(d, s->code, 1);
Parrot_unblock_GC_mark(d);
}
/*
=item C<PMC * Parrot_thread_transfer_sub(Parrot_Interp destination,
Parrot_Interp source, PMC *sub)>
Clones the sub so that it's suitable for the other interpreter.
=cut
*/
PARROT_CAN_RETURN_NULL
PMC *
Parrot_thread_transfer_sub(ARGOUT(Parrot_Interp destination),
ARGIN(Parrot_Interp source), ARGIN(PMC *sub))
{
ASSERT_ARGS(Parrot_thread_transfer_sub)
Parrot_io_eprintf(source, "copying over subroutine [%Ss]\n",
Parrot_sub_full_sub_name(source, sub));
return Parrot_thread_make_local_copy(destination, source, sub);
}
/*
=item C<static PMC * Parrot_thread_make_local_copy(PARROT_INTERP, Parrot_Interp
from, PMC *arg)>
Creates a local copy of the PMC. This includes workarounds for Parrot_clone()
not doing the Right Thing with subroutines (specifically, code segments aren't
preserved and it is difficult to do so as long as Parrot_clone() depends on
freezing).
=cut
*/
PARROT_CAN_RETURN_NULL
static PMC *
Parrot_thread_make_local_copy(PARROT_INTERP, ARGIN(Parrot_Interp from), ARGIN(PMC *arg))
{
ASSERT_ARGS(Parrot_thread_make_local_copy)
PMC *ret_val;
STRING * const _sub = interp->vtables[enum_class_Sub]->whoami;
STRING * const _multi_sub = interp->vtables[enum_class_MultiSub]->whoami;
if (PMC_IS_NULL(arg)) {
ret_val = PMCNULL;
}
else if (VTABLE_isa(from, arg, _multi_sub)) {
INTVAL i = 0;
const INTVAL n = VTABLE_elements(from, arg);
ret_val = Parrot_pmc_new(interp, enum_class_MultiSub);
for (i = 0; i < n; ++i) {
PMC *const orig = VTABLE_get_pmc_keyed_int(from, arg, i);
PMC *const copy = Parrot_thread_make_local_copy(interp, from, orig);
VTABLE_push_pmc(interp, ret_val, copy);
}
}
else if (VTABLE_isa(from, arg, _sub)) {
/* this is a workaround for cloning subroutines not actually
* working as one might expect mainly because the segment is
* not correctly copied
*/
Parrot_Sub_attributes *ret_val_sub, *arg_sub;
ret_val = Parrot_clone(interp, arg);
PMC_get_sub(interp, ret_val, ret_val_sub);
PMC_get_sub(interp, arg, arg_sub);
ret_val_sub->seg = arg_sub->seg;
/* Skip vtable overrides and methods. */
if (ret_val_sub->vtable_index == -1
&& !(ret_val_sub->comp_flags & SUB_COMP_FLAG_METHOD)) {
Parrot_ns_store_sub(interp, ret_val);
}
}
else {
ret_val = Parrot_clone(interp, arg);
}
return ret_val;
}
/*
=item C<static PMC * Parrot_thread_make_local_args_copy(PARROT_INTERP,
Parrot_Interp source, PMC *args)>
Make a local copy of the corresponding array of arguments.
=cut
*/
PARROT_CAN_RETURN_NULL
static PMC *
Parrot_thread_make_local_args_copy(PARROT_INTERP, ARGIN(Parrot_Interp source),
ARGIN_NULLOK(PMC *args))
{
ASSERT_ARGS(Parrot_thread_make_local_args_copy)
PMC *ret_val;
INTVAL old_size;
INTVAL i;
if (PMC_IS_NULL(args))
return PMCNULL;
old_size = VTABLE_get_integer(source, args);
/* XXX should this be a different type? */
ret_val = Parrot_pmc_new(interp, enum_class_FixedPMCArray);
VTABLE_set_integer_native(interp, ret_val, old_size);
for (i = 0; i < old_size; ++i) {
PMC * const copy = Parrot_thread_make_local_copy(interp, source,
VTABLE_get_pmc_keyed_int(source, args, i));
VTABLE_set_pmc_keyed_int(interp, ret_val, i, copy);
}
return ret_val;
}
/*
=item C<PMC** Parrot_thread_get_threads_array(PARROT_INTERP)>
Returns the threads array.
=cut
*/
PARROT_CANNOT_RETURN_NULL
PMC**
Parrot_thread_get_threads_array(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_thread_get_threads_array)
return threads_array;
}
/*
=item C<void Parrot_thread_init_threads_array(PARROT_INTERP)>
Initialize the threads array.
=cut
*/
void
Parrot_thread_init_threads_array(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_thread_init_threads_array)
int i = 0;
for (; i < MAX_THREADS; i++)
threads_array[i] = NULL;
}
/*
=item C<int Parrot_thread_get_free_threads_array_index(PARROT_INTERP)>
Returns an index of a free slot in the threads_array.
Returns -1 if no slot is available.
=cut
*/
int
Parrot_thread_get_free_threads_array_index(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_thread_get_free_threads_array_index)
int i = 0;
for (; i < MAX_THREADS; i++)
if (threads_array[i] == NULL)
return i;
return -1;
}
/*
=item C<void Parrot_thread_insert_thread(PARROT_INTERP, Interp* thread, int
index)>
Insert the given thread into the threads_array at index i.
=cut
*/
void
Parrot_thread_insert_thread(PARROT_INTERP, ARGIN(Interp* thread), int index)
{
ASSERT_ARGS(Parrot_thread_insert_thread)
threads_array[index] = thread;
}
/*
=back
=head1 SEE ALSO
F<include/parrot/thread.h>
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/