You are here

function lock_acquire in Memcache Storage 7

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

Parameters

$name: The name of the lock.

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

Return value

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

File

includes/lock.inc, line 47
A memcached based implementation of a locking mechanism. See includes/lock.inc for documentation.

Code

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

  // Insure that the timeout is at least 1 second.
  $expire = (int) max($timeout, 1);
  if (isset($locks[$name])) {

    // Try to extend the expiration of a lock we already acquired.
    $success = MemcacheStorageAPI::replace($name, _lock_id(), $expire, 'semaphore');
    if (!$success) {

      // The lock was broken.
      unset($locks[$name]);
    }
  }
  else {

    // Add new lock.
    if (MemcacheStorageAPI::add($name, _lock_id(), $expire, 'semaphore')) {

      // We track all acquired locks in the global variable.
      $locks[$name] = TRUE;
    }
  }
  return isset($locks[$name]);
}