1e6042d41ea52927b949a3dd2e8c7f9431b20ba4
[yaffs-website] / web / core / lib / Drupal / Core / Lock / LockBackendInterface.php
1 <?php
2
3 namespace Drupal\Core\Lock;
4
5 /**
6  * @defgroup lock Locking mechanisms
7  * @{
8  * Functions to coordinate long operations across requests.
9  *
10  * In most environments, multiple Drupal page requests (a.k.a. threads or
11  * processes) will execute in parallel. This leads to potential conflicts or
12  * race conditions when two requests execute the same code at the same time. For
13  * instance, some implementations of hook_cron() implicitly assume they are
14  * running only once, rather than having multiple calls in parallel. To prevent
15  * problems with such code, the cron system uses a locking process to ensure
16  * that cron is not started again if it is already running.
17  *
18  * To avoid these types of conflicts, Drupal has a cooperative, advisory lock
19  * system. Any long-running operation that could potentially be attempted in
20  * parallel by multiple requests should try to acquire a lock before
21  * proceeding. By obtaining a lock, one request notifies any other requests that
22  * a specific operation is in progress which must not be executed in parallel.
23  *
24  * To use this API, pick a unique name for the lock. A sensible choice is the
25  * name of the function performing the operation. Here is a simple example:
26  * @code
27  * function mymodule_long_operation() {
28  *   $lock = \Drupal::lock();
29  *   if ($lock->acquire('mymodule_long_operation')) {
30  *     // Do the long operation here.
31  *     // ...
32  *     $lock->release('mymodule_long_operation');
33  *   }
34  * }
35  * @endcode
36  *
37  * If a function acquires a lock it should always release it when the operation
38  * is complete by calling $lock->release(), as in the example.
39  *
40  * A function that has acquired a lock may attempt to renew a lock (extend the
41  * duration of the lock) by calling $lock->acquire() again during the operation.
42  * Failure to renew a lock is indicative that another request has acquired the
43  * lock, and that the current operation may need to be aborted.
44  *
45  * If a function fails to acquire a lock it may either immediately return, or
46  * it may call $lock->wait() if the rest of the current page request requires
47  * that the operation in question be complete. After $lock->wait() returns, the
48  * function may again attempt to acquire the lock, or may simply allow the page
49  * request to proceed on the assumption that a parallel request completed the
50  * operation.
51  *
52  * $lock->acquire() and $lock->wait() will automatically break (delete) a lock
53  * whose duration has exceeded the timeout specified when it was acquired.
54  *
55  * The following limitations in this implementation should be carefully noted:
56  * - Time: Timestamps are derived from the local system clock of the environment
57  *   the code is executing in. The orderly progression of time from this
58  *   viewpoint can be disrupted by external events such as NTP synchronization
59  *   and operator intervention. Where multiple web servers are involved in
60  *   serving the site, they will have their own independent clocks, introducing
61  *   another source of error in the time keeping process. Timeout values applied
62  *   to locks must therefore be considered approximate, and should not be relied
63  *   upon.
64  * - Uniqueness: Uniqueness of lock names is not enforced. The impact of the
65  *   use of a common lock name will depend on what processes and resources the
66  *   lock is being used to manage.
67  * - Sharing: There is limited support for resources shared across sites.
68  *   The locks are stored as rows in the semaphore table and, as such, they
69  *   have the same visibility as the table. If resources managed by a lock are
70  *   shared across sites then the semaphore table must be shared across sites
71  *   as well. This is a binary situation: either all resources are shared and
72  *   the semaphore table is shared or no resources are shared and the semaphore
73  *   table is not shared. Mixed mode operation is not supported.
74  *
75  * @} End of "defgroup lock".
76  */
77
78 /**
79  * Lock backend interface.
80  *
81  * @ingroup lock
82  */
83 interface LockBackendInterface {
84
85   /**
86    * Acquires a lock.
87    *
88    * @param string $name
89    *   Lock name. Limit of name's length is 255 characters.
90    * @param float $timeout
91    *   (optional) Lock lifetime in seconds. Defaults to 30.0.
92    *
93    * @return bool
94    */
95   public function acquire($name, $timeout = 30.0);
96
97   /**
98    * Checks if a lock is available for acquiring.
99    *
100    * @param string $name
101    *   Lock to acquire.
102    *
103    * @return bool
104    */
105   public function lockMayBeAvailable($name);
106
107   /**
108    * Waits a short amount of time before a second lock acquire attempt.
109    *
110    * While this method is subject to have a generic implementation in abstract
111    * backend implementation, some backends may provide non blocking or less I/O
112    * intensive wait mechanism: this is why this method remains on the backend
113    * interface.
114    *
115    * @param string $name
116    *   Lock name currently being locked.
117    * @param int $delay
118    *   Milliseconds to wait for. Defaults to 30.
119    *
120    * @return bool
121    *   TRUE if the lock holds, FALSE if it may be available. You still need to
122    *   acquire the lock manually and it may fail again.
123    */
124   public function wait($name, $delay = 30);
125
126   /**
127    * Releases the given lock.
128    *
129    * @param string $name
130    */
131   public function release($name);
132
133   /**
134    * Releases all locks for the given lock token identifier.
135    *
136    * @param string $lockId
137    *   (optional) If none given, remove all locks from the current page.
138    *   Defaults to NULL.
139    */
140   public function releaseAll($lockId = NULL);
141
142   /**
143    * Gets the unique page token for locks.
144    *
145    * Locks will be wiped out at the end of each page request on a token basis.
146    *
147    * @return string
148    */
149   public function getLockId();
150
151 }