Skip to content

Commit d64826a

Browse files
authored
Optimize code, fix tests. Improve the log component to resolve data consistency issues in multithreaded mode. (#5754)
1 parent 0c4759d commit d64826a

32 files changed

+211
-119
lines changed

core-tests/src/memory/global_memory.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,5 @@ TEST(global_memory, alloc) {
4545
ASSERT_STREQ(ptr2, "hello, world, #2");
4646
ASSERT_STREQ(ptr3, "hello, world, #3");
4747

48-
pool->destroy();
4948
delete pool;
5049
}

core-tests/src/network/stream.cpp

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

8585
kill(getpid(), SIGTERM);
86-
87-
swoole_signal_unblock_all();
8886
});
8987

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

core-tests/src/os/process_pool.cpp

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

279279
kill(getpid(), SIGTERM);
280-
swoole_signal_unblock_all();
281280
});
282281

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

core-tests/src/os/signal.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "test_core.h"
2+
#include "swoole_process_pool.h"
23
#include "swoole_signal.h"
34

45
#ifdef HAVE_SIGNALFD
@@ -34,3 +35,33 @@ TEST(os_signal, signalfd) {
3435
swoole_event_wait();
3536
}
3637
#endif
38+
39+
TEST(os_signal, block) {
40+
ASSERT_EQ(swoole::test::spawn_exec_and_wait([]() {
41+
sysv_signal(SIGIO, [](int signo) { exit(255); });
42+
43+
std::thread t([] {
44+
swoole_signal_block_all();
45+
pthread_kill(pthread_self(), SIGIO);
46+
});
47+
t.join();
48+
}),
49+
0);
50+
}
51+
52+
TEST(os_signal, unblock) {
53+
auto status = swoole::test::spawn_exec_and_wait([]() {
54+
sysv_signal(SIGIO, [](int signo) { exit(255); });
55+
56+
std::thread t([] {
57+
swoole_signal_block_all();
58+
pthread_kill(pthread_self(), SIGIO);
59+
swoole_signal_unblock_all();
60+
});
61+
t.join();
62+
});
63+
64+
auto exit_status = swoole::ExitStatus(getpid(), status);
65+
66+
ASSERT_EQ(exit_status.get_code(), 255);
67+
}

ext-src/swoole_coroutine.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,19 +1116,20 @@ PHP_FUNCTION(swoole_coroutine_defer) {
11161116

11171117
static PHP_METHOD(swoole_coroutine, stats) {
11181118
array_init(return_value);
1119+
11191120
add_assoc_long_ex(return_value, ZEND_STRL("event_num"), sw_reactor() ? sw_reactor()->get_event_num() : 0);
1120-
add_assoc_long_ex(
1121-
return_value, ZEND_STRL("signal_listener_num"), SwooleTG.signal_listener_num + SwooleTG.co_signal_listener_num);
1121+
add_assoc_long_ex(return_value, ZEND_STRL("signal_listener_num"), swoole_signal_get_listener_num());
11221122

1123-
if (SwooleTG.async_threads) {
1124-
add_assoc_long_ex(return_value, ZEND_STRL("aio_task_num"), SwooleTG.async_threads->get_task_num());
1125-
add_assoc_long_ex(return_value, ZEND_STRL("aio_worker_num"), SwooleTG.async_threads->get_worker_num());
1126-
add_assoc_long_ex(return_value, ZEND_STRL("aio_queue_size"), SwooleTG.async_threads->get_queue_size());
1123+
if (sw_async_threads()) {
1124+
add_assoc_long_ex(return_value, ZEND_STRL("aio_task_num"), sw_async_threads()->get_task_num());
1125+
add_assoc_long_ex(return_value, ZEND_STRL("aio_worker_num"), sw_async_threads()->get_worker_num());
1126+
add_assoc_long_ex(return_value, ZEND_STRL("aio_queue_size"), sw_async_threads()->get_queue_size());
11271127
} else {
11281128
add_assoc_long_ex(return_value, ZEND_STRL("aio_task_num"), 0);
11291129
add_assoc_long_ex(return_value, ZEND_STRL("aio_worker_num"), 0);
11301130
add_assoc_long_ex(return_value, ZEND_STRL("aio_queue_size"), 0);
11311131
}
1132+
11321133
add_assoc_long_ex(return_value, ZEND_STRL("c_stack_size"), Coroutine::get_stack_size());
11331134
add_assoc_long_ex(return_value, ZEND_STRL("coroutine_num"), Coroutine::count());
11341135
add_assoc_long_ex(return_value, ZEND_STRL("coroutine_peak_num"), Coroutine::get_peak_num());

ext-src/swoole_process.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ static PHP_METHOD(swoole_process, signal) {
486486
#endif
487487
signal_fci_caches[signo] = nullptr;
488488
swoole_event_defer(sw_callable_free, fci_cache);
489-
SwooleTG.signal_listener_num--;
489+
SwooleG.signal_listener_num--;
490490
RETURN_TRUE;
491491
} else {
492492
php_swoole_error(E_WARNING, "unable to find the callback of signal [" ZEND_LONG_FMT "]", signo);
@@ -506,7 +506,7 @@ static PHP_METHOD(swoole_process, signal) {
506506
if (signal_fci_caches[signo]) {
507507
sw_callable_free(signal_fci_caches[signo]);
508508
} else {
509-
SwooleTG.signal_listener_num++;
509+
SwooleG.signal_listener_num++;
510510
}
511511
signal_fci_caches[signo] = fci_cache;
512512
#ifdef SW_USE_THREAD_CONTEXT
@@ -521,15 +521,15 @@ static PHP_METHOD(swoole_process, signal) {
521521
if (!SwooleTG.reactor->isset_exit_condition(Reactor::EXIT_CONDITION_SIGNAL_LISTENER)) {
522522
SwooleTG.reactor->set_exit_condition(Reactor::EXIT_CONDITION_SIGNAL_LISTENER,
523523
[](Reactor *reactor, size_t &event_num) -> bool {
524-
return SwooleTG.signal_listener_num == 0 or !SwooleG.wait_signal;
524+
return SwooleG.signal_listener_num == 0 or !SwooleG.wait_signal;
525525
});
526526
}
527527

528528
if (signal_fci_caches[signo]) {
529529
// free the old fci_cache
530530
swoole_event_defer(sw_callable_free, signal_fci_caches[signo]);
531531
} else {
532-
SwooleTG.signal_listener_num++;
532+
SwooleG.signal_listener_num++;
533533
}
534534
signal_fci_caches[signo] = fci_cache;
535535

ext-src/swoole_timer.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ static void timer_add(INTERNAL_FUNCTION_PARAMETERS, bool persistent) {
183183
php_swoole_check_reactor();
184184
}
185185

186+
/**
187+
* In certain systems, such as macOS, zend_long is the long long type,
188+
* and it must be explicitly converted to long.
189+
*/
186190
tnode = swoole_timer_add((long) ms, persistent, timer_callback, fci);
187191
if (UNEXPECTED(!tnode)) {
188192
php_swoole_fatal_error(E_WARNING, "add timer failed");

include/swoole.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ struct TimerNode;
219219
struct Event;
220220
class Pipe;
221221
class MessageBus;
222+
class Server;
222223
namespace network {
223224
struct Socket;
224225
struct Address;
@@ -690,23 +691,21 @@ struct RecvData {
690691
struct ThreadGlobal {
691692
uint16_t id;
692693
uint8_t type;
694+
int32_t error;
693695
#ifdef SW_THREAD
694696
uint8_t process_type;
695697
uint32_t process_id;
696698
#endif
697699
String *buffer_stack;
698700
Reactor *reactor;
699-
Logger *logger;
700701
Timer *timer;
701702
TimerScheduler timer_scheduler;
702703
MessageBus *message_bus;
703704
AsyncThreads *async_threads;
704705
#ifdef SW_USE_IOURING
705706
Iouring *iouring;
706707
#endif
707-
uint32_t signal_listener_num;
708-
uint32_t co_signal_listener_num;
709-
int error;
708+
bool signal_blocking_all;
710709
};
711710

712711
struct Allocator {
@@ -762,6 +761,8 @@ struct Global {
762761
int signal_fd;
763762
bool signal_alarm;
764763
bool signal_dispatch;
764+
uint32_t signal_listener_num;
765+
uint32_t signal_async_listener_num;
765766

766767
long trace_flags;
767768

@@ -776,15 +777,18 @@ struct Global {
776777
MemoryPool *memory_pool;
777778
Allocator std_allocator;
778779
std::string task_tmpfile;
779-
//-----------------------[DNS]--------------------------
780+
//------------------[Single Instance]----------------------
781+
Logger *logger;
782+
Server *server;
783+
//-----------------------[DNS]-----------------------------
780784
std::string dns_server_host;
781785
int dns_server_port;
782786
double dns_cache_refresh_time;
783787
int dns_tries;
784788
std::string dns_resolvconf_path;
785789
std::string dns_hosts_path;
786790
std::list<NameResolver> name_resolvers;
787-
//-----------------------[AIO]--------------------------
791+
//-----------------------[AIO]----------------------------
788792
uint32_t aio_core_worker_num;
789793
uint32_t aio_worker_num;
790794
#ifdef SW_USE_IOURING

include/swoole_async.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,5 @@ void handler_getaddrinfo(AsyncEvent *event);
136136

137137
} // namespace async
138138
}; // namespace swoole
139+
140+
swoole::AsyncThreads *sw_async_threads();

include/swoole_log.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,20 @@ class Logger {
5858
std::string date_format = SW_LOG_DEFAULT_DATE_FORMAT;
5959
std::string log_file = "";
6060
std::string log_real_file;
61+
std::mutex lock;
6162
int log_rotation = SW_LOG_ROTATION_SINGLE;
6263

64+
void reopen_without_lock();
65+
6366
public:
6467
bool open(const char *logfile);
68+
void reopen();
6569
void set_stream(FILE *stream);
70+
/**
71+
* Only the `put` and `reopen` functions are thread-safe,
72+
* other functions must be used in a single-threaded environment.
73+
*/
6674
void put(int level, const char *content, size_t length);
67-
void reopen();
6875
void close(void);
6976
void reset();
7077
void set_level(int lv);

0 commit comments

Comments
 (0)