![]() 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/mutex/ |
<?php namespace malkusch\lock\mutex; use malkusch\lock\exception\ExecutionOutsideLockException; use malkusch\lock\exception\LockAcquireException; use malkusch\lock\exception\LockReleaseException; use malkusch\lock\util\DoubleCheckedLocking; /** * The mutex provides methods for exclusive execution. * * @author Markus Malkusch <[email protected]> * @link bitcoin:1P5FAZ4QhXCuwYPnLZdk3PJsqePbu1UDDA Donations * @license WTFPL */ abstract class Mutex { /** * Executes a block of code exclusively. * * This method implements Java's synchronized semantic. I.e. this method * waits until a lock could be acquired, executes the code exclusively and * releases the lock. * * The code block may throw an exception. In this case the lock will be * released as well. * * @param callable $code The synchronized execution block. * @return mixed The return value of the execution block. * * @throws \Exception The execution block threw an exception. * @throws LockAcquireException The mutex could not be acquired, no further side effects. * @throws LockReleaseException The mutex could not be released, the code was already executed. * @throws ExecutionOutsideLockException Some code was executed outside of the lock. */ abstract public function synchronized(callable $code); /** * Performs a double-checked locking pattern. * * Call {@link DoubleCheckedLocking::then()} on the returned object. * * Example: * <code> * $mutex->check(function () use ($bankAccount, $amount) { * return $bankAccount->getBalance() >= $amount; * * })->then(function () use ($bankAccount, $amount) { * $bankAccount->withdraw($amount); * }); * </code> * * @return DoubleCheckedLocking The double-checked locking pattern. */ public function check(callable $check) { $doubleCheckedLocking = new DoubleCheckedLocking($this); $doubleCheckedLocking->setCheck($check); return $doubleCheckedLocking; } }