Skip to content

Commit 8567b43

Browse files
committed
Added Thread::yield()/activeCount(). Optimize thread code
1 parent d204def commit 8567b43

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

ext-src/stubs/php_swoole_thread.stub.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ public function detach(): bool {}
1313
public static function getArguments(): ?array {}
1414
public static function getId(): int {}
1515
public static function getInfo(): array {}
16+
public static function activeCount(): int {}
17+
public static function yield(): void {}
1618

1719
public static function setName(string $name): bool {}
1820
#ifdef HAVE_CPU_AFFINITY
1921
public static function setAffinity(array $cpu_settings): bool {}
2022
public static function getAffinity(): array {}
2123
#endif
22-
public function setPriority(int $priority, int $policy = 0): bool {}
23-
public function getPriority(): array {}
24-
public function getNativeId(): int {}
24+
public static function setPriority(int $priority, int $policy = 0): bool {}
25+
public static function getPriority(): array {}
26+
public static function getNativeId(): int {}
2527
}
2628
}

ext-src/stubs/php_swoole_thread_arginfo.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 1a0aa05d8165c567920a234da0801c3231f38588 */
2+
* Stub hash: 46ec6a19af2d6a669e9c74651c12805626f832c8 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, script_file, IS_STRING, 0)
@@ -26,6 +26,11 @@ ZEND_END_ARG_INFO()
2626
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getInfo, 0, 0, IS_ARRAY, 0)
2727
ZEND_END_ARG_INFO()
2828

29+
#define arginfo_class_Swoole_Thread_activeCount arginfo_class_Swoole_Thread_getExitStatus
30+
31+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_yield, 0, 0, IS_VOID, 0)
32+
ZEND_END_ARG_INFO()
33+
2934
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_setName, 0, 1, _IS_BOOL, 0)
3035
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
3136
ZEND_END_ARG_INFO()

ext-src/swoole_thread.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static void thread_join(zend_object *object) {
7979
ThreadObject *to = thread_fetch_object(object);
8080
if (to->thread && to->thread->joinable()) {
8181
to->thread->join();
82-
php_swoole_thread_join(to->thread_id);
82+
php_swoole_thread_join(to->thread->native_handle());
8383
delete to->thread;
8484
to->thread = nullptr;
8585
}
@@ -110,6 +110,8 @@ static PHP_METHOD(swoole_thread, detach);
110110
static PHP_METHOD(swoole_thread, getArguments);
111111
static PHP_METHOD(swoole_thread, getId);
112112
static PHP_METHOD(swoole_thread, getInfo);
113+
static PHP_METHOD(swoole_thread, activeCount);
114+
static PHP_METHOD(swoole_thread, yield);
113115
static PHP_METHOD(swoole_thread, setName);
114116
#ifdef HAVE_CPU_AFFINITY
115117
static PHP_METHOD(swoole_thread, setAffinity);
@@ -131,6 +133,8 @@ static const zend_function_entry swoole_thread_methods[] = {
131133
PHP_ME(swoole_thread, getArguments, arginfo_class_Swoole_Thread_getArguments, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
132134
PHP_ME(swoole_thread, getId, arginfo_class_Swoole_Thread_getId, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
133135
PHP_ME(swoole_thread, getInfo, arginfo_class_Swoole_Thread_getInfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
136+
PHP_ME(swoole_thread, activeCount, arginfo_class_Swoole_Thread_activeCount, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
137+
PHP_ME(swoole_thread, yield, arginfo_class_Swoole_Thread_yield, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
134138
PHP_ME(swoole_thread, setName, arginfo_class_Swoole_Thread_setName, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
135139
#ifdef HAVE_CPU_AFFINITY
136140
PHP_ME(swoole_thread, setAffinity, arginfo_class_Swoole_Thread_setAffinity, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
@@ -580,6 +584,14 @@ static PHP_METHOD(swoole_thread, getInfo) {
580584
add_assoc_long(return_value, "thread_num", thread_num.load());
581585
}
582586

587+
static PHP_METHOD(swoole_thread, activeCount) {
588+
RETURN_LONG(thread_num.load());
589+
}
590+
591+
static PHP_METHOD(swoole_thread, yield) {
592+
std::this_thread::yield();
593+
}
594+
583595
#define CAST_OBJ_TO_RESOURCE(_name, _type) \
584596
else if (instanceof_function(Z_OBJCE_P(zvalue), swoole_thread_##_name##_ce)) { \
585597
value.resource = php_swoole_thread_##_name##_cast(zvalue); \

tests/swoole_thread/thread_status.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $t2->detach();
2424
usleep(10);
2525
Assert::false($t2->joinable());
2626
Assert::true($t2->isAlive());
27-
while (Thread::getInfo()['thread_num'] > 1) {
27+
while (Thread::activeCount() > 1) {
2828
usleep(10);
2929
}
3030
Assert::false($t2->isAlive());

tests/swoole_thread/yield.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
--TEST--
3+
swoole_thread: thread status
4+
--SKIPIF--
5+
<?php
6+
require __DIR__ . '/../include/skipif.inc';
7+
skip_if_nts();
8+
?>
9+
--FILE--
10+
<?php
11+
require __DIR__ . '/../include/bootstrap.php';
12+
13+
use Swoole\Thread;
14+
15+
$t1 = new Thread(TESTS_API_PATH . '/swoole_thread/sleep.php');
16+
Assert::false($t1->isAlive());
17+
$t1->detach();
18+
Thread::yield();
19+
usleep(10);
20+
Assert::true($t1->isAlive());
21+
while (Thread::activeCount() > 1) {
22+
usleep(10);
23+
}
24+
Assert::false($t1->isAlive());
25+
?>
26+
--EXPECT--

0 commit comments

Comments
 (0)