- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.php
More file actions
Latest commit
105 lines (90 loc) · 2.14 KB
/
Copy pathfunctions.php
File metadata and controls
105 lines (90 loc) · 2.14 KB
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
functiontask1()
{
for ($i = 1; $i <= 10; $i++) {
echo"This is task 1 iteration $i<br>";
yield;
}
}
functiontask2()
{
for ($i = 1; $i <= 5; $i++) {
echo"This is task 2 iteration $i<br>";
yield;
}
}
functiontask($max)
{
$tid = (yieldgetTaskId()); // do system call
for ($i = 1; $i <= $max; ++$i) {
echo"This is task $tid iteration $i.<br>";
yield;
}
}
functiongetTaskId()
{
returnnewSystemCall(function(Task$task, Scheduler$scheduler) {
$task->setSendValue($task->getTaskId());
$scheduler->schedule($task);
});
}
functionnewTask(Generator$coroutine)
{
returnnewSystemCall(
function(Task$task, Scheduler$scheduler) use ($coroutine) {
$task->setSendValue($scheduler->newTask($coroutine));
$scheduler->schedule($task);
}
);
}
functionkillTask($tid)
{
returnnewSystemCall(
function(Task$task, Scheduler$scheduler) use ($tid) {
$task->setSendValue($scheduler->killTask($tid));
$scheduler->schedule($task);
}
);
}
functionwaitForRead($socket)
{
returnnewSystemCall(
function(Task$task, Scheduler$scheduler) use ($socket) {
$scheduler->waitForRead($socket, $task);
}
);
}
functionwaitForWrite($socket)
{
returnnewSystemCall(
function(Task$task, Scheduler$scheduler) use ($socket) {
$scheduler->waitForWrite($socket, $task);
}
);
}
functionchildTask()
{
$tid = (yieldgetTaskId());
$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;
}
}
}
functionparentTask()
{
$tid = (yieldgetTaskId());
$childTid = (yieldnewTask(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>";
yieldkillTask($childTid);
}
}
}