-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.php
105 lines (90 loc) · 2.14 KB
/
functions.php
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
<?php
function task1()
{
for ($i = 1; $i <= 10; $i++) {
echo "This is task 1 iteration $i<br>";
yield;
}
}
function task2()
{
for ($i = 1; $i <= 5; $i++) {
echo "This is task 2 iteration $i<br>";
yield;
}
}
function task($max)
{
$tid = (yield getTaskId()); // do system call
for ($i = 1; $i <= $max; ++$i) {
echo "This is task $tid iteration $i.<br>";
yield;
}
}
function getTaskId()
{
return new SystemCall(function(Task $task, Scheduler $scheduler) {
$task->setSendValue($task->getTaskId());
$scheduler->schedule($task);
});
}
function newTask(Generator $coroutine)
{
return new SystemCall(
function(Task $task, Scheduler $scheduler) use ($coroutine) {
$task->setSendValue($scheduler->newTask($coroutine));
$scheduler->schedule($task);
}
);
}
function killTask($tid)
{
return new SystemCall(
function(Task $task, Scheduler $scheduler) use ($tid) {
$task->setSendValue($scheduler->killTask($tid));
$scheduler->schedule($task);
}
);
}
function waitForRead($socket)
{
return new SystemCall(
function(Task $task, Scheduler $scheduler) use ($socket) {
$scheduler->waitForRead($socket, $task);
}
);
}
function waitForWrite($socket)
{
return new SystemCall(
function(Task $task, Scheduler $scheduler) use ($socket) {
$scheduler->waitForWrite($socket, $task);
}
);
}
function childTask()
{
$tid = (yield getTaskId());
$iterations = 0;
while (true) {
echo "Child task $tid still alive.<br>";
yield;
if ($iterations++ > 10) {
echo "Ending child task because it never got killed.<br>";
break;
}
}
}
function parentTask()
{
$tid = (yield getTaskId());
$childTid = (yield newTask(childTask()));
for ($i = 1; $i <= 6; $i++) {
echo "Parent task $tid iteration $i.<br>";
yield;
if ($i == 3) {
echo "Parent sending message to kill child task.<br>";
yield killTask($childTid);
}
}
}