Skip to content

Commit 0c4759d

Browse files
committed
Optimize the thread initialization and cleanup logic code.
1 parent 91380b3 commit 0c4759d

File tree

9 files changed

+47
-41
lines changed

9 files changed

+47
-41
lines changed

core-tests/src/network/stream.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ TEST(stream, send) {
8383
swoole_event_wait();
8484

8585
kill(getpid(), SIGTERM);
86+
87+
swoole_signal_unblock_all();
8688
});
8789

8890
serv.onWorkerStart = [&lock](Server *serv, Worker *worker) { lock.unlock(); };

core-tests/src/os/process_pool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ TEST(process_pool, listen) {
277277
c.close();
278278

279279
kill(getpid(), SIGTERM);
280+
swoole_signal_unblock_all();
280281
});
281282

282283
ASSERT_EQ(pool.wait(), SW_OK);

ext-src/swoole_thread.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ void php_swoole_thread_start(std::shared_ptr<Thread> thread, zend_string *file,
428428
PG(enable_dl) = 0;
429429
#endif
430430

431-
swoole_thread_init();
431+
swoole_thread_init(false);
432432

433433
if (php_request_startup() != SUCCESS) {
434434
EG(exit_status) = 1;
@@ -471,7 +471,7 @@ void php_swoole_thread_start(std::shared_ptr<Thread> thread, zend_string *file,
471471
zend_string_release(file);
472472
thread->exit(EG(exit_status));
473473
ts_free_thread();
474-
swoole_thread_clean();
474+
swoole_thread_clean(false);
475475
thread_num.fetch_sub(1);
476476
}
477477

include/swoole.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,8 @@ void swoole_exit(int __status);
581581
pid_t swoole_fork(int flags);
582582
pid_t swoole_fork_exec(const std::function<void(void)> &child_fn);
583583
pid_t swoole_waitpid(pid_t __pid, int *__stat_loc, int __options);
584-
void swoole_thread_init(void);
585-
void swoole_thread_clean(void);
584+
void swoole_thread_init(bool main_thread);
585+
void swoole_thread_clean(bool main_thread);
586586
void swoole_redirect_stdout(int new_fd);
587587
void swoole_redirect_stdout(const char *file);
588588
int swoole_shell_exec(const char *command, pid_t *pid, bool get_error_stream);
@@ -696,6 +696,7 @@ struct ThreadGlobal {
696696
#endif
697697
String *buffer_stack;
698698
Reactor *reactor;
699+
Logger *logger;
699700
Timer *timer;
700701
TimerScheduler timer_scheduler;
701702
MessageBus *message_bus;

include/swoole_signal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ SW_API swSignalHandler swoole_signal_get_handler(int signo);
5353

5454
SW_API void swoole_signal_clear(void);
5555
SW_API void swoole_signal_block_all(void);
56+
SW_API void swoole_signal_unblock_all(void);
5657
SW_API char *swoole_signal_to_str(int sig);
5758
SW_API void swoole_signal_callback(int signo);
5859
/**

src/core/base.cc

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#endif
5555
#endif
5656

57+
using swoole::Logger;
5758
using swoole::NameResolver;
5859
using swoole::String;
5960
using swoole::coroutine::System;
@@ -101,12 +102,10 @@ thread_local swoole::ThreadGlobal SwooleTG = {};
101102
thread_local char sw_error[SW_ERROR_MSG_SIZE];
102103
std::mutex sw_thread_lock;
103104

104-
static swoole::Logger *g_logger_instance = nullptr;
105-
106105
static void swoole_fatal_error_impl(int code, const char *format, ...);
107106

108107
swoole::Logger *sw_logger() {
109-
return g_logger_instance;
108+
return SwooleTG.logger;
110109
}
111110

112111
void *sw_malloc(size_t size) {
@@ -170,7 +169,7 @@ void swoole_init(void) {
170169

171170
SwooleG.pid = getpid();
172171

173-
g_logger_instance = new swoole::Logger;
172+
swoole_thread_init(true);
174173

175174
#ifdef SW_DEBUG
176175
sw_logger()->set_level(0);
@@ -190,8 +189,6 @@ void swoole_init(void) {
190189
SwooleG.max_sockets = SW_MIN((uint32_t) rlmt.rlim_cur, SW_SESSION_LIST_SIZE);
191190
}
192191

193-
SwooleTG.buffer_stack = new swoole::String(SW_STACK_BUFFER_SIZE);
194-
195192
if (!swoole_set_task_tmpdir(SW_TASK_TMP_DIR)) {
196193
exit(4);
197194
}
@@ -238,23 +235,10 @@ SW_API int swoole_api_version_id(void) {
238235
SW_EXTERN_C_END
239236

240237
void swoole_clean(void) {
241-
if (SwooleTG.timer) {
242-
swoole_timer_free();
243-
}
244-
if (SwooleTG.reactor) {
245-
swoole_event_free();
246-
}
238+
swoole_thread_clean(true);
247239
if (SwooleG.memory_pool != nullptr) {
248240
delete SwooleG.memory_pool;
249241
}
250-
if (g_logger_instance) {
251-
delete g_logger_instance;
252-
g_logger_instance = nullptr;
253-
}
254-
if (SwooleTG.buffer_stack) {
255-
delete SwooleTG.buffer_stack;
256-
SwooleTG.buffer_stack = nullptr;
257-
}
258242
SW_LOOP_N(SW_MAX_HOOK_TYPE) {
259243
if (SwooleG.hooks[i]) {
260244
auto hooks = static_cast<std::list<swoole::Callback> *>(SwooleG.hooks[i]);
@@ -398,14 +382,9 @@ pid_t swoole_fork(int flags) {
398382
swoole_trace_log(SW_TRACE_REACTOR, "reactor has been destroyed");
399383
}
400384
} else {
401-
/**
402-
* close log fd
403-
*/
404385
sw_logger()->close();
405386
}
406-
/**
407-
* reset signal handler
408-
*/
387+
// reset signal handler
409388
swoole_signal_clear();
410389

411390
if (swoole_isset_hook(SW_GLOBAL_HOOK_AFTER_FORK)) {
@@ -416,18 +395,34 @@ pid_t swoole_fork(int flags) {
416395
return pid;
417396
}
418397

419-
void swoole_thread_init(void) {
398+
void swoole_thread_init(bool main_thread) {
420399
if (!SwooleTG.buffer_stack) {
421400
SwooleTG.buffer_stack = new String(SW_STACK_BUFFER_SIZE);
422401
}
423-
swoole_signal_block_all();
402+
if (!SwooleTG.logger) {
403+
SwooleTG.logger = new Logger();
404+
}
405+
if (!main_thread) {
406+
swoole_signal_block_all();
407+
}
424408
}
425409

426-
void swoole_thread_clean(void) {
410+
void swoole_thread_clean(bool main_thread) {
411+
if (SwooleTG.timer) {
412+
swoole_timer_free();
413+
}
414+
if (SwooleTG.reactor) {
415+
swoole_event_free();
416+
}
427417
if (SwooleTG.buffer_stack) {
428418
delete SwooleTG.buffer_stack;
429419
SwooleTG.buffer_stack = nullptr;
430420
}
421+
if (SwooleTG.logger) {
422+
SwooleTG.logger->close();
423+
delete SwooleTG.logger;
424+
SwooleTG.logger = nullptr;
425+
}
431426
}
432427

433428
void swoole_dump_ascii(const char *data, size_t size) {

src/os/async_thread.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class ThreadPool {
228228

229229
void ThreadPool::main_func(bool is_core_worker) {
230230
bool exit_flag = false;
231-
swoole_thread_init();
231+
swoole_thread_init(false);
232232

233233
while (running) {
234234
event_mutex.lock();
@@ -313,7 +313,7 @@ void ThreadPool::main_func(bool is_core_worker) {
313313
--n_waiting;
314314
}
315315
}
316-
swoole_thread_clean();
316+
swoole_thread_clean(false);
317317
}
318318

319319
void ThreadPool::create_thread(const bool is_core_worker) {

src/os/signal.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,21 @@ char *swoole_signal_to_str(int sig) {
7878
return buf;
7979
}
8080

81-
/**
82-
* block all singal
83-
*/
8481
void swoole_signal_block_all(void) {
8582
sigset_t mask;
8683
sigfillset(&mask);
8784
int ret = pthread_sigmask(SIG_BLOCK, &mask, nullptr);
8885
if (ret < 0) {
89-
swoole_sys_warning("pthread_sigmask() failed");
86+
swoole_sys_warning("pthread_sigmask(SIG_BLOCK) failed");
87+
}
88+
}
89+
90+
void swoole_signal_unblock_all(void) {
91+
sigset_t mask;
92+
sigfillset(&mask);
93+
int ret = pthread_sigmask(SIG_UNBLOCK, &mask, nullptr);
94+
if (ret < 0) {
95+
swoole_sys_warning("pthread_sigmask(SIG_UNBLOCK) failed");
9096
}
9197
}
9298

src/server/reactor_thread.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,9 @@ int Server::start_reactor_threads() {
720720

721721
SW_LOOP_N(reactor_num) {
722722
get_thread(i)->thread = std::thread([=]() {
723-
swoole_thread_init();
723+
swoole_thread_init(false);
724724
reactor_thread_main_loop(this, i);
725-
swoole_thread_clean();
725+
swoole_thread_clean(false);
726726
});
727727
}
728728

0 commit comments

Comments
 (0)