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