Skip to content

Commit

Permalink
swoole thread
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 19, 2024
1 parent 9bc606b commit 908644c
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 36 deletions.
9 changes: 9 additions & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ PHP_ARG_ENABLE([thread-context],
[whether to enable thread context],
[AS_HELP_STRING([--enable-thread-context],
[Use thread context])], [no], [no])

PHP_ARG_ENABLE([swoole-thread],
[whether to enable swoole thread support],
[AS_HELP_STRING([--enable-swoole-thread],
[Enable swoole thread support])], [no], [no])

PHP_ARG_ENABLE([swoole-coro-time],
[whether to enable coroutine execution time ],
Expand Down Expand Up @@ -877,6 +882,10 @@ EOF
AC_DEFINE(SW_LOG_TRACE_OPEN, 1, [enable trace log])
fi

if test "$PHP_SWOOLE_THREAD" != "no"; then
AC_DEFINE(SW_THREAD, 1, [enable swoole thread support])
fi

if test "$PHP_SOCKETS" = "yes"; then
AC_MSG_CHECKING([for php_sockets.h])

Expand Down
10 changes: 10 additions & 0 deletions examples/thread/co.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
$t1 = Swoole\Thread::run('mt.php', ['thread-1'], PHP_OS);
$t2 = Swoole\Thread::run('mt.php', ['thread-2'], PHP_OS);

var_dump($t1->id);
var_dump($t2->id);

$t1->join();
$t2->join();

13 changes: 13 additions & 0 deletions examples/thread/mt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
echo "begin\n";
var_dump(Swoole\Thread::getId());
$args = Swoole\Thread::getArguments();
var_dump($args);

if ($args[0] == 'thread-2') {
$t3 = Swoole\Thread::run('mt.php', ['thread-3'], PHP_OS);
$t3->join();
}

sleep(5);
echo "end\n";
10 changes: 9 additions & 1 deletion ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,16 @@ PHP_MINIT_FUNCTION(swoole) {
#ifdef SW_USE_ODBC
php_swoole_odbc_minit(module_number);
#endif

#ifdef SW_USE_ORACLE
php_swoole_oracle_minit(module_number);
#endif

#ifdef SW_USE_SQLITE
php_swoole_sqlite_minit(module_number);
#endif
#ifdef ZTS
php_swoole_thread_minit(module_number);
#endif

SwooleG.fatal_error = fatal_error;
Socket::default_buffer_size = SWOOLE_G(socket_buffer_size);
Expand Down Expand Up @@ -827,6 +829,9 @@ PHP_MINFO_FUNCTION(swoole) {
#ifdef HAVE_KQUEUE
php_info_print_table_row(2, "kqueue", "enabled");
#endif
#ifdef ZTS
php_info_print_table_row(2, "thread", "enabled");
#endif
#ifdef HAVE_SIGNALFD
php_info_print_table_row(2, "signalfd", "enabled");
#endif
Expand Down Expand Up @@ -1055,6 +1060,9 @@ PHP_RSHUTDOWN_FUNCTION(swoole) {
php_swoole_coroutine_scheduler_rshutdown();
php_swoole_runtime_rshutdown();
php_swoole_process_rshutdown();
#ifdef ZTS
php_swoole_thread_rshutdown();
#endif

SwooleG.running = 0;
SWOOLE_G(req_status) = PHP_SWOOLE_RSHUTDOWN_END;
Expand Down
12 changes: 6 additions & 6 deletions ext-src/php_swoole_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class PHPCoroutine {
bool enable_deadlock_check;
};

static thread_local zend_array *options;
static SW_THREAD_LOCAL zend_array *options;

enum HookType {
HOOK_NONE = 0,
Expand Down Expand Up @@ -261,12 +261,12 @@ class PHPCoroutine {
}

protected:
static thread_local bool activated;
static thread_local PHPContext main_context;
static thread_local Config config;
static SW_THREAD_LOCAL bool activated;
static SW_THREAD_LOCAL PHPContext main_context;
static SW_THREAD_LOCAL Config config;

static thread_local bool interrupt_thread_running;
static thread_local std::thread interrupt_thread;
static SW_THREAD_LOCAL bool interrupt_thread_running;
static SW_THREAD_LOCAL std::thread interrupt_thread;

static void activate();
static void deactivate(void *ptr);
Expand Down
10 changes: 10 additions & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ extern PHPAPI int php_array_merge(zend_array *dest, zend_array *src);
#error "only linux support iouring"
#endif

#if defined(SW_THREAD) && !defined(ZTS)
#error "swoole thread must be used with ZTS"
#endif

//--------------------------------------------------------
#define SW_MAX_FIND_COUNT 100 // for swoole_server::connection_list
#define SW_PHP_CLIENT_BUFFER_SIZE 65535
Expand Down Expand Up @@ -266,6 +270,9 @@ void php_swoole_http_server_coro_minit(int module_number);
void php_swoole_websocket_server_minit(int module_number);
void php_swoole_redis_server_minit(int module_number);
void php_swoole_name_resolver_minit(int module_number);
#ifdef ZTS
void php_swoole_thread_minit(int module_number);
#endif

/**
* RINIT
Expand All @@ -290,6 +297,9 @@ void php_swoole_process_rshutdown();
void php_swoole_coroutine_scheduler_rshutdown();
void php_swoole_runtime_rshutdown();
void php_swoole_server_rshutdown();
#ifdef ZTS
void php_swoole_thread_rshutdown();
#endif

int php_swoole_reactor_init();
void php_swoole_set_global_option(zend_array *vht);
Expand Down
12 changes: 12 additions & 0 deletions ext-src/stubs/php_swoole_thread.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Swoole {
class Thread {
public int $id;
private function __construct() {}

public function join(): bool {}
public static function run(string $script_file, mixed ...$args): Thread {}
public static function getArguments(): array {}
public static function getId(): int {}
}
}
19 changes: 19 additions & 0 deletions ext-src/stubs/php_swoole_thread_arginfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 557f0cd02152d61e4b5b697d3c95e3ae9bf494f0 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread___construct, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_join, 0, 0, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Thread_run, 0, 1, Swoole\\Thread, 0)
ZEND_ARG_TYPE_INFO(0, script_file, IS_STRING, 0)
ZEND_ARG_VARIADIC_TYPE_INFO(0, args, IS_MIXED, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getArguments, 0, 0, IS_ARRAY, 0)
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()
12 changes: 6 additions & 6 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ static zend_always_inline zend_vm_stack zend_vm_stack_new_page(size_t size, zend

enum sw_exit_flags { SW_EXIT_IN_COROUTINE = 1 << 1, SW_EXIT_IN_SERVER = 1 << 2 };

thread_local bool PHPCoroutine::activated = false;
thread_local zend_array *PHPCoroutine::options = nullptr;
SW_THREAD_LOCAL bool PHPCoroutine::activated = false;
SW_THREAD_LOCAL zend_array *PHPCoroutine::options = nullptr;

thread_local PHPCoroutine::Config PHPCoroutine::config{
SW_THREAD_LOCAL PHPCoroutine::Config PHPCoroutine::config{
SW_DEFAULT_MAX_CORO_NUM,
0,
false,
true,
};

thread_local PHPContext PHPCoroutine::main_context{};
thread_local std::thread PHPCoroutine::interrupt_thread;
thread_local bool PHPCoroutine::interrupt_thread_running = false;
SW_THREAD_LOCAL PHPContext PHPCoroutine::main_context{};
SW_THREAD_LOCAL std::thread PHPCoroutine::interrupt_thread;
SW_THREAD_LOCAL bool PHPCoroutine::interrupt_thread_running = false;

extern void php_swoole_load_library();

Expand Down
4 changes: 2 additions & 2 deletions ext-src/swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ static zend_internal_arg_info *get_arginfo(const char *name, size_t l_name) {
#define SW_HOOK_LIBRARY_FE(name, arg_info) \
ZEND_RAW_FENTRY("swoole_hook_" #name, PHP_FN(swoole_user_func_handler), arg_info, 0)

static thread_local zend_array *tmp_function_table = nullptr;
static thread_local std::unordered_map<std::string, zend_class_entry *> child_class_entries;
static SW_THREAD_LOCAL zend_array *tmp_function_table = nullptr;
static SW_THREAD_LOCAL std::unordered_map<std::string, zend_class_entry *> child_class_entries;

SW_EXTERN_C_BEGIN
#include "ext/standard/file.h"
Expand Down
Loading

0 comments on commit 908644c

Please sign in to comment.