|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace parallel; |
| 4 | + |
| 5 | +use Closure; |
| 6 | + |
| 7 | +/** |
| 8 | + * Each runtime represents a single PHP thread, the thread is created (and bootstrapped) upon construction. The thread |
| 9 | + * then waits for tasks to be scheduled: Scheduled tasks will be executed FIFO and then the thread will resume waiting |
| 10 | + * until more tasks are scheduled, or it's closed, killed, or destroyed by the normal scoping rules of PHP objects. |
| 11 | + * |
| 12 | + * Warning: When a runtime is destroyed by the normal scoping rules of PHP objects, it will first execute all of the |
| 13 | + * tasks that were scheduled, and block while doing so. |
| 14 | + * |
| 15 | + * When a new runtime is created, it does not share code with the thread (or process) that created it. This means it |
| 16 | + * doesn't have the same classes and functions loaded, nor the same autoloader set. In some cases, a very lightweight |
| 17 | + * runtime is desirable because the tasks that will be scheduled do not need access to the code in the parent thread. |
| 18 | + * In those cases where the tasks do need to access the same code, it is enough to set an autoloader as the bootstrap. |
| 19 | + * |
| 20 | + * Note: preloading may be used in conjunction with parallel, in this case preloaded code is available without |
| 21 | + * bootstrapping |
| 22 | + */ |
| 23 | +final class Runtime |
| 24 | +{ |
| 25 | + /* Create */ |
| 26 | + |
| 27 | + /** |
| 28 | + * @throws Runtime\Error if thread could not be created |
| 29 | + * @throws Runtime\Error\Bootstrap if bootstrapping failed |
| 30 | + */ |
| 31 | + public function __construct(?string $bootstrap = null) {} |
| 32 | + |
| 33 | + /* Execute */ |
| 34 | + |
| 35 | + /** |
| 36 | + * @param (Closure():T)|(Closure():void) $task |
| 37 | + * @param array<mixed> $argv |
| 38 | + * @template T |
| 39 | + * @return ($task is (Closure():T) ? Future<T> : null) |
| 40 | + * |
| 41 | + * @throws Runtime\Error\Closed if \parallel\Runtime was closed. |
| 42 | + * @throws Runtime\Error\IllegalFunction if task is a closure created from an internal function. |
| 43 | + * @throws Runtime\Error\IllegalInstruction if task contains illegal instructions. |
| 44 | + * @throws Runtime\Error\IllegalParameter if task accepts or argv contains illegal variables. |
| 45 | + * @throws Runtime\Error\IllegalReturn if task returns illegally. |
| 46 | + */ |
| 47 | + public function run(Closure $task, ?array $argv = null): ?Future {} |
| 48 | + |
| 49 | + /* Join */ |
| 50 | + |
| 51 | + /** |
| 52 | + * @throws Runtime\Error\Closed if Runtime was already closed. |
| 53 | + */ |
| 54 | + public function close(): void {} |
| 55 | + |
| 56 | + /** |
| 57 | + * @throws Runtime\Error\Closed if Runtime was closed. |
| 58 | + */ |
| 59 | + public function kill(): void {} |
| 60 | +} |
0 commit comments