Skip to content

Commit

Permalink
optimize code, add more methods for thread
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Mar 20, 2024
1 parent 7c3cb35 commit c190fc8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/thread/co.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
$t1 = Swoole\Thread::run('mt.php', ['thread-1'], PHP_OS);
$t2 = Swoole\Thread::run('mt.php', ['thread-2'], PHP_OS);
$t1 = Swoole\Thread::exec('mt.php', 'thread-1', PHP_OS);
$t2 = Swoole\Thread::exec('mt.php', 'thread-2', PHP_OS);

var_dump($t1->id);
var_dump($t2->id);
Expand Down
4 changes: 3 additions & 1 deletion examples/thread/mt.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
echo "begin\n";
$GLOBALS['uuid'] = uniqid();
var_dump(Swoole\Thread::getId());
$args = Swoole\Thread::getArguments();
var_dump($args);
var_dump($GLOBALS['uuid']);

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

Expand Down
5 changes: 4 additions & 1 deletion ext-src/stubs/php_swoole_thread.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class Thread {
private function __construct() {}

public function join(): bool {}
public static function run(string $script_file, mixed ...$args): Thread {}
public function joinable(): bool {}
public function detach(): bool {}

public static function exec(string $script_file, mixed ...$args): Thread {}
public static function getArguments(): array {}
public static function getId(): int {}
}
Expand Down
8 changes: 6 additions & 2 deletions ext-src/stubs/php_swoole_thread_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 557f0cd02152d61e4b5b697d3c95e3ae9bf494f0 */
* Stub hash: d728269b4bc73add11a02b17484d4cdb1d5cb368 */

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)
#define arginfo_class_Swoole_Thread_joinable arginfo_class_Swoole_Thread_join

#define arginfo_class_Swoole_Thread_detach arginfo_class_Swoole_Thread_join

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_Swoole_Thread_exec, 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()
Expand Down
45 changes: 36 additions & 9 deletions ext-src/swoole_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "php_swoole_cxx.h"

#ifdef SW_THREAD

#include <sys/ipc.h>
#include <sys/resource.h>

Expand Down Expand Up @@ -66,7 +68,9 @@ static zend_object *php_swoole_thread_create_object(zend_class_entry *ce) {
SW_EXTERN_C_BEGIN
static PHP_METHOD(swoole_thread, __construct);
static PHP_METHOD(swoole_thread, join);
static PHP_METHOD(swoole_thread, run);
static PHP_METHOD(swoole_thread, joinable);
static PHP_METHOD(swoole_thread, detach);
static PHP_METHOD(swoole_thread, exec);
static PHP_METHOD(swoole_thread, getArguments);
static PHP_METHOD(swoole_thread, getId);
SW_EXTERN_C_END
Expand All @@ -76,7 +80,9 @@ static const zend_function_entry swoole_thread_methods[] =
{
PHP_ME(swoole_thread, __construct, arginfo_class_Swoole_Thread___construct, ZEND_ACC_PUBLIC)
PHP_ME(swoole_thread, join, arginfo_class_Swoole_Thread_join, ZEND_ACC_PUBLIC)
PHP_ME(swoole_thread, run, arginfo_class_Swoole_Thread_run, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_thread, joinable, arginfo_class_Swoole_Thread_joinable, ZEND_ACC_PUBLIC)
PHP_ME(swoole_thread, detach, arginfo_class_Swoole_Thread_detach, ZEND_ACC_PUBLIC)
PHP_ME(swoole_thread, exec, arginfo_class_Swoole_Thread_exec, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_thread, getArguments, arginfo_class_Swoole_Thread_getArguments, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_thread, getId, arginfo_class_Swoole_Thread_getId, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
Expand All @@ -98,13 +104,32 @@ static PHP_METHOD(swoole_thread, __construct) {}

static PHP_METHOD(swoole_thread, join) {
ThreadObject *to = php_swoole_thread_fetch_object(Z_OBJ_P(ZEND_THIS));
if (to == nullptr || !to->thread->joinable()) {
if (!to || !to->thread || !to->thread->joinable()) {
RETURN_FALSE;
}
php_swoole_thread_join(Z_OBJ_P(ZEND_THIS));
RETURN_TRUE;
}

static PHP_METHOD(swoole_thread, joinable) {
ThreadObject *to = php_swoole_thread_fetch_object(Z_OBJ_P(ZEND_THIS));
if (to == nullptr || !to->thread) {
RETURN_FALSE;
}
RETURN_BOOL(to->thread->joinable());
}

static PHP_METHOD(swoole_thread, detach) {
ThreadObject *to = php_swoole_thread_fetch_object(Z_OBJ_P(ZEND_THIS));
if (to == nullptr || !to->thread) {
RETURN_FALSE;
}
to->thread->detach();
delete to->thread;
to->thread = nullptr;
RETURN_TRUE;
}

static PHP_METHOD(swoole_thread, getArguments) {
RETURN_ZVAL(&thread_argv, 1, 0);
}
Expand Down Expand Up @@ -182,25 +207,25 @@ void php_swoole_thread_start(const std::string &file, const std::string &argv) {
ts_free_thread();
}

static PHP_METHOD(swoole_thread, run) {
char *execfile = nullptr;
size_t execfile_len = 0;
static PHP_METHOD(swoole_thread, exec) {
char *script_file = nullptr;
size_t l_script_file = 0;
zval *args;
int argc;

ZEND_PARSE_PARAMETERS_START(1, -1)
Z_PARAM_STRING(execfile, execfile_len)
Z_PARAM_STRING(script_file, l_script_file)
Z_PARAM_VARIADIC('+', args, argc)
ZEND_PARSE_PARAMETERS_END();

if (execfile_len < 1) {
if (l_script_file < 1) {
php_swoole_fatal_error(E_WARNING, "exec file name is empty");
RETURN_FALSE;
}

object_init_ex(return_value, swoole_thread_ce);
ThreadObject *to = php_swoole_thread_fetch_object(Z_OBJ_P(return_value));
std::string file(execfile, execfile_len);
std::string file(script_file, l_script_file);

zval zargv;
array_init(&zargv);
Expand All @@ -214,3 +239,5 @@ static PHP_METHOD(swoole_thread, run) {
zend_update_property_long(
swoole_thread_ce, SW_Z8_OBJ_P(return_value), ZEND_STRL("id"), to->thread->native_handle());
}

#endif

0 comments on commit c190fc8

Please sign in to comment.