1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stu\Module\Control; |
4
|
|
|
|
5
|
|
|
use Override; |
|
|
|
|
6
|
|
|
use RuntimeException; |
7
|
|
|
use Stu\Component\Game\SemaphoreConstants; |
|
|
|
|
8
|
|
|
use Stu\Exception\SemaphoreException; |
9
|
|
|
use Stu\Module\Config\StuConfigInterface; |
10
|
|
|
use Stu\Module\Logging\LoggerEnum; |
|
|
|
|
11
|
|
|
use Stu\Module\Logging\LoggerUtilFactoryInterface; |
12
|
|
|
use Stu\Module\Logging\LoggerUtilInterface; |
13
|
|
|
use SysvSemaphore; |
14
|
|
|
|
15
|
|
|
final class SemaphoreUtil implements SemaphoreUtilInterface |
16
|
|
|
{ |
17
|
|
|
private LoggerUtilInterface $loggerUtil; |
18
|
|
|
|
19
|
1 |
|
public function __construct( |
20
|
|
|
private GameControllerInterface $game, |
21
|
|
|
private StuConfigInterface $stuConfig, |
22
|
|
|
LoggerUtilFactoryInterface $loggerUtilFactory |
23
|
|
|
) { |
24
|
1 |
|
$this->loggerUtil = $loggerUtilFactory->getLoggerUtil(); |
25
|
|
|
} |
26
|
|
|
|
27
|
38 |
|
#[Override] |
28
|
|
|
public function acquireSemaphore(int $key): null|int|SysvSemaphore |
29
|
|
|
{ |
30
|
38 |
|
if (!$this->isSemaphoreUsageActive()) { |
31
|
38 |
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$semaphore = $this->getSemaphore($key); |
35
|
|
|
|
36
|
|
|
if ($this->game->isSemaphoreAlreadyAcquired($key)) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$this->acquire($semaphore); |
41
|
|
|
$this->game->addSemaphore($key, $semaphore); |
42
|
|
|
|
43
|
|
|
return $semaphore; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function getSemaphore(int $key): SysvSemaphore |
47
|
|
|
{ |
48
|
|
|
$semaphore = sem_get( |
49
|
|
|
$key, |
50
|
|
|
1, |
51
|
|
|
0o666, |
52
|
|
|
SemaphoreConstants::AUTO_RELEASE_SEMAPHORES |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if ($semaphore === false) { |
56
|
|
|
throw new RuntimeException('Error getting semaphore'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $semaphore; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function acquire(SysvSemaphore $semaphore): void |
63
|
|
|
{ |
64
|
|
|
if (!sem_acquire($semaphore)) { |
65
|
|
|
throw new SemaphoreException("Error acquiring Semaphore!"); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
38 |
|
#[Override] |
70
|
|
|
public function releaseSemaphore(null|int|SysvSemaphore $semaphore, bool $doRemove = false): void |
71
|
|
|
{ |
72
|
38 |
|
if (!$this->isSemaphoreUsageActive() || !$semaphore instanceof SysvSemaphore) { |
73
|
38 |
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->release($semaphore, $doRemove); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function release(SysvSemaphore $semaphore, bool $doRemove): void |
80
|
|
|
{ |
81
|
|
|
if (!sem_release($semaphore)) { |
82
|
|
|
$this->loggerUtil->init('semaphores', LoggerEnum::LEVEL_ERROR); |
83
|
|
|
$this->loggerUtil->log("Error releasing Semaphore!"); |
84
|
|
|
return; |
85
|
|
|
//throw new SemaphoreException("Error releasing Semaphore!"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($doRemove && !sem_remove($semaphore)) { |
89
|
|
|
$this->loggerUtil->init('semaphores', LoggerEnum::LEVEL_ERROR); |
90
|
|
|
$this->loggerUtil->log("Error removing Semaphore!"); |
91
|
|
|
//throw new SemaphoreException("Error removing Semaphore!"); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
38 |
|
private function isSemaphoreUsageActive(): bool |
96
|
|
|
{ |
97
|
38 |
|
return $this->stuConfig->getGameSettings()->useSemaphores(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths