/
scheduler.c
777 lines (563 loc) · 20.7 KB
/
scheduler.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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
Copyright (C) 2007-2015, Parrot Foundation.
=head1 NAME
src/scheduler.c - The core routines for the concurrency scheduler
=head1 DESCRIPTION
Each interpreter has a concurrency scheduler element in its core struct. The
scheduler is responsible for receiving, dispatching, and monitoring events,
exceptions, async I/O, and concurrent tasks (threads).
=cut
*/
#include "parrot/parrot.h"
#include "parrot/extend.h"
#include "parrot/scheduler_private.h"
#include "parrot/runcore_api.h"
#include "parrot/alarm.h"
#include "parrot/scheduler.h"
#include "parrot/thread.h"
#include "pmc/pmc_scheduler.h"
#include "pmc/pmc_task.h"
#include "pmc/pmc_timer.h"
#include "pmc/pmc_alarm.h"
#include "pmc/pmc_pmclist.h"
#include "pmc/pmc_continuation.h"
#include "scheduler.str"
/* HEADERIZER HFILE: include/parrot/scheduler.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
static int Parrot_cx_preemption_enabled(PARROT_INTERP)
__attribute__nonnull__(1);
#define ASSERT_ARGS_Parrot_cx_preemption_enabled __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=head2 Scheduler Interface Functions
Functions to interface with the concurrency scheduler.
=over 4
=item C<void Parrot_cx_init_scheduler(PARROT_INTERP)>
Initialize the concurrency scheduler for the interpreter.
=cut
*/
void
Parrot_cx_init_scheduler(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_init_scheduler)
interp->quantum_done = Parrot_floatval_time() + PARROT_TASK_SWITCH_QUANTUM;
interp->scheduler = Parrot_pmc_new(interp, enum_class_Scheduler);
if (!interp->parent_interpreter) { /* only run once, for the master interp */
Parrot_alarm_init();
Parrot_thread_init_threads_array(interp);
Parrot_thread_insert_thread(interp, interp, 0);
}
}
/*
=item C<void Parrot_cx_begin_execution(PARROT_INTERP, PMC *main, PMC *argv)>
Construct the main task, add it to the task queue, and then execute tasks
until the task queue becomes empty.
=cut
*/
PARROT_EXPORT
void
Parrot_cx_begin_execution(PARROT_INTERP, ARGIN(PMC *main), ARGIN(PMC *argv))
{
ASSERT_ARGS(Parrot_cx_begin_execution)
PMC * const scheduler = interp->scheduler;
Parrot_Scheduler_attributes * const sched = PARROT_SCHEDULER(scheduler);
PMC * const main_task = Parrot_pmc_new(interp, enum_class_Task);
Parrot_Task_attributes * const tdata = PARROT_TASK(main_task);
INTVAL task_count;
tdata->code = main;
tdata->data = argv;
PARROT_GC_WRITE_BARRIER(interp, main_task);
SCHEDULER_enable_scheduler_SET(scheduler);
Parrot_cx_schedule_immediate(interp, main_task);
Parrot_cx_outer_runloop(interp);
task_count = VTABLE_get_integer(interp, sched->all_tasks);
if (task_count > 0)
Parrot_warn(interp, PARROT_WARNINGS_ALL_FLAG,
"Exiting with %d active tasks.\n", task_count);
}
/*
=item C<void Parrot_cx_outer_runloop(PARROT_INTERP)>
This is the core loop performed by each active OS thread. If it's the
thread that needs to be running, it invokes the Scheduler to pick a
task.
=cut
*/
void
Parrot_cx_outer_runloop(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_outer_runloop)
PMC * const scheduler = interp->scheduler;
Parrot_Scheduler_attributes * const sched = PARROT_SCHEDULER(scheduler);
INTVAL alarm_count, foreign_count, i;
/* Main loop. Continue to loop so long as we have any tasks, any alarms,
or any foreign tasks to execute. If we have none of these things, exit. */
do {
/* If we have tasks in the scheduler, run them in a loop until there
are no more. */
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);
}
/* Loop over all foreign tasks in the scheduler. If the foreign task
is killed, remove it from the scheduler. */
foreign_count = VTABLE_get_integer(interp, sched->foreign_tasks);
for (i = 0; i < foreign_count; i++) {
PMC * const task = VTABLE_get_pmc_keyed_int(interp, sched->foreign_tasks, i);
LOCK(PARROT_TASK(task)->waiters_lock);
if (PARROT_TASK(task)->killed) {
VTABLE_delete_keyed_int(interp, sched->foreign_tasks, i);
i--;
foreign_count--;
}
UNLOCK(PARROT_TASK(task)->waiters_lock);
}
/* If we have no scheduled tasks, but we do have an alarm or foreign
task, we can wait for one of those before we start executing things
again. */
alarm_count = VTABLE_get_integer(interp, sched->alarms);
if (VTABLE_get_integer(interp, scheduler) == 0 && (alarm_count > 0 || foreign_count > 0)) {
/* Nothing to do except to wait for the next alarm to expire */
Parrot_thread_wait_for_notification(interp);
Parrot_cx_check_alarms(interp, interp->scheduler);
}
} while (alarm_count || foreign_count || VTABLE_get_integer(interp, scheduler) > 0);
}
/*
=item C<void Parrot_cx_set_scheduler_alarm(PARROT_INTERP)>
Set the task switch alarm for the scheduler.
=cut
*/
void
Parrot_cx_set_scheduler_alarm(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_set_scheduler_alarm)
const FLOATVAL time_now = Parrot_floatval_time();
interp->quantum_done = time_now + PARROT_TASK_SWITCH_QUANTUM;
Parrot_alarm_set(interp->quantum_done);
}
/*
=item C<void Parrot_cx_next_task(PARROT_INTERP, PMC *scheduler)>
Run the task at the head of the task queue until it ends or is
pre-empted.
=cut
*/
void
Parrot_cx_next_task(PARROT_INTERP, ARGIN(PMC *scheduler))
{
ASSERT_ARGS(Parrot_cx_next_task)
PMC * const task = VTABLE_shift_pmc(interp, scheduler);
interp->cur_task = task;
if (!VTABLE_isa(interp, task, CONST_STRING(interp, "Task")))
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_OPERATION,
"Found a non-Task in the task queue");
/* If we have no tasks in the queue, we can disable task preemption and
save ourselves a few cycles. */
if (VTABLE_get_integer(interp, scheduler) > 0)
Parrot_cx_enable_preemption(interp);
else
Parrot_cx_disable_preemption(interp);
Parrot_ext_call(interp, task, "->");
}
/*
=item C<opcode_t* Parrot_cx_check_scheduler(PARROT_INTERP, opcode_t *next)>
Does the scheduler need to wake up and do anything? If so, do that now.
=cut
*/
PARROT_CAN_RETURN_NULL
opcode_t*
Parrot_cx_check_scheduler(PARROT_INTERP, ARGIN(opcode_t *next))
{
ASSERT_ARGS(Parrot_cx_check_scheduler)
PMC * const scheduler = interp->scheduler;
/* If we have any outstanding alarms, or if we have been requested to
wake up, run the scheduler. */
if (Parrot_alarm_check(&(interp->last_alarm))
|| SCHEDULER_wake_requested_TEST(scheduler)) {
SCHEDULER_wake_requested_CLEAR(scheduler);
return Parrot_cx_run_scheduler(interp, scheduler, next);
}
return next;
}
/*
=item C<opcode_t* Parrot_cx_run_scheduler(PARROT_INTERP, PMC *scheduler,
opcode_t *next)>
Checks to see if any tasks need to be scheduled or if the current task
needs to be pre-empted.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
opcode_t*
Parrot_cx_run_scheduler(PARROT_INTERP, ARGIN(PMC *scheduler), ARGIN(opcode_t *next))
{
ASSERT_ARGS(Parrot_cx_run_scheduler)
Parrot_cx_check_alarms(interp, scheduler);
Parrot_cx_check_quantum(interp, scheduler);
if (SCHEDULER_resched_requested_TEST(scheduler)) {
SCHEDULER_resched_requested_CLEAR(scheduler);
/* A task switch will only work in the outer runloop of a fully
booted Parrot. In a Parrot that hasn't called begin_execution,
or in a nested runloop, we silently ignore task switches. */
if (SCHEDULER_enable_scheduler_TEST(scheduler) && interp->current_runloop_level <= 1)
return Parrot_cx_preempt_task(interp, scheduler, next);
}
/* Some alarm seems to have fired, but not the scheduler's.
* Re-set the scheduler alarm */
if (Parrot_cx_preemption_enabled(interp))
Parrot_alarm_set(interp->quantum_done);
return next;
}
/*
=item C<void Parrot_cx_check_quantum(PARROT_INTERP, PMC *scheduler)>
If the quantum has expired, schedule the next task.
=cut
*/
void
Parrot_cx_check_quantum(PARROT_INTERP, ARGIN(PMC *scheduler))
{
ASSERT_ARGS(Parrot_cx_check_quantum)
/* If we are using preemption, check the current time and possibly
schedule the next preemption */
if (Parrot_cx_preemption_enabled(interp)) {
const FLOATVAL time_now = Parrot_floatval_time();
if (time_now >= interp->quantum_done)
SCHEDULER_resched_requested_SET(scheduler);
}
}
/*
=item C<PMC* Parrot_cx_stop_task(PARROT_INTERP, opcode_t *next)>
Stop the current task and pack it up into a PMC what can be used to resume later.
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
PMC*
Parrot_cx_stop_task(PARROT_INTERP, ARGIN(opcode_t *next))
{
ASSERT_ARGS(Parrot_cx_stop_task)
PMC * const task = Parrot_cx_current_task(interp);
Parrot_Task_attributes * const tdata = PARROT_TASK(task);
PMC * const cont = Parrot_pmc_new(interp, enum_class_Continuation);
VTABLE_set_pointer(interp, cont, next);
/* TODO: This check seems expensive. Do we need to have this active at all
times, or can we make this conditional on NDEBUG? */
if (PMC_IS_NULL(task) || !VTABLE_isa(interp, task, CONST_STRING(interp, "Task")))
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_OPERATION,
"Attempt to stop invalid interp->current_task");
tdata->code = cont;
PARROT_GC_WRITE_BARRIER(interp, task);
TASK_in_preempt_SET(task);
return task;
}
/*
=item C<opcode_t* Parrot_cx_preempt_task(PARROT_INTERP, PMC *scheduler, opcode_t
*next)>
Pre-empt the current task. It goes on the foot of the task queue,
and then we jump all the way back to the task scheduling loop.
=cut
*/
PARROT_CAN_RETURN_NULL
opcode_t*
Parrot_cx_preempt_task(PARROT_INTERP, ARGIN(PMC *scheduler), ARGIN(opcode_t *next))
{
ASSERT_ARGS(Parrot_cx_preempt_task)
PMC * const task = Parrot_cx_stop_task(interp, next);
VTABLE_push_pmc(interp, scheduler, task);
return (opcode_t*)NULL;
}
/*
=item C<void Parrot_cx_runloop_wake(PARROT_INTERP, PMC *scheduler)>
Wake a sleeping scheduler runloop (generally called when new tasks are added to
the scheduler's task list).
=cut
*/
void
Parrot_cx_runloop_wake(PARROT_INTERP, ARGIN(PMC *scheduler))
{
ASSERT_ARGS(Parrot_cx_runloop_wake)
Parrot_runcore_enable_event_checking(interp);
SCHEDULER_wake_requested_SET(scheduler);
}
/*
=item C<void Parrot_cx_schedule_task(PARROT_INTERP, PMC *task_or_sub)>
Add a task to to the task queue for execution.
Probably cannot be called across interpreters/threads, must instead be
called from within the interpreter's runloop.
=cut
*/
PARROT_EXPORT
void
Parrot_cx_schedule_task(PARROT_INTERP, ARGIN(PMC *task_or_sub))
{
ASSERT_ARGS(Parrot_cx_schedule_task)
PMC * task = PMCNULL;
int index;
if (!interp->scheduler)
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_OPERATION,
"Scheduler was not initialized for this interpreter");
/* TODO: Can we do anything less expensive than an ISA check here? */
if (VTABLE_isa(interp, task_or_sub, CONST_STRING(interp, "Task")))
task = task_or_sub;
else if (VTABLE_isa(interp, task_or_sub, CONST_STRING(interp, "Sub"))) {
Parrot_Task_attributes *tdata;
task = Parrot_pmc_new(interp, enum_class_Task);
tdata = PARROT_TASK(task);
tdata->code = task_or_sub;
PARROT_GC_WRITE_BARRIER(interp, task);
}
else
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_OPERATION,
"Can only schedule Tasks and Subs");
#ifdef PARROT_HAS_THREADS
/* Search for a thread that is free. If we have a free thread, schedule
the task there. Otherwise, find the thread with the fewest tasks in its
queue and schedule it there. */
index = Parrot_thread_get_free_threads_array_index(NULL);
if (index > -1) { /* start a new thread */
PMC * const thread = Parrot_thread_create(interp,
enum_class_ParrotInterpreter,
PARROT_CLONE_DEFAULT);
Interp * const thread_interp = (Interp *)VTABLE_get_pointer(interp, thread);
Parrot_thread_schedule_task(interp, thread_interp, task);
Parrot_thread_insert_thread(interp, thread_interp, index);
Parrot_thread_run(interp, thread, task, NULL);
}
else {
/* find the thread with the fewest tasks */
Interp ** const threads_array = Parrot_thread_get_threads_array(interp);
int numthreads = Parrot_get_num_threads(interp);
Interp * candidate = NULL;
int i, min_tasks = INT_MAX;
for (i = 1; i < numthreads; i++)
if (threads_array[i]) {
int const tasks = VTABLE_get_integer(threads_array[i], threads_array[i]->scheduler);
if (tasks < min_tasks) {
min_tasks = tasks;
candidate = threads_array[i];
}
}
if (candidate == NULL)
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_OPERATION,
"Could not find a free thread");
Parrot_thread_schedule_task(interp, candidate, task);
Parrot_thread_notify_thread(candidate);
/* going from single to multi tasking? */
if (VTABLE_get_integer(interp, interp->scheduler) == 1)
Parrot_cx_enable_preemption(interp);
}
#else
/* If we don't have threads, we still have tasks and basic preemption. Add
the task to the queue. */
VTABLE_push_pmc(interp, interp->scheduler, task);
/* going from single to multi tasking? */
if (VTABLE_get_integer(interp, interp->scheduler) == 1)
Parrot_cx_enable_preemption(interp);
#endif
}
/*
=item C<void Parrot_cx_schedule_immediate(PARROT_INTERP, PMC *task_or_sub)>
Add a task to the task queue for immediate execution.
=cut
*/
PARROT_EXPORT
void
Parrot_cx_schedule_immediate(PARROT_INTERP, ARGIN(PMC *task_or_sub))
{
ASSERT_ARGS(Parrot_cx_schedule_immediate)
PMC *task;
/* TODO: Can we do something less expensive than ISA? */
if (VTABLE_isa(interp, task_or_sub, CONST_STRING(interp, "Task")))
task = task_or_sub;
else if (VTABLE_isa(interp, task_or_sub, CONST_STRING(interp, "Sub"))) {
Parrot_Task_attributes *tdata;
task = Parrot_pmc_new(interp, enum_class_Task);
tdata = PARROT_TASK(task);
tdata->code = task_or_sub;
PARROT_GC_WRITE_BARRIER(interp, task);
}
else
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_OPERATION,
"Can only schedule Tasks and Subs");
VTABLE_unshift_pmc(interp, interp->scheduler, task);
SCHEDULER_wake_requested_SET(interp->scheduler);
SCHEDULER_resched_requested_SET(interp->scheduler);
Parrot_thread_notify_thread(interp);
}
/*
=item C<PMC* Parrot_cx_current_task(PARROT_INTERP)>
Returns the task that is currently running.
=cut
*/
PARROT_CANNOT_RETURN_NULL
PARROT_PURE_FUNCTION
PMC*
Parrot_cx_current_task(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_current_task)
return interp->cur_task;
}
/*
=back
=head2 Scheduler Message Interface Functions
Functions that are used to interface with the message queue in the concurrency
scheduler.
=over 4
=item C<void Parrot_cx_send_message(PARROT_INTERP, STRING *messagetype, PMC
*payload)>
Send a message to a scheduler in a different interpreter/thread.
=cut
*/
PARROT_EXPORT
void
Parrot_cx_send_message(PARROT_INTERP, ARGIN(STRING *messagetype), ARGIN(SHIM(PMC *payload)))
{
ASSERT_ARGS(Parrot_cx_send_message)
if (interp->scheduler) {
Parrot_Scheduler_attributes * const sched_struct =
PARROT_SCHEDULER(interp->scheduler);
PMC * const message = Parrot_pmc_new(interp, enum_class_SchedulerMessage);
VTABLE_set_string_native(interp, message, messagetype);
VTABLE_push_pmc(interp, sched_struct->messages, message);
Parrot_cx_runloop_wake(interp, interp->scheduler);
}
}
/*
=item C<void Parrot_cx_schedule_alarm(PARROT_INTERP, PMC *alarm)>
Schedule an alarm.
=cut
*/
void
Parrot_cx_schedule_alarm(PARROT_INTERP, ARGIN(PMC *alarm))
{
ASSERT_ARGS(Parrot_cx_schedule_alarm)
Parrot_Scheduler_attributes * const sched = PARROT_SCHEDULER(interp->scheduler);
FLOATVAL alarm_time = VTABLE_get_number(interp, alarm);
Parrot_alarm_set(alarm_time);
/* Insert new alarm at correct (ordered by time) position in array. */
Parrot_pmc_list_insert_by_number(interp, sched->alarms, alarm);
}
/*
=item C<void Parrot_cx_check_alarms(PARROT_INTERP, PMC *scheduler)>
Add the subs attached to any expired alarms to the task queue.
=cut
*/
PARROT_EXPORT
void
Parrot_cx_check_alarms(PARROT_INTERP, ARGIN(PMC *scheduler))
{
ASSERT_ARGS(Parrot_cx_check_alarms)
Parrot_Scheduler_attributes * const sched = PARROT_SCHEDULER(scheduler);
INTVAL alarm_count = VTABLE_get_integer(interp, sched->alarms);
const FLOATVAL now_time = Parrot_floatval_time();
/* Loop over all alarms, searching for expired ones. Since they are ordered
by execution time, as soon as we find one that is not expired we can
exit the loop. For each alarm that is expired, add the associated
Sub/Task to the queue. */
while (alarm_count) {
PMC * const alarm = VTABLE_shift_pmc(interp, sched->alarms);
const FLOATVAL alarm_time = VTABLE_get_number(interp, alarm);
if (alarm_time < now_time) {
Parrot_Alarm_attributes * const data = PARROT_ALARM(alarm);
Parrot_cx_schedule_immediate(interp, data->alarm_task);
}
else {
Parrot_alarm_set(alarm_time);
VTABLE_unshift_pmc(interp, sched->alarms, alarm);
break;
}
alarm_count--;
}
}
/*
=back
=head2 Opcode Functions
Functions that are called from within opcodes, that take and return an
opcode_t* to allow for changing the code flow.
=over 4
=item C<opcode_t * Parrot_cx_schedule_sleep(PARROT_INTERP, FLOATVAL time,
opcode_t *next)>
Add a sleep timer to the scheduler. This function is called by the C<sleep>
opcode.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
opcode_t *
Parrot_cx_schedule_sleep(PARROT_INTERP, FLOATVAL time, ARGIN_NULLOK(opcode_t *next))
{
ASSERT_ARGS(Parrot_cx_schedule_sleep)
const FLOATVAL now_time = Parrot_floatval_time();
const FLOATVAL done_time = now_time + time;
PMC * const alarm = Parrot_pmc_new(interp, enum_class_Alarm);
Parrot_Alarm_attributes * const adata = PARROT_ALARM(alarm);
PMC * const task = Parrot_cx_stop_task(interp, next);
adata->alarm_time = done_time;
PARROT_ASSERT_INTERP(task, interp);
adata->alarm_task = task;
PARROT_GC_WRITE_BARRIER(interp, alarm);
(void) VTABLE_invoke(interp, alarm, NULL);
return (opcode_t*) NULL;
}
/*
=back
=head2 Internal functions
Functions that are used by the scheduler itself.
=over 4
=item C<void Parrot_cx_enable_preemption(PARROT_INTERP)>
Enable preemption. Used when more than one task is runnable.
=cut
*/
void
Parrot_cx_enable_preemption(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_enable_preemption)
PMC * const scheduler = interp->scheduler;
SCHEDULER_enable_preemption_SET(scheduler);
Parrot_cx_set_scheduler_alarm(interp);
}
/*
=item C<void Parrot_cx_disable_preemption(PARROT_INTERP)>
Disable preemption. Used when only one task is runnable.
=cut
*/
void
Parrot_cx_disable_preemption(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_disable_preemption)
PMC * const scheduler = interp->scheduler;
SCHEDULER_enable_preemption_CLEAR(scheduler);
}
/*
=item C<static int Parrot_cx_preemption_enabled(PARROT_INTERP)>
Checks wether preemption is enabled or not.
=cut
*/
static int
Parrot_cx_preemption_enabled(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_cx_preemption_enabled)
PMC * const scheduler = interp->scheduler;
return SCHEDULER_enable_preemption_TEST(scheduler);
}
/*
=back
=head1 SEE ALSO
F<include/parrot/scheduler.h>
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/