Skip to content

Commit

Permalink
optimize code[2]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 27, 2024
1 parent ebdf41e commit b01b79f
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 26 deletions.
2 changes: 2 additions & 0 deletions ext-src/swoole_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,8 @@ void php_swoole_thread_start(zend_string *file, zend_string *argv) {
#endif
zend_file_handle file_handle{};

swoole_thread_init();

if (php_request_startup() != SUCCESS) {
EG(exit_status) = 1;
goto _startup_error;
Expand Down
1 change: 1 addition & 0 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ void swoole_init(void);
void swoole_clean(void);
pid_t swoole_fork(int flags);
pid_t swoole_fork_exec(const std::function<void(void)> &child_fn);
void swoole_thread_init(void);
void swoole_redirect_stdout(int new_fd);
int swoole_shell_exec(const char *command, pid_t *pid, bool get_error_stream);
int swoole_daemon(int nochdir, int noclose);
Expand Down
18 changes: 9 additions & 9 deletions src/core/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ static void bug_report_message_init() {

void swoole_init(void) {
if (SwooleG.init) {
#ifdef SW_THREAD
SwooleTG.buffer_stack = new swoole::String(SW_STACK_BUFFER_SIZE);
g_logger_instance = new swoole::Logger;
#ifdef SW_DEBUG
sw_logger()->set_level(0);
#else
sw_logger()->set_level(SW_LOG_INFO);
#endif
#endif
return;
}

Expand Down Expand Up @@ -440,6 +431,15 @@ pid_t swoole_fork(int flags) {
return pid;
}

void swoole_thread_init(void) {
SwooleTG.buffer_stack = new String(SW_STACK_BUFFER_SIZE);
ON_SCOPE_EXIT {
delete SwooleTG.buffer_stack;
SwooleTG.buffer_stack = nullptr;
};
swoole_signal_block_all();
}

void swoole_dump_ascii(const char *data, size_t size) {
for (size_t i = 0; i < size; i++) {
printf("%u ", (unsigned) data[i]);
Expand Down
8 changes: 1 addition & 7 deletions src/os/async_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,7 @@ void ThreadPool::create_thread(const bool is_core_worker) {
try {
std::thread *_thread = new std::thread([this, is_core_worker]() {
bool exit_flag = false;
SwooleTG.buffer_stack = new String(SW_STACK_BUFFER_SIZE);
ON_SCOPE_EXIT {
delete SwooleTG.buffer_stack;
SwooleTG.buffer_stack = nullptr;
};

swoole_signal_block_all();
swoole_thread_init();

while (running) {
event_mutex.lock();
Expand Down
2 changes: 0 additions & 2 deletions src/server/master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,6 @@ int Server::start() {
* initializing server config, set default
*/
Server::Server(enum Mode _mode) {
swoole_init();

reactor_num = SW_CPU_NUM > SW_REACTOR_MAX_THREAD ? SW_REACTOR_MAX_THREAD : SW_CPU_NUM;
worker_num = SW_CPU_NUM;
max_connection = SW_MIN(SW_MAX_CONNECTION, SwooleG.max_sockets);
Expand Down
9 changes: 1 addition & 8 deletions src/server/reactor_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -797,12 +797,7 @@ int ReactorThread::init(Server *serv, Reactor *reactor, uint16_t reactor_id) {
static void ReactorThread_loop(Server *serv, int reactor_id) {
SwooleTG.id = reactor_id;
SwooleTG.type = Server::THREAD_REACTOR;

SwooleTG.buffer_stack = new String(SW_STACK_BUFFER_SIZE);
ON_SCOPE_EXIT {
delete SwooleTG.buffer_stack;
SwooleTG.buffer_stack = nullptr;
};
swoole_thread_init();

if (swoole_event_init(0) < 0) {
return;
Expand Down Expand Up @@ -830,8 +825,6 @@ static void ReactorThread_loop(Server *serv, int reactor_id) {
}
#endif

swoole_signal_block_all();

if (thread->init(serv, reactor, reactor_id) < 0) {
return;
}
Expand Down

0 comments on commit b01b79f

Please sign in to comment.