Skip to content

Commit

Permalink
Optimize thread callback (#5515)
Browse files Browse the repository at this point in the history
* Optimize thread callback

* clean_worker_connections

* fix

* fix core tests, --filter=[core]

* fix tests, --filter=[unit]

* fix

* fix 2
  • Loading branch information
matyhtf authored Oct 16, 2024
1 parent 24ceae1 commit 6957d64
Show file tree
Hide file tree
Showing 22 changed files with 422 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/thread.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
pwd
echo "`git log -20 --pretty --oneline`"
echo "`git log -10 --stat --pretty --oneline`"
- name: Run Swoole test
- name: Run tests
run: |
export SWOOLE_BRANCH=${GITHUB_REF##*/}
export SWOOLE_THREAD=1
Expand Down
1 change: 0 additions & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,6 @@ EOF
thirdparty/multipart_parser.c"

if test "$PHP_NGHTTP2_DIR" = "no"; then
PHP_ADD_INCLUDE([$ext_srcdir/thirdparty])
swoole_source_file="$swoole_source_file \
thirdparty/nghttp2/nghttp2_hd.c \
thirdparty/nghttp2/nghttp2_rcbuf.c \
Expand Down
21 changes: 21 additions & 0 deletions examples/thread/exit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Swoole\Thread;
use Swoole\Thread\Lock;

$args = Thread::getArguments();

if (empty($args)) {
$lock = new Lock;
$lock->lock();
$thread = new Thread(__FILE__, $lock);
echo "main thread\n";
$lock->unlock();
$thread->join();
var_dump($thread->getExitStatus());
} else {
$lock = $args[0];
$lock->lock();
sleep(1);
exit(234);
}
7 changes: 6 additions & 1 deletion ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ PHP_RINIT_FUNCTION(swoole) {
* This would cause php_swoole_load_library function not to execute correctly, so it must be replaced
* with the execute_ex function.
*/
void (*old_zend_execute_ex)(zend_execute_data *execute_data) = nullptr;
void (*old_zend_execute_ex)(zend_execute_data * execute_data) = nullptr;
if (UNEXPECTED(zend_execute_ex != execute_ex)) {
old_zend_execute_ex = zend_execute_ex;
zend_execute_ex = execute_ex;
Expand Down Expand Up @@ -1508,7 +1508,12 @@ static PHP_FUNCTION(swoole_test_fn) {
swoole_fatal_error(SW_ERROR_FOR_TEST, "test");
php_printf("never be executed here\n");
} else if (SW_STRCASEEQ(test_case, test_case_len, "bailout")) {
EG(exit_status) = 95;
#ifdef SW_THREAD
php_swoole_thread_bailout();
#else
zend_bailout();
#endif
} else if (SW_STRCASEEQ(test_case, test_case_len, "abort")) {
abort();
}
Expand Down
1 change: 0 additions & 1 deletion ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ zend_bool php_swoole_signal_isset_handler(int signo);
#endif

#define sw_zend7_object zend_object
#define SW_Z7_OBJ_P(object) object
#define SW_Z8_OBJ_P(zobj) Z_OBJ_P(zobj)

typedef ssize_t php_stream_size_t;
Expand Down
4 changes: 3 additions & 1 deletion ext-src/php_swoole_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extern zend_class_entry *swoole_thread_map_ce;
extern zend_class_entry *swoole_thread_queue_ce;

void php_swoole_thread_start(zend_string *file, ZendArray *argv);
void php_swoole_thread_join(pthread_t ptid);
int php_swoole_thread_get_exit_status(pthread_t ptid);
zend_string *php_swoole_serialize(zval *zdata);
bool php_swoole_unserialize(zend_string *data, zval *zv);
void php_swoole_thread_bailout(void);
Expand Down Expand Up @@ -255,7 +257,7 @@ class ZendArray : public ThreadResource {

void keys(zval *return_value);
void values(zval *return_value);
void toArray(zval *return_value);
void to_array(zval *return_value);
void find(zval *search, zval *return_value);

void intkey_offsetGet(zend_long index, zval *return_value) {
Expand Down
1 change: 1 addition & 0 deletions ext-src/stubs/php_swoole_thread.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public function __construct(string $script_file, mixed ...$args) {}

public function join(): bool {}
public function joinable(): bool {}
public function getExitStatus(): int {}
public function detach(): bool {}

public static function getArguments(): ?array {}
Expand Down
10 changes: 6 additions & 4 deletions ext-src/stubs/php_swoole_thread_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 51be89a17d2714b489a0d67a927b6b3e8a1f0cff */
* Stub hash: d1d1e5d35cfda110527408faf9376dda24680c7f */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread___construct, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, script_file, IS_STRING, 0)
Expand All @@ -11,13 +11,15 @@ ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_joinable arginfo_class_Swoole_Thread_join

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getExitStatus, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_detach arginfo_class_Swoole_Thread_join

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getArguments, 0, 0, IS_ARRAY, 1)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getId, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
#define arginfo_class_Swoole_Thread_getId arginfo_class_Swoole_Thread_getExitStatus

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getInfo, 0, 0, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
Expand All @@ -44,4 +46,4 @@ ZEND_END_ARG_INFO()

#define arginfo_class_Swoole_Thread_getPriority arginfo_class_Swoole_Thread_getInfo

#define arginfo_class_Swoole_Thread_getNativeId arginfo_class_Swoole_Thread_getId
#define arginfo_class_Swoole_Thread_getNativeId arginfo_class_Swoole_Thread_getExitStatus
13 changes: 8 additions & 5 deletions ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ void php_swoole_server_rshutdown() {
serv->drain_worker_pipe();

if (serv->is_started() && serv->worker_is_running() && !serv->is_user_worker()) {
SwooleWG.shutdown = true;
#ifdef SW_THREAD
if (serv->is_thread_mode()) {
serv->abort_worker(sw_worker());
if (serv->is_event_worker() && !serv->is_process_mode()) {
serv->clean_worker_connections(sw_worker());
}
#endif
if (php_swoole_is_fatal_error()) {
swoole_error_log(SW_LOG_ERROR,
SW_ERROR_PHP_FATAL_ERROR,
Expand Down Expand Up @@ -2664,6 +2661,12 @@ static PHP_METHOD(swoole_server, start) {
}
php_swoole_thread_start(bootstrap_copy, thread_argv);
};

serv->worker_thread_get_exit_status = [](pthread_t ptid) -> int {
return php_swoole_thread_get_exit_status(ptid);
};

serv->worker_thread_join = [](pthread_t ptid) { php_swoole_thread_join(ptid); };
}
#endif

Expand Down
Loading

0 comments on commit 6957d64

Please sign in to comment.