forked from krakjoe/pthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource-keeping.phpt
149 lines (126 loc) · 3.07 KB
/
resource-keeping.phpt
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
--TEST--
Test sane handling of resources from foreign contexts
--DESCRIPTION--
Test that resources from other thread contexts can properly be accessed and are only destroyed by their owner
--FILE--
<?php
class myWorker extends Worker {
private $foreignResource;
public function __construct($useResource) {
$this->foreignResource = $useResource;
var_dump([
'Resource received and stored:',
is_resource($useResource),
is_resource($this->foreignResource),
]);
}
public function run () {
// polute EG(regular_list) with another resource of our own
$localThreadFP = tmpfile();
var_dump(array(
'Foreign resource conversion test:',
// test conversion followed by destruction
// the resource shall be added and removed from
// PTHREADS_ZG(resources) and EG(regular_list) by the following
// statement:
is_resource($this->foreignResource),
// now make the resource permanently available locally to tasks by
// storing it in a variable. It will stay in PTHREADS_ZG(resources)
// and EG(regular_list) until the worker is shutdown):
is_resource($foreignResource = $this->foreignResource),
// test that we still get the correct resource when it's already in
// EG(regular_list)
$foreignResource === $this->foreignResource
));
fclose($localThreadFP);
}
}
class Work extends Threaded {
public function run () {
$foreignResource = $this->worker->foreignResource;
var_dump(array(
'Foreign resource is accessible from task:',
is_resource($foreignResource),
$foreignResource === $this->worker->foreignResource,
'wrote ' . fwrite($foreignResource, '42') . ' bytes',
));
}
}
$fp = tmpfile();
$task = new Work();
$worker = new myWorker($fp);
$worker->start();
$worker->stack($task);
var_dump(array(
'Our worker is shutdown:',
$worker->shutdown(),
));
var_dump(array(
'Our resource is still valid:',
is_resource($fp) && rewind($fp),
fread($fp, 2) === "42",
));
var_dump(array(
'Our resource can be closed:',
fclose($fp),
));
var_dump(array(
'Our resource is closed:',
!is_resource($fp),
));
?>
--EXPECT--
array(3) {
[0]=>
string(29) "Resource received and stored:"
[1]=>
bool(true)
[2]=>
bool(true)
}
array(4) {
[0]=>
string(33) "Foreign resource conversion test:"
[1]=>
bool(true)
[2]=>
bool(true)
[3]=>
bool(true)
}
array(4) {
[0]=>
string(41) "Foreign resource is accessible from task:"
[1]=>
bool(true)
[2]=>
bool(true)
[3]=>
string(13) "wrote 2 bytes"
}
array(2) {
[0]=>
string(23) "Our worker is shutdown:"
[1]=>
bool(true)
}
array(3) {
[0]=>
string(28) "Our resource is still valid:"
[1]=>
bool(true)
[2]=>
bool(true)
}
array(2) {
[0]=>
string(27) "Our resource can be closed:"
[1]=>
bool(true)
}
array(2) {
[0]=>
string(23) "Our resource is closed:"
[1]=>
bool(true)
}