-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathasync.php
More file actions
2104 lines (1852 loc) · 62.1 KB
/
async.php
File metadata and controls
2104 lines (1852 loc) · 62.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* TrueAsync extension stubs for PhpStorm.
*
* Provides IDE-level type information for the `async` PHP extension.
*
* @since 8.6
* @version 1.0.0
* @link https://github.com/true-async/php-async
*/
declare(strict_types=1);
namespace Async;
// ---------------------------------------------------------------------------
// Exceptions & Errors
// ---------------------------------------------------------------------------
/**
* Exception thrown when a Coroutine is canceled.
*
* Code inside the Coroutine must properly handle this exception
* to ensure graceful termination.
*
* @since 8.6
*/
class AsyncCancellation extends \Cancellation {}
/**
* Exception thrown when an awaited operation is cancelled by a cancellation token.
*
* This exception wraps the original reason from the token as `$previous`,
* allowing the caller to distinguish a token-triggered cancellation from
* an exception thrown by the awaitable itself.
*
* @since 8.6
*/
class OperationCanceledException extends AsyncCancellation {}
/**
* Common base exception for the async extension.
*
* @since 8.6
*/
class AsyncException extends \Exception {}
/**
* General exception for input/output operations.
*
* Can be used with sockets, files, pipes, and other I/O descriptors.
*
* @since 8.6
*/
class InputOutputException extends \Exception {}
/**
* Exception for DNS-related errors: getaddrinfo and getnameinfo.
*
* @since 8.6
*/
class DnsException extends \Exception {}
/**
* Exception thrown when an operation exceeds its time limit.
*
* @since 8.6
*/
class TimeoutException extends \Exception {}
/**
* Exception thrown when a poll operation fails.
*
* @since 8.6
*/
class PollException extends \Exception {}
/**
* Error thrown when a deadlock is detected.
*
* @since 8.6
*/
class DeadlockError extends \Error {}
/**
* Exception thrown when a service is unavailable.
*
* Used by the circuit breaker when the circuit is in the INACTIVE state.
*
* @since 8.6
*/
class ServiceUnavailableException extends AsyncException {}
/**
* Exception that aggregates multiple exceptions.
*
* Used when several exceptions occur simultaneously, for example in finally handlers.
*
* @since 8.6
*/
final class CompositeException extends \Exception
{
/** @var \Throwable[] */
private array $exceptions;
/**
* Add an exception to the composite.
*/
public function addException(\Throwable $exception): void {}
/**
* Get all aggregated exceptions.
*
* @return \Throwable[]
*/
public function getExceptions(): array {}
}
/**
* Exception thrown when operating on a closed channel.
*
* @since 8.6
*/
class ChannelException extends AsyncException {}
/**
* Exception thrown when operating on a closed or exhausted pool.
*
* @since 8.6
*/
class PoolException extends AsyncException {}
// ---------------------------------------------------------------------------
// Core Interfaces
// ---------------------------------------------------------------------------
/**
* Marker interface for objects that can be awaited.
*
* @since 8.6
*/
interface Awaitable {}
/**
* Interface for objects that represent a completable asynchronous operation.
*
* @since 8.6
*/
interface Completable extends Awaitable
{
/**
* Request cancellation of the operation.
*
* @param AsyncCancellation|null $cancellation Optional cancellation reason.
*/
public function cancel(?AsyncCancellation $cancellation = null): void;
/**
* Return true if the operation has finished (successfully or with an error).
*/
public function isCompleted(): bool;
/**
* Return true if the operation was cancelled.
*/
public function isCancelled(): bool;
}
/**
* Interface for objects that can provide a Scope instance.
*
* @since 8.6
*/
interface ScopeProvider
{
/**
* Return the Scope, or null if none is available.
*/
public function provideScope(): ?Scope;
}
/**
* Strategy interface for controlling how coroutines are enqueued in a Scope.
*
* Implement this interface to customise scheduling behaviour before and after
* a coroutine enters the run-queue.
*
* @since 8.6
*/
interface SpawnStrategy extends ScopeProvider
{
/**
* Called before the coroutine is enqueued.
*
* @param Coroutine $coroutine The coroutine about to be enqueued.
* @param Scope $scope The owning scope.
* @return array Arbitrary metadata passed to afterCoroutineEnqueue.
*/
public function beforeCoroutineEnqueue(Coroutine $coroutine, Scope $scope): array;
/**
* Called immediately after the coroutine has been enqueued.
*
* @param Coroutine $coroutine The enqueued coroutine.
* @param Scope $scope The owning scope.
*/
public function afterCoroutineEnqueue(Coroutine $coroutine, Scope $scope): void;
}
// ---------------------------------------------------------------------------
// Circuit Breaker
// ---------------------------------------------------------------------------
/**
* Circuit breaker states.
*
* @since 8.6
*/
enum CircuitBreakerState
{
/**
* Service is working normally.
* All requests are allowed through.
*/
case ACTIVE;
/**
* Service is unavailable.
* All requests are rejected immediately.
*/
case INACTIVE;
/**
* Testing whether the service has recovered.
* Limited requests are allowed through.
*/
case RECOVERING;
}
/**
* Circuit breaker state machine.
*
* Manages state transitions for service availability.
* This interface defines **how** to transition between states.
* Use {@see CircuitBreakerStrategy} to define **when** to transition.
*
* @since 8.6
*/
interface CircuitBreaker
{
/**
* Get the current circuit breaker state.
*/
public function getState(): CircuitBreakerState;
/**
* Transition to ACTIVE state (service available).
*/
public function activate(): void;
/**
* Transition to INACTIVE state (service unavailable).
*/
public function deactivate(): void;
/**
* Transition to RECOVERING state (probing for recovery).
*/
public function recover(): void;
}
/**
* Circuit breaker strategy interface.
*
* Defines **when** to transition between circuit breaker states.
* Implement this interface to create custom failure-detection logic.
*
* @since 8.6
*/
interface CircuitBreakerStrategy
{
/**
* Called when an operation succeeds.
*
* @param mixed $source The object reporting the event (e.g., {@see Pool}).
*/
public function reportSuccess(mixed $source): void;
/**
* Called when an operation fails.
*
* @param mixed $source The object reporting the event (e.g., {@see Pool}).
* @param \Throwable $error The error that occurred.
*/
public function reportFailure(mixed $source, \Throwable $error): void;
/**
* Check if the circuit should attempt to recover.
*
* Called periodically while the circuit is INACTIVE to determine
* whether it should transition to RECOVERING.
*/
public function shouldRecover(): bool;
}
// ---------------------------------------------------------------------------
// OS Signal Enum
// ---------------------------------------------------------------------------
/**
* OS signal identifiers.
*
* @since 8.6
*/
enum Signal: int
{
case SIGHUP = 1;
case SIGINT = 2;
case SIGQUIT = 3;
case SIGILL = 4;
case SIGABRT = 6;
case SIGFPE = 8;
case SIGKILL = 9;
case SIGUSR1 = 10;
case SIGSEGV = 11;
case SIGUSR2 = 12;
case SIGTERM = 15;
case SIGBREAK = 21;
case SIGABRT2 = 22;
case SIGWINCH = 28;
}
// ---------------------------------------------------------------------------
// Context
// ---------------------------------------------------------------------------
/**
* Immutable key-value store propagated through a coroutine hierarchy.
*
* Each coroutine inherits the context of its parent. Calling {@see set()}
* or {@see unset()} returns a new Context instance; the original is not
* modified.
*
* @since 8.6
*/
final class Context
{
/**
* Find a value by key, searching the current context and all ancestors.
*
* @param string|object $key
*/
public function find(string|object $key): mixed {}
/**
* Get a value by key from the current context only.
*
* @param string|object $key
*/
public function get(string|object $key): mixed {}
/**
* Check if a key exists in the current context or any ancestor.
*
* @param string|object $key
*/
public function has(string|object $key): bool {}
/**
* Find a value by key in the local (non-inherited) context only.
*
* @param string|object $key
*/
public function findLocal(string|object $key): mixed {}
/**
* Get a value by key from the local context only.
*
* @param string|object $key
*/
public function getLocal(string|object $key): mixed {}
/**
* Check if a key exists in the local context only.
*
* @param string|object $key
*/
public function hasLocal(string|object $key): bool {}
/**
* Return a new Context with the given key-value pair set.
*
* @param string|object $key
* @param mixed $value
* @param bool $replace Allow replacing an existing key.
* @return Context A new Context instance.
*/
public function set(string|object $key, mixed $value, bool $replace = false): Context {}
/**
* Return a new Context with the given key removed.
*
* @param string|object $key
* @return Context A new Context instance.
*/
public function unset(string|object $key): Context {}
}
// ---------------------------------------------------------------------------
// Coroutine
// ---------------------------------------------------------------------------
/**
* Represents a running or suspended asynchronous coroutine.
*
* Coroutines are created via {@see spawn()} or {@see Scope::spawn()}.
* They implement {@see Completable} and can be awaited by other coroutines.
*
* @since 8.6
*/
final class Coroutine implements Completable
{
/**
* Return the numeric coroutine ID.
*/
public function getId(): int {}
/**
* Mark the coroutine as high-priority and return it.
*
* @return Coroutine $this
*/
public function asHiPriority(): Coroutine {}
/**
* Return the local context of this coroutine.
*/
public function getContext(): Context {}
/**
* Return the coroutine result, or null if it has not finished yet.
*/
public function getResult(): mixed {}
/**
* Return the exception that terminated the coroutine, or null if it has
* not finished or finished successfully.
*
* If the coroutine was cancelled, returns an {@see AsyncCancellation}.
*
* @throws \RuntimeException If the coroutine is still running.
*/
public function getException(): mixed {}
/**
* Return the backtrace of the suspended coroutine, or null if it is not suspended.
*
* @param int $options {@see DEBUG_BACKTRACE_PROVIDE_OBJECT}, {@see DEBUG_BACKTRACE_IGNORE_ARGS}
* @param int $limit Maximum number of stack frames (0 = unlimited).
* @return array<int, array<string, mixed>>|null
*/
public function getTrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): ?array {}
/**
* Return an array with the file and line where the coroutine was spawned.
*
* @return array{file: string, line: int}
*/
public function getSpawnFileAndLine(): array {}
/**
* Return the spawn location as a human-readable string.
*/
public function getSpawnLocation(): string {}
/**
* Return an array with the file and line where the coroutine is currently suspended.
*
* @return array{file: string, line: int}
*/
public function getSuspendFileAndLine(): array {}
/**
* Return the suspend location as a human-readable string.
*/
public function getSuspendLocation(): string {}
/**
* Return true if the coroutine has been started.
*/
public function isStarted(): bool {}
/**
* Return true if the coroutine is waiting in the run-queue.
*/
public function isQueued(): bool {}
/**
* Return true if the coroutine is actively executing.
*/
public function isRunning(): bool {}
/**
* Return true if the coroutine is currently suspended.
*/
public function isSuspended(): bool {}
/**
* Return true if the coroutine has been cancelled.
*/
public function isCancelled(): bool {}
/**
* Return true if a cancellation has been requested but not yet delivered.
*/
public function isCancellationRequested(): bool {}
/**
* Return true if the coroutine has finished (success, error, or cancellation).
*/
public function isCompleted(): bool {}
/**
* Return debug information about what this coroutine is currently awaiting.
*
* @return array<string, mixed>
*/
public function getAwaitingInfo(): array {}
/**
* Request cancellation of the coroutine.
*
* @param AsyncCancellation|null $cancellation Optional cancellation reason.
*/
public function cancel(?AsyncCancellation $cancellation = null): void {}
/**
* Register a callback to be executed when the coroutine finishes.
*
* @param \Closure $callback Invoked with no arguments after the coroutine completes.
*/
public function finally(\Closure $callback): void {}
}
// ---------------------------------------------------------------------------
// Scope
// ---------------------------------------------------------------------------
/**
* Structured-concurrency scope that owns a group of coroutines.
*
* A Scope forms a parent–child hierarchy: when a scope is cancelled or
* disposed, all coroutines it owns are cancelled as well.
*
* @since 8.6
*/
final class Scope implements ScopeProvider
{
/**
* Create a new Scope that inherits from the given parent scope.
*
* If no parent is provided, the new scope inherits from the current one.
*
* @param Scope|null $parentScope
* @return Scope
*/
public static function inherit(?Scope $parentScope = null): Scope {}
/** @inheritDoc */
#[\Override]
public function provideScope(): Scope {}
/**
* Create a new root Scope.
*/
public function __construct() {}
/**
* Mark the scope as "not safely disposable" and return it.
*
* @return Scope $this
*/
public function asNotSafely(): Scope {}
/**
* Spawn a new coroutine inside this scope.
*
* @param \Closure $callable Coroutine body.
* @param mixed ...$params Arguments forwarded to the closure.
* @return Coroutine The new coroutine.
*/
public function spawn(\Closure $callable, mixed ...$params): Coroutine {}
/**
* Cancel all coroutines owned by this scope.
*
* @param AsyncCancellation|null $cancellationError Optional cancellation reason.
*/
public function cancel(?AsyncCancellation $cancellationError = null): void {}
/**
* Suspend the current coroutine until all child coroutines have finished.
*
* @param Awaitable $cancellation Cancellation token.
* @throws OperationCanceledException If the cancellation token fires.
*/
public function awaitCompletion(Awaitable $cancellation): void {}
/**
* Await scope completion after cancellation, optionally handling errors.
*
* @param callable|null $errorHandler Called for each unhandled child error.
* @param Awaitable|null $cancellation Cancellation token.
* @throws OperationCanceledException If the cancellation token fires.
*/
public function awaitAfterCancellation(?callable $errorHandler = null, ?Awaitable $cancellation = null): void {}
/**
* Return true if all child coroutines have finished.
*/
public function isFinished(): bool {}
/**
* Return true if the scope has been closed.
*/
public function isClosed(): bool {}
/**
* Return true if the scope has been cancelled.
*/
public function isCancelled(): bool {}
/**
* Set a handler invoked when a child coroutine propagates an unhandled exception.
*
* @param callable $exceptionHandler
*/
public function setExceptionHandler(callable $exceptionHandler): void {}
/**
* Set an exception handler for child scopes.
*
* Setting this handler prevents the exception from propagating further up.
*
* @param callable $exceptionHandler
*/
public function setChildScopeExceptionHandler(callable $exceptionHandler): void {}
/**
* Register a callback to be executed when the scope finishes.
*
* @param \Closure $callback
*/
public function finally(\Closure $callback): void {}
/**
* Cancel and dispose of the scope immediately.
*/
public function dispose(): void {}
/**
* Dispose of the scope, waiting for in-flight coroutines to finish gracefully.
*/
public function disposeSafely(): void {}
/**
* Dispose of the scope after a timeout, even if coroutines have not finished.
*
* @param int $timeout Timeout in milliseconds.
*/
public function disposeAfterTimeout(int $timeout): void {}
/**
* Return all direct child scopes.
*
* @return Scope[]
*/
public function getChildScopes(): array {}
}
// ---------------------------------------------------------------------------
// Future & FutureState
// ---------------------------------------------------------------------------
/**
* Write-side handle for a {@see Future}.
*
* FutureState holds the mutable half of the Future pair: it can be resolved
* (via {@see complete()}) or rejected (via {@see error()}) exactly once.
* The matching {@see Future} is the read-only consumer side.
*
* ## Cross-thread transfer
*
* FutureState is the **only** Future-related object that can be transferred
* between OS threads. Transferring FutureState also transfers **ownership**:
* only one thread may call complete()/error() — a second transfer throws.
*
* Typical pattern:
* ```php
* $state = new FutureState();
* $future = new Future($state); // parent keeps Future (read side)
*
* spawn_thread(function() use ($state) { // child gets FutureState (write side)
* $state->complete(computeResult()); // wakes parent's await($future)
* });
*
* $result = await($future);
* ```
*
* @template T
* @since 8.6
*/
final class FutureState
{
public function __construct() {}
/**
* Resolve the Future with a result value.
*
* @param T $result
*/
public function complete(mixed $result): void {}
/**
* Reject the Future with an error.
*
* @param \Throwable $throwable
*/
public function error(\Throwable $throwable): void {}
/**
* Return true if the Future has already been resolved or rejected.
*/
public function isCompleted(): bool {}
/**
* Suppress error forwarding to the event-loop error handler when no
* consumer handles the error.
*/
public function ignore(): void {}
/**
* Return debug information about awaiters of this FutureState.
*
* @return array<string, mixed>
*/
public function getAwaitingInfo(): array {}
/**
* Return the file and line where this FutureState was created.
*
* @return array{file: string, line: int}
*/
public function getCreatedFileAndLine(): array {}
/**
* Return the creation location as a human-readable string.
*/
public function getCreatedLocation(): string {}
/**
* Return the file and line where this FutureState was completed.
*
* @return array{file: string, line: int}
*/
public function getCompletedFileAndLine(): array {}
/**
* Return the completion location as a human-readable string.
*/
public function getCompletedLocation(): string {}
}
/**
* Read-side handle for an asynchronous operation.
*
* A Future is completed by its paired {@see FutureState}. It can be
* chained via {@see map()}, {@see catch()}, and {@see finally()}, and
* awaited inside a coroutine via {@see await()}.
*
* @template-covariant T
* @since 8.6
*/
final class Future implements Completable
{
/**
* Create an already-resolved Future.
*
* @template Tv
* @param Tv $value
* @return Future<Tv>
*/
public static function completed(mixed $value = null): Future {}
/**
* Create an already-rejected Future.
*
* @return Future<never>
*/
public static function failed(\Throwable $throwable): Future {}
/**
* Create a Future backed by the given FutureState.
*
* @param FutureState<T> $state
*/
public function __construct(FutureState $state) {}
/**
* Return true if the Future has been resolved or rejected.
*/
public function isCompleted(): bool {}
/**
* Return true if the Future was cancelled.
*/
public function isCancelled(): bool {}
/**
* Request cancellation of the Future.
*
* @param AsyncCancellation|null $cancellation
*/
public function cancel(?AsyncCancellation $cancellation = null): void {}
/**
* Suppress error forwarding to the event-loop handler.
*
* @return Future<T>
*/
public function ignore(): Future {}
/**
* Transform the resolved value.
*
* The returned Future resolves with the return value of $map, or rejects
* if $map throws.
*
* @template Tr
* @param callable(T): Tr $map
* @return Future<Tr>
*/
public function map(callable $map): Future {}
/**
* Handle a rejection.
*
* The returned Future resolves with the return value of $catch, or
* re-rejects if $catch throws.
*
* @template Tr
* @param callable(\Throwable): Tr $catch
* @return Future<Tr>
*/
public function catch(callable $catch): Future {}
/**
* Attach a callback that runs regardless of success or failure.
*
* The returned Future carries the same result as this Future after the
* callback completes. If the callback throws, the returned Future rejects
* with that exception.
*
* @param callable(): void $finally
* @return Future<T>
*/
public function finally(callable $finally): Future {}
/**
* Suspend the current coroutine until this Future is settled.
*
* @param Completable|null $cancellation Optional cancellation token.
* @return T
* @throws \Throwable If the Future was rejected.
* @throws OperationCanceledException If the cancellation token fires.
*/
public function await(?Completable $cancellation = null): mixed {}
/**
* Return debug information about awaiters of this Future.
*
* @return array<string, mixed>
*/
public function getAwaitingInfo(): array {}
/**
* Return the file and line where this Future was created.
*
* @return array{file: string, line: int}
*/
public function getCreatedFileAndLine(): array {}
/**
* Return the creation location as a human-readable string.
*/
public function getCreatedLocation(): string {}
/**
* Return the file and line where this Future was completed.
*
* @return array{file: string, line: int}
*/
public function getCompletedFileAndLine(): array {}
/**
* Return the completion location as a human-readable string.
*/
public function getCompletedLocation(): string {}
}
// ---------------------------------------------------------------------------
// Timeout
// ---------------------------------------------------------------------------
/**
* A one-shot cancellable timer that implements {@see Completable}.
*
* Obtain a Timeout via {@see timeout()}.
*
* @since 8.6
*/
final class Timeout implements Completable
{
private function __construct() {}
/** @inheritDoc */
public function cancel(?AsyncCancellation $cancellation = null): void {}
/** @inheritDoc */
public function isCompleted(): bool {}
/** @inheritDoc */
public function isCancelled(): bool {}
}
// ---------------------------------------------------------------------------
// Thread Exceptions
// ---------------------------------------------------------------------------
/**
* Wraps an exception that originated in a child thread.
* The original exception is accessible via getRemoteException().
* @since 8.6
*/
class RemoteException extends AsyncException
{
private ?\Throwable $remoteException = null;
private string $remoteClass = '';
/** Get the original exception from the child thread. */
public function getRemoteException(): ?\Throwable {}
/** Get the class name of the original exception in the child thread. */
public function getRemoteClass(): string {}
}
/**
* Thrown when data transfer between threads fails.
* @since 8.6
*/
class ThreadTransferException extends AsyncException {}
// ---------------------------------------------------------------------------
// Thread
// ---------------------------------------------------------------------------
/**
* Represents a running OS thread.
*
* Obtain a Thread via {@see spawn_thread()}.
* Each thread has its own PHP runtime (TSRM) and event loop.
*
* Data transfer between threads follows deep-copy semantics:
* scalars, arrays, objects with declared properties, Closures, WeakReference,
* WeakMap, and FutureState are transferable. stdClass, PHP references, and
* resources are not — attempting to transfer them throws ThreadTransferException.
*
* @since 8.6
*/
final class Thread implements Completable
{
private function __construct() {}
/**
* Return true if the thread is currently running.
*/
public function isRunning(): bool {}
/**
* Return true if the thread has completed execution.
*/
public function isCompleted(): bool {}
/**
* Return true if the thread was cancelled.
*/
public function isCancelled(): bool {}
/**