@@ -45,7 +45,7 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
4545
4646[ ` Mutex::check() ` ] ( http://malkusch.github.io/lock/api/class-malkusch.lock.mutex.Mutex.html#_check )
4747performs a double-checked locking pattern. I.e. if the check fails, no lock
48- was acquired. Else if the check was true, a lock will be acquired and the
48+ will be acquired. Else if the check was true, a lock will be acquired and the
4949check will be perfomed as well together with the critical code.
5050
5151Example:
@@ -72,6 +72,8 @@ The `Mutex` is an abstract class. You will have to chose an implementation:
7272- [ ` SemaphoreMutex ` ] ( #semaphoremutex )
7373- [ ` TransactionalMutex ` ] ( #transactionalmutex )
7474- [ ` MySQLMutex ` ] ( #mysqlmutex )
75+ - [ ` PgAdvisoryLockMutex ` ] ( #pgadvisorylockmutex )
76+
7577
7678#### CASMutex
7779
@@ -237,7 +239,7 @@ $mutex->synchronized(function () use ($pdo, $accountId, $amount) {
237239
238240The ** MySQLMutex** uses MySQL's
239241[ ` GET_LOCK ` ] ( https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_get-lock )
240- function to create lock back end .
242+ function.
241243
242244It supports time outs. If the connection to the database server is lost or interrupted, the lock is
243245automatically released.
@@ -259,6 +261,31 @@ $mutex->synchronized(function () use ($bankAccount, $amount) {
259261 $bankAccount->setBalance($balance);
260262});
261263```
264+ #### PgAdvisoryLockMutex
265+
266+ The ** PgAdvisoryLockMutex** uses PostgreSQL's
267+ [ advisory locking] ( https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS )
268+ functions.
269+
270+ Named locks are offered. PostgreSQL locking functions require integers but the conversion is handled automatically.
271+
272+ No time outs are supported. If the connection to the database server is lost or interrupted, the lock is
273+ automatically released.
274+
275+ ``` php
276+ $pdo = new PDO("pgsql:host=localhost;dbname=test;", "username");
277+
278+ $mutex = new PgAdvisoryLockMutex($pdo, "balance");
279+ $mutex->synchronized(function () use ($bankAccount, $amount) {
280+ $balance = $bankAccount->getBalance();
281+ $balance -= $amount;
282+ if ($balance < 0) {
283+ throw new \DomainException("You have no credit.");
284+
285+ }
286+ $bankAccount->setBalance($balance);
287+ });
288+ ```
262289
263290
264291
0 commit comments