Skip to content

Commit

Permalink
Update library
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 30, 2024
1 parent 8f585bb commit 380cfa0
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions ext-src/php_swoole_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: fa7b522bcdd905d18e08b545edb54d142c766064 */
/* $Id: 6f6113a57c450c84e00246f2d3c15cf1e7f1f692 */

#ifndef SWOOLE_LIBRARY_H
#define SWOOLE_LIBRARY_H
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 = '<?php' . PHP_EOL;\n"
" $script .= '$arguments = Swoole\\Thread::getArguments();' . PHP_EOL;\n"
Expand All @@ -9260,10 +9263,11 @@ static const char* swoole_library_source_core_thread_pool =
" $script .= '$queue = $arguments[2];' . PHP_EOL;\n"
" $script .= '$classDefinitionFile = $arguments[3];' . PHP_EOL;\n"
" $script .= '$running = $arguments[4];' . PHP_EOL;\n"
" $script .= '$threadArguments = array_slice($arguments, 5);' . PHP_EOL;\n"
" $script .= 'require_once $autoloader;' . PHP_EOL;\n"
" $script .= '$index = $arguments[5];' . PHP_EOL;\n"
" $script .= '$threadArguments = array_slice($arguments, 6);' . PHP_EOL;\n"
" $script .= 'if ($autoloader) require_once $autoloader;' . PHP_EOL;\n"
" $script .= 'if ($classDefinitionFile) require_once $classDefinitionFile;' . PHP_EOL;\n"
" $script .= '$runnable = new $runnableClass($running);' . PHP_EOL;\n"
" $script .= '$runnable = new $runnableClass($running, $index);' . PHP_EOL;\n"
" $script .= 'try { $runnable->run($threadArguments); }' . PHP_EOL;\n"
" $script .= 'finally { $queue->push($threadId, Swoole\\Thread\\Queue::NOTIFY_ONE); }' . PHP_EOL;\n"
" $script .= PHP_EOL;\n"
Expand All @@ -9273,23 +9277,30 @@ 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"
" $thread->join();\n"
" }\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"
Expand Down Expand Up @@ -9326,16 +9337,18 @@ 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"
" $this->runnableClass,\n"
" $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";
Expand All @@ -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"
Expand Down

0 comments on commit 380cfa0

Please sign in to comment.