|
28 | 28 | #include <ifaddrs.h>
|
29 | 29 | #include <sys/ioctl.h>
|
30 | 30 |
|
| 31 | +#include <queue> |
| 32 | + |
31 | 33 | ZEND_DECLARE_MODULE_GLOBALS(swoole)
|
32 | 34 |
|
33 | 35 | extern sapi_module_struct sapi_module;
|
34 | 36 |
|
| 37 | +struct rshutdown_func |
| 38 | +{ |
| 39 | + swCallback callback; |
| 40 | + void *private_data; |
| 41 | +}; |
| 42 | + |
| 43 | +std::queue<rshutdown_func *> rshutdown_functions; |
| 44 | + |
35 | 45 | ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
|
36 | 46 | ZEND_END_ARG_INFO()
|
37 | 47 |
|
@@ -275,26 +285,6 @@ void swoole_set_property_by_handle(uint32_t handle, int property_id, void *ptr)
|
275 | 285 | swoole_objects.property[property_id][handle] = ptr;
|
276 | 286 | }
|
277 | 287 |
|
278 |
| -int swoole_register_rshutdown_function(swCallback func, int push_back) |
279 |
| -{ |
280 |
| - if (SWOOLE_G(rshutdown_functions) == NULL) |
281 |
| - { |
282 |
| - SWOOLE_G(rshutdown_functions) = swLinkedList_new(0, NULL); |
283 |
| - if (SWOOLE_G(rshutdown_functions) == NULL) |
284 |
| - { |
285 |
| - return SW_ERR; |
286 |
| - } |
287 |
| - } |
288 |
| - if (push_back) |
289 |
| - { |
290 |
| - return swLinkedList_append(SWOOLE_G(rshutdown_functions), (void*) func); |
291 |
| - } |
292 |
| - else |
293 |
| - { |
294 |
| - return swLinkedList_prepend(SWOOLE_G(rshutdown_functions), (void*) func); |
295 |
| - } |
296 |
| -} |
297 |
| - |
298 | 288 | void php_swoole_register_shutdown_function(const char *function)
|
299 | 289 | {
|
300 | 290 | php_shutdown_function_entry shutdown_function_entry;
|
@@ -327,6 +317,23 @@ void php_swoole_register_shutdown_function_prepend(const char *function)
|
327 | 317 | FREE_HASHTABLE(old_user_shutdown_function_names);
|
328 | 318 | }
|
329 | 319 |
|
| 320 | +void php_swoole_register_rshutdown_callback(swCallback cb, void *private_data) |
| 321 | +{ |
| 322 | + rshutdown_func *rf = (rshutdown_func*) emalloc(sizeof(rshutdown_func)); |
| 323 | + rshutdown_functions.push(rf); |
| 324 | +} |
| 325 | + |
| 326 | +static void execute_rshutdown_callback() |
| 327 | +{ |
| 328 | + while(!rshutdown_functions.empty()) |
| 329 | + { |
| 330 | + rshutdown_func *rf = rshutdown_functions.front(); |
| 331 | + rshutdown_functions.pop(); |
| 332 | + rf->callback(rf->private_data); |
| 333 | + efree(rf); |
| 334 | + } |
| 335 | +} |
| 336 | + |
330 | 337 | static sw_inline zend_string* get_debug_print_backtrace(zend_long options, zend_long limit)
|
331 | 338 | {
|
332 | 339 | SW_PHP_OB_START(zoutput)
|
@@ -370,23 +377,6 @@ static void fatal_error(int code, const char *format, ...)
|
370 | 377 | exit(255);
|
371 | 378 | }
|
372 | 379 |
|
373 |
| -void swoole_call_rshutdown_function(void *arg) |
374 |
| -{ |
375 |
| - if (SWOOLE_G(rshutdown_functions)) |
376 |
| - { |
377 |
| - swLinkedList *rshutdown_functions = SWOOLE_G(rshutdown_functions); |
378 |
| - swLinkedList_node *node = rshutdown_functions->head; |
379 |
| - swCallback func = NULL; |
380 |
| - |
381 |
| - while (node) |
382 |
| - { |
383 |
| - func = (swCallback) node->data; |
384 |
| - func(arg); |
385 |
| - node = node->next; |
386 |
| - } |
387 |
| - } |
388 |
| -} |
389 |
| - |
390 | 380 | swoole_object_array swoole_objects;
|
391 | 381 |
|
392 | 382 | /* {{{ PHP_MINIT_FUNCTION
|
@@ -823,46 +813,15 @@ PHP_RINIT_FUNCTION(swoole)
|
823 | 813 | PHP_RSHUTDOWN_FUNCTION(swoole)
|
824 | 814 | {
|
825 | 815 | SWOOLE_G(req_status) = PHP_SWOOLE_RSHUTDOWN_BEGIN;
|
826 |
| - swoole_call_rshutdown_function(NULL); |
| 816 | + execute_rshutdown_callback(); |
827 | 817 |
|
828 |
| - //clear pipe buffer |
829 |
| - if (SwooleG.serv && swIsWorker()) |
830 |
| - { |
831 |
| - swWorker_clean(); |
832 |
| - } |
833 |
| - |
834 |
| - if (SwooleG.serv && SwooleG.serv->gs->start > 0 && !SwooleG.serv->gs->shutdown) |
835 |
| - { |
836 |
| - if (PG(last_error_message)) |
837 |
| - { |
838 |
| - switch(PG(last_error_type)) |
839 |
| - { |
840 |
| - case E_ERROR: |
841 |
| - case E_CORE_ERROR: |
842 |
| - case E_USER_ERROR: |
843 |
| - case E_COMPILE_ERROR: |
844 |
| - swoole_error_log( |
845 |
| - SW_LOG_ERROR, SW_ERROR_PHP_FATAL_ERROR, "Fatal error: %s in %s on line %d", |
846 |
| - PG(last_error_message), PG(last_error_file)?PG(last_error_file):"-", PG(last_error_lineno) |
847 |
| - ); |
848 |
| - break; |
849 |
| - default: |
850 |
| - break; |
851 |
| - } |
852 |
| - } |
853 |
| - else |
854 |
| - { |
855 |
| - swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SERVER_WORKER_TERMINATED, "worker process is terminated by exit()/die()"); |
856 |
| - } |
857 |
| - } |
858 |
| - |
859 |
| - swoole_async_coro_shutdown(); |
860 |
| - swoole_redis_server_shutdown(); |
861 |
| - swoole_coroutine_shutdown(); |
862 |
| - swoole_runtime_shutdown(); |
| 818 | + swoole_server_rshutdown(); |
| 819 | + swoole_async_coro_rshutdown(); |
| 820 | + swoole_redis_server_rshutdown(); |
| 821 | + swoole_coroutine_rshutdown(); |
| 822 | + swoole_runtime_rshutdown(); |
863 | 823 |
|
864 | 824 | SwooleG.running = 0;
|
865 |
| - |
866 | 825 | SWOOLE_G(req_status) = PHP_SWOOLE_RSHUTDOWN_END;
|
867 | 826 |
|
868 | 827 | return SUCCESS;
|
|
0 commit comments