Skip to content

Commit

Permalink
Optimize runtime thread safe (#5647)
Browse files Browse the repository at this point in the history
* Optimize thread safety of runtime hooks

* Fix arginfo memory leak

* fix

* fix 4, --filter=[unit]

* fix 5, --filter=[unit]

* fix 6 --filter=[thread]
  • Loading branch information
matyhtf authored Dec 31, 2024
1 parent 1a40cd5 commit b3c5484
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 98 deletions.
24 changes: 24 additions & 0 deletions examples/thread/hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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

$args = Thread::getArguments();

if (empty($args)) {
Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
$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();
Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
sleep(1);
Swoole\Runtime::enableCoroutine(0);
exit(234);
}
12 changes: 12 additions & 0 deletions ext-src/php_swoole_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,19 @@ static inline bool sw_is_main_thread() {
#endif
}

#ifdef SW_THREAD
size_t sw_active_thread_count(void);
#else
static inline size_t sw_active_thread_count(void) {
return 1;
}
#endif

void sw_php_exit(int status);
void sw_php_print_backtrace(zend_long cid = 0,
zend_long options = 0,
zend_long limit = 0,
zval *return_value = nullptr);

//----------------------------------Constant API------------------------------------

Expand Down
33 changes: 20 additions & 13 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1399,18 +1399,7 @@ static PHP_METHOD(swoole_coroutine, getBackTrace) {
}
}

static PHP_METHOD(swoole_coroutine, printBackTrace) {
zend_long cid = 0;
zend_long options = 0;
zend_long limit = 0;

ZEND_PARSE_PARAMETERS_START(0, 3)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(cid)
Z_PARAM_LONG(options)
Z_PARAM_LONG(limit)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

void sw_php_print_backtrace(zend_long cid, zend_long options, zend_long limit, zval *return_value) {
zval argv[2];
ZVAL_LONG(&argv[0], options);
ZVAL_LONG(&argv[1], limit);
Expand All @@ -1421,7 +1410,10 @@ static PHP_METHOD(swoole_coroutine, printBackTrace) {
PHPContext *ctx = (PHPContext *) PHPCoroutine::get_context_by_cid(cid);
if (UNEXPECTED(!ctx)) {
swoole_set_last_error(SW_ERROR_CO_NOT_EXISTS);
RETURN_FALSE;
if (return_value) {
RETVAL_FALSE;
}
return;
}
zend_execute_data *ex_backup = EG(current_execute_data);
EG(current_execute_data) = ctx->execute_data;
Expand All @@ -1430,6 +1422,21 @@ static PHP_METHOD(swoole_coroutine, printBackTrace) {
}
}

static PHP_METHOD(swoole_coroutine, printBackTrace) {
zend_long cid;
zend_long options = 0;
zend_long limit = 0;

ZEND_PARSE_PARAMETERS_START(0, 3)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(cid)
Z_PARAM_LONG(options)
Z_PARAM_LONG(limit)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

sw_php_print_backtrace(cid, options, limit, return_value);
}

static PHP_METHOD(swoole_coroutine, list) {
zval zlist;
array_init(&zlist);
Expand Down
Loading

0 comments on commit b3c5484

Please sign in to comment.