Skip to content

[Lock] Added paragraph about auto release #15049

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 1 commit into from
Mar 5, 2021
Merged
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
32 changes: 32 additions & 0 deletions components/lock.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,38 @@ This component also provides two useful methods related to expiring locks:
``getRemainingLifetime()`` (which returns ``null`` or a ``float``
as seconds) and ``isExpired()`` (which returns a boolean).

Automatically Releasing The Lock
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A Lock will be automatically released when the Lock object is destructed. This is
an implementation detail that will be important when Locks are shared between processes.
In the example below, the ``pcntl_fork()`` will create two processes and the Lock
will be released automatically as soon as one process finishes::

// ...
$lock = $factory->createLock('report-generation', 3600);
if (!$lock->acquire()) {
return;
}

$pid = pcntl_fork();
if ($pid == -1) {
// Could not fork
exit(1);
} elseif ($pid) {
// Parent process
sleep(30);
} else {
// Child process
echo 'The lock will be released now.'
exit(0);
}
// ...

To disable this behavior, one needs to set the 3rd argument to the ``LockFactory::createLock()``
to false. That will make the Lock acquired for 3600 seconds or until ``Lock::release()``
is called.

The Owner of The Lock
---------------------

Expand Down