Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 13, 2024
1 parent c75a5f7 commit 146ad6f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
8 changes: 4 additions & 4 deletions examples/thread/thread_server.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
echo "[worker#" . $http->getWorkerId() . "]\treceived pipe message[$msg] from " . $srcWorkerId . "\n";
});

//$http->addProcess(new \Swoole\Process(function () {
// echo "user process, id=" . \Swoole\Thread::getId() . "\n";
// sleep(2);
//}));
$http->addProcess(new \Swoole\Process(function () {
echo "user process, id=" . \Swoole\Thread::getId() . "\n";
sleep(2);
}));

$http->on('Task', function ($server, $taskId, $srcWorkerId, $data) {
var_dump($taskId, $srcWorkerId, $data);
Expand Down
33 changes: 22 additions & 11 deletions ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2547,22 +2547,33 @@ static PHP_METHOD(swoole_server, addProcess) {
zval *tmp_process = (zval *) emalloc(sizeof(zval));
memcpy(tmp_process, process, sizeof(zval));
process = tmp_process;
Z_TRY_ADDREF_P(process);

ServerObject *server_object = server_fetch_object(Z_OBJ_P(ZEND_THIS));
server_object->property->user_processes.push_back(process);
int worker_id;

Z_TRY_ADDREF_P(process);
if (serv->is_worker_thread()) {
if (!serv->is_user_worker()) {
swoole_set_last_error(SW_ERROR_OPERATION_NOT_SUPPORT);
RETURN_FALSE;
}
worker_id = swoole_get_process_id();
Worker *worker = serv->get_worker(worker_id);
worker->ptr = process;
} else {
ServerObject *server_object = server_fetch_object(Z_OBJ_P(ZEND_THIS));
server_object->property->user_processes.push_back(process);

Worker *worker = php_swoole_process_get_and_check_worker(process);
worker->ptr = process;
Worker *worker = php_swoole_process_get_and_check_worker(process);
worker->ptr = process;

int id = serv->add_worker(worker);
if (id < 0) {
php_swoole_fatal_error(E_WARNING, "Server::add_worker() failed");
RETURN_FALSE;
worker_id = serv->add_worker(worker);
if (worker_id < 0) {
php_swoole_fatal_error(E_WARNING, "Server::add_worker() failed");
RETURN_FALSE;
}
}
zend_update_property_long(swoole_process_ce, SW_Z8_OBJ_P(process), ZEND_STRL("id"), id);
RETURN_LONG(id);
zend_update_property_long(swoole_process_ce, SW_Z8_OBJ_P(process), ZEND_STRL("id"), worker_id);
RETURN_LONG(worker_id);
}

static PHP_METHOD(swoole_server, addCommand) {
Expand Down
14 changes: 12 additions & 2 deletions include/swoole_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,12 @@ class Server {
EventData *task_result = nullptr;

/**
* user process
* Used for process management, saving the mapping relationship between PID and worker pointers
*/
std::vector<Worker *> user_worker_list;
std::unordered_map<pid_t, Worker *> user_worker_map;
/**
* Shared memory, sharing state between processes
*/
Worker *user_workers = nullptr;

std::unordered_map<std::string, Command> commands;
Expand Down Expand Up @@ -1195,6 +1197,10 @@ class Server {
return swoole_get_thread_type() == Server::THREAD_REACTOR;
}

bool is_single_worker() {
return (worker_num == 1 && task_worker_num == 0 && max_request == 0 && get_user_worker_num() == 0);
}

bool isset_hook(enum HookType type) {
assert(type <= HOOK_END);
return hooks[type];
Expand Down Expand Up @@ -1478,6 +1484,10 @@ class Server {
*/
uint16_t reactor_pipe_num = 0;
ReactorThread *reactor_threads = nullptr;
/**
* Only used for temporarily saving pointers in add_worker()
*/
std::vector<Worker *> user_worker_list;

int start_check();
void check_port_type(ListenPort *ls);
Expand Down
1 change: 1 addition & 0 deletions src/server/master.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,7 @@ void Server::timer_callback(Timer *timer, TimerNode *tnode) {

int Server::add_worker(Worker *worker) {
user_worker_list.push_back(worker);
worker->id = user_worker_list.size() - 1;
return worker->id;
}

Expand Down
7 changes: 1 addition & 6 deletions src/server/reactor_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ static int ReactorProcess_onPipeRead(Reactor *reactor, Event *event);
static int ReactorProcess_onClose(Reactor *reactor, Event *event);
static void ReactorProcess_onTimeout(Timer *timer, TimerNode *tnode);

static bool Server_is_single(Server *serv) {
return (serv->worker_num == 1 && serv->task_worker_num == 0 && serv->max_request == 0 &&
serv->user_worker_list.empty());
}

int Server::start_reactor_processes() {
single_thread = 1;

Expand Down Expand Up @@ -81,7 +76,7 @@ int Server::start_reactor_processes() {
return SW_ERR;
}

if (Server_is_single(this)) {
if (is_single_worker()) {
Worker *worker = &gs->event_workers.workers[0];
SwooleWG.worker = worker;
int retval = worker_main_loop(&gs->event_workers, worker);
Expand Down
2 changes: 1 addition & 1 deletion src/server/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ void ThreadFactory::spawn_task_worker(WorkerId i) {

void ThreadFactory::spawn_user_worker(WorkerId i) {
create_thread(i, [=]() {
Worker *worker = server_->user_worker_list.at(i - server_->task_worker_num - server_->worker_num);
Worker *worker = server_->get_worker(i);
swoole_set_process_type(SW_PROCESS_USERWORKER);
swoole_set_thread_type(Server::THREAD_WORKER);
swoole_set_process_id(i);
Expand Down

0 comments on commit 146ad6f

Please sign in to comment.