Skip to content

Commit

Permalink
optimize code [2]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Aug 13, 2024
1 parent 146ad6f commit c0957fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
24 changes: 12 additions & 12 deletions ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ static PHP_METHOD(swoole_server, set) {
ServerObject *server_object = server_fetch_object(Z_OBJ_P(ZEND_THIS));
Server *serv = php_swoole_server_get_and_check_server(ZEND_THIS);
if (serv->is_worker_thread()) {
swoole_set_last_error(SW_ERROR_OPERATION_NOT_SUPPORT);
swoole_set_last_error(SW_ERROR_SERVER_UNRELATED_THREAD);
RETURN_FALSE;
}
if (serv->is_started()) {
Expand Down Expand Up @@ -2524,6 +2524,8 @@ static PHP_METHOD(swoole_server, addProcess) {
RETURN_FALSE;
}

int worker_id;
Worker *worker;
zval *process = nullptr;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &process) == FAILURE) {
Expand All @@ -2549,29 +2551,27 @@ static PHP_METHOD(swoole_server, addProcess) {
process = tmp_process;
Z_TRY_ADDREF_P(process);

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

if (serv->is_worker_thread()) {
if (!serv->is_user_worker()) {
swoole_set_last_error(SW_ERROR_OPERATION_NOT_SUPPORT);
swoole_set_last_error(SW_ERROR_SERVER_UNRELATED_THREAD);
RETURN_FALSE;
}
worker_id = swoole_get_process_id();
Worker *worker = serv->get_worker(worker_id);
worker->ptr = process;
worker = serv->get_worker(worker_id);
worker_id -= (serv->worker_num + serv->task_worker_num);
} 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 = php_swoole_process_get_and_check_worker(process);
worker_id = serv->add_worker(worker);
if (worker_id < 0) {
php_swoole_fatal_error(E_WARNING, "Server::add_worker() failed");
php_swoole_fatal_error(E_WARNING, "failed to add worker");
RETURN_FALSE;
}
}

worker->ptr = process;
zend_update_property_long(swoole_process_ce, SW_Z8_OBJ_P(process), ZEND_STRL("id"), worker_id);
RETURN_LONG(worker_id);
}
Expand Down
1 change: 1 addition & 0 deletions include/swoole_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ enum swErrorCode {
SW_ERROR_SERVER_IS_NOT_REGULAR_FILE,
SW_ERROR_SERVER_SEND_TO_WOKER_TIMEOUT,
SW_ERROR_SERVER_INVALID_CALLBACK,
SW_ERROR_SERVER_UNRELATED_THREAD,

/**
* Process exit timeout, forced to end.
Expand Down

0 comments on commit c0957fd

Please sign in to comment.