1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\PHPThread; |
4
|
|
|
|
5
|
|
|
use ByJG\PHPThread\Handler\ForkHandler; |
6
|
|
|
use ByJG\PHPThread\Handler\ParallelHandler; |
7
|
|
|
use ByJG\PHPThread\Handler\ThreadInterface; |
8
|
|
|
use Closure; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Native Implementation of Threads in PHP. |
14
|
|
|
* |
15
|
|
|
* A class to spawn a thread. Only works in *nix environments, |
16
|
|
|
* as Windows platform is missing libpcntl. |
17
|
|
|
* |
18
|
|
|
* Forks the process. |
19
|
|
|
*/ |
20
|
|
|
class Thread implements ThreadInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ThreadInterface |
24
|
|
|
*/ |
25
|
|
|
private $threadInstance = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* constructor method |
29
|
|
|
* |
30
|
|
|
* @param Closure $closure |
31
|
|
|
*/ |
32
|
3 |
|
public function __construct(Closure $closure) |
33
|
|
|
{ |
34
|
3 |
|
$this->setClosure($closure); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private $threadHandlerArguments = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $arguments |
41
|
|
|
*/ |
42
|
|
|
public function setThreadHandlerArguments($arguments) |
43
|
|
|
{ |
44
|
|
|
$this->threadHandlerArguments = $arguments; |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
private function getThreadHandlerArguments($property) |
48
|
|
|
{ |
49
|
3 |
|
return isset($this->threadHandlerArguments[$property]) ? $this->threadHandlerArguments[$property] : null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ThreadInterface |
54
|
|
|
*/ |
55
|
3 |
|
public function getThreadInstance() |
56
|
|
|
{ |
57
|
3 |
|
if (!is_null($this->threadInstance)) { |
58
|
3 |
|
return $this->threadInstance; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
if (class_exists('\parallel\Runtime', true)) { |
62
|
|
|
$this->threadInstance = new ParallelHandler(); |
63
|
3 |
|
} elseif (function_exists('pcntl_fork')) { |
64
|
3 |
|
$this->threadInstance = new ForkHandler( |
65
|
3 |
|
$this->getThreadHandlerArguments('max-size'), |
66
|
3 |
|
$this->getThreadHandlerArguments('default-permission') |
67
|
|
|
); |
68
|
|
|
} else { |
69
|
|
|
throw new RuntimeException( |
70
|
|
|
'PHP need to be compiled with ZTS extension or compiled with the --enable-pcntl. ' . |
71
|
|
|
'Windows is not supported.' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $this->threadInstance; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Start the thread |
81
|
|
|
* |
82
|
|
|
* @throws RuntimeException |
83
|
|
|
*/ |
84
|
3 |
|
public function execute() |
85
|
|
|
{ |
86
|
3 |
|
$args = func_get_args(); |
87
|
3 |
|
call_user_func_array([$this->getThreadInstance(), 'execute'], $args); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the thread result from the shared memory block and erase it |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
* @throws \Throwable |
95
|
|
|
*/ |
96
|
2 |
|
public function getResult() |
97
|
|
|
{ |
98
|
2 |
|
return $this->getThreadInstance()->getResult(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Kill a thread |
103
|
|
|
* |
104
|
|
|
* @param int $signal |
105
|
|
|
* @param bool $wait |
106
|
|
|
*/ |
107
|
1 |
|
public function stop($signal = SIGKILL, $wait = false) |
108
|
|
|
{ |
109
|
1 |
|
return $this->getThreadInstance()->stop($signal, $wait); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check if the forked process is alive |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
3 |
|
public function isAlive() |
117
|
|
|
{ |
118
|
3 |
|
return $this->getThreadInstance()->isAlive(); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
public function waitFinish() |
122
|
|
|
{ |
123
|
2 |
|
$this->getThreadInstance()->waitFinish(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Set the thread callable method |
128
|
|
|
* @param Closure $closure |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
3 |
|
public function setClosure(Closure $closure) |
132
|
|
|
{ |
133
|
3 |
|
$this->getThreadInstance()->setClosure($closure); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getClassName() |
137
|
|
|
{ |
138
|
|
|
return $this->getThreadInstance()->getClassName(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|