-
Notifications
You must be signed in to change notification settings - Fork 24
/
an-empty-channel.php
executable file
·37 lines (31 loc) · 1.02 KB
/
an-empty-channel.php
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
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* This example shows how deadlock happens when popping data from an empty channel.
*
* How to run this script:
* # To show deadlock information, run following command:
* docker compose exec -t client bash -c "./csp/deadlocks/an-empty-channel.php"
* docker compose exec -t client bash -c "./csp/deadlocks/an-empty-channel.php 1"
*
* # To hide deadlock information, run following command:
* docker compose exec -t client bash -c "./csp/deadlocks/an-empty-channel.php 0"
*/
use Swoole\Constant;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\run;
Coroutine::set(
[
Constant::OPTION_ENABLE_DEADLOCK_CHECK => (bool) ($argv[1] ?? true),
]
);
run(function () {
Coroutine::create(function () {
echo '1', PHP_EOL; // This will be printed out.
(new Channel(1))->pop();
echo '3', PHP_EOL; // This will never be printed out.
});
echo '2', PHP_EOL; // This will be printed out.
});