![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/vendor/malkusch/lock/classes/util/ |
<?php namespace malkusch\lock\util; use malkusch\lock\exception\LockAcquireException; use malkusch\lock\exception\LockReleaseException; use malkusch\lock\mutex\Mutex; /** * The double-checked locking pattern. * * You should not instantiate this class directly. Use {@link Mutex::check()}. * * @author Markus Malkusch <[email protected]> * @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations * @license WTFPL */ class DoubleCheckedLocking { /** * @var Mutex The mutex. */ private $mutex; /** * @var callable The check. */ private $check; /** * Sets the mutex. * * @param Mutex $mutex The mutex. * @internal */ public function __construct(Mutex $mutex) { $this->mutex = $mutex; } /** * Sets the check. * * @param callable $check The check. * @internal */ public function setCheck(callable $check) { $this->check = $check; } /** * Executes a code only if a check is true. * * Both the check and the code execution are locked by a mutex. * Only if the check fails the method returns before acquiring a lock. * * @param callable $code The locked code. * * @throws \Exception The execution block or the check threw an exception. * @throws LockAcquireException The mutex could not be acquired. * @throws LockReleaseException The mutex could not be released. */ public function then(callable $code) { if (!call_user_func($this->check)) { return; } $this->mutex->synchronized(function () use ($code) { if (call_user_func($this->check)) { call_user_func($code); } }); } }