function lock_may_be_available

lock_may_be_available($name)

Check if lock acquired by a different process may be available.

If an existing lock has expired, it is removed.

Parameters

$name: The name of the lock.

Return value

TRUE if there is no lock or it was removed, FALSE otherwise.

Related topics

File

includes/lock.inc, line 166
A database-mediated implementation of a locking mechanism.

Code

function lock_may_be_available($name) {
  $lock = db_query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc();
  if (!$lock) {
    return TRUE;
  }
  $expire = (float) $lock['expire'];
  $now = microtime(TRUE);
  if ($now > $expire) {
    // We check two conditions to prevent a race condition where another
    // request acquired the lock and set a new expire time. We add a small
    // number to $expire to avoid errors with float to string conversion.
    return (bool) db_delete('semaphore')
      ->condition('name', $name)
      ->condition('value', $lock['value'])
      ->condition('expire', 0.0001 + $expire, '<=')
      ->execute();
  }
  return FALSE;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/includes!lock.inc/function/lock_may_be_available/7.x