3 namespace Drupal\Core\Lock;
6 * @defgroup lock Locking mechanisms
8 * Functions to coordinate long operations across requests.
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.
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.
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:
27 * function mymodule_long_operation() {
28 * $lock = \Drupal::lock();
29 * if ($lock->acquire('mymodule_long_operation')) {
30 * // Do the long operation here.
32 * $lock->release('mymodule_long_operation');
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.
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.
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
52 * $lock->acquire() and $lock->wait() will automatically break (delete) a lock
53 * whose duration has exceeded the timeout specified when it was acquired.
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
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.
75 * @} End of "defgroup lock".
79 * Lock backend interface.
83 interface LockBackendInterface {
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.
95 public function acquire($name, $timeout = 30.0);
98 * Checks if a lock is available for acquiring.
100 * @param string $name
105 public function lockMayBeAvailable($name);
108 * Waits a short amount of time before a second lock acquire attempt.
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
115 * @param string $name
116 * Lock name currently being locked.
118 * Milliseconds to wait for. Defaults to 30.
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.
124 public function wait($name, $delay = 30);
127 * Releases the given lock.
129 * @param string $name
131 public function release($name);
134 * Releases all locks for the given lock token identifier.
136 * @param string $lockId
137 * (optional) If none given, remove all locks from the current page.
140 public function releaseAll($lockId = NULL);
143 * Gets the unique page token for locks.
145 * Locks will be wiped out at the end of each page request on a token basis.
149 public function getLockId();