X-Git-Url: http://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/pie.php?a=blobdiff_plain;f=includes%2Ffilebackend%2Flockmanager%2FLockManager.php;h=eff031b58b5333e9b56036e1f56a4a3ea1c34c8e;hb=d48c33e89835b1dcf8b8d50f473778c5ee3bbd54;hp=8115fd42a9618190c9695c9c89172e51e5032b2e;hpb=c6bcef30ae0e56d379d34cf9d96e6593238a0341;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/filebackend/lockmanager/LockManager.php b/includes/filebackend/lockmanager/LockManager.php index 8115fd42a9..eff031b58b 100644 --- a/includes/filebackend/lockmanager/LockManager.php +++ b/includes/filebackend/lockmanager/LockManager.php @@ -44,14 +44,14 @@ */ abstract class LockManager { /** @var array Mapping of lock types to the type actually used */ - protected $lockTypeMap = array( + protected $lockTypeMap = [ self::LOCK_SH => self::LOCK_SH, self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH self::LOCK_EX => self::LOCK_EX - ); + ]; /** @var array Map of (resource path => lock type => count) */ - protected $locksHeld = array(); + protected $locksHeld = []; protected $domain; // string; domain (usually wiki ID) protected $lockTTL; // integer; maximum time locks can be held @@ -87,10 +87,10 @@ abstract class LockManager { * @param array $paths List of resource names * @param int $type LockManager::LOCK_* constant * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21) - * @return Status + * @return StatusValue */ final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) { - return $this->lockByType( array( $type => $paths ), $timeout ); + return $this->lockByType( [ $type => $paths ], $timeout ); } /** @@ -98,22 +98,22 @@ abstract class LockManager { * * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21) - * @return Status + * @return StatusValue * @since 1.22 */ final public function lockByType( array $pathsByType, $timeout = 0 ) { $pathsByType = $this->normalizePathsByType( $pathsByType ); - $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times - $start = microtime( true ); - do { - $status = $this->doLockByType( $pathsByType ); - $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 ); + + $status = null; + $loop = new WaitConditionLoop( + function () use ( &$status, $pathsByType ) { + $status = $this->doLockByType( $pathsByType ); + + return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE; + }, + $timeout + ); + $loop->invoke(); return $status; } @@ -123,17 +123,17 @@ abstract class LockManager { * * @param array $paths List of paths * @param int $type LockManager::LOCK_* constant - * @return Status + * @return StatusValue */ final public function unlock( array $paths, $type = self::LOCK_EX ) { - return $this->unlockByType( array( $type => $paths ) ); + return $this->unlockByType( [ $type => $paths ] ); } /** * Unlock the resources at the given abstract paths * * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths - * @return Status + * @return StatusValue * @since 1.22 */ final public function unlockByType( array $pathsByType ) { @@ -152,7 +152,7 @@ abstract class LockManager { * @return string */ final protected function sha1Base36Absolute( $path ) { - return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 ); + return Wikimedia\base_convert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 ); } /** @@ -176,7 +176,7 @@ abstract class LockManager { * @since 1.22 */ final protected function normalizePathsByType( array $pathsByType ) { - $res = array(); + $res = []; foreach ( $pathsByType as $type => $paths ) { $res[$this->lockTypeMap[$type]] = array_unique( $paths ); } @@ -187,12 +187,12 @@ abstract class LockManager { /** * @see LockManager::lockByType() * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths - * @return Status + * @return StatusValue * @since 1.22 */ protected function doLockByType( array $pathsByType ) { $status = Status::newGood(); - $lockedByType = array(); // map of (type => paths) + $lockedByType = []; // map of (type => paths) foreach ( $pathsByType as $type => $paths ) { $status->merge( $this->doLock( $paths, $type ) ); if ( $status->isOK() ) { @@ -214,14 +214,14 @@ abstract class LockManager { * * @param array $paths List of paths * @param int $type LockManager::LOCK_* constant - * @return Status + * @return StatusValue */ abstract protected function doLock( array $paths, $type ); /** * @see LockManager::unlockByType() * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths - * @return Status + * @return StatusValue * @since 1.22 */ protected function doUnlockByType( array $pathsByType ) { @@ -238,7 +238,7 @@ abstract class LockManager { * * @param array $paths List of paths * @param int $type LockManager::LOCK_* constant - * @return Status + * @return StatusValue */ abstract protected function doUnlock( array $paths, $type ); }