Skip to content

Commit 112f552

Browse files
authored
Optimize thread co socket (#5370)
* Optimize co socket resource security * Optimize code * Optimize code * fix tests * remove thread::exec()
1 parent 990d4aa commit 112f552

31 files changed

+204
-119
lines changed

examples/thread/aio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$threads = [];
1313
$atomic = new Swoole\Thread\Atomic();
1414
for ($i = 0; $i < $c; $i++) {
15-
$threads[] = Thread::exec(__FILE__, $i, $atomic);
15+
$threads[] = new Thread(__FILE__, $i, $atomic);
1616
}
1717
for ($i = 0; $i < $c; $i++) {
1818
$threads[$i]->join();

examples/thread/argv.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
if (empty($args)) {
88
var_dump($GLOBALS['argv']);
9-
$thread = Thread::exec(__FILE__, 'thread-1', $argc, $argv);
10-
$thread->join();
9+
$n = 2;
10+
while ($n--) {
11+
$thread = new Thread(__FILE__, 'thread-' . $n, $argc, $argv);
12+
$thread->join();
13+
}
1114
} else {
1215
var_dump($args[0], $args[1], $args[2]);
1316
sleep(1);

examples/thread/atomic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$a1 = new Atomic;
1414
$a2 = new Long;
1515
for ($i = 0; $i < $c; $i++) {
16-
$threads[] = Thread::exec(__FILE__, $i, $a1, $a2);
16+
$threads[] = new Thread(__FILE__, $i, $a1, $a2);
1717
}
1818
for ($i = 0; $i < $c; $i++) {
1919
$threads[$i]->join();

examples/thread/co.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
$list[1] = uniqid();
99
var_dump(count($list));
1010

11-
$t1 = Swoole\Thread::exec('mt.php', 'thread-1', PHP_OS, $map, $list);
12-
$t2 = Swoole\Thread::exec('mt.php', 'thread-2', PHP_OS, $map, $list);
11+
$t1 = new Swoole\Thread('mt.php', 'thread-1', PHP_OS, $map, $list);
12+
$t2 = new Swoole\Thread('mt.php', 'thread-2', PHP_OS, $map, $list);
1313

1414
//var_dump($t1->id);
1515
//var_dump($t2->id);

examples/thread/lock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
if (empty($args)) {
99
$lock = new Lock;
1010
$lock->lock();
11-
$thread = Thread::exec(__FILE__, $lock);
11+
$thread = new Thread(__FILE__, $lock);
1212
$lock->lock();
1313
echo "main thread\n";
1414
$thread->join();

examples/thread/mt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
var_dump(count($list));
2020

2121
//if ($args[0] == 'thread-2') {
22-
// $t3 = Swoole\Thread::exec('mt.php', 'thread-3', PHP_OS);
22+
// $t3 = new Swoole\Thread('mt.php', 'thread-3', PHP_OS);
2323
// $t3->join();
2424
//}
2525

examples/thread/pipe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
if (empty($args)) {
88
Co\run(function () {
99
$sockets = swoole_coroutine_socketpair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
10-
$thread = Thread::exec(__FILE__, $sockets);
10+
$thread = new Thread(__FILE__, $sockets);
1111
echo $sockets[0]->recv(8192), PHP_EOL;
1212
$thread->join();
1313
});

examples/thread/run_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$threads = [];
88

99
for ($i = 0; $i < $c; $i++) {
10-
$threads[] = Swoole\Thread::exec('benchmark.php', 'thread-' . ($i + 1), $map);
10+
$threads[] = new Swoole\Thread('benchmark.php', 'thread-' . ($i + 1), $map);
1111
}
1212

1313
for ($i = 0; $i < $c; $i++) {

examples/thread/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
$threads = [];
1212
$queue = new Queue;
1313
for ($i = 0; $i < $c; $i++) {
14-
$threads[] = Thread::exec(__FILE__, $i, $queue);
14+
$threads[] = new Thread(__FILE__, $i, $queue);
1515
}
1616
for ($i = 0; $i < $c; $i++) {
1717
$threads[$i]->join();

examples/thread/signal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Co\run(function () {
1010
echo "main thread\n";
1111
$sockets = swoole_coroutine_socketpair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
12-
$thread = Thread::exec(__FILE__, $sockets);
12+
$thread = new Thread(__FILE__, $sockets);
1313
$parent_pipe = $sockets[1];
1414
// 收到信号之后向子线程发送指令让子线程退出
1515
if (System::waitSignal(SIGTERM)) {

examples/thread/thread_pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
$threads = [];
1212
$queue = new Queue;
1313
for ($i = 0; $i < $c; $i++) {
14-
$threads[] = Thread::exec(__FILE__, $i, $queue);
14+
$threads[] = new Thread(__FILE__, $i, $queue);
1515
}
1616
while ($n--) {
1717
$queue->push(base64_encode(random_bytes(16)), Queue::NOTIFY_ONE);

ext-src/php_swoole.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ zend_class_entry *swoole_error_ce;
180180
zend_object_handlers swoole_error_handlers;
181181

182182
#ifdef COMPILE_DL_SWOOLE
183+
#ifdef ZTS
184+
ZEND_TSRMLS_CACHE_DEFINE()
185+
#endif
183186
ZEND_GET_MODULE(swoole)
184187
#endif
185188

@@ -1089,8 +1092,7 @@ PHP_RSHUTDOWN_FUNCTION(swoole) {
10891092
if (!zstream) {
10901093
return;
10911094
}
1092-
stream =
1093-
(php_stream *) zend_fetch_resource2_ex((zstream), NULL, php_file_le_stream(), php_file_le_pstream());
1095+
stream = (php_stream *) zend_fetch_resource2_ex((zstream), NULL, php_file_le_stream(), php_file_le_pstream());
10941096
if (!stream) {
10951097
return;
10961098
}

ext-src/php_swoole_cxx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ extern zend_string **sw_zend_known_strings;
127127
SW_API bool php_swoole_is_enable_coroutine();
128128
SW_API zend_object *php_swoole_create_socket(enum swSocketType type);
129129
SW_API zend_object *php_swoole_create_socket_from_fd(int fd, enum swSocketType type);
130+
SW_API zend_object *php_swoole_create_socket_from_fd(int fd, int _domain, int _type, int _protocol);
130131
SW_API bool php_swoole_export_socket(zval *zobject, swoole::coroutine::Socket *_socket);
131132
SW_API zend_object *php_swoole_dup_socket(int fd, enum swSocketType type);
132133
SW_API void php_swoole_init_socket_object(zval *zobject, swoole::coroutine::Socket *socket);

ext-src/php_swoole_thread.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,23 @@ bool php_swoole_thread_resource_free(ThreadResourceId resource_id, ThreadResourc
3131
ThreadResource *php_swoole_thread_resource_fetch(ThreadResourceId resource_id);
3232

3333
void php_swoole_thread_start(zend_string *file, zend_string *argv);
34-
zend_string *php_swoole_thread_serialize(zval *zdata);
35-
bool php_swoole_thread_unserialize(zend_string *data, zval *zv);
34+
zend_string *php_swoole_thread_argv_serialize(zval *zdata);
35+
bool php_swoole_thread_argv_unserialize(zend_string *data, zval *zv);
36+
zend_string *php_swoole_serialize(zval *zdata);
37+
bool php_swoole_unserialize(zend_string *data, zval *zv);
38+
void php_swoole_thread_argv_clean(zval *zdata);
3639
void php_swoole_thread_bailout(void);
3740

3841
zval *php_swoole_thread_get_arguments();
3942

4043
#define EMSG_NO_RESOURCE "resource not found"
4144
#define ECODE_NO_RESOURCE -2
4245

43-
#define IS_STREAM_SOCKET 98
44-
#define IS_SERIALIZED_OBJECT 99
46+
enum {
47+
IS_CO_SOCKET = 97,
48+
IS_STREAM_SOCKET = 98,
49+
IS_SERIALIZED_OBJECT = 99,
50+
};
4551

4652
struct ThreadResource {
4753
uint32_t ref_count;
@@ -66,6 +72,10 @@ struct ArrayItem {
6672
zend_string *str;
6773
zend_long lval;
6874
double dval;
75+
struct {
76+
int fd;
77+
swSocketType type;
78+
} socket;
6979
zend_string *serialized_object;
7080
} value;
7181

ext-src/stubs/php_swoole_socket_coro.stub.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,5 @@ public function getpeername(): false|array {}
3535
public function isClosed(): bool {}
3636
/** @param resource $stream */
3737
public static function import($stream) : Socket | false {}
38-
#ifdef SW_THREAD
39-
public function __wakeup(): void {}
40-
#endif
4138
}
4239
}

ext-src/stubs/php_swoole_thread.stub.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public function join(): bool {}
88
public function joinable(): bool {}
99
public function detach(): bool {}
1010

11-
public static function exec(string $script_file, mixed ...$args): Thread {}
1211
public static function getArguments(): array {}
1312
public static function getId(): int {}
1413
public static function getTsrmInfo(): array {}

ext-src/swoole_server.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2651,7 +2651,7 @@ static PHP_METHOD(swoole_server, start) {
26512651

26522652
if (!ZVAL_IS_NULL(&server_object->init_arguments)) {
26532653
call_user_function(NULL, NULL, &server_object->init_arguments, &thread_argv, 0, NULL);
2654-
thread_argv_serialized = php_swoole_thread_serialize(&thread_argv);
2654+
thread_argv_serialized = php_swoole_thread_argv_serialize(&thread_argv);
26552655
}
26562656

26572657
serv->worker_thread_start = [bootstrap, thread_argv_serialized](const WorkerFn &fn) {

ext-src/swoole_socket_coro.cc

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ static PHP_METHOD(swoole_socket_coro, getsockname);
8989
static PHP_METHOD(swoole_socket_coro, getpeername);
9090
static PHP_METHOD(swoole_socket_coro, isClosed);
9191
static PHP_METHOD(swoole_socket_coro, import);
92-
#ifdef SW_THREAD
93-
static PHP_METHOD(swoole_socket_coro, __wakeup);
94-
#endif
9592
SW_EXTERN_C_END
9693

9794
// clang-format off
@@ -132,9 +129,6 @@ static const zend_function_entry swoole_socket_coro_methods[] =
132129
PHP_ME(swoole_socket_coro, getsockname, arginfo_class_Swoole_Coroutine_Socket_getsockname, ZEND_ACC_PUBLIC)
133130
PHP_ME(swoole_socket_coro, isClosed, arginfo_class_Swoole_Coroutine_Socket_isClosed, ZEND_ACC_PUBLIC)
134131
PHP_ME(swoole_socket_coro, import, arginfo_class_Swoole_Coroutine_Socket_import, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
135-
#ifdef SW_THREAD
136-
PHP_ME(swoole_socket_coro, __wakeup, arginfo_class_Swoole_Coroutine_Socket___wakeup, ZEND_ACC_PUBLIC)
137-
#endif
138132
PHP_FE_END
139133
};
140134
// clang-format on
@@ -720,9 +714,7 @@ static void socket_coro_register_constants(int module_number) {
720714

721715
void php_swoole_socket_coro_minit(int module_number) {
722716
SW_INIT_CLASS_ENTRY(swoole_socket_coro, "Swoole\\Coroutine\\Socket", "Co\\Socket", swoole_socket_coro_methods);
723-
#ifndef SW_THREAD
724717
SW_SET_CLASS_NOT_SERIALIZABLE(swoole_socket_coro);
725-
#endif
726718
SW_SET_CLASS_CLONEABLE(swoole_socket_coro, sw_zend_class_clone_deny);
727719
SW_SET_CLASS_UNSET_PROPERTY_HANDLER(swoole_socket_coro, sw_zend_class_unset_property_deny);
728720
SW_SET_CLASS_CUSTOM_OBJECT(
@@ -829,12 +821,12 @@ SW_API void php_swoole_socket_set_error_properties(zval *zobject, Socket *socket
829821
php_swoole_socket_set_error_properties(zobject, socket->errCode, socket->errMsg);
830822
}
831823

832-
SW_API zend_object *php_swoole_create_socket_from_fd(int fd, enum swSocketType type) {
824+
static zend_object *create_socket_object(Socket *socket) {
833825
zval zobject;
834826
zend_object *object = socket_coro_create_object(swoole_socket_coro_ce);
835827
SocketObject *sock = (SocketObject *) socket_coro_fetch_object(object);
836828

837-
sock->socket = new Socket(fd, type);
829+
sock->socket = socket;
838830
if (UNEXPECTED(sock->socket->get_fd() < 0)) {
839831
php_swoole_sys_error(E_WARNING, "new Socket() failed");
840832
delete sock->socket;
@@ -848,6 +840,14 @@ SW_API zend_object *php_swoole_create_socket_from_fd(int fd, enum swSocketType t
848840
return object;
849841
}
850842

843+
SW_API zend_object *php_swoole_create_socket_from_fd(int fd, enum swSocketType type) {
844+
return create_socket_object(new Socket(fd, type));
845+
}
846+
847+
SW_API zend_object *php_swoole_create_socket_from_fd(int fd, int _domain, int _type, int _protocol) {
848+
return create_socket_object(new Socket(fd, _domain, _type, _protocol));
849+
}
850+
851851
SW_API Socket *php_swoole_get_socket(zval *zobject) {
852852
SW_ASSERT(Z_OBJCE_P(zobject) == swoole_socket_coro_ce);
853853
SocketObject *sock = (SocketObject *) socket_coro_fetch_object(Z_OBJ_P(zobject));
@@ -2217,31 +2217,3 @@ static PHP_METHOD(swoole_socket_coro, import) {
22172217

22182218
RETURN_OBJ(object);
22192219
}
2220-
2221-
#ifdef SW_THREAD
2222-
static PHP_METHOD(swoole_socket_coro, __wakeup) {
2223-
zend_long sockfd = zend::object_get_long(ZEND_THIS, ZEND_STRL("fd"));
2224-
if (sockfd < 0) {
2225-
zend_throw_exception(swoole_exception_ce, EMSG_NO_RESOURCE, ECODE_NO_RESOURCE);
2226-
return;
2227-
}
2228-
2229-
zend_long new_sockfd = dup(sockfd);
2230-
if (sockfd < 0) {
2231-
zend_throw_exception(swoole_exception_ce, EMSG_NO_RESOURCE, ECODE_NO_RESOURCE);
2232-
return;
2233-
}
2234-
2235-
SocketObject *sock = (SocketObject *) socket_coro_fetch_object(Z_OBJ_P(ZEND_THIS));
2236-
2237-
zend_long domain = zend::object_get_long(ZEND_THIS, ZEND_STRL("domain"));
2238-
zend_long type = zend::object_get_long(ZEND_THIS, ZEND_STRL("type"));
2239-
zend_long protocol = zend::object_get_long(ZEND_THIS, ZEND_STRL("protocol"));
2240-
2241-
php_swoole_check_reactor();
2242-
sock->socket = new Socket((int) new_sockfd, (int) domain, (int) type, (int) protocol);
2243-
sock->socket->set_zero_copy(true);
2244-
sock->socket->set_buffer_allocator(sw_zend_string_allocator());
2245-
zend_update_property_long(swoole_socket_coro_ce, SW_Z8_OBJ_P(ZEND_THIS), ZEND_STRL("fd"), sock->socket->get_fd());
2246-
}
2247-
#endif

0 commit comments

Comments
 (0)