Skip to content

Commit 1ebd25c

Browse files
authored
Improve abstract mutex classes naming (#68)
1 parent bbc300c commit 1ebd25c

19 files changed

+73
-62
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ This library uses the namespace `Malkusch\Lock`.
4848

4949
### Mutex
5050

51-
The [`Malkusch\Lock\Mutex\Mutex`][5] class is an abstract class and provides the
52-
base API for this library.
51+
The [`Malkusch\Lock\Mutex\Mutex`][5] interface provides the base API for this library.
5352

5453
#### Mutex::synchronized()
5554

@@ -125,9 +124,9 @@ if ($newBalance === false) {
125124
}
126125
```
127126

128-
### Extracting code result after lock release exception
127+
#### Extracting code result after lock release exception
129128

130-
Mutex implementations based on [`Malkush\Lock\Mutex\LockMutex`][10] will throw
129+
Mutex implementations based on [`Malkush\Lock\Mutex\AbstractLockMutex`][10] will throw
131130
[`Malkusch\Lock\Exception\LockReleaseException`][11] in case of lock release
132131
problem, but the synchronized code block will be already executed at this point.
133132
In order to read the code result (or an exception thrown there),
@@ -160,9 +159,8 @@ try {
160159

161160
### Implementations
162161

163-
Because the [`Malkusch\Lock\Mutex\Mutex`](#mutex) class is an abstract class,
164-
you can choose from one of the provided implementations or create/extend your
165-
own implementation.
162+
You can choose from one of the provided [`Malkusch\Lock\Mutex\Mutex`](#mutex) interface
163+
implementations or create/extend your own implementation.
166164

167165
- [`FlockMutex`](#flockmutex)
168166
- [`MemcachedMutex`](#memcachedmutex)
@@ -171,7 +169,7 @@ own implementation.
171169
- [`SemaphoreMutex`](#semaphoremutex)
172170
- [`TransactionalMutex`](#transactionalmutex)
173171
- [`MySQLMutex`](#mysqlmutex)
174-
- [`PgAdvisoryLockMutex`](#pgadvisorylockmutex)
172+
- [`PostgreSQLMutex`](#PostgreSQLMutex)
175173

176174
#### FlockMutex
177175

@@ -342,9 +340,9 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
342340
});
343341
```
344342

345-
#### PgAdvisoryLockMutex
343+
#### PostgreSQLMutex
346344

347-
The **PgAdvisoryLockMutex** uses PostgreSQL's
345+
The **PostgreSQLMutex** uses PostgreSQL's
348346
[advisory locking](https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS)
349347
functions.
350348

@@ -357,7 +355,7 @@ interrupted, the lock is automatically released.
357355
```php
358356
$pdo = new \PDO('pgsql:host=localhost;dbname=test', 'username');
359357

360-
$mutex = new PgAdvisoryLockMutex($pdo, 'balance');
358+
$mutex = new PostgreSQLMutex($pdo, 'balance');
361359
$mutex->synchronized(function () use ($bankAccount, $amount) {
362360
$balance = $bankAccount->getBalance();
363361
$balance -= $amount;

src/Exception/ExecutionOutsideLockException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* Should only be used in contexts where the is being released.
1616
*
17-
* @see \Malkusch\Lock\Mutex\SpinlockMutex::unlock()
17+
* @see \Malkusch\Lock\Mutex\AbstractSpinlockMutex::unlock()
1818
*/
1919
class ExecutionOutsideLockException extends LockReleaseException
2020
{

src/Mutex/LockMutex.php renamed to src/Mutex/AbstractLockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @internal
1414
*/
15-
abstract class LockMutex extends Mutex
15+
abstract class AbstractLockMutex extends AbstractMutex
1616
{
1717
/**
1818
* Acquires the lock.

src/Mutex/AbstractMutex.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Malkusch\Lock\Mutex;
6+
7+
use Malkusch\Lock\Util\DoubleCheckedLocking;
8+
9+
abstract class AbstractMutex implements Mutex
10+
{
11+
#[\Override]
12+
public function check(callable $check): DoubleCheckedLocking
13+
{
14+
return new DoubleCheckedLocking($this, $check);
15+
}
16+
}

src/Mutex/SpinlockMutex.php renamed to src/Mutex/AbstractSpinlockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @internal
1717
*/
18-
abstract class SpinlockMutex extends LockMutex
18+
abstract class AbstractSpinlockMutex extends AbstractLockMutex
1919
{
2020
/** @var float The timeout in seconds a lock may live */
2121
private $timeout;

src/Mutex/FlockMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Flock() based mutex implementation.
1616
*/
17-
class FlockMutex extends LockMutex
17+
class FlockMutex extends AbstractLockMutex
1818
{
1919
public const INFINITE_TIMEOUT = -1.0;
2020

src/Mutex/MemcachedMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Memcached based spinlock implementation.
99
*/
10-
class MemcachedMutex extends SpinlockMutex
10+
class MemcachedMutex extends AbstractSpinlockMutex
1111
{
1212
/** @var \Memcached */
1313
private $memcache;

src/Mutex/Mutex.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use Malkusch\Lock\Util\DoubleCheckedLocking;
1111

1212
/**
13-
* The mutex provides methods for exclusive execution.
13+
* Mutex interface for exclusive execution.
1414
*/
15-
abstract class Mutex
15+
interface Mutex
1616
{
1717
/**
1818
* Executes a block of code exclusively.
@@ -35,7 +35,7 @@ abstract class Mutex
3535
* @throws LockReleaseException The mutex could not be released, the code was already executed
3636
* @throws ExecutionOutsideLockException Some code has been executed outside of the lock
3737
*/
38-
abstract public function synchronized(callable $code);
38+
public function synchronized(callable $code);
3939

4040
/**
4141
* Performs a double-checked locking pattern.
@@ -57,8 +57,5 @@ abstract public function synchronized(callable $code);
5757
*
5858
* @return DoubleCheckedLocking The double-checked locking pattern
5959
*/
60-
public function check(callable $check): DoubleCheckedLocking
61-
{
62-
return new DoubleCheckedLocking($this, $check);
63-
}
60+
public function check(callable $check): DoubleCheckedLocking;
6461
}

src/Mutex/MySQLMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Malkusch\Lock\Exception\TimeoutException;
99
use Malkusch\Lock\Util\LockUtil;
1010

11-
class MySQLMutex extends LockMutex
11+
class MySQLMutex extends AbstractLockMutex
1212
{
1313
/** @var \PDO */
1414
private $pdo;

src/Mutex/NoMutex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Synchronization is not provided! This mutex is just implementing the
1111
* interface without locking.
1212
*/
13-
class NoMutex extends Mutex
13+
class NoMutex extends AbstractMutex
1414
{
1515
#[\Override]
1616
public function synchronized(callable $code)

0 commit comments

Comments
 (0)