diff -u b/core/includes/common.inc b/core/includes/common.inc --- b/core/includes/common.inc +++ b/core/includes/common.inc @@ -3200,7 +3200,7 @@ drupal_alter('queue_info', $queues); // Try to acquire cron lock. - if (!lock()->acquire('cron', 240.0)) { + if (!\Drupal::lock()->acquire('cron', 240.0)) { // Cron is still running normally. watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING); } @@ -3229,7 +3229,7 @@ watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE); // Release cron lock. - lock()->release('cron'); + \Drupal::lock()->release('cron'); // Return TRUE so other functions can check if it did run successfully $return = TRUE; @@ -3262,7 +3262,7 @@ watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE); // Release cron lock. - Drupal::lock()->release('cron'); + lock()->release('cron'); // Return TRUE so other functions can check if it did run successfully $return = TRUE; diff -u b/core/includes/menu.inc b/core/includes/menu.inc --- b/core/includes/menu.inc +++ b/core/includes/menu.inc @@ -2521,11 +2521,11 @@ * in parallel and the current thread just waited for completion. */ function menu_router_rebuild() { - if (!lock()->acquire(__FUNCTION__)) { + if (!\Drupal::lock()->acquire(__FUNCTION__)) { // Wait for another request that is already doing this work. // We choose to block here since otherwise the router item may not // be available during routing resulting in a 404. - lock()->wait(__FUNCTION__); + \Drupal::lock()->wait(__FUNCTION__); return FALSE; } @@ -2545,7 +2545,7 @@ watchdog_exception('menu', $e); } - lock()->release(__FUNCTION__); + \Drupal::lock()->release(__FUNCTION__); return TRUE; } @@ -2715,11 +2715,11 @@ * in parallel and the current thread just waited for completion. */ function menu_router_rebuild() { - if (!Drupal::lock()->acquire(__FUNCTION__)) { + if (!lock()->acquire(__FUNCTION__)) { // Wait for another request that is already doing this work. // We choose to block here since otherwise the router item may not // be available during routing resulting in a 404. - Drupal::lock()->wait(__FUNCTION__); + lock()->wait(__FUNCTION__); return FALSE; } @@ -2739,7 +2739,7 @@ watchdog_exception('menu', $e); } - Drupal::lock()->release(__FUNCTION__); + lock()->release(__FUNCTION__); return TRUE; } diff -u b/core/lib/Drupal/Core/Utility/CacheArray.php b/core/lib/Drupal/Core/Utility/CacheArray.php --- b/core/lib/Drupal/Core/Utility/CacheArray.php +++ b/core/lib/Drupal/Core/Utility/CacheArray.php @@ -201,13 +201,13 @@ // Lock cache writes to help avoid stampedes. // To implement locking for cache misses, override __construct(). $lock_name = $this->cid . ':' . $this->bin; - if (!$lock || Drupal::lock()->acquire($lock_name)) { + if (!$lock || \Drupal::lock()->acquire($lock_name)) { if ($cached = cache($this->bin)->get($this->cid)) { $data = $cached->data + $data; } cache($this->bin)->set($this->cid, $data, CacheBackendInterface::CACHE_PERMANENT, $this->tags); if ($lock) { - Drupal::lock()->release($lock_name); + \Drupal::lock()->release($lock_name); } } } reverted: --- b/core/lib/Drupal/Core/Utility/ThemeRegistry.php +++ a/core/lib/Drupal/Core/Utility/ThemeRegistry.php @@ -126,7 +126,7 @@ */ public function set($data, $lock = TRUE) { $lock_name = $this->cid . ':' . $this->bin; + if (!$lock || lock()->acquire($lock_name)) { - if (!$lock || Drupal::lock()->acquire($lock_name)) { if ($cached = cache($this->bin)->get($this->cid)) { // Use array merge instead of union so that filled in values in $data // overwrite empty values in the current cache. @@ -138,7 +138,7 @@ } cache($this->bin)->set($this->cid, $data, CacheBackendInterface::CACHE_PERMANENT, $this->tags); if ($lock) { + lock()->release($lock_name); - Drupal::lock()->release($lock_name); } } } reverted: --- b/core/modules/comment/lib/Drupal/comment/Plugin/Core/Entity/Comment.php +++ a/core/modules/comment/lib/Drupal/comment/Plugin/Core/Entity/Comment.php @@ -327,7 +327,7 @@ */ protected function releaseThreadLock() { if ($this->threadLock) { + lock()->release($this->threadLock); - Drupal::lock()->release($this->threadLock); $this->threadLock = ''; } } diff -u b/core/modules/system/lib/Drupal/system/Tests/Lock/LockFunctionalTest.php b/core/modules/system/lib/Drupal/system/Tests/Lock/LockFunctionalTest.php --- b/core/modules/system/lib/Drupal/system/Tests/Lock/LockFunctionalTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Lock/LockFunctionalTest.php @@ -36,33 +36,33 @@ $lock_acquired = 'TRUE: Lock successfully acquired in system_test_lock_acquire()'; $lock_not_acquired = 'FALSE: Lock not acquired in system_test_lock_acquire()'; - $this->assertTrue(lock()->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock'); - $this->assertTrue(lock()->acquire('system_test_lock_acquire'), 'Lock extended by this request.', 'Lock'); - Drupal::lock()->release('system_test_lock_acquire'); + $this->assertTrue(\Drupal::lock()->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock'); + $this->assertTrue(\Drupal::lock()->acquire('system_test_lock_acquire'), 'Lock extended by this request.', 'Lock'); + \Drupal::lock()->release('system_test_lock_acquire'); // Cause another request to acquire the lock. $this->drupalGet('system-test/lock-acquire'); $this->assertText($lock_acquired, 'Lock acquired by the other request.', 'Lock'); // The other request has finished, thus it should have released its lock. - $this->assertTrue(Drupal::lock()->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock'); + $this->assertTrue(\Drupal::lock()->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock'); // This request holds the lock, so the other request cannot acquire it. $this->drupalGet('system-test/lock-acquire'); $this->assertText($lock_not_acquired, 'Lock not acquired by the other request.', 'Lock'); - Drupal::lock()->release('system_test_lock_acquire'); + \Drupal::lock()->release('system_test_lock_acquire'); // Try a very short timeout and lock breaking. - $this->assertTrue(Drupal::lock()->acquire('system_test_lock_acquire', 0.5), 'Lock acquired by this request.', 'Lock'); + $this->assertTrue(\Drupal::lock()->acquire('system_test_lock_acquire', 0.5), 'Lock acquired by this request.', 'Lock'); sleep(1); // The other request should break our lock. $this->drupalGet('system-test/lock-acquire'); $this->assertText($lock_acquired, 'Lock acquired by the other request, breaking our lock.', 'Lock'); // We cannot renew it, since the other thread took it. - $this->assertFalse(Drupal::lock()->acquire('system_test_lock_acquire'), 'Lock cannot be extended by this request.', 'Lock'); + $this->assertFalse(\Drupal::lock()->acquire('system_test_lock_acquire'), 'Lock cannot be extended by this request.', 'Lock'); // Check the shut-down function. $lock_acquired_exit = 'TRUE: Lock successfully acquired in system_test_lock_exit()'; $lock_not_acquired_exit = 'FALSE: Lock not acquired in system_test_lock_exit()'; $this->drupalGet('system-test/lock-exit'); $this->assertText($lock_acquired_exit, 'Lock acquired by the other request before exit.', 'Lock'); - $this->assertTrue(lock()->acquire('system_test_lock_exit'), 'Lock acquired by this request after the other request exits.', 'Lock'); + $this->assertTrue(\Drupal::lock()->acquire('system_test_lock_exit'), 'Lock acquired by this request after the other request exits.', 'Lock'); } } diff -u b/core/modules/system/tests/modules/system_test/system_test.module b/core/modules/system/tests/modules/system_test/system_test.module --- b/core/modules/system/tests/modules/system_test/system_test.module +++ b/core/modules/system/tests/modules/system_test/system_test.module @@ -77,8 +77,8 @@ * @deprecated \Drupal\system_test\Controller\SystemTestController::lockAcquire() */ function system_test_lock_acquire() { - if (lock()->acquire('system_test_lock_acquire')) { - lock()->release('system_test_lock_acquire'); + if (\Drupal::lock()->acquire('system_test_lock_acquire')) { + \Drupal::lock()->release('system_test_lock_acquire'); return 'TRUE: Lock successfully acquired in system_test_lock_acquire()'; } else { @@ -92,7 +92,7 @@ * @deprecated \Drupal\system_test\Controller\SystemTestController::lockExit() */ function system_test_lock_exit() { - if (lock()->acquire('system_test_lock_exit', 900)) { + if (\Drupal::lock()->acquire('system_test_lock_exit', 900)) { echo 'TRUE: Lock successfully acquired in system_test_lock_exit()'; // The shut-down function should release the lock. exit(); @@ -170,8 +170,8 @@ * Try to acquire a named lock and report the outcome. */ function system_test_lock_acquire() { - if (Drupal::lock()->acquire('system_test_lock_acquire')) { - Drupal::lock()->release('system_test_lock_acquire'); + if (lock()->acquire('system_test_lock_acquire')) { + lock()->release('system_test_lock_acquire'); return 'TRUE: Lock successfully acquired in system_test_lock_acquire()'; } else { only in patch2: unchanged: --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -3015,70 +3015,3 @@ function drupal_check_memory_limit($required, $memory_limit = NULL) { // the operation. return ((!$memory_limit) || ($memory_limit == -1) || (parse_size($memory_limit) >= parse_size($required))); } - -/** - * @defgroup lock Locking mechanisms - * @{ - * Functions to coordinate long-running operations across requests. - * - * In most environments, multiple Drupal page requests (a.k.a. threads or - * processes) will execute in parallel. This leads to potential conflicts or - * race conditions when two requests execute the same code at the same time. A - * common example of this is a rebuild like menu_router_rebuild() where we - * invoke many hook implementations to get and process data from all active - * modules, and then delete the current data in the database to insert the new - * afterwards. - * - * This is a cooperative, advisory lock system. Any long-running operation - * that could potentially be attempted in parallel by multiple requests should - * try to acquire a lock before proceeding. By obtaining a lock, one request - * notifies any other requests that a specific operation is in progress which - * must not be executed in parallel. - * - * To use this API, pick a unique name for the lock. A sensible choice is the - * name of the function performing the operation. A very simple example use of - * this API: - * @code - * function mymodule_long_operation() { - * if (lock()->acquire('mymodule_long_operation')) { - * // Do the long operation here. - * // ... - * lock()->release('mymodule_long_operation'); - * } - * } - * @endcode - * - * If a function acquires a lock it should always release it when the - * operation is complete by calling lock()->release(), as in the example. - * - * A function that has acquired a lock may attempt to renew a lock (extend the - * duration of the lock) by calling lock()->acquire() again during the operation. - * Failure to renew a lock is indicative that another request has acquired - * the lock, and that the current operation may need to be aborted. - * - * If a function fails to acquire a lock it may either immediately return, or - * it may call lock()->wait() if the rest of the current page request requires - * that the operation in question be complete. After lock()->wait() returns, - * the function may again attempt to acquire the lock, or may simply allow the - * page request to proceed on the assumption that a parallel request completed - * the operation. - * - * lock()->acquire() and lock()->wait() will automatically break (delete) a lock - * whose duration has exceeded the timeout specified when it was acquired. - */ - -/** - * Get locking layer instance. - * - * @deprecated Use \Drupal::lock() instead, or even better have the lock service - * injected into your object. - * - * @return \Drupal\Core\Lock\LockBackendInterface - */ -function lock() { - return \Drupal::lock(); -} - -/** - * @} End of "defgroup lock". - */