-
Notifications
You must be signed in to change notification settings - Fork 24
/
swoole-lock.php
executable file
·39 lines (32 loc) · 1.26 KB
/
swoole-lock.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
38
39
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* This example shows how deadlock happens when using the same \Swoole\Lock object across different coroutines.
*
* Different from other deadlock examples in this repository, this example does not show deadlock information (a fatal
* error message from Swoole), making it hard to find out what's wrong.
*
* When using \Swoole\Lock objects in coroutines, make sure there is no coroutine context switching (to switch execution
* between different coroutines) between method calls to lock() and unlock().
*
* How to run this script:
* docker compose exec -t client bash -c "./csp/deadlocks/swoole-lock.php" # It will run forever.
*/
use Swoole\Coroutine;
use Swoole\Lock;
use function Swoole\Coroutine\go;
use function Swoole\Coroutine\run;
run(function (): void {
$lock = new Lock();
go(function () use ($lock): void {
$lock->lock(); // 1. The lock is acquired.
Coroutine::sleep(1); // 2. The sleep() method call will switch execution to another coroutine.
$lock->unlock();
});
go(function () use ($lock): void {
$lock->lock(); // 3. Trying to acquire the lock again? This will cause deadlock.
Coroutine::sleep(1);
$lock->unlock();
});
});