-
Notifications
You must be signed in to change notification settings - Fork 1
/
Future.php
220 lines (184 loc) · 5.07 KB
/
Future.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
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
<?php
/**
* Future
*
* PHP Version 5.3
*
* @author Sean Crystal <[email protected]>
* @copyright 2011 Sean Crystal
* @license http://www.opensource.org/licenses/BSD-3-Clause BSD 3-Clause
* @link https://github.com/spiralout/Futures
*/
class Future
{
/** max value size */
const SIZE = 100000;
/** unique key for the Futures message queue */
const MSG_QUEUE_KEY = 0xFAC3;
/**
* Constructor
*
* @var Closure $func
* @var bool $autoStart
* @var resource $collectionId Resource Identifier for collection queue
*/
function __construct(Closure $func, $autoStart = true, $collectionKey = NULL)
{
self::$futureCount++;
$this->func = $func;
$this->autoStart = $autoStart;
$this->collectionKey = $collectionKey;
$this->initMessageQueue();
$autoStart and $this->doFork();
}
/**
* Retrieve the value of this Future. Blocks until value is available
*
* @return mixed
*/
function __invoke()
{
return $this->get();
}
/**
* Get the value of this Future. Blocks until the value is available
*
* @return mixed
*/
function get()
{
if (!$this->completed) {
if (!(msg_receive($this->mqid, $this->messageType, $msgType, self::SIZE, $this->value, true, 0, $error))) {
return false;
}
$this->cleanUp();
$this->completed = true;
}
return $this->value;
}
/**
* Get the value of this Future if available, but do not block. Returns false if the
* value is not yet available
*
* @return mixed|false
*/
function getNoWait()
{
if (!$this->completed) {
if (!msg_receive($this->mqid, $this->messageType, $msgType, self::SIZE, $this->value, true, MSG_IPC_NOWAIT, $error)) {
return false;
}
$this->cleanUp();
$this->completed = true;
}
return $this->value;
}
/**
* Start the Future computation if it was not autostarted
*/
function start()
{
$this->autoStart or $this->doFork();
}
/**
* Check if this Future is finished computing its value
*
* @return bool
*/
function isCompleted()
{
return $this->completed;
}
/**
* Return this futures queue id
*
* @return int
*/
function getFutureId()
{
return $this->messageType;
}
/**
* Fork a child process to compute the value of this Future
*/
private function doFork()
{
if ($this->pid = pcntl_fork()) { // parent
/* nop */
} else { //child
$func = $this->func;
try {
$value = $func();
} catch (Exception $e) {
$value = $e;
}
// Put the value on the message queue and send a Value Ready message to the collection queue
if (!msg_send($this->mqid, $this->messageType, $value, true, true, $error)) {
$this->cleanUp();
}
// Set VALUE_READY on the collection Queue;
if(isset($this->collectionKey))
{
msg_send($this->collectionQueueId, FutureCollection::VALUE_READY, $this->messageType, true, true, $error);
}
exit(0);
}
}
/**
* Cleanup any resources acquired
*/
private function cleanUp()
{
self::$futureCount--;
if (self::$futureCount == 0) {
msg_remove_queue($this->mqid);
msg_remove_queue($this->collectionQueueId);
}
}
/**
* Generate a unique message type key
*
* @return int
*/
private function getMessageType()
{
return floor(microtime(true) * 1000000 + rand(2, 1000));
}
/**
* Create a message queue to return the result of this Future to the creator
*/
private function initMessageQueue()
{
$this->messageType = $this->getMessageType();
if (!($this->mqid = msg_get_queue(self::MSG_QUEUE_KEY))) {
throw new Exception('Could not create message queue with key '. self::MSG_QUEUE_KEY);
}
if(isset($this->collectionKey))
{
if (!($this->collectionQueueId = msg_get_queue($this->collectionKey)))
{
throw new Exception("Could not create collection message queue with key ". $this->collectionKey);
}
}
}
/** @var bool */
private $completed = false;
/** @var bool */
private $autoStart = true;
/** @var mixed */
private $value;
/** @var resource */
private $mqid;
/** @var resource Collection Queue ID */
private $collectionQueueId;
/** @var int Collection Queue Key */
private $collectionKey;
/** @var int */
private $pid;
/** @var Closure */
private $func;
/** @var int */
private $messageType;
/** @staticvar $int */
static $futureCount = 0;
}