1. drupal
    1. 6
    2. 8
    3. 7
6 – 7 lock_acquire($name, $timeout = 30.0)

Acquire (or renew) a lock, but do not block if it fails.


$name The name of the lock.

$timeout A number of seconds (float) before the lock expires (minimum of 0.001).

Return value

TRUE if the lock was acquired, FALSE if it failed.

Related topics

▾ 9 functions call lock_acquire()

DrupalCacheArray::set in includes/bootstrap.inc
Immediately write a value to the persistent cache.
drupal_cron_run in includes/common.inc
Executes a cron run when called.
image_style_deliver in modules/image/image.module
Menu callback; Given a style and image path, generate a derivative.
locale in modules/locale/locale.module
Provides interface translation services.
LockFunctionalTest::testLockAcquire in modules/simpletest/tests/lock.test
Confirm that we can acquire and release locks in two parallel requests.
menu_rebuild in includes/menu.inc
(Re)populate the database tables used by various menu functions.
system_test_lock_acquire in modules/simpletest/tests/system_test.module
Try to acquire a named lock and report the outcome.
system_test_lock_exit in modules/simpletest/tests/system_test.module
Try to acquire a specific lock, and then exit.
variable_initialize in includes/bootstrap.inc
Load the persistent variable table.


includes/lock.inc, line 102

function lock_acquire($name, $timeout = 30.0) {
  global $locks;

  // Insure that the timeout is at least 1 ms.
  $timeout = max($timeout, 0.001);
  $expire = microtime(TRUE) + $timeout;
  if (isset($locks[$name])) {
    // Try to extend the expiration of a lock we already acquired.
    $success = (bool) db_update('semaphore')
      ->fields(array('expire' => $expire))
      ->condition('name', $name)
      ->condition('value', _lock_id())
    if (!$success) {
      // The lock was broken.
    return $success;
  else {
    // Optimistically try to acquire the lock, then retry once if it fails.
    // The first time through the loop cannot be a retry.
    $retry = FALSE;
    // We always want to do this code at least once.
    do {
      try {
          'name' => $name, 
          'value' => _lock_id(), 
          'expire' => $expire,
        // We track all acquired locks in the global variable.
        $locks[$name] = TRUE;
        // We never need to try again.
        $retry = FALSE;
      catch (PDOException $e) {
        // Suppress the error. If this is our first pass through the loop,
        // then $retry is FALSE. In this case, the insert must have failed
        // meaning some other request acquired the lock but did not release it.
        // We decide whether to retry by checking lock_may_be_available()
        // Since this will break the lock in case it is expired.
        $retry = $retry ? FALSE : lock_may_be_available($name);
      // We only retry in case the first attempt failed, but we then broke
      // an expired lock.
    } while ($retry);
  return isset($locks[$name]);