Exemplo n.º 1
0
 /**
  * Tries to obtain a lock with a name given by the string $lockname, 
  * using a timeout of $timeout seconds. Returns 1 if the lock was obtained 
  * successfully, 0 if the attempt timed out 
  * (for example, because another client has previously locked the name),
  * or NULL if an error occurred
  * If a name has been locked by one client, any request by another client
  * for a lock with the same name is blocked.
  * 
  * @param string $lockname
  * @param number $timeout in seconds
  * @throws UnexpectedValueException if there is already an active lock
  * @return integer 1 if the lock was obtained successfully, 0 if the attempt timed out 
  */
 public static function get($lockname, $timeout = 10)
 {
     if (self::$current !== null) {
         throw new UnexpectedValueException(sprintf('could not acquire new lock, %s still active'));
     }
     $ok = DBManager::get()->fetchColumn("SELECT GET_LOCK(?,?)", array(self::lockname($lockname), $timeout));
     if ($ok) {
         self::$current = $lockname;
     }
     return $ok;
 }