|
6 | 6 | use Stu\Component\Game\SemaphoreConstants; |
7 | 7 | use Stu\Exception\SemaphoreException; |
8 | 8 | use Stu\Module\Config\StuConfigInterface; |
9 | | -use Stu\Module\Logging\LogLevelEnum; |
10 | | -use Stu\Module\Logging\LoggerUtilFactoryInterface; |
11 | | -use Stu\Module\Logging\LoggerUtilInterface; |
| 9 | +use Stu\Module\Logging\LogTypeEnum; |
| 10 | +use Stu\Module\Logging\StuLogger; |
12 | 11 | use SysvSemaphore; |
13 | 12 |
|
14 | 13 | final class SemaphoreUtil implements SemaphoreUtilInterface |
15 | 14 | { |
16 | 15 | /** @var array<int, SysvSemaphore> */ |
17 | 16 | public static array $semaphores = []; |
18 | 17 |
|
19 | | - private LoggerUtilInterface $loggerUtil; |
20 | | - |
21 | | - public function __construct( |
22 | | - private readonly StuConfigInterface $stuConfig, |
23 | | - LoggerUtilFactoryInterface $loggerUtilFactory |
24 | | - ) { |
25 | | - $this->loggerUtil = $loggerUtilFactory->getLoggerUtil(); |
26 | | - } |
| 18 | + public function __construct(private readonly StuConfigInterface $stuConfig) {} |
27 | 19 |
|
28 | 20 | #[Override] |
29 | 21 | public function isSemaphoreAlreadyAcquired(int $key): bool |
@@ -83,18 +75,36 @@ public function releaseSemaphore(null|int|SysvSemaphore $semaphore, bool $doRemo |
83 | 75 | $this->release($semaphore, $doRemove); |
84 | 76 | } |
85 | 77 |
|
| 78 | + #[Override] |
| 79 | + public function releaseAllSemaphores(bool $doRemove = false): void |
| 80 | + { |
| 81 | + if (!$this->isSemaphoreUsageActive()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + foreach (self::$semaphores as $semaphore) { |
| 86 | + $this->release($semaphore, $doRemove); |
| 87 | + } |
| 88 | + } |
| 89 | + |
86 | 90 | private function release(SysvSemaphore $semaphore, bool $doRemove): void |
87 | 91 | { |
| 92 | + $key = array_search($semaphore, self::$semaphores, true); |
| 93 | + if ($key === false) { |
| 94 | + return; |
| 95 | + } |
| 96 | + unset(self::$semaphores[$key]); |
| 97 | + |
88 | 98 | if (!sem_release($semaphore)) { |
89 | | - $this->loggerUtil->init('semaphores', LogLevelEnum::ERROR); |
90 | | - $this->loggerUtil->log("Error releasing Semaphore!"); |
| 99 | + StuLogger::log(sprintf("Error releasing Semaphore with key %d!", $key), LogTypeEnum::SEMAPHORE); |
91 | 100 | return; |
92 | 101 | //throw new SemaphoreException("Error releasing Semaphore!"); |
| 102 | + } else { |
| 103 | + StuLogger::log(sprintf(' Released semaphore %d', $key), LogTypeEnum::SEMAPHORE); |
93 | 104 | } |
94 | 105 |
|
95 | 106 | if ($doRemove && !sem_remove($semaphore)) { |
96 | | - $this->loggerUtil->init('semaphores', LogLevelEnum::ERROR); |
97 | | - $this->loggerUtil->log("Error removing Semaphore!"); |
| 107 | + StuLogger::log(sprintf("Error removing Semaphore with key %d!", $key), LogTypeEnum::SEMAPHORE); |
98 | 108 | //throw new SemaphoreException("Error removing Semaphore!"); |
99 | 109 | } |
100 | 110 | } |
|
0 commit comments