Skip to content

Commit a1b7348

Browse files
committed
Optimization func name
1 parent cf95fb9 commit a1b7348

9 files changed

+90
-90
lines changed

include/server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -985,11 +985,11 @@ void swWorker_free(swWorker *worker);
985985
void swWorker_onStart(swServer *serv);
986986
void swWorker_onStop(swServer *serv);
987987
int swWorker_loop(swServer *serv, int worker_pti);
988+
void swWorker_clean_pipe_buffer(swServer *serv);
988989
int swWorker_send2reactor(swServer *serv, swEventData *ev_data, size_t sendn, int fd);
989990
int swWorker_send2worker(swWorker *dst_worker, void *buf, int n, int flag);
990991
void swWorker_signal_handler(int signo);
991992
void swWorker_signal_init(void);
992-
void swWorker_clean(void);
993993

994994
/**
995995
* reactor_id: The fd in which the reactor.

php_swoole.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ PHP_FUNCTION(swoole_strerror);
296296
PHP_FUNCTION(swoole_errno);
297297
PHP_FUNCTION(swoole_last_error);
298298

299-
/** <Sort by dependency> **/
299+
300+
/**
301+
* MINIT <Sort by dependency>
302+
* ==============================================================
303+
*/
300304
void swoole_event_init(int module_number);
301305
// base
302306
void swoole_atomic_init(int module_number);
@@ -335,10 +339,11 @@ void swoole_redis_server_init(int module_number);
335339
* RSHUTDOWN
336340
* ==============================================================
337341
*/
338-
void swoole_async_coro_shutdown();
339-
void swoole_redis_server_shutdown();
340-
void swoole_coroutine_shutdown();
341-
void swoole_runtime_shutdown();
342+
void swoole_async_coro_rshutdown();
343+
void swoole_redis_server_rshutdown();
344+
void swoole_coroutine_rshutdown();
345+
void swoole_runtime_rshutdown();
346+
void swoole_server_rshutdown();
342347

343348
void php_swoole_process_clean();
344349
int php_swoole_process_start(swWorker *process, zval *zobject);
@@ -348,6 +353,7 @@ void php_swoole_reactor_init();
348353
// shutdown
349354
void php_swoole_register_shutdown_function(const char *function);
350355
void php_swoole_register_shutdown_function_prepend(const char *function);
356+
void php_swoole_register_rshutdown_callback(swCallback cb, void *private_data);
351357

352358
// event
353359
void php_swoole_event_init();
@@ -416,8 +422,6 @@ static sw_inline void swoole_set_property(zval *zobject, int property_id, void *
416422

417423
int swoole_convert_to_fd(zval *zsocket);
418424
int swoole_convert_to_fd_ex(zval *zsocket, int *async);
419-
int swoole_register_rshutdown_function(swCallback func, int push_back);
420-
void swoole_call_rshutdown_function(void *arg);
421425

422426
#ifdef SWOOLE_SOCKETS_SUPPORT
423427
php_socket *swoole_convert_to_socket(int sock);

src/server/worker.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,9 @@ static int swWorker_reactor_is_empty(swReactor *reactor)
642642
return SW_FALSE;
643643
}
644644

645-
void swWorker_clean(void)
645+
void swWorker_clean_pipe_buffer(swServer *serv)
646646
{
647647
int i;
648-
swServer *serv = (swServer *) SwooleWG.worker->pool->ptr;
649-
650648
for (i = 0; i < serv->worker_num + serv->task_worker_num; i++)
651649
{
652650
swWorker *worker = swServer_get_worker(serv, i);
@@ -734,7 +732,7 @@ int swWorker_loop(swServer *serv, int worker_id)
734732
//main loop
735733
reactor->wait(reactor, NULL);
736734
//clear pipe buffer
737-
swWorker_clean();
735+
swWorker_clean_pipe_buffer(serv);
738736
//worker shutdown
739737
swWorker_onStop(serv);
740738
return SW_OK;

swoole.cc

+33-74
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,20 @@
2828
#include <ifaddrs.h>
2929
#include <sys/ioctl.h>
3030

31+
#include <queue>
32+
3133
ZEND_DECLARE_MODULE_GLOBALS(swoole)
3234

3335
extern sapi_module_struct sapi_module;
3436

37+
struct rshutdown_func
38+
{
39+
swCallback callback;
40+
void *private_data;
41+
};
42+
43+
std::queue<rshutdown_func *> rshutdown_functions;
44+
3545
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
3646
ZEND_END_ARG_INFO()
3747

@@ -275,26 +285,6 @@ void swoole_set_property_by_handle(uint32_t handle, int property_id, void *ptr)
275285
swoole_objects.property[property_id][handle] = ptr;
276286
}
277287

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-
298288
void php_swoole_register_shutdown_function(const char *function)
299289
{
300290
php_shutdown_function_entry shutdown_function_entry;
@@ -327,6 +317,23 @@ void php_swoole_register_shutdown_function_prepend(const char *function)
327317
FREE_HASHTABLE(old_user_shutdown_function_names);
328318
}
329319

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+
330337
static sw_inline zend_string* get_debug_print_backtrace(zend_long options, zend_long limit)
331338
{
332339
SW_PHP_OB_START(zoutput)
@@ -370,23 +377,6 @@ static void fatal_error(int code, const char *format, ...)
370377
exit(255);
371378
}
372379

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-
390380
swoole_object_array swoole_objects;
391381

392382
/* {{{ PHP_MINIT_FUNCTION
@@ -823,46 +813,15 @@ PHP_RINIT_FUNCTION(swoole)
823813
PHP_RSHUTDOWN_FUNCTION(swoole)
824814
{
825815
SWOOLE_G(req_status) = PHP_SWOOLE_RSHUTDOWN_BEGIN;
826-
swoole_call_rshutdown_function(NULL);
816+
execute_rshutdown_callback();
827817

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();
863823

864824
SwooleG.running = 0;
865-
866825
SWOOLE_G(req_status) = PHP_SWOOLE_RSHUTDOWN_END;
867826

868827
return SUCCESS;

swoole_async_coro.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void swoole_async_coro_init(int module_number)
7878
SwooleAIO.max_thread_count = SW_AIO_THREAD_MAX_NUM;
7979
}
8080

81-
void swoole_async_coro_shutdown()
81+
void swoole_async_coro_rshutdown()
8282
{
8383
for(auto i = request_cache_map.begin(); i != request_cache_map.end(); i++)
8484
{

swoole_coroutine_util.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ void swoole_coroutine_util_init(int module_number)
262262
}
263263
}
264264

265-
void swoole_coroutine_shutdown()
265+
void swoole_coroutine_rshutdown()
266266
{
267267
PHPCoroutine::shutdown();
268268
}

swoole_redis_server.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void swoole_redis_server_init(int module_number)
8181
zend_declare_class_constant_long(swoole_redis_server_ce, ZEND_STRL("MAP"), SW_REDIS_REPLY_MAP);
8282
}
8383

84-
void swoole_redis_server_shutdown()
84+
void swoole_redis_server_rshutdown()
8585
{
8686
for (auto i = redis_handlers.begin(); i != redis_handlers.end(); i++)
8787
{

swoole_runtime.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct real_func
173173
zval name;
174174
};
175175

176-
void swoole_runtime_shutdown()
176+
void swoole_runtime_rshutdown()
177177
{
178178
if (!function_table)
179179
{

swoole_server.cc

+39
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,43 @@ static sw_inline zend_bool is_enable_coroutine(swServer *serv)
466466
}
467467
}
468468

469+
void swoole_server_rshutdown()
470+
{
471+
if (!SwooleG.serv)
472+
{
473+
return;
474+
}
475+
476+
swServer *serv = SwooleG.serv;
477+
478+
swWorker_clean_pipe_buffer(serv);
479+
480+
if (serv->gs->start > 0 && !serv->gs->shutdown)
481+
{
482+
if (PG(last_error_message))
483+
{
484+
switch(PG(last_error_type))
485+
{
486+
case E_ERROR:
487+
case E_CORE_ERROR:
488+
case E_USER_ERROR:
489+
case E_COMPILE_ERROR:
490+
swoole_error_log(
491+
SW_LOG_ERROR, SW_ERROR_PHP_FATAL_ERROR, "Fatal error: %s in %s on line %d",
492+
PG(last_error_message), PG(last_error_file)?PG(last_error_file):"-", PG(last_error_lineno)
493+
);
494+
break;
495+
default:
496+
break;
497+
}
498+
}
499+
else
500+
{
501+
swoole_error_log(SW_LOG_NOTICE, SW_ERROR_SERVER_WORKER_TERMINATED, "worker process is terminated by exit()/die()");
502+
}
503+
}
504+
}
505+
469506
void swoole_server_init(int module_number)
470507
{
471508
SW_INIT_CLASS_ENTRY(swoole_server, "Swoole\\Server", "swoole_server", NULL, swoole_server_methods);
@@ -1500,6 +1537,8 @@ static void php_swoole_onWorkerStart(swServer *serv, int worker_id)
15001537
zend_update_property_bool(swoole_server_ce, zserv, ZEND_STRL("taskworker"), worker_id >= serv->worker_num);
15011538
zend_update_property_long(swoole_server_ce, zserv, ZEND_STRL("worker_pid"), getpid());
15021539

1540+
php_swoole_register_rshutdown_callback((swCallback) swWorker_clean_pipe_buffer, serv);
1541+
15031542
if (!is_enable_coroutine(serv))
15041543
{
15051544
SwooleG.enable_coroutine = 0;

0 commit comments

Comments
 (0)