Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ composer require malkusch/lock

## Usage

This library uses the namespace `malkusch\lock`.
This library uses the namespace `Malkusch\Lock`.

### Mutex

The [`malkusch\lock\mutex\Mutex`][5] class is an abstract class and provides the
The [`Malkusch\Lock\Mutex\Mutex`][5] class is an abstract class and provides the
base API for this library.

#### Mutex::synchronized()

[`malkusch\lock\mutex\Mutex::synchronized()`][6] executes code exclusively. This
[`Malkusch\Lock\Mutex\Mutex::synchronized()`][6] executes code exclusively. This
method guarantees that the code is only executed by one process at once. Other
processes have to wait until the mutex is available. The critical code may throw
an exception, which would release the lock as well.
Expand Down Expand Up @@ -82,8 +82,8 @@ $newBalance = $mutex->synchronized(function () use (

#### Mutex::check()

[`malkusch\lock\mutex\Mutex::check()`][7] sets a callable, which will be
executed when [`malkusch\lock\util\DoubleCheckedLocking::then()`][8] is called,
[`Malkusch\Lock\Mutex\Mutex::check()`][7] sets a callable, which will be
executed when [`Malkusch\Lock\Util\DoubleCheckedLocking::then()`][8] is called,
and performs a double-checked locking pattern, where it's return value decides
if the lock needs to be acquired and the synchronized code to be executed.

Expand All @@ -92,12 +92,12 @@ detailed explanation of that feature.

If the check's callable returns `false`, no lock will be acquired and the
synchronized code will not be executed. In this case the
[`malkusch\lock\util\DoubleCheckedLocking::then()`][8] method, will also return
[`Malkusch\Lock\Util\DoubleCheckedLocking::then()`][8] method, will also return
`false` to indicate that the check did not pass either before or after acquiring
the lock.

In the case where the check's callable returns a value other than `false`, the
[`malkusch\lock\util\DoubleCheckedLocking::then()`][8] method, will
[`Malkusch\Lock\Util\DoubleCheckedLocking::then()`][8] method, will
try to acquire the lock and on success will perform the check again. Only when
the check returns something other than `false` a second time, the synchronized
code callable, which has been passed to `then()` will be executed. In this case
Expand Down Expand Up @@ -127,8 +127,8 @@ if ($newBalance === false) {

### Extracting code result after lock release exception

Mutex implementations based on [`malkush\lock\mutex\LockMutex`][12] will throw
[`malkusch\lock\exception\LockReleaseException`][13] in case of lock release
Mutex implementations based on [`Malkush\Lock\Mutex\LockMutex`][12] will throw
[`Malkusch\Lock\Exception\LockReleaseException`][13] in case of lock release
problem, but the synchronized code block will be already executed at this point.
In order to read the code result (or an exception thrown there),
`LockReleaseException` provides methods to extract it.
Expand Down Expand Up @@ -160,7 +160,7 @@ try {

### Implementations

Because the [`malkusch\lock\mutex\Mutex`](#mutex) class is an abstract class,
Because the [`Malkusch\Lock\Mutex\Mutex`](#mutex) class is an abstract class,
you can choose from one of the provided implementations or create/extend your
own implementation.

Expand All @@ -179,7 +179,7 @@ own implementation.
The **CASMutex** has to be used with a [Compare-and-swap][10] operation. This
mutex is lock free. It will repeat executing the code until the CAS operation
was successful. The code should therefore notify the mutex by calling
[`malkusch\lock\mutex\CASMutex::notify()`][11].
[`Malkusch\Lock\Mutex\CASMutex::notify()`][11].

As the mutex keeps executing the critical code, it must not have any side
effects as long as the CAS operation was not successful.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
"prefer-stable": true,
"autoload": {
"psr-4": {
"malkusch\\lock\\": "src/"
"Malkusch\\Lock\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"malkusch\\lock\\Tests\\": "tests/"
"Malkusch\\Lock\\Tests\\": "tests/"
}
},
"config": {
Expand Down
14 changes: 7 additions & 7 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ parameters:
ignoreErrors:
# TODO
-
path: 'src/mutex/RedisMutex.php'
path: 'src/Mutex/RedisMutex.php'
identifier: if.condNotBoolean
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
count: 1
-
path: 'src/mutex/TransactionalMutex.php'
path: 'src/Mutex/TransactionalMutex.php'
identifier: if.condNotBoolean
message: '~^Only booleans are allowed in an if condition, mixed given\.$~'
count: 1
-
message: '~^Parameter #1 \$(redisAPI|redis) \(Redis\|RedisCluster\) of method malkusch\\lock\\mutex\\PHPRedisMutex::(add|evalScript)\(\) should be contravariant with parameter \$redisAPI \(mixed\) of method malkusch\\lock\\mutex\\RedisMutex::(add|evalScript)\(\)$~'
message: '~^Parameter #1 \$(redisAPI|redis) \(Redis\|RedisCluster\) of method Malkusch\\Lock\\Mutex\\PHPRedisMutex::(add|evalScript)\(\) should be contravariant with parameter \$redisAPI \(mixed\) of method Malkusch\\Lock\\Mutex\\RedisMutex::(add|evalScript)\(\)$~'
identifier: method.childParameterType
path: 'src/mutex/PHPRedisMutex.php'
path: 'src/Mutex/PHPRedisMutex.php'
count: 2
-
message: '~^Parameter #1 \$(redisAPI|client) \(Predis\\ClientInterface\) of method malkusch\\lock\\mutex\\PredisMutex::(add|evalScript)\(\) should be contravariant with parameter \$redisAPI \(mixed\) of method malkusch\\lock\\mutex\\RedisMutex::(add|evalScript)\(\)$~'
message: '~^Parameter #1 \$(redisAPI|client) \(Predis\\ClientInterface\) of method Malkusch\\Lock\\Mutex\\PredisMutex::(add|evalScript)\(\) should be contravariant with parameter \$redisAPI \(mixed\) of method Malkusch\\Lock\\Mutex\\RedisMutex::(add|evalScript)\(\)$~'
identifier: method.childParameterType
path: 'src/mutex/PredisMutex.php'
path: 'src/Mutex/PredisMutex.php'
count: 2
-
path: 'tests/mutex/*Test.php'
path: 'tests/Mutex/*Test.php'
identifier: empty.notAllowed
message: '~^Construct empty\(\) is not allowed\. Use more strict comparison\.$~'
count: 6
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

class DeadlineException extends \RuntimeException implements PhpLockException {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

/**
* Execution outside lock exception.
Expand All @@ -14,7 +14,7 @@
*
* Should only be used in contexts where the is being released.
*
* @see \malkusch\lock\mutex\SpinlockMutex::unlock()
* @see \Malkusch\Lock\Mutex\SpinlockMutex::unlock()
*/
class ExecutionOutsideLockException extends LockReleaseException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

/**
* Lock acquire exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

/**
* Lock release exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

/**
* Mutex exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

/**
* Common php-lock/lock exception interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\exception;
namespace Malkusch\Lock\Exception;

/**
* Timeout exception.
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/CASMutex.php → src/Mutex/CASMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\TimeoutException;
use malkusch\lock\util\Loop;
use Malkusch\Lock\Exception\TimeoutException;
use Malkusch\Lock\Util\Loop;

/**
* CAS based mutex implementation.
Expand Down
16 changes: 8 additions & 8 deletions src/mutex/FlockMutex.php → src/Mutex/FlockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;

use malkusch\lock\exception\DeadlineException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\exception\TimeoutException;
use malkusch\lock\util\Loop;
use malkusch\lock\util\PcntlTimeout;
namespace Malkusch\Lock\Mutex;

use Malkusch\Lock\Exception\DeadlineException;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;
use Malkusch\Lock\Exception\TimeoutException;
use Malkusch\Lock\Util\Loop;
use Malkusch\Lock\Util\PcntlTimeout;

/**
* Flock() based mutex implementation.
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/LockMutex.php → src/Mutex/LockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;

/**
* Locking mutex.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

/**
* Memcached based spinlock implementation.
Expand Down
12 changes: 6 additions & 6 deletions src/mutex/Mutex.php → src/Mutex/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\ExecutionOutsideLockException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\util\DoubleCheckedLocking;
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.
Expand Down Expand Up @@ -40,7 +40,7 @@ abstract public function synchronized(callable $code);
/**
* Performs a double-checked locking pattern.
*
* Call {@link \malkusch\lock\util\DoubleCheckedLocking::then()} on the
* Call {@link \Malkusch\Lock\Util\DoubleCheckedLocking::then()} on the
* returned object.
*
* Example:
Expand Down
8 changes: 4 additions & 4 deletions src/mutex/MySQLMutex.php → src/Mutex/MySQLMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\TimeoutException;
use malkusch\lock\util\LockUtil;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\TimeoutException;
use Malkusch\Lock\Util\LockUtil;

class MySQLMutex extends LockMutex
{
Expand Down
2 changes: 1 addition & 1 deletion src/mutex/NoMutex.php → src/Mutex/NoMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

/**
* This mutex doesn't lock at all.
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/PHPRedisMutex.php → src/Mutex/PHPRedisMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;

/**
* Mutex based on the Redlock algorithm using the phpredis extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\util\LockUtil;
use Malkusch\Lock\Util\LockUtil;

class PgAdvisoryLockMutex extends LockMutex
{
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/PredisMutex.php → src/Mutex/PredisMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;
use Predis\ClientInterface;
use Predis\PredisException;

Expand Down
8 changes: 4 additions & 4 deletions src/mutex/RedisMutex.php → src/Mutex/RedisMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\util\LockUtil;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;
use Malkusch\Lock\Util\LockUtil;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
Expand Down
6 changes: 3 additions & 3 deletions src/mutex/SemaphoreMutex.php → src/Mutex/SemaphoreMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;

/**
* System V IPC based mutex implementation.
Expand Down
12 changes: 6 additions & 6 deletions src/mutex/SpinlockMutex.php → src/Mutex/SpinlockMutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace malkusch\lock\mutex;
namespace Malkusch\Lock\Mutex;

use malkusch\lock\exception\ExecutionOutsideLockException;
use malkusch\lock\exception\LockAcquireException;
use malkusch\lock\exception\LockReleaseException;
use malkusch\lock\util\LockUtil;
use malkusch\lock\util\Loop;
use Malkusch\Lock\Exception\ExecutionOutsideLockException;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockReleaseException;
use Malkusch\Lock\Util\LockUtil;
use Malkusch\Lock\Util\Loop;

/**
* Spinlock implementation.
Expand Down
Loading