-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathwait.cc
229 lines (198 loc) Β· 6.54 KB
/
wait.cc
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
218
219
220
221
222
223
224
225
226
227
228
229
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "swoole_api.h"
#include "swoole_process_pool.h"
#include "swoole_coroutine.h"
#include "swoole_coroutine_system.h"
#include "swoole_signal.h"
#include <list>
#include <unordered_map>
using namespace swoole;
using swoole::coroutine::System;
struct WaitTask {
Coroutine *co;
pid_t pid;
int status;
};
/**
* Wait, waitpid, and signal cannot be used in a multi-threaded environment;
* they are only applicable to the main thread. There is no need to treat them as thread-local variables.
*/
static std::list<WaitTask *> wait_list;
static std::unordered_map<pid_t, WaitTask *> waitpid_map;
static std::unordered_map<pid_t, int> child_processes;
bool signal_ready = false;
static void signal_handler(int signo) {
if (signo != SIGCHLD) {
return;
}
while (true) {
auto exit_status = swoole::wait_process(-1, WNOHANG);
if (exit_status.get_pid() <= 0) {
break;
}
WaitTask *task = nullptr;
if (waitpid_map.find(exit_status.get_pid()) != waitpid_map.end()) {
task = waitpid_map[exit_status.get_pid()];
} else if (!wait_list.empty()) {
task = wait_list.front();
} else {
child_processes[exit_status.get_pid()] = exit_status.get_status();
}
if (task) {
task->status = exit_status.get_status();
task->pid = exit_status.get_pid();
task->co->resume();
}
}
}
static void signal_init() {
if (!signal_ready) {
Reactor *reactor = SwooleTG.reactor;
swoole_signal_set(SIGCHLD, signal_handler);
reactor->set_exit_condition(Reactor::EXIT_CONDITION_WAIT_PID, [](Reactor *reactor, size_t &event_num) -> bool {
return swoole_coroutine_wait_count() == 0;
});
reactor->add_destroy_callback([](void *) {
signal_ready = false;
swoole_signal_clear();
});
signal_ready = true;
}
}
pid_t System::wait(int *__stat_loc, double timeout) {
return System::waitpid(-1, __stat_loc, 0, timeout);
}
pid_t System::waitpid_safe(pid_t __pid, int *__stat_loc, int __options) {
if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current() || (__options & WNOHANG))) {
return ::waitpid(__pid, __stat_loc, __options);
}
pid_t retval;
auto success = wait_for([__pid, &retval, __stat_loc]() -> bool {
retval = ::waitpid(__pid, __stat_loc, WNOHANG);
return retval != 0;
});
return success ? retval : -1;
}
/**
* @error: errno & swoole_get_last_error()
*/
pid_t System::waitpid(pid_t __pid, int *__stat_loc, int __options, double timeout) {
if (__pid < 0) {
if (!child_processes.empty()) {
auto i = child_processes.begin();
pid_t __pid = i->first;
*__stat_loc = i->second;
child_processes.erase(i);
return __pid;
}
} else {
auto i = child_processes.find(__pid);
if (i != child_processes.end()) {
*__stat_loc = i->second;
child_processes.erase(i);
return __pid;
}
}
if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current() || (__options & WNOHANG))) {
return ::waitpid(__pid, __stat_loc, __options);
}
/* try once if failed we init the task, and we must register SIGCHLD before try waitpid, or we may lose the SIGCHLD
*/
WaitTask task;
signal_init();
task.pid = ::waitpid(__pid, __stat_loc, __options | WNOHANG);
if (task.pid != 0) {
return task.pid;
}
task.pid = -1;
task.status = 0;
task.co = Coroutine::get_current();
/* enqueue */
if (__pid < 0) {
wait_list.push_back(&task);
} else {
waitpid_map[__pid] = &task;
}
/* timeout controller */
TimerNode *timer = nullptr;
if (timeout > 0) {
timer = swoole_timer_add(
timeout,
false,
[](Timer *timer, TimerNode *tnode) {
Coroutine *co = (Coroutine *) tnode->data;
co->resume();
},
task.co);
}
Coroutine::CancelFunc cancel_fn = [timer](Coroutine *co) {
if (timer) {
swoole_timer_del(timer);
}
co->resume();
return true;
};
task.co->yield(&cancel_fn);
/* dequeue */
if (__pid < 0) {
if (task.pid > 0) {
wait_list.pop_front();
} else {
/* timeout so we should remove it from the list */
wait_list.remove(&task);
}
} else {
waitpid_map.erase(__pid);
}
/* clear and assign result */
if (task.pid > 0) {
if (timer) {
swoole_timer_del(timer);
}
*__stat_loc = task.status;
} else {
swoole_set_last_error(task.co->is_canceled() ? SW_ERROR_CO_CANCELED : ETIMEDOUT);
errno = swoole_get_last_error();
}
return task.pid;
}
extern "C" {
size_t swoole_coroutine_wait_count() {
return wait_list.size() + waitpid_map.size();
}
pid_t swoole_coroutine_wait(int *__stat_loc) {
return System::wait(__stat_loc);
}
pid_t swoole_coroutine_waitpid(pid_t __pid, int *__stat_loc, int __options) {
return System::waitpid(__pid, __stat_loc, __options);
}
}
pid_t swoole_waitpid(pid_t __pid, int *__stat_loc, int __options) {
pid_t retval;
SW_LOOP {
retval = waitpid(__pid, __stat_loc, __options);
if (!(retval < 0 && errno == EINTR)) {
break;
}
swoole_signal_dispatch();
if (sw_timer()) {
sw_timer()->select();
}
}
return retval;
}