/
task.pmc
548 lines (385 loc) · 14.5 KB
/
task.pmc
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
src/pmc/task.pmc - Task PMC
=head1 DESCRIPTION
A Task PMC represents a concurrent native thread,
initialized at startup in a threadpool.
=head2 Functions
=over 4
=cut
*/
#include "parrot/scheduler_private.h"
#include "pmc/pmc_scheduler.h"
#include "pmc/pmc_proxy.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass Task provides invokable auto_attrs {
ATTR UINTVAL id; /* Unique identifier for this task */
ATTR FLOATVAL birthtime; /* The creation time stamp of the task */
ATTR Parrot_Interp interp; /* The interpreter that created the task */
ATTR PMC *code; /* An (optional) code for the task */
ATTR PMC *data; /* Additional data for the task */
ATTR INTVAL killed; /* Dead tasks don't get run */
ATTR PMC *mailbox; /* List of incoming messages */
ATTR Parrot_mutex mailbox_lock;
ATTR PMC *waiters; /* Tasks waiting on this one */
ATTR Parrot_mutex waiters_lock;
ATTR PMC *shared; /* List of variables shared with this task */
ATTR PMC *partner; /* Copy of this task on the other side of a GC barrier,
meaning in another thread */
/*
=item C<void init()>
Initialize a concurrency task object.
=cut
*/
VTABLE void init() {
Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF);
Parrot_Scheduler_attributes * const sched_data =
PARROT_SCHEDULER(interp->scheduler);
/* Set flags for custom GC mark. */
PObj_custom_mark_SET(SELF);
/* Set up the core struct. */
core_struct->birthtime = Parrot_floatval_time();
core_struct->code = PMCNULL;
core_struct->data = PMCNULL;
core_struct->interp = INTERP;
core_struct->killed = 0;
core_struct->mailbox = PMCNULL; /* Created lazily on demand */
core_struct->waiters = PMCNULL; /* Created lazily on demand */
core_struct->shared = Parrot_pmc_new(INTERP, enum_class_ResizablePMCArray);
core_struct->partner = NULL; /* Set by Parrot_thread_create_local_task */
MUTEX_INIT(core_struct->mailbox_lock);
MUTEX_INIT(core_struct->waiters_lock);
/* Assign a unique ID */
/* TODO: Fix collisions. */
core_struct->id = sched_data->next_task_id;
sched_data->next_task_id += 1;
/* By default, all flags are clear. */
TASK_active_CLEAR(SELF);
TASK_in_preempt_CLEAR(SELF);
TASK_recv_block_CLEAR(SELF);
}
/*
=item C<void init_pmc(PMC *data)>
Initializes a new Task with a C<Hash> PMC with any or all of the keys:
=over 4
=item C<birthtime>
The time at which this Task was created.
=item C<code>
An C<invokable> PMC related to this task.
=item C<data>
Some data that will be passed to C<code> when invoked.
=back
=cut
*/
VTABLE void init_pmc(PMC *data) {
Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF);
SELF.init();
if (VTABLE_isa(INTERP, data, CONST_STRING(INTERP, "Sub"))) {
core_struct->code = data;
}
else if (VTABLE_isa(INTERP, data, CONST_STRING(INTERP, "Hash"))) {
PMC * elem = VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "birthtime"));
if (! PMC_IS_NULL(elem))
core_struct->birthtime = VTABLE_get_number(INTERP, elem);
elem = VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "code"));
if (! PMC_IS_NULL(elem))
core_struct->code = elem;
elem = VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "data"));
if (! PMC_IS_NULL(elem))
core_struct->data = elem;
}
else {
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_INVALID_OPERATION,
"Task initializer must be a Hash or Sub");
}
}
/*
=item C<opcode_t *invoke(void *next)>
Invokes whatever is in the Task's associated code.
If the Task's data attribute is not null, pass it to the
code as the first argument.
=cut
*/
VTABLE opcode_t *invoke(void *next) :no_wb {
Parrot_Task_attributes * const task = PARROT_TASK(SELF);
Parrot_Scheduler_attributes * const sdata =
PARROT_SCHEDULER(interp->scheduler);
PMC * const active_tasks = sdata->all_tasks;
PARROT_ASSERT(! PMC_IS_NULL(task->code));
/* If a task is pre-empted, this will be set again. */
TASK_in_preempt_CLEAR(SELF);
if (! task->killed) {
const INTVAL current_depth =
Parrot_pcc_get_recursion_depth(interp, CURRENT_CONTEXT(interp));
/* Add the task to the set of active Tasks */
PMC * const task_id = Parrot_pmc_new(interp, enum_class_Integer);
VTABLE_set_integer_native(interp, task_id, task->id);
VTABLE_set_pmc_keyed(interp, active_tasks, task_id, SELF);
TASK_active_SET(SELF);
/* Actually run the task */
Parrot_ext_call(interp, task->code, "P->", task->data);
/* Restore recursion_depth since Parrot_Sub_invoke increments recursion_depth
which would not be decremented anymore if the sub is preempted */
Parrot_pcc_set_recursion_depth(interp, CURRENT_CONTEXT(interp), current_depth);
}
/* Fixed possibly racy read with write in send TASK_recv_block_CLEAR(partner) */
if (task->killed || !TASK_in_preempt_TEST(SELF)) {
/* The task is done. */
/* Remove it from the set of active Tasks */
INTVAL i, n = 0;
PMC * const task_id = Parrot_pmc_new(interp, enum_class_Integer);
VTABLE_set_integer_native(interp, task_id, task->id);
TASK_active_CLEAR(SELF);
VTABLE_delete_keyed(interp, active_tasks, task_id);
task->killed = 1;
/* schedule any waiters. */
if (!PMC_IS_NULL(task->waiters))
n = VTABLE_get_integer(interp, task->waiters);
for (i = 0; i < n; ++i) {
PMC * const wtask = VTABLE_get_pmc_keyed_int(interp, task->waiters, i);
Parrot_cx_schedule_task(interp, wtask);
}
if (task->partner) { /* TODO how can we know if the partner's still alive? */
Parrot_Task_attributes * const partner_task = PARROT_TASK(task->partner);
LOCK(partner_task->waiters_lock);
if (!PMC_IS_NULL(partner_task->waiters)) {
Parrot_block_GC_mark_locked(partner_task->interp);
partner_task->killed = 1;
n = VTABLE_get_integer(interp, partner_task->waiters);
for (i = 0; i < n; ++i) {
PMC * const wtask =
VTABLE_get_pmc_keyed_int(interp, partner_task->waiters, i);
Parrot_cx_schedule_immediate(partner_task->interp, wtask);
}
Parrot_unblock_GC_mark_locked(partner_task->interp);
}
else
partner_task->killed = 1;
UNLOCK(partner_task->waiters_lock);
}
}
return (opcode_t*) next;
}
/*
=item C<PMC *clone()>
Create a copy of the task, resetting status, ID, and birthtime.
=cut
*/
VTABLE PMC *clone() :no_wb {
/* Create the new task PMC, of the same type of this one (we may
* have been subclassed). */
PMC * const copy = Parrot_pmc_new(INTERP, SELF->vtable->base_type);
Parrot_Task_attributes * const new_struct = PARROT_TASK(copy);
const Parrot_Task_attributes * const old_struct = PARROT_TASK(SELF);
new_struct->code = VTABLE_clone(INTERP, old_struct->code);
new_struct->data = VTABLE_clone(INTERP, old_struct->data);
new_struct->shared = VTABLE_clone(INTERP, old_struct->shared);
return copy;
}
/*
=item C<PMC *get_attr_str(STRING *name)>
Gets the value of an attribute for this task.
=cut
*/
VTABLE PMC *get_attr_str(STRING *name) :no_wb {
Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF);
PMC * value = PMCNULL;
if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "birthtime"))) {
value = Parrot_pmc_new(INTERP, enum_class_Float);
VTABLE_set_number_native(INTERP, value, core_struct->birthtime);
}
else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "code"))) {
value = core_struct->code;
}
else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "data"))) {
value = core_struct->data;
}
return value;
}
/*
=item C<void set_attr_str(STRING *name, PMC *value)>
Sets the value of an attribute for this task.
=cut
*/
VTABLE void set_attr_str(STRING *name, PMC *value) {
Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF);
if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "birthtime"))) {
core_struct->birthtime = VTABLE_get_number(INTERP, value);
}
else if (Parrot_str_equal(INTERP, name, CONST_STRING(INTERP, "code"))) {
core_struct->code = value;
}
else if (STRING_equal(INTERP, name, CONST_STRING(INTERP, "data"))) {
core_struct->data = value;
}
}
/*
=item C<void push_pmc(PMC *value)>
Add value to the list of PMCs shared with this task.
=cut
*/
VTABLE void push_pmc(PMC *value) :manual_wb {
VTABLE_push_pmc(interp, PARROT_TASK(SELF)->shared, value);
}
/*
=item C<PMC *pop_pmc()>
Return a proxy for the last shared PMC.
=cut
*/
VTABLE PMC *pop_pmc() :manual_wb {
PMC *ret = VTABLE_pop_pmc(interp, PARROT_TASK(SELF)->shared);
RETURN(PMC *ret);
}
/*
=item C<void mark()>
Mark any referenced strings and PMCs.
=cut
*/
VTABLE void mark() :no_wb {
Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF);
if (core_struct) {
Parrot_gc_mark_PMC_alive(INTERP, core_struct->code);
Parrot_gc_mark_PMC_alive(INTERP, core_struct->data);
Parrot_gc_mark_PMC_alive(INTERP, core_struct->mailbox);
Parrot_gc_mark_PMC_alive(INTERP, core_struct->waiters);
Parrot_gc_mark_PMC_alive(INTERP, core_struct->shared);
/* don't mark our partner, since it belongs to another GC */
}
}
/*
=item C<void visit(PMC *info)>
This is used by freeze/thaw to visit the contents of the task.
C<*info> is the visit info, (see F<include/parrot/pmc_freeze.h>).
=cut
*/
VTABLE void visit(PMC *info) :no_wb {
/* 1) visit code block */
VISIT_PMC_ATTR(INTERP, info, SELF, Task, code);
VISIT_PMC_ATTR(INTERP, info, SELF, Task, data);
VISIT_PMC_ATTR(INTERP, info, SELF, Task, mailbox);
VISIT_PMC_ATTR(INTERP, info, SELF, Task, waiters);
}
/*
=item C<void freeze(PMC *info)>
Used to archive the task.
=cut
*/
VTABLE void freeze(PMC *info) :no_wb {
const Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF);
VTABLE_push_float(INTERP, info, core_struct->birthtime);
}
/*
=item C<void thaw(PMC *info)>
Used to unarchive the task.
=cut
*/
VTABLE void thaw(PMC *info) {
const FLOATVAL birthtime = VTABLE_shift_float(INTERP, info);
/* Allocate the task's core data struct and set custom flags. */
SELF.init();
/* Set the task's birthtime to the frozen birthtime */
PARROT_TASK(SELF)->birthtime = birthtime;
}
/*
=item C<void thawfinish(PMC *info)>
Called after the task has been thawed.
=cut
*/
VTABLE void thawfinish(PMC *info) :no_wb {
UNUSED(INTERP)
UNUSED(SELF)
UNUSED(info)
/* Parrot_Task_attributes * const core_struct = PARROT_TASK(SELF); */
}
/*
=back
=head2 Methods
=over 4
=item METHOD send(PMC *message)
Send a message to this task.
=cut
*/
METHOD send(PMC *message) {
Parrot_Task_attributes * const tdata = PARROT_TASK(SELF);
LOCK(tdata->mailbox_lock);
if (PMC_IS_NULL(tdata->mailbox)) {
tdata->mailbox = Parrot_pmc_new(interp, enum_class_PMCList);
PARROT_GC_WRITE_BARRIER(interp, SELF);
}
VTABLE_push_pmc(interp, tdata->mailbox, message);
UNLOCK(tdata->mailbox_lock);
if (tdata->partner) {
PMC * const partner = tdata->partner;
Parrot_Task_attributes * const pdata = PARROT_TASK(partner);
LOCK(tdata->mailbox_lock);
Parrot_block_GC_mark_locked(pdata->interp);
if (TASK_recv_block_TEST(partner)) {
/* Was: racy write with read in invoke task->killed || in_preempt */
/* TASK_recv_block_CLEAR(partner); */
Parrot_cx_schedule_immediate(pdata->interp, partner);
TASK_recv_block_CLEAR(partner);
}
Parrot_unblock_GC_mark_locked(pdata->interp);
UNLOCK(tdata->mailbox_lock);
}
else {
if (TASK_recv_block_TEST(SELF)) {
TASK_recv_block_CLEAR(SELF);
Parrot_cx_schedule_task(interp, SELF);
}
}
}
/*
METHOD receive() {
}
*/
/*
=item METHOD code(PMC * code :optional)
Read or write optional code, an C<invokable> PMC related to this task.
=item METHOD data(PMC * data :optional)
Reads or writes optional task-specific data, that will be passed to C<code> when invoked.
=cut
*/
METHOD code(PMC * code :optional, INTVAL has_code :opt_flag) :manual_wb {
Parrot_Task_attributes * const tdata = PARROT_TASK(SELF);
if (has_code) {
tdata->code = code;
PARROT_GC_WRITE_BARRIER(INTERP, SELF);
}
code = tdata->code;
RETURN(PMC *code);
}
METHOD data(PMC * data :optional, INTVAL has_data :opt_flag) :manual_wb {
Parrot_Task_attributes * const tdata = PARROT_TASK(SELF);
if (has_data) {
tdata->data = data;
PARROT_GC_WRITE_BARRIER(INTERP, SELF);
}
data = tdata->data;
RETURN(PMC *data);
}
/*
=item METHOD kill()
Kill this task.
=cut
*/
METHOD kill() :no_wb {
Parrot_Task_attributes * const tdata = PARROT_TASK(SELF);
tdata->killed = 1;
}
}
/*
=back
=head1 SEE ALSO
F<docs/pdds/pdd25_concurrency.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/