3 * @defgroup LockManager Lock management
8 * Resource locking handling.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 * @ingroup LockManager
27 * @author Aaron Schulz
31 * @brief Class for handling resource locking.
33 * Locks on resource keys can either be shared or exclusive.
35 * Implementations must keep track of what is locked by this proccess
36 * in-memory and support nested locking calls (using reference counting).
37 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
38 * Locks should either be non-blocking or have low wait timeouts.
40 * Subclasses should avoid throwing exceptions at all costs.
42 * @ingroup LockManager
45 abstract class LockManager
{
46 /** @var array Mapping of lock types to the type actually used */
47 protected $lockTypeMap = array(
48 self
::LOCK_SH
=> self
::LOCK_SH
,
49 self
::LOCK_UW
=> self
::LOCK_EX
, // subclasses may use self::LOCK_SH
50 self
::LOCK_EX
=> self
::LOCK_EX
53 /** @var array Map of (resource path => lock type => count) */
54 protected $locksHeld = array();
56 protected $domain; // string; domain (usually wiki ID)
57 protected $lockTTL; // integer; maximum time locks can be held
59 /** Lock types; stronger locks have higher values */
60 const LOCK_SH
= 1; // shared lock (for reads)
61 const LOCK_UW
= 2; // shared lock (for reads used to write elsewhere)
62 const LOCK_EX
= 3; // exclusive lock (for writes)
65 * Construct a new instance from configuration
67 * @param array $config Paramaters include:
68 * - domain : Domain (usually wiki ID) that all resources are relative to [optional]
69 * - lockTTL : Age (in seconds) at which resource locks should expire.
70 * This only applies if locks are not tied to a connection/process.
72 public function __construct( array $config ) {
73 $this->domain
= isset( $config['domain'] ) ?
$config['domain'] : wfWikiID();
74 if ( isset( $config['lockTTL'] ) ) {
75 $this->lockTTL
= max( 1, $config['lockTTL'] );
76 } elseif ( PHP_SAPI
=== 'cli' ) {
77 $this->lockTTL
= 2 * 3600;
79 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
80 $this->lockTTL
= max( 5 * 60, 2 * (int)$met );
85 * Lock the resources at the given abstract paths
87 * @param array $paths List of resource names
88 * @param int $type LockManager::LOCK_* constant
89 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
92 final public function lock( array $paths, $type = self
::LOCK_EX
, $timeout = 0 ) {
93 return $this->lockByType( array( $type => $paths ), $timeout );
97 * Lock the resources at the given abstract paths
99 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
100 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
104 final public function lockByType( array $pathsByType, $timeout = 0 ) {
105 wfProfileIn( __METHOD__
);
106 $status = Status
::newGood();
107 $pathsByType = $this->normalizePathsByType( $pathsByType );
108 $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
109 $start = microtime( true );
111 $status = $this->doLockByType( $pathsByType );
112 $elapsed = microtime( true ) - $start;
113 if ( $status->isOK() ||
$elapsed >= $timeout ||
$elapsed < 0 ) {
114 break; // success, timeout, or clock set back
116 usleep( 1e3
* ( next( $msleep ) ?
: 1000 ) ); // use 1 sec after enough times
117 $elapsed = microtime( true ) - $start;
118 } while ( $elapsed < $timeout && $elapsed >= 0 );
119 wfProfileOut( __METHOD__
);
125 * Unlock the resources at the given abstract paths
127 * @param array $paths List of paths
128 * @param int $type LockManager::LOCK_* constant
131 final public function unlock( array $paths, $type = self
::LOCK_EX
) {
132 return $this->unlockByType( array( $type => $paths ) );
136 * Unlock the resources at the given abstract paths
138 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
142 final public function unlockByType( array $pathsByType ) {
143 wfProfileIn( __METHOD__
);
144 $pathsByType = $this->normalizePathsByType( $pathsByType );
145 $status = $this->doUnlockByType( $pathsByType );
146 wfProfileOut( __METHOD__
);
152 * Get the base 36 SHA-1 of a string, padded to 31 digits.
153 * Before hashing, the path will be prefixed with the domain ID.
154 * This should be used interally for lock key or file names.
156 * @param string $path
159 final protected function sha1Base36Absolute( $path ) {
160 return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
164 * Get the base 16 SHA-1 of a string, padded to 31 digits.
165 * Before hashing, the path will be prefixed with the domain ID.
166 * This should be used interally for lock key or file names.
168 * @param string $path
171 final protected function sha1Base16Absolute( $path ) {
172 return sha1( "{$this->domain}:{$path}" );
176 * Normalize the $paths array by converting LOCK_UW locks into the
177 * appropriate type and removing any duplicated paths for each lock type.
179 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
183 final protected function normalizePathsByType( array $pathsByType ) {
185 foreach ( $pathsByType as $type => $paths ) {
186 $res[$this->lockTypeMap
[$type]] = array_unique( $paths );
193 * @see LockManager::lockByType()
194 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
198 protected function doLockByType( array $pathsByType ) {
199 $status = Status
::newGood();
200 $lockedByType = array(); // map of (type => paths)
201 foreach ( $pathsByType as $type => $paths ) {
202 $status->merge( $this->doLock( $paths, $type ) );
203 if ( $status->isOK() ) {
204 $lockedByType[$type] = $paths;
206 // Release the subset of locks that were acquired
207 foreach ( $lockedByType as $lType => $lPaths ) {
208 $status->merge( $this->doUnlock( $lPaths, $lType ) );
218 * Lock resources with the given keys and lock type
220 * @param array $paths List of paths
221 * @param int $type LockManager::LOCK_* constant
224 abstract protected function doLock( array $paths, $type );
227 * @see LockManager::unlockByType()
228 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
232 protected function doUnlockByType( array $pathsByType ) {
233 $status = Status
::newGood();
234 foreach ( $pathsByType as $type => $paths ) {
235 $status->merge( $this->doUnlock( $paths, $type ) );
242 * Unlock resources with the given keys and lock type
244 * @param array $paths List of paths
245 * @param int $type LockManager::LOCK_* constant
248 abstract protected function doUnlock( array $paths, $type );
252 * Simple version of LockManager that does nothing
255 class NullLockManager
extends LockManager
{
256 protected function doLock( array $paths, $type ) {
257 return Status
::newGood();
260 protected function doUnlock( array $paths, $type ) {
261 return Status
::newGood();