Skip to content

Commit

Permalink
optimize code[3]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 27, 2024
1 parent b01b79f commit f245a74
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ext-src/swoole_http2_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using HttpContext = swoole::http::Context;
using Http2Stream = Http2::Stream;
using Http2Session = Http2::Session;

static std::unordered_map<SessionId, Http2Session *> http2_sessions;
static SW_THREAD_LOCAL std::unordered_map<SessionId, Http2Session *> http2_sessions;

static bool http2_server_respond(HttpContext *ctx, const String *body);
static bool http2_server_send_range_file(HttpContext *ctx, swoole::http_server::StaticHandler *handler);
Expand Down
2 changes: 1 addition & 1 deletion ext-src/swoole_redis_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Redis = swoole::redis;
zend_class_entry *swoole_redis_server_ce;
zend_object_handlers swoole_redis_server_handlers;

static std::unordered_map<std::string, zend_fcall_info_cache> redis_handlers;
static SW_THREAD_LOCAL std::unordered_map<std::string, zend_fcall_info_cache> redis_handlers;

SW_EXTERN_C_BEGIN
static PHP_METHOD(swoole_redis_server, setHandler);
Expand Down
4 changes: 0 additions & 4 deletions ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2579,10 +2579,6 @@ static PHP_METHOD(swoole_server, start) {
server_object->register_callback();
server_object->on_before_start();

#ifdef SW_THREAD
serv->worker_num = 1;
#endif

if (serv->start() < 0) {
php_swoole_fatal_error(E_ERROR, "failed to start server. Error: %s", sw_error);
}
Expand Down
2 changes: 1 addition & 1 deletion ext-src/swoole_websocket_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static zend_object_handlers swoole_websocket_frame_handlers;
static zend_class_entry *swoole_websocket_closeframe_ce;
static zend_object_handlers swoole_websocket_closeframe_handlers;

static String *swoole_websocket_buffer = nullptr;
static SW_THREAD_LOCAL String *swoole_websocket_buffer = nullptr;

SW_EXTERN_C_BEGIN
static PHP_METHOD(swoole_websocket_server, push);
Expand Down
2 changes: 1 addition & 1 deletion include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ typedef unsigned long ulong_t;

#ifdef SW_THREAD
#define SW_THREAD_LOCAL thread_local
extern std::mutex sw_thread_lock;
#else
#define SW_THREAD_LOCAL
#endif
Expand Down Expand Up @@ -779,7 +780,6 @@ double microtime(void);

extern swoole::Global SwooleG; // Local Global Variable
extern __thread swoole::ThreadGlobal SwooleTG; // Thread Global Variable
extern std::mutex sw_thread_lock;

#define SW_CPU_NUM (SwooleG.cpu_num)

Expand Down
15 changes: 8 additions & 7 deletions src/server/master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "swoole_server.h"
#include "swoole_memory.h"
#include "swoole_lock.h"
#include "swoole_util.h"

#include <assert.h>
Expand Down Expand Up @@ -699,10 +700,10 @@ int Server::start() {
file_put_contents(pid_file, sw_tg_buffer()->str, n);
}
int ret;
if (is_process_mode()) {
ret = start_reactor_threads();
} else {
if (is_base_mode()) {
ret = start_reactor_processes();
} else {
ret = start_reactor_threads();
}
// failed to start
if (ret < 0) {
Expand Down Expand Up @@ -852,12 +853,12 @@ int Server::create() {
}

int retval;
if (is_process_mode()) {
factory = new ProcessFactory(this);
retval = create_reactor_threads();
} else {
if (is_base_mode()) {
factory = new BaseFactory(this);
retval = create_reactor_processes();
} else {
factory = new ProcessFactory(this);
retval = create_reactor_threads();
}

#ifdef HAVE_PTHREAD_BARRIER
Expand Down

0 comments on commit f245a74

Please sign in to comment.