From: Aaron Schulz Date: Wed, 13 Mar 2013 08:07:41 +0000 (-0700) Subject: [LockManager] Added timeout to the lock() function. X-Git-Tag: 1.31.0-rc.0~20126^2 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dcompta/comptes/journal.php?a=commitdiff_plain;h=0193a82d56f818b7f3c8ad588378e4034df22e00;p=lhc%2Fweb%2Fwiklou.git [LockManager] Added timeout to the lock() function. Change-Id: I32f97b813fc6036b2774d67c14c574420e7a1437 --- diff --git a/includes/filebackend/lockmanager/LockManager.php b/includes/filebackend/lockmanager/LockManager.php index 0512a01b1e..f534eff38b 100644 --- a/includes/filebackend/lockmanager/LockManager.php +++ b/includes/filebackend/lockmanager/LockManager.php @@ -88,11 +88,22 @@ abstract class LockManager { * * @param array $paths List of resource names * @param $type integer LockManager::LOCK_* constant + * @param integer $timeout Timeout in seconds (0 means non-blocking) (since 1.21) * @return Status */ - final public function lock( array $paths, $type = self::LOCK_EX ) { + final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) { wfProfileIn( __METHOD__ ); - $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] ); + $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times + $start = microtime( true ); + do { + $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] ); + $elapsed = microtime( true ) - $start; + if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) { + break; // success, timeout, or clock set back + } + usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times + $elapsed = microtime( true ) - $start; + } while ( $elapsed < $timeout && $elapsed >= 0 ); wfProfileOut( __METHOD__ ); return $status; }