@@ -49,6 +49,7 @@ typedef std::thread Thread;
4949
5050struct ThreadObject {
5151 Thread *thread;
52+ pthread_t thread_id;
5253 zend_object std;
5354};
5455
@@ -58,6 +59,7 @@ static thread_local zval thread_argv = {};
5859static thread_local JMP_BUF *thread_bailout = nullptr ;
5960static std::atomic<size_t > thread_num (1 );
6061static zend::ConcurrencyHashMap<pthread_t , int > thread_exit_status (-1 );
62+ static zend::ConcurrencyHashMap<pthread_t , bool > thread_living (false );
6163
6264static sw_inline ThreadObject *thread_fetch_object (zend_object *obj) {
6365 return (ThreadObject *) ((char *) obj - swoole_thread_handlers.offset );
@@ -77,14 +79,16 @@ static void thread_join(zend_object *object) {
7779 ThreadObject *to = thread_fetch_object (object);
7880 if (to->thread && to->thread ->joinable ()) {
7981 to->thread ->join ();
80- php_swoole_thread_join (to->thread -> native_handle () );
82+ php_swoole_thread_join (to->thread_id );
8183 delete to->thread ;
8284 to->thread = nullptr ;
8385 }
8486}
8587
8688static void thread_free_object (zend_object *object) {
89+ ThreadObject *to = thread_fetch_object (object);
8790 thread_join (object);
91+ thread_living.del (to->thread_id );
8892 zend_object_std_dtor (object);
8993}
9094
@@ -98,6 +102,7 @@ static zend_object *thread_create_object(zend_class_entry *ce) {
98102
99103SW_EXTERN_C_BEGIN
100104static PHP_METHOD (swoole_thread, __construct);
105+ static PHP_METHOD (swoole_thread, isAlive);
101106static PHP_METHOD (swoole_thread, join);
102107static PHP_METHOD (swoole_thread, joinable);
103108static PHP_METHOD (swoole_thread, getExitStatus);
@@ -118,6 +123,7 @@ SW_EXTERN_C_END
118123// clang-format off
119124static const zend_function_entry swoole_thread_methods[] = {
120125 PHP_ME (swoole_thread, __construct, arginfo_class_Swoole_Thread___construct, ZEND_ACC_PUBLIC)
126+ PHP_ME (swoole_thread, isAlive, arginfo_class_Swoole_Thread_isAlive, ZEND_ACC_PUBLIC)
121127 PHP_ME (swoole_thread, join, arginfo_class_Swoole_Thread_join, ZEND_ACC_PUBLIC)
122128 PHP_ME (swoole_thread, joinable, arginfo_class_Swoole_Thread_joinable, ZEND_ACC_PUBLIC)
123129 PHP_ME (swoole_thread, getExitStatus, arginfo_class_Swoole_Thread_getExitStatus, ZEND_ACC_PUBLIC)
@@ -202,8 +208,14 @@ static PHP_METHOD(swoole_thread, __construct) {
202208 zend_throw_exception (swoole_exception_ce, e.what (), SW_ERROR_SYSTEM_CALL_FAIL);
203209 return ;
204210 }
205- zend_update_property_long (
206- swoole_thread_ce, SW_Z8_OBJ_P (ZEND_THIS), ZEND_STRL (" id" ), (zend_long) to->thread ->native_handle ());
211+
212+ to->thread_id = to->thread ->native_handle ();
213+ zend::object_set (ZEND_THIS, ZEND_STRL (" id" ), (zend_long) to->thread_id );
214+ }
215+
216+ static PHP_METHOD (swoole_thread, isAlive) {
217+ ThreadObject *to = thread_fetch_object (Z_OBJ_P (ZEND_THIS));
218+ RETURN_BOOL (thread_living.get (to->thread_id ));
207219}
208220
209221static PHP_METHOD (swoole_thread, join) {
@@ -403,6 +415,7 @@ static void thread_register_stdio_file_handles(bool no_close) {
403415
404416void php_swoole_thread_start (zend_string *file, ZendArray *argv) {
405417 thread_num.fetch_add (1 );
418+ thread_living.set (pthread_self (), true );
406419 ts_resource (0 );
407420#if defined(COMPILE_DL_SWOOLE) && defined(ZTS)
408421 ZEND_TSRMLS_CACHE_UPDATE ();
@@ -463,6 +476,7 @@ void php_swoole_thread_start(zend_string *file, ZendArray *argv) {
463476 zend_string_release (file);
464477 ts_free_thread ();
465478 swoole_thread_clean ();
479+ thread_living.set (pthread_self (), false );
466480 thread_num.fetch_sub (1 );
467481}
468482
0 commit comments