From 91b280859be5a5a9b99e205e9da98a14509258d5 Mon Sep 17 00:00:00 2001 From: matyhtf Date: Sat, 24 Aug 2019 14:51:43 +0800 Subject: [PATCH] remove SwooleG.main_reactor --- core-tests/include/tests.h | 6 +-- core-tests/src/os/signal.cpp | 2 +- gdbinit | 14 +++--- include/client.h | 6 +-- include/coroutine_socket.h | 2 +- include/socks5.h | 8 ++-- include/swoole.h | 1 - php_swoole.h | 2 +- src/core/base.c | 4 +- src/coroutine/file_lock.cc | 2 +- src/coroutine/hook.cc | 62 +++++++++++++------------- src/network/client.c | 38 ++++++++-------- src/network/timer.c | 4 +- src/os/async_thread.cc | 6 +-- src/os/base.c | 8 ++-- src/os/signal.c | 10 ++--- src/os/wait.cc | 8 ++-- src/server/base.c | 2 +- src/server/manager.cc | 2 +- src/server/master.cc | 8 ++-- src/server/process.cc | 6 +-- src/server/reactor_process.cc | 10 ++--- src/server/reactor_thread.cc | 28 ++++++------ src/server/task_worker.cc | 14 +++--- src/server/worker.cc | 26 +++++------ src/wrapper/event.cc | 13 ++---- swoole_async_coro.cc | 2 +- swoole_coroutine.cc | 14 +++--- swoole_coroutine_scheduler.cc | 2 +- swoole_coroutine_system.cc | 10 ++--- swoole_event.cc | 84 +++++++++++++++++------------------ swoole_process.cc | 18 ++++---- swoole_process_pool.cc | 2 +- swoole_redis_coro.cc | 4 +- swoole_server.cc | 6 +-- 35 files changed, 214 insertions(+), 220 deletions(-) diff --git a/core-tests/include/tests.h b/core-tests/include/tests.h index dd8992433..2e71796a3 100755 --- a/core-tests/include/tests.h +++ b/core-tests/include/tests.h @@ -37,14 +37,14 @@ static void coro_test_fn(void *arg) static inline void coro_test_wait(int *complete_num, int total_num) { - SwooleG.main_reactor->once = true; + SwooleTG.reactor->once = true; while (*complete_num != total_num) { - SwooleG.main_reactor->wait(SwooleG.main_reactor, nullptr); + SwooleTG.reactor->wait(SwooleTG.reactor, nullptr); } - SwooleG.main_reactor->once = false; + SwooleTG.reactor->once = false; } static inline void coro_test_create(coroutine_func_t fn, void *arg, int *complete_num) diff --git a/core-tests/src/os/signal.cpp b/core-tests/src/os/signal.cpp index a0d577f5c..7b35b02f5 100644 --- a/core-tests/src/os/signal.cpp +++ b/core-tests/src/os/signal.cpp @@ -17,7 +17,7 @@ TEST(os_signal, swSignalfd_set) swSignalfd_init(); swSignal_add(SIGUSR1,sig_usr1); - swSignalfd_setup(SwooleG.main_reactor); + swSignalfd_setup(SwooleTG.reactor); sigemptyset(&curset); sigprocmask(SIG_BLOCK, NULL, &curset); diff --git a/gdbinit b/gdbinit index e2ed8a755..abda1178d 100644 --- a/gdbinit +++ b/gdbinit @@ -21,13 +21,13 @@ define timer_list end define reactor_info - if SwooleG.main_reactor - printf "\t reactor id: %d\n",SwooleG.main_reactor->id - printf "\t running: %d\n", SwooleG.main_reactor->running - printf "\t event_num: %d\n", SwooleG.main_reactor->event_num - printf "\t max_event_num: %d\n", SwooleG.main_reactor->max_event_num - printf "\t check_timer: %d\n", SwooleG.main_reactor->check_timer - printf "\t timeout_msec: %d\n", SwooleG.main_reactor->timeout_msec + if SwooleTG.reactor + printf "\t reactor id: %d\n",SwooleTG.reactor->id + printf "\t running: %d\n", SwooleTG.reactor->running + printf "\t event_num: %d\n", SwooleTG.reactor->event_num + printf "\t max_event_num: %d\n", SwooleTG.reactor->max_event_num + printf "\t check_timer: %d\n", SwooleTG.reactor->check_timer + printf "\t timeout_msec: %d\n", SwooleTG.reactor->timeout_msec end end diff --git a/include/client.h b/include/client.h index 0cea8bd68..11665e1f1 100644 --- a/include/client.h +++ b/include/client.h @@ -146,9 +146,9 @@ typedef struct _swClient void (*onBufferFull)(struct _swClient *cli); void (*onBufferEmpty)(struct _swClient *cli); - int (*connect)(struct _swClient *cli, char *host, int port, double _timeout, int sock_flag); - int (*send)(struct _swClient *cli, char *data, int length, int flags); - int (*sendfile)(struct _swClient *cli, char *filename, off_t offset, size_t length); + int (*connect)(struct _swClient *cli, const char *host, int port, double _timeout, int sock_flag); + int (*send)(struct _swClient *cli, const char *data, int length, int flags); + int (*sendfile)(struct _swClient *cli, const char *filename, off_t offset, size_t length); int (*recv)(struct _swClient *cli, char *data, int len, int flags); int (*pipe)(struct _swClient *cli, int write_fd, int is_session_id); int (*close)(struct _swClient *cli); diff --git a/include/coroutine_socket.h b/include/coroutine_socket.h index e19d722b1..c587c17b7 100644 --- a/include/coroutine_socket.h +++ b/include/coroutine_socket.h @@ -146,7 +146,7 @@ class Socket static inline swReactor* get_reactor() { - return SwooleTG.reactor ? SwooleTG.reactor : SwooleG.main_reactor; + return SwooleTG.reactor ? SwooleTG.reactor : SwooleTG.reactor; } static inline void init_reactor(swReactor *reactor) diff --git a/include/socks5.h b/include/socks5.h index 8b16e069a..a40c98b81 100644 --- a/include/socks5.h +++ b/include/socks5.h @@ -42,7 +42,7 @@ enum swSocks5_method typedef struct _swSocks5 { - char *host; + const char *host; int port; uint8_t state; @@ -50,12 +50,12 @@ typedef struct _swSocks5 uint8_t method; uint8_t dns_tunnel; - char *username; - char *password; + const char *username; + const char *password; uint16_t l_username; uint16_t l_password; - char *target_host; + const char *target_host; int target_port; uint16_t l_target_host; diff --git a/include/swoole.h b/include/swoole.h index 8bcb49432..aefd1d08f 100644 --- a/include/swoole.h +++ b/include/swoole.h @@ -2372,7 +2372,6 @@ typedef struct swServer *serv; swMemoryPool *memory_pool; - swReactor *main_reactor; swTimer timer; char *task_tmpdir; diff --git a/php_swoole.h b/php_swoole.h index 3d9c300fc..a6ec52d7c 100644 --- a/php_swoole.h +++ b/php_swoole.h @@ -1163,7 +1163,7 @@ static sw_inline int php_swoole_check_reactor() { return -1; } - if (sw_unlikely(!SwooleG.main_reactor)) + if (sw_unlikely(!SwooleTG.reactor)) { return php_swoole_reactor_init() == SW_OK ? 1 : -1; } diff --git a/src/core/base.c b/src/core/base.c index 8501f2279..99fccf02a 100644 --- a/src/core/base.c +++ b/src/core/base.c @@ -149,7 +149,7 @@ void swoole_clean(void) { sw_free(SwooleG.task_tmpdir); } - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_free(); } @@ -209,7 +209,7 @@ pid_t swoole_fork(int flags) /** * reset eventLoop */ - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_free(); swTraceLog(SW_TRACE_REACTOR, "reactor has been destroyed"); diff --git a/src/coroutine/file_lock.cc b/src/coroutine/file_lock.cc index 8f89f5e88..50776a5de 100644 --- a/src/coroutine/file_lock.cc +++ b/src/coroutine/file_lock.cc @@ -129,7 +129,7 @@ static inline int lock_nb(char *filename, int fd, int operation) int swoole_coroutine_flock_ex(char *filename, int fd, int operation) { Coroutine *co = Coroutine::get_current(); - if (sw_unlikely(SwooleG.main_reactor == nullptr || !co)) + if (sw_unlikely(SwooleTG.reactor == nullptr || !co)) { return ::flock(fd, operation); } diff --git a/src/coroutine/hook.cc b/src/coroutine/hook.cc index b85c5d003..1f31585e1 100644 --- a/src/coroutine/hook.cc +++ b/src/coroutine/hook.cc @@ -34,7 +34,7 @@ extern "C" int swoole_coroutine_socket(int domain, int type, int protocol) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return socket(domain, type, protocol); } @@ -49,11 +49,11 @@ int swoole_coroutine_socket(int domain, int type, int protocol) ssize_t swoole_coroutine_send(int sockfd, const void *buf, size_t len, int flags) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { _no_coro: return ::send(sockfd, buf, len, flags); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, sockfd); + swSocket *conn = swReactor_get(SwooleTG.reactor, sockfd); if (conn == nullptr) { goto _no_coro; @@ -64,11 +64,11 @@ ssize_t swoole_coroutine_send(int sockfd, const void *buf, size_t len, int flags ssize_t swoole_coroutine_sendmsg(int sockfd, const struct msghdr *msg, int flags) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { _no_coro: return ::sendmsg(sockfd, msg, flags); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, sockfd); + swSocket *conn = swReactor_get(SwooleTG.reactor, sockfd); if (conn == nullptr) { goto _no_coro; @@ -79,11 +79,11 @@ ssize_t swoole_coroutine_sendmsg(int sockfd, const struct msghdr *msg, int flags ssize_t swoole_coroutine_recvmsg(int sockfd, struct msghdr *msg, int flags) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { _no_coro: return ::recvmsg(sockfd, msg, flags); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, sockfd); + swSocket *conn = swReactor_get(SwooleTG.reactor, sockfd); if (conn == nullptr) { goto _no_coro; @@ -94,11 +94,11 @@ ssize_t swoole_coroutine_recvmsg(int sockfd, struct msghdr *msg, int flags) ssize_t swoole_coroutine_recv(int sockfd, void *buf, size_t len, int flags) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { _no_coro: return ::recv(sockfd, buf, len, flags); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, sockfd); + swSocket *conn = swReactor_get(SwooleTG.reactor, sockfd); if (conn == nullptr) { goto _no_coro; @@ -116,11 +116,11 @@ ssize_t swoole_coroutine_recv(int sockfd, void *buf, size_t len, int flags) int swoole_coroutine_close(int fd) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { _no_coro: return close(fd); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, fd); + swSocket *conn = swReactor_get(SwooleTG.reactor, fd); if (conn == nullptr) { goto _no_coro; @@ -139,11 +139,11 @@ int swoole_coroutine_close(int fd) int swoole_coroutine_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { _no_coro: return connect(sockfd, addr, addrlen); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, sockfd); + swSocket *conn = swReactor_get(SwooleTG.reactor, sockfd); if (conn == nullptr) { goto _no_coro; @@ -154,11 +154,11 @@ int swoole_coroutine_connect(int sockfd, const struct sockaddr *addr, socklen_t int swoole_coroutine_poll(struct pollfd *fds, nfds_t nfds, int timeout) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current() || nfds != 1 || timeout == 0)) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current() || nfds != 1 || timeout == 0)) { _poll: return poll(fds, nfds, timeout); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, fds[0].fd); + swSocket *conn = swReactor_get(SwooleTG.reactor, fds[0].fd); if (conn == nullptr) { goto _poll; @@ -258,7 +258,7 @@ static void aio_onCompleted(swAio_event *event) int swoole_coroutine_open(const char *pathname, int flags, mode_t mode) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return open(pathname, flags, mode); } @@ -284,12 +284,12 @@ int swoole_coroutine_open(const char *pathname, int flags, mode_t mode) ssize_t swoole_coroutine_read(int fd, void *buf, size_t count) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return read(fd, buf, count); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, fd); + swSocket *conn = swReactor_get(SwooleTG.reactor, fd); if (conn && conn->fdtype == SW_FD_CORO_SOCKET) { Socket *socket = (Socket *) conn->object; @@ -317,12 +317,12 @@ ssize_t swoole_coroutine_read(int fd, void *buf, size_t count) ssize_t swoole_coroutine_write(int fd, const void *buf, size_t count) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return write(fd, buf, count); } - swSocket *conn = swReactor_get(SwooleG.main_reactor, fd); + swSocket *conn = swReactor_get(SwooleTG.reactor, fd); if (conn && conn->fdtype == SW_FD_CORO_SOCKET) { Socket *socket = (Socket *) conn->object; @@ -350,7 +350,7 @@ ssize_t swoole_coroutine_write(int fd, const void *buf, size_t count) off_t swoole_coroutine_lseek(int fd, off_t offset, int whence) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return lseek(fd, offset, whence); } @@ -376,7 +376,7 @@ off_t swoole_coroutine_lseek(int fd, off_t offset, int whence) int swoole_coroutine_fstat(int fd, struct stat *statbuf) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return fstat(fd, statbuf); } @@ -401,7 +401,7 @@ int swoole_coroutine_fstat(int fd, struct stat *statbuf) int swoole_coroutine_unlink(const char *pathname) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return unlink(pathname); } @@ -425,7 +425,7 @@ int swoole_coroutine_unlink(const char *pathname) int swoole_coroutine_statvfs(const char *path, struct statvfs *buf) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return statvfs(path, buf); } @@ -450,7 +450,7 @@ int swoole_coroutine_statvfs(const char *path, struct statvfs *buf) int swoole_coroutine_mkdir(const char *pathname, mode_t mode) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return mkdir(pathname, mode); } @@ -475,7 +475,7 @@ int swoole_coroutine_mkdir(const char *pathname, mode_t mode) int swoole_coroutine_rmdir(const char *pathname) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return rmdir(pathname); } @@ -499,7 +499,7 @@ int swoole_coroutine_rmdir(const char *pathname) int swoole_coroutine_rename(const char *oldpath, const char *newpath) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return rename(oldpath, newpath); } @@ -524,7 +524,7 @@ int swoole_coroutine_rename(const char *oldpath, const char *newpath) int swoole_coroutine_access(const char *pathname, int mode) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return access(pathname, mode); } @@ -549,7 +549,7 @@ int swoole_coroutine_access(const char *pathname, int mode) int swoole_coroutine_flock(int fd, int operation) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return flock(fd, operation); } @@ -589,7 +589,7 @@ static void handler_readdir(swAio_event *event) DIR *swoole_coroutine_opendir(const char *name) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return opendir(name); } @@ -613,7 +613,7 @@ DIR *swoole_coroutine_opendir(const char *name) struct dirent *swoole_coroutine_readdir(DIR *dirp) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return readdir(dirp); } diff --git a/src/network/client.c b/src/network/client.c index cca3815d5..00649dac0 100644 --- a/src/network/client.c +++ b/src/network/client.c @@ -20,19 +20,19 @@ #include "socks5.h" #include "async.h" -static int swClient_inet_addr(swClient *cli, char *host, int port); -static int swClient_tcp_connect_sync(swClient *cli, char *host, int port, double _timeout, int udp_connect); -static int swClient_tcp_connect_async(swClient *cli, char *host, int port, double timeout, int nonblock); +static int swClient_inet_addr(swClient *cli, const char *host, int port); +static int swClient_tcp_connect_sync(swClient *cli, const char *host, int port, double _timeout, int udp_connect); +static int swClient_tcp_connect_async(swClient *cli, const char *host, int port, double timeout, int nonblock); -static int swClient_tcp_send_sync(swClient *cli, char *data, int length, int flags); -static int swClient_tcp_send_async(swClient *cli, char *data, int length, int flags); +static int swClient_tcp_send_sync(swClient *cli, const char *data, int length, int flags); +static int swClient_tcp_send_async(swClient *cli, const char *data, int length, int flags); static int swClient_tcp_pipe(swClient *cli, int write_fd, int flags); -static int swClient_udp_send(swClient *cli, char *data, int length, int flags); +static int swClient_udp_send(swClient *cli, const char *data, int length, int flags); -static int swClient_tcp_sendfile_sync(swClient *cli, char *filename, off_t offset, size_t length); -static int swClient_tcp_sendfile_async(swClient *cli, char *filename, off_t offset, size_t length); +static int swClient_tcp_sendfile_sync(swClient *cli, const char *filename, off_t offset, size_t length); +static int swClient_tcp_sendfile_async(swClient *cli, const char *filename, off_t offset, size_t length); static int swClient_tcp_recv_no_buffer(swClient *cli, char *data, int len, int flags); -static int swClient_udp_connect(swClient *cli, char *host, int port, double _timeout, int udp_connect); +static int swClient_udp_connect(swClient *cli, const char *host, int port, double _timeout, int udp_connect); static int swClient_udp_recv(swClient *cli, char *data, int len, int waitall); static int swClient_close(swClient *cli); @@ -342,7 +342,7 @@ int swClient_ssl_verify(swClient *cli, int allow_self_signed) #endif -static int swClient_inet_addr(swClient *cli, char *host, int port) +static int swClient_inet_addr(swClient *cli, const char *host, int port) { //enable socks5 proxy if (cli->socks5_proxy) @@ -533,7 +533,7 @@ static int swClient_close(swClient *cli) return close(fd); } -static int swClient_tcp_connect_sync(swClient *cli, char *host, int port, double timeout, int nonblock) +static int swClient_tcp_connect_sync(swClient *cli, const char *host, int port, double timeout, int nonblock) { int ret, n; @@ -657,7 +657,7 @@ static int swClient_tcp_connect_sync(swClient *cli, char *host, int port, double return ret; } -static int swClient_tcp_connect_async(swClient *cli, char *host, int port, double timeout, int nonblock) +static int swClient_tcp_connect_async(swClient *cli, const char *host, int port, double timeout, int nonblock) { int ret; @@ -806,7 +806,7 @@ static int swClient_tcp_pipe(swClient *cli, int write_fd, int flags) return SW_OK; } -static int swClient_tcp_send_async(swClient *cli, char *data, int length, int flags) +static int swClient_tcp_send_async(swClient *cli, const char *data, int length, int flags) { int n = length; if (swoole_event_write(cli->socket->fd, data, length) < 0) @@ -830,7 +830,7 @@ static int swClient_tcp_send_async(swClient *cli, char *data, int length, int fl return n; } -static int swClient_tcp_send_sync(swClient *cli, char *data, int length, int flags) +static int swClient_tcp_send_sync(swClient *cli, const char *data, int length, int flags) { int written = 0; int n; @@ -864,7 +864,7 @@ static int swClient_tcp_send_sync(swClient *cli, char *data, int length, int fla return written; } -static int swClient_tcp_sendfile_sync(swClient *cli, char *filename, off_t offset, size_t length) +static int swClient_tcp_sendfile_sync(swClient *cli, const char *filename, off_t offset, size_t length) { if (swSocket_sendfile_sync(cli->socket->fd, filename, offset, length, cli->timeout) < 0) { @@ -874,7 +874,7 @@ static int swClient_tcp_sendfile_sync(swClient *cli, char *filename, off_t offse return SW_OK; } -static int swClient_tcp_sendfile_async(swClient *cli, char *filename, off_t offset, size_t length) +static int swClient_tcp_sendfile_async(swClient *cli, const char *filename, off_t offset, size_t length) { if (swConnection_sendfile(cli->socket, filename, offset, length) < 0) { @@ -945,7 +945,7 @@ static int swClient_tcp_recv_no_buffer(swClient *cli, char *data, int len, int f return ret; } -static int swClient_udp_connect(swClient *cli, char *host, int port, double timeout, int udp_connect) +static int swClient_udp_connect(swClient *cli, const char *host, int port, double timeout, int udp_connect) { if (swClient_inet_addr(cli, host, port) < 0) { @@ -1011,7 +1011,7 @@ static int swClient_udp_connect(swClient *cli, char *host, int port, double time } } -static int swClient_udp_send(swClient *cli, char *data, int len, int flags) +static int swClient_udp_send(swClient *cli, const char *data, int len, int flags) { int n; n = sendto(cli->socket->fd, data, len, 0, (struct sockaddr *) &cli->server_addr.addr, cli->server_addr.len); @@ -1412,7 +1412,7 @@ static void swClient_onTimeout(swTimer *timer, swTimer_node *tnode) static void swClient_onResolveCompleted(swAio_event *event) { - swSocket *socket = swReactor_get(SwooleG.main_reactor, event->fd); + swSocket *socket = swReactor_get(SwooleTG.reactor, event->fd); if (socket->removed) { sw_free(event->buf); diff --git a/src/network/timer.c b/src/network/timer.c index a4e4f763c..d69ec73f9 100644 --- a/src/network/timer.c +++ b/src/network/timer.c @@ -96,9 +96,9 @@ static int swTimer_init(swTimer *timer, long msec) { ret = swReactorTimer_init(SwooleTG.reactor, timer, msec); } - else if (SwooleG.main_reactor) + else if (SwooleTG.reactor) { - ret = swReactorTimer_init(SwooleG.main_reactor, timer, msec); + ret = swReactorTimer_init(SwooleTG.reactor, timer, msec); } else { diff --git a/src/os/async_thread.cc b/src/os/async_thread.cc index af575e40b..ead183ffd 100644 --- a/src/os/async_thread.cc +++ b/src/os/async_thread.cc @@ -112,7 +112,7 @@ class async_thread_pool ~async_thread_pool() { shutdown(); - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_del(_pipe_read); } @@ -321,7 +321,7 @@ static int swAio_init() swWarn("AIO has already been initialized"); return SW_ERR; } - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { swWarn("no event loop, cannot initialized"); return SW_ERR; @@ -346,7 +346,7 @@ static int swAio_init() SwooleAIO.max_thread_count = SwooleAIO.min_thread_count; } - swReactor_add_destroy_callback(SwooleG.main_reactor, swAio_free, nullptr); + swReactor_add_destroy_callback(SwooleTG.reactor, swAio_free, nullptr); pool = new async_thread_pool(SwooleAIO.min_thread_count, SwooleAIO.max_thread_count); pool->start(); diff --git a/src/os/base.c b/src/os/base.c index 2d4cf9b24..079c918bc 100644 --- a/src/os/base.c +++ b/src/os/base.c @@ -39,7 +39,7 @@ int swAio_init(void) swWarn("AIO has already been initialized"); return SW_ERR; } - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { swWarn("No eventloop, cannot initialized"); return SW_ERR; @@ -67,7 +67,7 @@ int swAio_init(void) _pipe_read = _aio_pipe.getFd(&_aio_pipe, 0); _pipe_write = _aio_pipe.getFd(&_aio_pipe, 1); - SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_AIO, swAio_onCompleted); + SwooleTG.reactor->setHandle(SwooleTG.reactor, SW_FD_AIO, swAio_onCompleted); swoole_event_add(_pipe_read, SW_FD_AIO); if (swThreadPool_run(&pool) < 0) @@ -177,9 +177,9 @@ void swAio_free(void) return; } swThreadPool_free(&pool); - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - SwooleG.main_reactor->del(SwooleG.main_reactor, _pipe_read); + SwooleTG.reactor->del(SwooleTG.reactor, _pipe_read); } _aio_pipe.close(&_aio_pipe); SwooleAIO.init = 0; diff --git a/src/os/signal.c b/src/os/signal.c index 2ee7db834..3a66549c1 100644 --- a/src/os/signal.c +++ b/src/os/signal.c @@ -117,7 +117,7 @@ void swSignal_add(int signo, swSignalHandler handler) // SIGCHLD can not be monitored by kqueue, if blocked by SIG_IGN // see https://www.freebsd.org/cgi/man.cgi?kqueue // if there's no main reactor, signals cannot be monitored either - if (signo != SIGCHLD && SwooleG.main_reactor) + if (signo != SIGCHLD && SwooleTG.reactor) { swKqueueSignal_set(signo, handler); } @@ -134,9 +134,9 @@ void swSignal_add(int signo, swSignalHandler handler) static void swSignal_async_handler(int signo) { - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - SwooleG.main_reactor->singal_no = signo; + SwooleTG.reactor->singal_no = signo; } else { @@ -197,7 +197,7 @@ void swSignal_clear(void) if (signals[i].active) { #ifdef HAVE_KQUEUE - if (signals[i].signo != SIGCHLD && SwooleG.main_reactor) + if (signals[i].signo != SIGCHLD && SwooleTG.reactor) { swKqueueSignal_set(signals[i].signo, NULL); } @@ -319,7 +319,7 @@ static int swSignalfd_onSignal(swReactor *reactor, swEvent *event) static void swKqueueSignal_set(int signo, swSignalHandler handler) { struct kevent ev; - swReactor *reactor = SwooleG.main_reactor; + swReactor *reactor = SwooleTG.reactor; struct { int fd; diff --git a/src/os/wait.cc b/src/os/wait.cc index 0d79ab313..3f6a3b323 100644 --- a/src/os/wait.cc +++ b/src/os/wait.cc @@ -85,9 +85,9 @@ void swoole_coroutine_signal_init() signal_init = true; swSignal_add(SIGCHLD, signal_handler); #ifdef HAVE_SIGNALFD - if (SwooleG.use_signalfd && !swReactor_isset_handler(SwooleG.main_reactor, SW_FD_SIGNAL)) + if (SwooleG.use_signalfd && !swReactor_isset_handler(SwooleTG.reactor, SW_FD_SIGNAL)) { - swSignalfd_setup(SwooleG.main_reactor); + swSignalfd_setup(SwooleTG.reactor); } #endif } @@ -108,7 +108,7 @@ pid_t swoole_coroutine_waitpid(pid_t __pid, int *__stat_loc, int __options) return __pid; } - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current() || (__options & WNOHANG))) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current() || (__options & WNOHANG))) { return waitpid(__pid, __stat_loc, __options); } @@ -134,7 +134,7 @@ pid_t swoole_coroutine_waitpid(pid_t __pid, int *__stat_loc, int __options) pid_t swoole_coroutine_wait(int *__stat_loc) { - if (sw_unlikely(SwooleG.main_reactor == nullptr || !Coroutine::get_current())) + if (sw_unlikely(SwooleTG.reactor == nullptr || !Coroutine::get_current())) { return wait( __stat_loc); } diff --git a/src/server/base.c b/src/server/base.c index bfc1337cf..f8ed72cf4 100644 --- a/src/server/base.c +++ b/src/server/base.c @@ -168,7 +168,7 @@ static int swFactory_end(swFactory *factory, int fd) if (swBuffer_empty(conn->socket->out_buffer) || conn->peer_closed) { - swReactor *reactor = SwooleG.main_reactor; + swReactor *reactor = SwooleTG.reactor; return swReactorThread_close(reactor, conn->fd); } else diff --git a/src/server/manager.cc b/src/server/manager.cc index f31969ca4..586aee656 100644 --- a/src/server/manager.cc +++ b/src/server/manager.cc @@ -258,7 +258,7 @@ static int swManager_loop(swServer *serv) int status; SwooleG.use_signalfd = 0; - SwooleG.main_reactor = NULL; + SwooleTG.reactor = NULL; SwooleG.enable_coroutine = 0; ManagerProcess.reload_workers = (swWorker *) sw_calloc(serv->worker_num + serv->task_worker_num, sizeof(swWorker)); diff --git a/src/server/master.cc b/src/server/master.cc index 1acfa5bc7..68f6d174f 100644 --- a/src/server/master.cc +++ b/src/server/master.cc @@ -791,9 +791,9 @@ int swServer_shutdown(swServer *serv) { serv->running = 0; //stop all thread - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - swReactor *reactor = SwooleG.main_reactor; + swReactor *reactor = SwooleTG.reactor; reactor->wait_exit = 1; swListenPort *port; LL_FOREACH(serv->listen_list, port) @@ -996,7 +996,7 @@ int swServer_master_send(swServer *serv, swSendData *_send) if (serv->single_thread) { - reactor = SwooleG.main_reactor; + reactor = SwooleTG.reactor; } else { @@ -1795,7 +1795,7 @@ static swConnection* swServer_connection_new(swServer *serv, swListenPort *ls, i connection = &(serv->connection_list[fd]); bzero(connection, sizeof(swConnection)); - swSocket *_socket = swReactor_get(SwooleG.main_reactor, fd); + swSocket *_socket = swReactor_get(SwooleTG.reactor, fd); _socket->object = connection; _socket->buffer_size = ls->socket_buffer_size; _socket->fd = fd; diff --git a/src/server/process.cc b/src/server/process.cc index 66fe81cc9..2305932c4 100644 --- a/src/server/process.cc +++ b/src/server/process.cc @@ -444,15 +444,15 @@ static int swFactoryProcess_finish(swFactory *factory, swSendData *resp) { int _len = resp->info.len; int _header = htonl(_len + sizeof(resp->info)); - if (SwooleG.main_reactor->write(SwooleG.main_reactor, serv->last_stream_fd, (char*) &_header, sizeof(_header)) < 0) + if (SwooleTG.reactor->write(SwooleTG.reactor, serv->last_stream_fd, (char*) &_header, sizeof(_header)) < 0) { return SW_ERR; } - if (SwooleG.main_reactor->write(SwooleG.main_reactor, serv->last_stream_fd, &resp->info, sizeof(resp->info)) < 0) + if (SwooleTG.reactor->write(SwooleTG.reactor, serv->last_stream_fd, &resp->info, sizeof(resp->info)) < 0) { return SW_ERR; } - if (SwooleG.main_reactor->write(SwooleG.main_reactor, serv->last_stream_fd, resp->data, _len) < 0) + if (SwooleTG.reactor->write(SwooleTG.reactor, serv->last_stream_fd, resp->data, _len) < 0) { return SW_ERR; } diff --git a/src/server/reactor_process.cc b/src/server/reactor_process.cc index 4b2779676..486802d1d 100644 --- a/src/server/reactor_process.cc +++ b/src/server/reactor_process.cc @@ -308,14 +308,14 @@ static int swReactorProcess_loop(swProcessPool *pool, swWorker *worker) swServer_worker_init(serv, worker); //create reactor - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { if (swoole_event_init() < 0) { return SW_ERR; } } - swReactor *reactor = SwooleG.main_reactor; + swReactor *reactor = SwooleTG.reactor; int n_buffer = serv->worker_num + serv->task_worker_num + serv->user_worker_num; if (swReactorProcess_alloc_output_buffer(n_buffer)) @@ -352,7 +352,7 @@ static int swReactorProcess_loop(swProcessPool *pool, swWorker *worker) #ifdef HAVE_SIGNALFD if (SwooleG.use_signalfd) { - swSignalfd_setup(SwooleG.main_reactor); + swSignalfd_setup(SwooleTG.reactor); } #endif @@ -511,13 +511,13 @@ static int swReactorProcess_onClose(swReactor *reactor, swEvent *event) static int swReactorProcess_send2worker(int pipe_fd, const void *data, int length) { - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { return swSocket_write_blocking(pipe_fd, data, length); } else { - return SwooleG.main_reactor->write(SwooleG.main_reactor, pipe_fd, data, length); + return SwooleTG.reactor->write(SwooleTG.reactor, pipe_fd, data, length); } } diff --git a/src/server/reactor_thread.cc b/src/server/reactor_thread.cc index b11f08105..b70bfe89d 100644 --- a/src/server/reactor_thread.cc +++ b/src/server/reactor_thread.cc @@ -819,14 +819,14 @@ int swReactorThread_start(swServer *serv) return SW_ERR; } - swReactor *main_reactor = SwooleG.main_reactor; - main_reactor->disable_accept = 0; - main_reactor->enable_accept = swServer_enable_accept; + swReactor *reactor = SwooleTG.reactor; + reactor->disable_accept = 0; + reactor->enable_accept = swServer_enable_accept; #ifdef HAVE_SIGNALFD if (SwooleG.use_signalfd) { - swSignalfd_setup(main_reactor); + swSignalfd_setup(reactor); } #endif @@ -841,9 +841,9 @@ int swReactorThread_start(swServer *serv) if (swPort_listen(ls) < 0) { _failed: - main_reactor->free(main_reactor); - SwooleG.main_reactor = nullptr; - sw_free(main_reactor); + reactor->free(reactor); + SwooleTG.reactor = nullptr; + sw_free(reactor); return SW_ERR; } } @@ -873,12 +873,12 @@ int swReactorThread_start(swServer *serv) { continue; } - main_reactor->add(main_reactor, ls->sock, SW_FD_STREAM_SERVER); + reactor->add(reactor, ls->sock, SW_FD_STREAM_SERVER); } if (serv->single_thread) { - swReactorThread_init(serv, main_reactor, 0); + swReactorThread_init(serv, reactor, 0); goto _init_master_thread; } /** @@ -889,7 +889,7 @@ int swReactorThread_start(swServer *serv) /** * set a special id */ - main_reactor->id = serv->reactor_num; + reactor->id = serv->reactor_num; SwooleTG.id = serv->reactor_num; } @@ -939,12 +939,12 @@ int swReactorThread_start(swServer *serv) SwooleTG.type = SW_THREAD_MASTER; SwooleTG.update_time = 1; - SwooleG.main_reactor = main_reactor; + SwooleTG.reactor = reactor; SwooleG.pid = getpid(); SwooleG.process_type = SW_PROCESS_MASTER; - main_reactor->ptr = serv; - swReactor_set_handler(main_reactor, SW_FD_STREAM_SERVER, swServer_master_onAccept); + reactor->ptr = serv; + swReactor_set_handler(reactor, SW_FD_STREAM_SERVER, swServer_master_onAccept); if (serv->hooks[SW_SERVER_HOOK_MASTER_START]) { @@ -1341,7 +1341,7 @@ static void swHeartbeatThread_loop(swThreadParam *param) if (serv->single_thread) { - reactor = SwooleG.main_reactor; + reactor = SwooleTG.reactor; } else { diff --git a/src/server/task_worker.cc b/src/server/task_worker.cc index 4fbea537e..968d90cfc 100644 --- a/src/server/task_worker.cc +++ b/src/server/task_worker.cc @@ -159,7 +159,7 @@ void swTaskWorker_onStart(swProcessPool *pool, int worker_id) else { SwooleG.enable_signalfd = 0; - SwooleG.main_reactor = NULL; + SwooleTG.reactor = NULL; } swTaskWorker_signal_init(pool); @@ -187,7 +187,7 @@ void swTaskWorker_onStart(swProcessPool *pool, int worker_id) void swTaskWorker_onStop(swProcessPool *pool, int worker_id) { - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_free(); } @@ -238,9 +238,9 @@ static int swTaskWorker_loop_async(swProcessPool *pool, swWorker *worker) int pipe_worker = worker->pipe_worker; swSocket_set_nonblock(pipe_worker); - SwooleG.main_reactor->ptr = pool; + SwooleTG.reactor->ptr = pool; swoole_event_add(pipe_worker, SW_EVENT_READ, SW_FD_PIPE); - swReactor_set_handler(SwooleG.main_reactor, SW_FD_PIPE, swTaskWorker_onPipeReceive); + swReactor_set_handler(SwooleTG.reactor, SW_FD_PIPE, swTaskWorker_onPipeReceive); /** * set pipe buffer size @@ -250,14 +250,14 @@ static int swTaskWorker_loop_async(swProcessPool *pool, swWorker *worker) for (i = 0; i < serv->worker_num + serv->task_worker_num; i++) { worker = swServer_get_worker(serv, i); - pipe_socket = swReactor_get(SwooleG.main_reactor, worker->pipe_master); + pipe_socket = swReactor_get(SwooleTG.reactor, worker->pipe_master); pipe_socket->buffer_size = INT_MAX; - pipe_socket = swReactor_get(SwooleG.main_reactor, worker->pipe_worker); + pipe_socket = swReactor_get(SwooleTG.reactor, worker->pipe_worker); pipe_socket->buffer_size = INT_MAX; } //main loop - return SwooleG.main_reactor->wait(SwooleG.main_reactor, NULL); + return SwooleTG.reactor->wait(SwooleTG.reactor, NULL); } /** diff --git a/src/server/worker.cc b/src/server/worker.cc index 70b8d4f04..a07c39ea8 100644 --- a/src/server/worker.cc +++ b/src/server/worker.cc @@ -57,7 +57,7 @@ void swWorker_signal_handler(int signo) /** * Event worker */ - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swWorker_stop(SwooleWG.worker); } @@ -231,7 +231,7 @@ static int swWorker_onStreamPackage(swProtocol *proto, swSocket *conn, char *dat * stream end */ int _end = 0; - SwooleG.main_reactor->write(SwooleG.main_reactor, conn->fd, (void *) &_end, sizeof(_end)); + SwooleTG.reactor->write(SwooleTG.reactor, conn->fd, (void *) &_end, sizeof(_end)); return SW_OK; } @@ -467,9 +467,9 @@ void swWorker_onStart(swServer *serv) } #ifdef HAVE_SIGNALFD - if (SwooleG.use_signalfd && SwooleG.main_reactor && SwooleG.signal_fd == 0) + if (SwooleG.use_signalfd && SwooleTG.reactor && SwooleG.signal_fd == 0) { - swSignalfd_setup(SwooleG.main_reactor); + swSignalfd_setup(SwooleTG.reactor); } #endif @@ -489,7 +489,7 @@ void swWorker_stop(swWorker *worker) swServer *serv = (swServer *) worker->pool->ptr; worker->status = SW_WORKER_BUSY; - swReactor *reactor = SwooleG.main_reactor; + swReactor *reactor = SwooleTG.reactor; /** * force to end @@ -636,15 +636,15 @@ void swWorker_clean_pipe_buffer(swServer *serv) for (i = 0; i < serv->worker_num + serv->task_worker_num; i++) { swWorker *worker = swServer_get_worker(serv, i); - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { if (worker->pipe_worker) { - swReactor_wait_write_buffer(SwooleG.main_reactor, worker->pipe_worker); + swReactor_wait_write_buffer(SwooleTG.reactor, worker->pipe_worker); } if (worker->pipe_master) { - swReactor_wait_write_buffer(SwooleG.main_reactor, worker->pipe_master); + swReactor_wait_write_buffer(SwooleTG.reactor, worker->pipe_master); } } } @@ -666,7 +666,7 @@ int swWorker_loop(swServer *serv, int worker_id) return SW_ERR; } - swReactor * reactor = SwooleG.main_reactor; + swReactor * reactor = SwooleTG.reactor; /** * set pipe buffer size */ @@ -727,9 +727,9 @@ int swWorker_send2reactor(swServer *serv, swEventData *ev_data, size_t sendn, in int ret; int _pipe_fd = swWorker_get_send_pipe(serv, session_id, ev_data->info.reactor_id); - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - ret = SwooleG.main_reactor->write(SwooleG.main_reactor, _pipe_fd, ev_data, sendn); + ret = SwooleTG.reactor->write(SwooleTG.reactor, _pipe_fd, ev_data, sendn); } else { @@ -803,9 +803,9 @@ int swWorker_send2worker(swWorker *dst_worker, const void *buf, int n, int flag) return swMsgQueue_push(dst_worker->pool->queue, (swQueue_data *) &msg, n); } - if ((flag & SW_PIPE_NONBLOCK) && SwooleG.main_reactor) + if ((flag & SW_PIPE_NONBLOCK) && SwooleTG.reactor) { - return SwooleG.main_reactor->write(SwooleG.main_reactor, pipefd, buf, n); + return SwooleTG.reactor->write(SwooleTG.reactor, pipefd, buf, n); } else { diff --git a/src/wrapper/event.cc b/src/wrapper/event.cc index d582882f5..0ba6fd745 100644 --- a/src/wrapper/event.cc +++ b/src/wrapper/event.cc @@ -16,23 +16,18 @@ #include "swoole_api.h" -#ifdef SW_CO_MT +#define sw_reactor() (SwooleTG.reactor) + #include #include -#define sw_reactor() (SwooleTG.reactor) + std::once_flag init_flag; -#else -#define sw_reactor() (SwooleG.main_reactor) -#endif int swoole_event_init() { -#ifdef SW_CO_MT call_once(init_flag, swoole_init); SwooleTG.reactor = (swReactor *) sw_malloc(sizeof(swReactor)); -#else - SwooleG.main_reactor = (swReactor *) sw_malloc(sizeof(swReactor)); -#endif + if (!sw_reactor()) { swSysWarn("malloc failed."); diff --git a/swoole_async_coro.cc b/swoole_async_coro.cc index e6735d659..41b089b00 100644 --- a/swoole_async_coro.cc +++ b/swoole_async_coro.cc @@ -62,7 +62,7 @@ void php_swoole_async_coro_rshutdown() PHP_FUNCTION(swoole_async_set) { - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { php_swoole_fatal_error(E_ERROR, "eventLoop has already been created. unable to change settings"); RETURN_FALSE; diff --git a/swoole_coroutine.cc b/swoole_coroutine.cc index dd8c68430..8acd3dbe7 100644 --- a/swoole_coroutine.cc +++ b/swoole_coroutine.cc @@ -299,7 +299,7 @@ static void coro_interrupt_function(zend_execute_data *execute_data) php_coro_task *task = PHPCoroutine::get_task(); if (task && task->co && PHPCoroutine::is_schedulable(task)) { - SwooleG.main_reactor->defer(SwooleG.main_reactor, coro_interrupt_resume, (void *) task->co); + SwooleTG.reactor->defer(SwooleTG.reactor, coro_interrupt_resume, (void *) task->co); task->co->yield(); } if (orig_interrupt_function) @@ -376,7 +376,7 @@ inline void PHPCoroutine::activate() /** * deactivate when reactor free. */ - swReactor_add_destroy_callback(SwooleG.main_reactor, deactivate, nullptr); + swReactor_add_destroy_callback(SwooleTG.reactor, deactivate, nullptr); active = true; } @@ -389,7 +389,7 @@ void PHPCoroutine::error(int type, const char *error_filename, const uint32_t er /* update the last coroutine's info */ save_task(get_task()); } - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_free(); } @@ -784,7 +784,7 @@ void PHPCoroutine::main_func(void *arg) #ifdef SW_CORO_SUPPORT_BAILOUT } zend_catch { Coroutine::bailout([](){ - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_free(); } @@ -957,10 +957,10 @@ PHP_FUNCTION(swoole_coroutine_defer) PHP_METHOD(swoole_coroutine, stats) { array_init(return_value); - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - add_assoc_long_ex(return_value, ZEND_STRL("event_num"), SwooleG.main_reactor->event_num); - add_assoc_long_ex(return_value, ZEND_STRL("signal_listener_num"), SwooleG.main_reactor->signal_listener_num); + add_assoc_long_ex(return_value, ZEND_STRL("event_num"), SwooleTG.reactor->event_num); + add_assoc_long_ex(return_value, ZEND_STRL("signal_listener_num"), SwooleTG.reactor->signal_listener_num); } add_assoc_long_ex(return_value, ZEND_STRL("aio_task_num"), SwooleAIO.task_num); add_assoc_long_ex(return_value, ZEND_STRL("aio_thread_num"), swAio_thread_count()); diff --git a/swoole_coroutine_scheduler.cc b/swoole_coroutine_scheduler.cc index 482f7be2e..9e5499bc6 100644 --- a/swoole_coroutine_scheduler.cc +++ b/swoole_coroutine_scheduler.cc @@ -253,7 +253,7 @@ static PHP_METHOD(swoole_coroutine_scheduler, start) { scheduler_t *s = scheduler_get_object(Z_OBJ_P(ZEND_THIS)); - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { php_swoole_fatal_error(E_WARNING, "eventLoop has already been created. unable to start %s", SW_Z_OBJCE_NAME_VAL_P(ZEND_THIS)); RETURN_FALSE; diff --git a/swoole_coroutine_system.cc b/swoole_coroutine_system.cc index 0ddc12424..8fe5eb5ed 100644 --- a/swoole_coroutine_system.cc +++ b/swoole_coroutine_system.cc @@ -292,10 +292,10 @@ static int co_socket_onWritable(swReactor *reactor, swEvent *event) static void co_socket_read(int fd, zend_long length, INTERNAL_FUNCTION_PARAMETERS) { php_swoole_check_reactor(); - if (!swReactor_isset_handler(SwooleG.main_reactor, PHP_SWOOLE_FD_SOCKET)) + if (!swReactor_isset_handler(SwooleTG.reactor, PHP_SWOOLE_FD_SOCKET)) { - swReactor_set_handler(SwooleG.main_reactor, PHP_SWOOLE_FD_CO_UTIL | SW_EVENT_READ, co_socket_onReadable); - swReactor_set_handler(SwooleG.main_reactor, PHP_SWOOLE_FD_CO_UTIL | SW_EVENT_WRITE, co_socket_onWritable); + swReactor_set_handler(SwooleTG.reactor, PHP_SWOOLE_FD_CO_UTIL | SW_EVENT_READ, co_socket_onReadable); + swReactor_set_handler(SwooleTG.reactor, PHP_SWOOLE_FD_CO_UTIL | SW_EVENT_WRITE, co_socket_onWritable); } if (swoole_event_add(fd, SW_EVENT_READ, PHP_SWOOLE_FD_CO_UTIL) < 0) @@ -304,7 +304,7 @@ static void co_socket_read(int fd, zend_long length, INTERNAL_FUNCTION_PARAMETER RETURN_FALSE; } - swSocket *_socket = swReactor_get(SwooleG.main_reactor, fd); + swSocket *_socket = swReactor_get(SwooleTG.reactor, fd); util_socket *sock = (util_socket *) emalloc(sizeof(util_socket)); bzero(sock, sizeof(util_socket)); _socket->object = sock; @@ -340,7 +340,7 @@ static void co_socket_write(int fd, char* str, size_t l_str, INTERNAL_FUNCTION_P RETURN_FALSE; } - swSocket *_socket = swReactor_get(SwooleG.main_reactor, fd); + swSocket *_socket = swReactor_get(SwooleTG.reactor, fd); util_socket *sock = (util_socket *) emalloc(sizeof(util_socket)); bzero(sock, sizeof(util_socket)); _socket->object = sock; diff --git a/swoole_event.cc b/swoole_event.cc index 57a92b316..92e29697d 100644 --- a/swoole_event.cc +++ b/swoole_event.cc @@ -245,12 +245,12 @@ int php_swoole_reactor_init() return SW_ERR; } } - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { swTraceLog(SW_TRACE_PHP, "init reactor"); swoole_event_init(); - SwooleG.main_reactor->wait_exit = 1; + SwooleTG.reactor->wait_exit = 1; php_swoole_register_shutdown_function("Swoole\\Event::rshutdown"); } @@ -273,18 +273,18 @@ void php_swoole_event_wait() } } - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { return; } #ifdef HAVE_SIGNALFD - if (SwooleG.main_reactor->check_signalfd) + if (SwooleTG.reactor->check_signalfd) { - swSignalfd_setup(SwooleG.main_reactor); + swSignalfd_setup(SwooleTG.reactor); } #endif - if (!swReactor_empty(SwooleG.main_reactor)) + if (!swReactor_empty(SwooleTG.reactor)) { // Don't disable object slot reuse while running shutdown functions: // https://github.com/php/php-src/commit/bd6eabd6591ae5a7c9ad75dfbe7cc575fa907eac @@ -292,7 +292,7 @@ void php_swoole_event_wait() zend_bool in_shutdown = EG(flags) & EG_FLAGS_IN_SHUTDOWN; EG(flags) &= ~EG_FLAGS_IN_SHUTDOWN; #endif - int ret = SwooleG.main_reactor->wait(SwooleG.main_reactor, NULL); + int ret = SwooleTG.reactor->wait(SwooleTG.reactor, NULL); if (ret < 0) { php_swoole_sys_error(E_ERROR, "reactor wait failed"); @@ -309,9 +309,9 @@ void php_swoole_event_wait() void php_swoole_event_exit() { - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - SwooleG.main_reactor->running = 0; + SwooleTG.reactor->running = 0; } } @@ -464,11 +464,11 @@ static void check_reactor() { php_swoole_check_reactor(); - if (!swReactor_isset_handler(SwooleG.main_reactor, SW_FD_USER)) + if (!swReactor_isset_handler(SwooleTG.reactor, SW_FD_USER)) { - swReactor_set_handler(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_READ, php_swoole_event_onRead); - swReactor_set_handler(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_WRITE, php_swoole_event_onWrite); - swReactor_set_handler(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_ERROR, php_swoole_event_onError); + swReactor_set_handler(SwooleTG.reactor, SW_FD_USER | SW_EVENT_READ, php_swoole_event_onRead); + swReactor_set_handler(SwooleTG.reactor, SW_FD_USER | SW_EVENT_WRITE, php_swoole_event_onWrite); + swReactor_set_handler(SwooleTG.reactor, SW_FD_USER | SW_EVENT_ERROR, php_swoole_event_onError); } } @@ -532,7 +532,7 @@ static PHP_FUNCTION(swoole_event_add) RETURN_FALSE; } - swSocket *socket = swReactor_get(SwooleG.main_reactor, socket_fd); + swSocket *socket = swReactor_get(SwooleTG.reactor, socket_fd); socket->object = peo; socket->nonblock = 1; socket->fdtype = SW_FD_USER; @@ -577,7 +577,7 @@ static PHP_FUNCTION(swoole_event_write) static PHP_FUNCTION(swoole_event_set) { - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { php_swoole_fatal_error(E_WARNING, "reactor is not ready, cannot call swoole_event_set"); RETURN_FALSE; @@ -605,7 +605,7 @@ static PHP_FUNCTION(swoole_event_set) RETURN_FALSE; } - swSocket *socket = swReactor_get(SwooleG.main_reactor, socket_fd); + swSocket *socket = swReactor_get(SwooleTG.reactor, socket_fd); if (socket->fdtype != SW_FD_USER || socket->removed) { php_swoole_fatal_error(E_WARNING, "socket[%d] is not found in the reactor", socket_fd); @@ -653,7 +653,7 @@ static PHP_FUNCTION(swoole_event_del) { zval *zfd; - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { php_swoole_fatal_error(E_WARNING, "reactor is not ready, cannot call swoole_event_del"); RETURN_FALSE; @@ -671,7 +671,7 @@ static PHP_FUNCTION(swoole_event_del) RETURN_FALSE; } - swSocket *socket = swReactor_get(SwooleG.main_reactor, socket_fd); + swSocket *socket = swReactor_get(SwooleTG.reactor, socket_fd); if (socket->object) { swoole_event_defer(php_event_object_free, socket->object); @@ -692,14 +692,14 @@ static PHP_FUNCTION(swoole_event_defer) php_swoole_check_reactor(); sw_zend_fci_cache_persist(fci_cache); - SwooleG.main_reactor->defer(SwooleG.main_reactor, php_swoole_event_onDefer, fci_cache); + SwooleTG.reactor->defer(SwooleTG.reactor, php_swoole_event_onDefer, fci_cache); RETURN_TRUE; } static PHP_FUNCTION(swoole_event_cycle) { - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { php_swoole_fatal_error(E_WARNING, "reactor is not ready, cannot call %s", ZSTR_VAL(swoole_event_ce->name)); RETURN_FALSE; @@ -717,15 +717,15 @@ static PHP_FUNCTION(swoole_event_cycle) if (_fci.size == 0) { - if (SwooleG.main_reactor->idle_task.callback == NULL) + if (SwooleTG.reactor->idle_task.callback == NULL) { RETURN_FALSE; } else { - SwooleG.main_reactor->defer(SwooleG.main_reactor, sw_zend_fci_cache_free, SwooleG.main_reactor->idle_task.data); - SwooleG.main_reactor->idle_task.callback = NULL; - SwooleG.main_reactor->idle_task.data = NULL; + SwooleTG.reactor->defer(SwooleTG.reactor, sw_zend_fci_cache_free, SwooleTG.reactor->idle_task.data); + SwooleTG.reactor->idle_task.callback = NULL; + SwooleTG.reactor->idle_task.data = NULL; RETURN_TRUE; } } @@ -737,25 +737,25 @@ static PHP_FUNCTION(swoole_event_cycle) if (!before) { - if (SwooleG.main_reactor->idle_task.data != NULL) + if (SwooleTG.reactor->idle_task.data != NULL) { - SwooleG.main_reactor->defer(SwooleG.main_reactor, sw_zend_fci_cache_free, SwooleG.main_reactor->idle_task.data); + SwooleTG.reactor->defer(SwooleTG.reactor, sw_zend_fci_cache_free, SwooleTG.reactor->idle_task.data); } - SwooleG.main_reactor->idle_task.callback = php_swoole_event_onEndCallback; - SwooleG.main_reactor->idle_task.data = fci_cache; + SwooleTG.reactor->idle_task.callback = php_swoole_event_onEndCallback; + SwooleTG.reactor->idle_task.data = fci_cache; } else { - if (SwooleG.main_reactor->future_task.data != NULL) + if (SwooleTG.reactor->future_task.data != NULL) { - SwooleG.main_reactor->defer(SwooleG.main_reactor, sw_zend_fci_cache_free, SwooleG.main_reactor->future_task.data); + SwooleTG.reactor->defer(SwooleTG.reactor, sw_zend_fci_cache_free, SwooleTG.reactor->future_task.data); } - SwooleG.main_reactor->future_task.callback = php_swoole_event_onEndCallback; - SwooleG.main_reactor->future_task.data = fci_cache; + SwooleTG.reactor->future_task.callback = php_swoole_event_onEndCallback; + SwooleTG.reactor->future_task.data = fci_cache; //Registration onBegin callback function - swReactor_activate_future_task(SwooleG.main_reactor); + swReactor_activate_future_task(SwooleTG.reactor); } RETURN_TRUE; @@ -768,7 +768,7 @@ static PHP_FUNCTION(swoole_event_exit) static PHP_FUNCTION(swoole_event_wait) { - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { return; } @@ -787,32 +787,32 @@ static PHP_FUNCTION(swoole_event_rshutdown) static PHP_FUNCTION(swoole_event_dispatch) { - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { RETURN_FALSE; } - SwooleG.main_reactor->once = 1; + SwooleTG.reactor->once = 1; #ifdef HAVE_SIGNALFD - if (SwooleG.main_reactor->check_signalfd) + if (SwooleTG.reactor->check_signalfd) { - swSignalfd_setup(SwooleG.main_reactor); + swSignalfd_setup(SwooleTG.reactor); } #endif - int ret = SwooleG.main_reactor->wait(SwooleG.main_reactor, NULL); + int ret = SwooleTG.reactor->wait(SwooleTG.reactor, NULL); if (ret < 0) { php_swoole_sys_error(E_ERROR, "reactor wait failed"); } - SwooleG.main_reactor->once = 0; + SwooleTG.reactor->once = 0; RETURN_TRUE; } static PHP_FUNCTION(swoole_event_isset) { - if (!SwooleG.main_reactor) + if (!SwooleTG.reactor) { RETURN_FALSE; } @@ -832,7 +832,7 @@ static PHP_FUNCTION(swoole_event_isset) RETURN_FALSE; } - swSocket *_socket = swReactor_get(SwooleG.main_reactor, socket_fd); + swSocket *_socket = swReactor_get(SwooleTG.reactor, socket_fd); if (_socket == NULL || _socket->removed) { RETURN_FALSE; diff --git a/swoole_process.cc b/swoole_process.cc index c86b50365..b881052d8 100644 --- a/swoole_process.cc +++ b/swoole_process.cc @@ -543,8 +543,8 @@ static PHP_METHOD(swoole_process, signal) { swSignal_add(signo, NULL); signal_fci_caches[signo] = NULL; - SwooleG.main_reactor->defer(SwooleG.main_reactor, sw_zend_fci_cache_free, fci_cache); - SwooleG.main_reactor->signal_listener_num--; + SwooleTG.reactor->defer(SwooleTG.reactor, sw_zend_fci_cache_free, fci_cache); + SwooleTG.reactor->signal_listener_num--; RETURN_TRUE; } else @@ -574,16 +574,16 @@ static PHP_METHOD(swoole_process, signal) } // for swSignalfd_setup - SwooleG.main_reactor->check_signalfd = 1; + SwooleTG.reactor->check_signalfd = 1; if (signal_fci_caches[signo]) { // free the old fci_cache - SwooleG.main_reactor->defer(SwooleG.main_reactor, sw_zend_fci_cache_free, signal_fci_caches[signo]); + SwooleTG.reactor->defer(SwooleTG.reactor, sw_zend_fci_cache_free, signal_fci_caches[signo]); } else { - SwooleG.main_reactor->signal_listener_num++; + SwooleTG.reactor->signal_listener_num++; } signal_fci_caches[signo] = fci_cache; @@ -845,9 +845,9 @@ static PHP_METHOD(swoole_process, write) int ret; //async write - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - swSocket *_socket = swReactor_get(SwooleG.main_reactor, process->pipe); + swSocket *_socket = swReactor_get(SwooleTG.reactor, process->pipe); if (_socket && _socket->nonblock) { ret = swoole_event_write(process->pipe, data, (size_t) data_len); @@ -1229,9 +1229,9 @@ static PHP_METHOD(swoole_process, setBlocking) { swSocket_set_nonblock(process->pipe); } - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { - swSocket *_socket = swReactor_get(SwooleG.main_reactor, process->pipe); + swSocket *_socket = swReactor_get(SwooleTG.reactor, process->pipe); if (_socket) { _socket->nonblock = blocking ? 0 : 1; diff --git a/swoole_process_pool.cc b/swoole_process_pool.cc index 413701780..079551a5e 100644 --- a/swoole_process_pool.cc +++ b/swoole_process_pool.cc @@ -453,7 +453,7 @@ static PHP_METHOD(swoole_process_pool, start) RETURN_FALSE; } - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { swoole_event_free(); } diff --git a/swoole_redis_coro.cc b/swoole_redis_coro.cc index 4ce16d8fa..64ddd22b3 100644 --- a/swoole_redis_coro.cc +++ b/swoole_redis_coro.cc @@ -941,9 +941,9 @@ static sw_inline swRedisClient* swoole_get_redis_client(zval *zobject) static sw_inline Socket* swoole_redis_coro_get_socket(redisContext *context) { - if (context->fd > 0 && SwooleG.main_reactor) + if (context->fd > 0 && SwooleTG.reactor) { - swSocket *conn = swReactor_get(SwooleG.main_reactor, context->fd); + swSocket *conn = swReactor_get(SwooleTG.reactor, context->fd); return conn ? (Socket *) conn->object : nullptr; } return nullptr; diff --git a/swoole_server.cc b/swoole_server.cc index d15d17537..c5f6778e4 100644 --- a/swoole_server.cc +++ b/swoole_server.cc @@ -1926,7 +1926,7 @@ static PHP_METHOD(swoole_server, __construct) RETURN_FALSE; } - if (SwooleG.main_reactor) + if (SwooleTG.reactor) { zend_throw_exception_ex(swoole_exception_ce, -2, "eventLoop has already been created. unable to create %s", SW_Z_OBJCE_NAME_VAL_P(zserv)); RETURN_FALSE; @@ -4052,9 +4052,9 @@ static PHP_METHOD(swoole_server, stop) if (worker_id == SwooleWG.id && wait_reactor == 0) { - if (SwooleG.main_reactor != NULL) + if (SwooleTG.reactor != NULL) { - SwooleG.main_reactor->running = 0; + SwooleTG.reactor->running = 0; } SwooleG.running = 0; }