Skip to content

Remove CASMutex class #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2024
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
35 changes: 4 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
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`][10] will throw
[`Malkusch\Lock\Exception\LockReleaseException`][11] 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 @@ -164,7 +164,6 @@ 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.

- [`CASMutex`](#casmutex)
- [`FlockMutex`](#flockmutex)
- [`MemcachedMutex`](#memcachedmutex)
- [`PHPRedisMutex`](#phpredismutex)
Expand All @@ -174,30 +173,6 @@ own implementation.
- [`MySQLMutex`](#mysqlmutex)
- [`PgAdvisoryLockMutex`](#pgadvisorylockmutex)

#### CASMutex

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].

As the mutex keeps executing the critical code, it must not have any side
effects as long as the CAS operation was not successful.

Example:

```php
$mutex = new CASMutex();
$mutex->synchronized(function () use ($memcached, $mutex, $amount): void {
$balance = $memcached->get('balance', null, $casToken);
$balance -= $amount;
if (!$memcached->cas($casToken, 'balance', $balance)) {
return;
}
$mutex->notify();
});
```

#### FlockMutex

The **FlockMutex** is a lock implementation based on
Expand Down Expand Up @@ -406,7 +381,5 @@ This project is free and is licensed under the MIT.
[7]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/Mutex.php#L60
[8]: https://github.com/php-lock/lock/blob/35526aee28/src/util/DoubleCheckedLocking.php#L63
[9]: https://en.wikipedia.org/wiki/Double-checked_locking
[10]: https://en.wikipedia.org/wiki/Compare-and-swap
[11]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/CASMutex.php#L42
[12]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/LockMutex.php
[13]: https://github.com/php-lock/lock/blob/35526aee28/src/exception/LockReleaseException.php
[10]: https://github.com/php-lock/lock/blob/35526aee28/src/mutex/LockMutex.php
[11]: https://github.com/php-lock/lock/blob/35526aee28/src/exception/LockReleaseException.php
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"redlock",
"memcache",
"redis",
"cas",
"advisory-locks",
"mysql",
"postgresql"
Expand Down
75 changes: 0 additions & 75 deletions src/Mutex/CASMutex.php

This file was deleted.

92 changes: 0 additions & 92 deletions tests/Mutex/CASMutexTest.php

This file was deleted.

44 changes: 44 additions & 0 deletions tests/Mutex/FixCiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Malkusch\Lock\Tests\Mutex;

use phpmock\environment\SleepEnvironmentBuilder;
use phpmock\MockEnabledException;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\TestCase;

/**
* This class is currently needed to pass the tests in CI.
*
* I do not know why yet. TODO remove it asap.
*/
class FixCiTest extends TestCase
{
use PHPMock;

#[\Override]
protected function setUp(): void
{
parent::setUp();

$sleepBuilder = new SleepEnvironmentBuilder();
$sleepBuilder->addNamespace(__NAMESPACE__);
$sleepBuilder->addNamespace('Malkusch\Lock\Mutex');
$sleepBuilder->addNamespace('Malkusch\Lock\Util');
$sleep = $sleepBuilder->build();
try {
$sleep->enable();
$this->registerForTearDown($sleep);
} catch (MockEnabledException $e) {
// workaround for burn testing
\assert($e->getMessage() === 'microtime is already enabled.Call disable() on the existing mock.');
}
}

public function testDummy(): void
{
self::assertTrue(microtime(true) > 1.0);
}
}
Loading