From 380cfa0022bb399ddce856989ebfa94d13dd4ff5 Mon Sep 17 00:00:00 2001 From: matyhtf Date: Mon, 30 Sep 2024 18:15:54 +0800 Subject: [PATCH] Update library --- ext-src/php_swoole_library.h | 41 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/ext-src/php_swoole_library.h b/ext-src/php_swoole_library.h index f0b97f5cf6..7f51c97a85 100644 --- a/ext-src/php_swoole_library.h +++ b/ext-src/php_swoole_library.h @@ -14,7 +14,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: fa7b522bcdd905d18e08b545edb54d142c766064 */ +/* $Id: 6f6113a57c450c84e00246f2d3c15cf1e7f1f692 */ #ifndef SWOOLE_LIBRARY_H #define SWOOLE_LIBRARY_H @@ -9186,6 +9186,7 @@ static const char* swoole_library_source_core_thread_pool = " private object $running;\n" "\n" " private object $queue;\n" + " private array $indexes = [];\n" "\n" " public function __construct(string $runnableClass, int $threadNum)\n" " {\n" @@ -9246,11 +9247,13 @@ static const char* swoole_library_source_core_thread_pool = " }\n" " }\n" " }\n" - " if (empty($this->autoloader)) {\n" - " throw new \\Exception('autoload file not found');\n" + "\n" + " if ($this->autoloader) {\n" + " $this->proxyFile = dirname($this->autoloader) . '/thread_runner.php';\n" + " } else {\n" + " $this->proxyFile = dirname($this->classDefinitionFile) . '/thread_runner.php';\n" " }\n" "\n" - " $this->proxyFile = dirname($this->autoloader) . '/thread_runner.php';\n" " if (!is_file($this->proxyFile)) {\n" " $script = 'run($threadArguments); }' . PHP_EOL;\n" " $script .= 'finally { $queue->push($threadId, Swoole\\Thread\\Queue::NOTIFY_ONE); }' . PHP_EOL;\n" " $script .= PHP_EOL;\n" @@ -9273,16 +9277,18 @@ static const char* swoole_library_source_core_thread_pool = " $this->queue = new Queue();\n" " $this->running = new Atomic(1);\n" "\n" - " for ($i = 0; $i < $this->threadNum; $i++) {\n" - " $this->createThread();\n" + " for ($index = 0; $index < $this->threadNum; $index++) {\n" + " $this->createThread($index);\n" " }\n" "\n" " while ($this->running->get()) {\n" " $threadId = $this->queue->pop(-1);\n" - " $thread = $this->threads[$threadId];\n" + " $thread = $this->threads[$threadId];\n" + " $index = $this->indexes[$threadId];\n" " $thread->join();\n" " unset($this->threads[$threadId]);\n" - " $this->createThread();\n" + " unset($this->indexes[$threadId]);\n" + " $this->createThread($index);\n" " }\n" "\n" " foreach ($this->threads as $thread) {\n" @@ -9290,6 +9296,11 @@ static const char* swoole_library_source_core_thread_pool = " }\n" " }\n" "\n" + " public function shutdown(): void\n" + " {\n" + " $this->running->set(0);\n" + " }\n" + "\n" " protected function isValidPhpFile($filePath): bool\n" " {\n" " $allowedNodeTypes = [\n" @@ -9326,7 +9337,7 @@ static const char* swoole_library_source_core_thread_pool = " return true;\n" " }\n" "\n" - " protected function createThread(): void\n" + " protected function createThread($index): void\n" " {\n" " $thread = new Thread($this->proxyFile,\n" " $this->autoloader,\n" @@ -9334,8 +9345,10 @@ static const char* swoole_library_source_core_thread_pool = " $this->queue,\n" " $this->classDefinitionFile,\n" " $this->running,\n" + " $index,\n" " ...$this->arguments\n" " );\n" + " $this->indexes[$thread->id] = $index;\n" " $this->threads[$thread->id] = $thread;\n" " }\n" "}\n"; @@ -9357,10 +9370,12 @@ static const char* swoole_library_source_core_thread_runnable = "abstract class Runnable\n" "{\n" " protected Atomic $running;\n" + " protected int $id;\n" "\n" - " public function __construct($running)\n" + " public function __construct($running, $index)\n" " {\n" " $this->running = $running;\n" + " $this->id = $index;\n" " }\n" "\n" " abstract public function run(array $args): void;\n"