author | Dan |
Tue, 23 Sep 2008 23:24:13 -0400 | |
changeset 48 | d643bfb862d8 |
child 53 | a6b339665650 |
permissions | -rw-r--r-- |
48
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
1 |
<?php |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
2 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
3 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
4 |
* Multi-threading (well sort of) tools |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
5 |
* |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
6 |
* Greyhound - real web management for Amarok |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
7 |
* Copyright (C) 2008 Dan Fuhry |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
8 |
* |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
9 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
10 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
11 |
* |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
13 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
14 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
15 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
16 |
require_once('json.php'); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
17 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
18 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
19 |
* Global signal handler for SIGCHLD. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
20 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
21 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
22 |
function Threader_SigChld() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
23 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
24 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
25 |
foreach ( $threader_instances as &$mt ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
26 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
27 |
if ( is_object($mt) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
28 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
29 |
$mt->event_sigchld(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
30 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
31 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
32 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
33 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
34 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
35 |
* Global signal handler for SIGUSR2. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
36 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
37 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
38 |
function Threader_SigUsr2() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
39 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
40 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
41 |
foreach ( $threader_instances as &$mt ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
42 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
43 |
if ( is_object($mt) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
44 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
45 |
$parchild = $mt->is_child() ? 'child' : 'parent'; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
46 |
$mt->event_sigusr2(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
47 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
48 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
49 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
50 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
51 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
52 |
* List of Threader instances. Needed for global handling of signals. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
53 |
* @var array |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
54 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
55 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
56 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
57 |
$threader_instances = array(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
58 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
59 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
60 |
* Tools for emulating multi-threaded operation in PHP scripts. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
61 |
* @package Amarok |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
62 |
* @subpackage WebControl |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
63 |
* @author Dan Fuhry |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
64 |
* @license GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html> |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
65 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
66 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
67 |
class Threader |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
68 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
69 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
70 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
71 |
* Return value of fork() if the process is a child. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
72 |
* @const int |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
73 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
74 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
75 |
const FORK_CHILD = -1; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
76 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
77 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
78 |
* Set to true if this is a child process. No exceptions. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
79 |
* @var bool |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
80 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
81 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
82 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
83 |
private $is_child = false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
84 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
85 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
86 |
* Sockets for inter-process communication. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
87 |
* @var array |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
88 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
89 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
90 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
91 |
protected $ipc_sockets = array(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
92 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
93 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
94 |
* Socket for communication with the parent. Obviously only used after calling fork(). |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
95 |
* @var resource |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
96 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
97 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
98 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
99 |
protected $parent_sock = false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
100 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
101 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
102 |
* Services_JSON instance. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
103 |
* @var object |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
104 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
105 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
106 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
107 |
protected $json = false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
108 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
109 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
110 |
* PID of the parent process. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
111 |
* @var int |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
112 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
113 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
114 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
115 |
protected $parent_pid = 1; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
116 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
117 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
118 |
* List of actions for IPC events. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
119 |
* @var array |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
120 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
121 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
122 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
123 |
protected $ipc_actions = array(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
124 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
125 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
126 |
* Constructor. Sets up signal handlers. Nothing to see here, move along. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
127 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
128 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
129 |
public function __construct() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
130 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
131 |
global $threader_instances; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
132 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
133 |
declare(ticks=1); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
134 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
135 |
$threader_instances[] =& $this; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
136 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
137 |
pcntl_signal(SIGUSR2, 'Threader_SigUsr2'); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
138 |
pcntl_signal(SIGCHLD, 'Threader_SigChld'); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
139 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
140 |
$this->json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
141 |
$this->parent_pid = getmypid(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
142 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
143 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
144 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
145 |
* Forks the current process. See your system's fork(2) man page for details. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
146 |
* @return int FORK_CHILD if child process, PID of child if parent process. Returns false on failure. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
147 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
148 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
149 |
public function fork() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
150 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
151 |
// create our new sockets for IPC |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
152 |
$socket_pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
153 |
// fork (emoticon of the day: --<E) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
154 |
$fork_result = pcntl_fork(); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
155 |
if ( $fork_result == -1 ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
156 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
157 |
// fork failed. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
158 |
return false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
159 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
160 |
else if ( $fork_result ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
161 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
162 |
// we are the parent - register the child |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
163 |
fclose($socket_pair[0]); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
164 |
$this->ipc_sockets[$fork_result] = $socket_pair[1]; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
165 |
return $fork_result; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
166 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
167 |
else |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
168 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
169 |
// we are the child. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
170 |
fclose($socket_pair[1]); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
171 |
$this->parent_sock = $socket_pair[0]; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
172 |
$this->is_child = true; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
173 |
return FORK_CHILD; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
174 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
175 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
176 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
177 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
178 |
* Are we a child? |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
179 |
* @return bool |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
180 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
181 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
182 |
public function is_child() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
183 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
184 |
return $this->is_child; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
185 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
186 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
187 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
188 |
* Register an action so that when it is fired over IPC, a custom function can be called. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
189 |
* @param string Action |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
190 |
* @param callback Function to call |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
191 |
* @return true on success, false on failure |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
192 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
193 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
194 |
function ipc_register($action, $callback) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
195 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
196 |
if ( !is_string($action) || empty($action) || !is_callable($callback) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
197 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
198 |
return false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
199 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
200 |
$this->ipc_actions[$action] = $callback; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
201 |
return true; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
202 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
203 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
204 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
205 |
* Send through an IPC event. If this is a child, it only notifies the parent; if we're the parent, all children are notified. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
206 |
* @param array Data to be sent through. This must be an associative array containing an "action" key at minimum. If this a key "propagate" set to true, a parent that receives this will propagate the message to all children. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
207 |
* @return null |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
208 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
209 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
210 |
function ipc_send($data) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
211 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
212 |
if ( !isset($data['action']) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
213 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
214 |
return false; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
215 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
216 |
$data = $this->json->encode($data); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
217 |
if ( $this->is_child() ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
218 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
219 |
fwrite($this->parent_sock, "$data\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
220 |
// signal the parent that we've got an event |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
221 |
posix_kill($this->parent_pid, SIGUSR2); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
222 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
223 |
else |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
224 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
225 |
// signal each child |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
226 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
227 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
228 |
fwrite($socket, "$data\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
229 |
posix_kill($pid, SIGUSR2); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
230 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
231 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
232 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
233 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
234 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
235 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
236 |
* Handler for SIGCHLD events. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
237 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
238 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
239 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
240 |
function event_sigchld() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
241 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
242 |
// this should never happen to children. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
243 |
if ( $this->is_child() ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
244 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
245 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
246 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
247 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
248 |
// wait for child to exit. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
249 |
pcntl_wait($status); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
250 |
// for each child PID, kill with signal 0 (effectively, test if process is alive) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
251 |
// if posix_kill fails, it's dead so remove it from the list. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
252 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
253 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
254 |
if ( !@posix_kill($pid, 0) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
255 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
256 |
// signal failed. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
257 |
fclose($socket); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
258 |
unset($this->ipc_sockets[$pid]); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
259 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
260 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
261 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
262 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
263 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
264 |
* Handler for IPC events. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
265 |
* @access private |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
266 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
267 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
268 |
function event_sigusr2() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
269 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
270 |
if ( $this->is_child() ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
271 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
272 |
// this is easy - the parent sent the signal. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
273 |
$command = rtrim(fgets($this->parent_sock, 102400), "\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
274 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
275 |
else |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
276 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
277 |
// since we can't find which PID sent the signal, set the timeout to a very small amount |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
278 |
// of time and try to read; if we get something, awesome. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
279 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
280 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
281 |
// 1000 microseconds = 1/80th of the time it takes you to blink. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
282 |
@stream_set_timeout($socket, 0, 1000); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
283 |
$command = rtrim(@fgets($socket, 102400), "\n"); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
284 |
if ( !empty($command) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
285 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
286 |
break; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
287 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
288 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
289 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
290 |
if ( empty($command) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
291 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
292 |
// hmm, got a sigusr2 without an incoming command. oh well, ignore. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
293 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
294 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
295 |
$command = $this->json->decode($command); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
296 |
if ( !isset($command['action']) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
297 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
298 |
// no action = no way to figure out how to handle this. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
299 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
300 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
301 |
if ( !isset($this->ipc_actions[$command['action']]) ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
302 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
303 |
// action not registered |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
304 |
return null; |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
305 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
306 |
// should we propagate this event? |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
307 |
if ( !$this->is_child() && isset($command['propagate']) && $command['propagate'] === true ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
308 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
309 |
$this->ipc_send($command); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
310 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
311 |
// we're good |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
312 |
@call_user_func($this->ipc_actions[$command['action']], $command, $this); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
313 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
314 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
315 |
/** |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
316 |
* Kills all child processes. |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
317 |
* @access public |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
318 |
*/ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
319 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
320 |
public function kill_all_children() |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
321 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
322 |
foreach ( $this->ipc_sockets as $pid => $socket ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
323 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
324 |
$socklen = count($this->ipc_sockets); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
325 |
posix_kill($pid, SIGTERM); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
326 |
// wait until we are conscious of this child's death |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
327 |
while ( count($this->ipc_sockets) >= $socklen ) |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
328 |
{ |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
329 |
usleep(20000); |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
330 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
331 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
332 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
333 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
334 |
} |
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
335 |
|
d643bfb862d8
Replaced multithreading in WebServer with a full multithreading library that properly handles IPC and child management
Dan
parents:
diff
changeset
|
336 |
?> |