Skip to content

Commit cf0f1db

Browse files
committed
Refactor
1 parent c2b4472 commit cf0f1db

17 files changed

+142
-45
lines changed

examples/thread/aio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
if (empty($args)) {
1212
$threads = [];
13-
$atomic = new Swoole\Atomic();
13+
$atomic = new Swoole\Thread\Atomic();
1414
for ($i = 0; $i < $c; $i++) {
1515
$threads[] = Thread::exec(__FILE__, $i, $atomic);
1616
}

examples/thread/atomic.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use Swoole\Thread;
4-
use Swoole\Atomic;
5-
use Swoole\Atomic\Long;
4+
use Swoole\Thread\Atomic;
5+
use Swoole\Thread\Atomic\Long;
66

77
$args = Thread::getArguments();
88
$c = 4;

examples/thread/lock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Swoole\Thread;
4-
use Swoole\Lock;
4+
use Swoole\Thread\Lock;
55

66
$args = Thread::getArguments();
77

ext-src/php_swoole.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,9 @@ PHP_RINIT_FUNCTION(swoole) {
10331033
#ifdef SW_USE_ORACLE
10341034
php_swoole_oracle_rinit();
10351035
#endif
1036+
#ifdef SW_THREAD
1037+
php_swoole_thread_rinit();
1038+
#endif
10361039

10371040
SWOOLE_G(req_status) = PHP_SWOOLE_RINIT_END;
10381041

ext-src/php_swoole_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ void php_swoole_runtime_rinit();
286286
#ifdef SW_USE_ORACLE
287287
void php_swoole_oracle_rinit();
288288
#endif
289+
void php_swoole_thread_rinit();
289290

290291
/**
291292
* RSHUTDOWN

ext-src/stubs/php_swoole_thread.stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public function detach(): bool {}
1111
public static function exec(string $script_file, mixed ...$args): Thread {}
1212
public static function getArguments(): array {}
1313
public static function getId(): int {}
14+
public static function getTsrmInfo(): array {}
1415
}
1516
}

ext-src/stubs/php_swoole_thread_arginfo.h

Lines changed: 3 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: 47148b0067d99c44ab50739f4184b021db929a6a */
2+
* Stub hash: 57a2a703c0e0a37729ab2e2df280fbb24e78404f */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Swoole_Thread___construct, 0, 0, 0)
55
ZEND_END_ARG_INFO()
@@ -21,3 +21,5 @@ ZEND_END_ARG_INFO()
2121

2222
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Swoole_Thread_getId, 0, 0, IS_LONG, 0)
2323
ZEND_END_ARG_INFO()
24+
25+
#define arginfo_class_Swoole_Thread_getTsrmInfo arginfo_class_Swoole_Thread_getArguments

ext-src/swoole_server.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,13 +1905,8 @@ static PHP_METHOD(swoole_server, __construct) {
19051905
size_t host_len = 0;
19061906
zend_long sock_type = SW_SOCK_TCP;
19071907
zend_long serv_port = 0;
1908-
#ifdef SW_THREAD
1909-
zend_long serv_mode = Server::MODE_THREAD;
1910-
#else
19111908
zend_long serv_mode = Server::MODE_BASE;
1912-
#endif
19131909

1914-
// only cli env
19151910
if (!SWOOLE_G(cli)) {
19161911
zend_throw_exception_ex(
19171912
swoole_exception_ce, -1, "%s can only be used in CLI mode", SW_Z_OBJCE_NAME_VAL_P(zserv));
@@ -1983,7 +1978,8 @@ static PHP_METHOD(swoole_server, set) {
19831978
ServerObject *server_object = server_fetch_object(Z_OBJ_P(ZEND_THIS));
19841979
Server *serv = php_swoole_server_get_and_check_server(ZEND_THIS);
19851980
if (serv->is_worker_thread()) {
1986-
return;
1981+
swoole_set_last_error(SW_ERROR_OPERATION_NOT_SUPPORT);
1982+
RETURN_FALSE;
19871983
}
19881984
if (serv->is_started()) {
19891985
php_swoole_fatal_error(

ext-src/swoole_thread.cc

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sys/resource.h>
2424

2525
#include <thread>
26+
#include <unordered_map>
2627

2728
#include "swoole_lock.h"
2829

@@ -33,6 +34,12 @@ END_EXTERN_C()
3334
zend_class_entry *swoole_thread_ce;
3435
static zend_object_handlers swoole_thread_handlers;
3536

37+
static struct {
38+
char *path_translated;
39+
zend_string *argv_serialized;
40+
int argc;
41+
} request_info;
42+
3643
TSRMLS_CACHE_DEFINE();
3744

3845
typedef std::thread Thread;
@@ -110,6 +117,7 @@ static PHP_METHOD(swoole_thread, detach);
110117
static PHP_METHOD(swoole_thread, exec);
111118
static PHP_METHOD(swoole_thread, getArguments);
112119
static PHP_METHOD(swoole_thread, getId);
120+
static PHP_METHOD(swoole_thread, getTsrmInfo);
113121
SW_EXTERN_C_END
114122

115123
// clang-format off
@@ -121,6 +129,7 @@ static const zend_function_entry swoole_thread_methods[] = {
121129
PHP_ME(swoole_thread, exec, arginfo_class_Swoole_Thread_exec, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
122130
PHP_ME(swoole_thread, getArguments, arginfo_class_Swoole_Thread_getArguments, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
123131
PHP_ME(swoole_thread, getId, arginfo_class_Swoole_Thread_getId, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
132+
PHP_ME(swoole_thread, getTsrmInfo, arginfo_class_Swoole_Thread_getTsrmInfo, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
124133
PHP_FE_END
125134
};
126135
// clang-format on
@@ -215,14 +224,37 @@ bool php_swoole_thread_unserialize(zend_string *data, zval *zv) {
215224
return unserialized;
216225
}
217226

227+
void php_swoole_thread_rinit() {
228+
if (tsrm_is_main_thread()) {
229+
if (SG(request_info).path_translated) {
230+
request_info.path_translated = strdup(SG(request_info).path_translated);
231+
}
232+
// Return reference
233+
zval *global_argv = zend_hash_find_ind(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV));
234+
request_info.argv_serialized = php_swoole_thread_serialize(global_argv);
235+
request_info.argc = SG(request_info).argc;
236+
}
237+
}
238+
218239
void php_swoole_thread_rshutdown() {
219240
zval_dtor(&thread_argv);
241+
if (tsrm_is_main_thread()) {
242+
if (request_info.path_translated) {
243+
free((void *) request_info.path_translated);
244+
request_info.path_translated = nullptr;
245+
}
246+
if (request_info.argv_serialized) {
247+
zend_string_release(request_info.argv_serialized);
248+
request_info.argv_serialized = nullptr;
249+
}
250+
}
220251
}
221252

222-
void php_swoole_thread_start(zend_string *file, zend_string *argv) {
253+
void php_swoole_thread_start(zend_string *file, zend_string *argv_serialized) {
223254
ts_resource(0);
224255
TSRMLS_CACHE_UPDATE();
225256
zend_file_handle file_handle{};
257+
zval global_argc, global_argv;
226258

227259
PG(expose_php) = 0;
228260
PG(auto_globals_jit) = 1;
@@ -243,15 +275,23 @@ void php_swoole_thread_start(zend_string *file, zend_string *argv) {
243275
SG(sapi_started) = 0;
244276
SG(headers_sent) = 1;
245277
SG(request_info).no_headers = 1;
278+
SG(request_info).path_translated = request_info.path_translated;
279+
SG(request_info).argc = request_info.argc;
246280

247281
zend_stream_init_filename(&file_handle, ZSTR_VAL(file));
248282
file_handle.primary_script = 1;
249283

250284
zend_first_try {
251-
if (argv == nullptr || ZSTR_LEN(argv) == 0) {
285+
if (argv_serialized == nullptr || ZSTR_LEN(argv_serialized) == 0) {
252286
array_init(&thread_argv);
253287
} else {
254-
php_swoole_thread_unserialize(argv, &thread_argv);
288+
php_swoole_thread_unserialize(argv_serialized, &thread_argv);
289+
}
290+
if (request_info.argv_serialized) {
291+
php_swoole_thread_unserialize(request_info.argv_serialized, &global_argv);
292+
ZVAL_LONG(&global_argc, request_info.argc);
293+
zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGV), &global_argv);
294+
zend_hash_update(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_ARGC), &global_argc);
255295
}
256296
php_execute_script(&file_handle);
257297
}
@@ -263,7 +303,7 @@ void php_swoole_thread_start(zend_string *file, zend_string *argv) {
263303

264304
_startup_error:
265305
zend_string_release(file);
266-
zend_string_release(argv);
306+
zend_string_release(argv_serialized);
267307
ts_free_thread();
268308
swoole_thread_clean();
269309
}
@@ -306,4 +346,11 @@ static PHP_METHOD(swoole_thread, exec) {
306346
swoole_thread_ce, SW_Z8_OBJ_P(return_value), ZEND_STRL("id"), to->thread->native_handle());
307347
}
308348

349+
static PHP_METHOD(swoole_thread, getTsrmInfo) {
350+
array_init(return_value);
351+
add_assoc_bool(return_value, "is_main_thread", tsrm_is_main_thread());
352+
add_assoc_bool(return_value, "is_shutdown", tsrm_is_shutdown());
353+
add_assoc_string(return_value, "api_name", tsrm_api_name());
354+
}
355+
309356
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace SwooleTest;
4+
5+
use Swoole\Thread;
6+
7+
class ThreadManager extends ProcessManager
8+
{
9+
public $useConstantPorts = true;
10+
11+
function run($redirectStdout = false): void
12+
{
13+
$args = Thread::getArguments();
14+
if (empty($args)) {
15+
($this->parentFunc)();
16+
} else {
17+
($this->childFunc)(...$args);
18+
exit(0);
19+
}
20+
}
21+
}

tests/include/skipif.inc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ skip(
2626
(function () {
2727
global $argv;
2828
skip_if_php_version_lower_than('7.0');
29-
if (defined('SWOOLE_THREAD')) {
30-
$targs = Swoole\Thread::getArguments();
31-
if ($targs) {
32-
$argv = $targs[0];
33-
}
34-
}
3529
if (!getenv('PHPT') && substr($argv[0], -4) === '.php') {
3630
skip('please read ' . dirname(dirname(__FILE__)) . '/README.md and try again');
3731
}

tests/swoole_thread/async-io.phpt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ if (empty($args)) {
2424
$atomic = new Swoole\Thread\Atomic();
2525
$atomicLong = new Swoole\Thread\Atomic\Long();
2626
for ($i = 0; $i < C; $i++) {
27-
$threads[] = Thread::exec(__FILE__, $argv, $i, $atomic, $atomicLong);
27+
$threads[] = Thread::exec(__FILE__, $i, $atomic, $atomicLong);
2828
}
2929
for ($i = 0; $i < C; $i++) {
3030
$threads[$i]->join();
3131
}
3232
Assert::eq($atomic->get(), C * N);
3333
Assert::eq($atomicLong->get(), C * N * M);
3434
} else {
35-
$atomic = $args[2];
36-
$atomicLong = $args[3];
35+
$id = $args[0];
36+
$atomic = $args[1];
37+
$atomicLong = $args[2];
3738
Co\run(function () use ($atomic, $atomicLong, $md5) {
3839
$n = N;
3940
while ($n--) {

tests/swoole_thread/info.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
swoole_thread: info
3+
--SKIPIF--
4+
<?php
5+
require __DIR__ . '/../include/skipif.inc';
6+
skip_if_nts();
7+
?>
8+
--FILE--
9+
<?php
10+
require __DIR__ . '/../include/bootstrap.php';
11+
12+
use Swoole\Thread;
13+
14+
$tm = new \SwooleTest\ThreadManager();
15+
16+
$tm->parentFunc = function () {
17+
$thread = Thread::exec(__FILE__, 'child');
18+
$info = Thread::getTsrmInfo();
19+
Assert::true($info['is_main_thread']);
20+
Assert::eq($info['api_name'], 'POSIX Threads');
21+
$thread->join();
22+
};
23+
24+
$tm->childFunc = function () {
25+
$info = Thread::getTsrmInfo();
26+
Assert::false($info['is_main_thread']);
27+
Assert::eq($info['api_name'], 'POSIX Threads');
28+
};
29+
30+
$tm->run();
31+
?>
32+
--EXPECTF--
33+

tests/swoole_thread/lock.phpt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,25 @@ require __DIR__ . '/../include/bootstrap.php';
1212
use Swoole\Thread;
1313
use Swoole\Thread\Lock;
1414

15-
$args = Thread::getArguments();
15+
$tm = new \SwooleTest\ThreadManager();
1616

17-
if (empty($args)) {
18-
global $argv;
17+
$tm->parentFunc = function () {
1918
$lock = new Lock;
2019
$lock->lock();
21-
$thread = Thread::exec(__FILE__, $argv, $lock);
20+
$thread = Thread::exec(__FILE__, $lock);
2221
$lock->lock();
2322
echo "main thread\n";
2423
$thread->join();
25-
} else {
24+
};
25+
26+
$tm->childFunc = function ($lock) {
2627
echo "child thread\n";
27-
$lock = $args[1];
2828
usleep(200_000);
2929
$lock->unlock();
3030
exit(0);
31-
}
31+
};
32+
33+
$tm->run();
3234
?>
3335
--EXPECTF--
3436
child thread

tests/swoole_thread/pipe.phpt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ $args = Thread::getArguments();
1616
if (empty($args)) {
1717
$rdata = random_bytes(random_int(1024, 2048));
1818
Co\run(function () use ($rdata) {
19-
global $argv;
2019
$sockets = swoole_coroutine_socketpair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
21-
$thread = Thread::exec(__FILE__, $argv, $sockets, $rdata);
20+
$thread = Thread::exec(__FILE__, $sockets, $rdata);
2221
Assert::eq($sockets[0]->recv(8192), $rdata);
2322
$thread->join();
2423
echo "DONE\n";
2524
});
2625
} else {
27-
$argv = $args[0];
28-
$sockets = $args[1];
29-
$rdata = $args[2];
26+
$sockets = $args[0];
27+
$rdata = $args[1];
3028
// Child threads are not allowed to modify hook flags
3129
Assert::false(Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL));
3230
Co\run(function () use ($sockets, $rdata, $argv) {

tests/swoole_thread/queue.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (empty($args)) {
2424
$queue = new Queue;
2525
$map = new Thread\Map();
2626
for ($i = 0; $i < C; $i++) {
27-
$threads[] = Thread::exec(__FILE__, $argv, $i, $queue, $map);
27+
$threads[] = Thread::exec(__FILE__, $i, $queue, $map);
2828
}
2929
$n = N;
3030
while ($n--) {
@@ -44,9 +44,9 @@ if (empty($args)) {
4444
Assert::eq($queue->count(), 0);
4545
Assert::eq($total_parent, $total_child);
4646
} else {
47-
$i = $args[1];
48-
$queue = $args[2];
49-
$map = $args[3];
47+
$i = $args[0];
48+
$queue = $args[1];
49+
$map = $args[2];
5050
$map[$i] = 0;
5151
while (1) {
5252
$job = $queue->pop(-1);

0 commit comments

Comments
 (0)