Skip to content

Commit f248420

Browse files
committed
[Sempahore] Added first round of documentation
1 parent 94656d4 commit f248420

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

components/semaphore.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. index::
2+
single: Semaphore
3+
single: Components; Semaphore
4+
5+
The Semaphore Component
6+
=======================
7+
8+
The Semaphore Component manages `semaphores`_, a mechanism to provide
9+
exclusive access to a shared resource.
10+
11+
Installation
12+
------------
13+
14+
.. code-block:: terminal
15+
16+
$ composer require symfony/semaphore
17+
18+
.. include:: /components/require_autoload.rst.inc
19+
20+
Usage
21+
-----
22+
23+
Semaphore are used to guarantee exclusive access to some shared resource.
24+
25+
Semaphore are created using a :class:`Symfony\\Component\\Semaphore\\SemaphoreFactory` class,
26+
which in turn requires another class to manage the storage of Semaphore::
27+
28+
use Symfony\Component\Semaphore\SemaphoreFactory;
29+
use Symfony\Component\Semaphore\Store\RedisStore;
30+
31+
$redis = new Redis();
32+
$redis->connect('172.17.0.2');
33+
34+
$store = new RedisStore($redis);
35+
$factory = new SemaphoreFactory(new RedisStore($redis));
36+
37+
38+
The semaphore is created by calling the
39+
:method:`Symfony\\Component\\Semaphore\\SemaphoreFactory::createSemaphore`
40+
method. Its first argument is an arbitrary string that represents the locked
41+
resource. Its second argument is the number of process allowed. Then, a call to
42+
the :method:`Symfony\\Component\\Semaphore\\SemaphoreInterface::acquire` method
43+
will try to acquire the semaphore::
44+
45+
// ...
46+
$semaphore = $factory->createSemaphore('pdf-invoice-generation', 2);
47+
48+
if ($semaphore->acquire()) {
49+
// The resource "pdf-invoice-generation" is locked.
50+
// You can compute and generate invoice safely here.
51+
52+
$semaphore->release();
53+
}
54+
55+
If the semaphore can not be acquired, the method returns ``false``. The
56+
``acquire()`` method can be safely called repeatedly, even if the semaphore is
57+
already acquired.
58+
59+
.. note::
60+
61+
Unlike other implementations, the Semaphore Component distinguishes
62+
semaphores instances even when they are created for the same resource. If a
63+
semaphore has to be used by several services, they should share the same
64+
``Semaphore`` instance returned by the ``SemaphoreFactory::createSemaphore``
65+
method.
66+
67+
.. tip::
68+
69+
If you don't release the semaphore explicitly, it will be released
70+
automatically on instance destruction. In some cases, it can be useful to
71+
lock a resource across several requests. To disable the automatic release
72+
behavior, set the last argument of the ``createLock()`` method to
73+
``false``.
74+
75+
.. _`semaphores`: https://en.wikipedia.org/wiki/Semaphore_(programming)

0 commit comments

Comments
 (0)