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 /* Lock types; stronger locks have higher values */
57 const LOCK_SH
= 1; // shared lock (for reads)
58 const LOCK_UW
= 2; // shared lock (for reads used to write elsewhere)
59 const LOCK_EX
= 3; // exclusive lock (for writes)
62 * Construct a new instance from configuration
64 * @param $config Array
66 public function __construct( array $config ) {}
69 * Lock the resources at the given abstract paths
71 * @param $paths Array List of resource names
72 * @param $type integer LockManager::LOCK_* constant
75 final public function lock( array $paths, $type = self
::LOCK_EX
) {
76 wfProfileIn( __METHOD__
);
77 $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap
[$type] );
78 wfProfileOut( __METHOD__
);
83 * Unlock the resources at the given abstract paths
85 * @param $paths Array List of storage paths
86 * @param $type integer LockManager::LOCK_* constant
89 final public function unlock( array $paths, $type = self
::LOCK_EX
) {
90 wfProfileIn( __METHOD__
);
91 $status = $this->doUnlock( array_unique( $paths ), $this->lockTypeMap
[$type] );
92 wfProfileOut( __METHOD__
);
97 * Get the base 36 SHA-1 of a string, padded to 31 digits
102 final protected static function sha1Base36( $path ) {
103 return wfBaseConvert( sha1( $path ), 16, 36, 31 );
107 * Lock resources with the given keys and lock type
109 * @param $paths Array List of storage paths
110 * @param $type integer LockManager::LOCK_* constant
113 abstract protected function doLock( array $paths, $type );
116 * Unlock resources with the given keys and lock type
118 * @param $paths Array List of storage paths
119 * @param $type integer LockManager::LOCK_* constant
122 abstract protected function doUnlock( array $paths, $type );
126 * Self releasing locks
128 * LockManager helper class to handle scoped locks, which
129 * release when an object is destroyed or goes out of scope.
131 * @ingroup LockManager
135 /** @var LockManager */
139 /** @var Array List of resource paths*/
142 protected $type; // integer lock type
145 * @param $manager LockManager
146 * @param $paths Array List of storage paths
147 * @param $type integer LockManager::LOCK_* constant
148 * @param $status Status
150 protected function __construct(
151 LockManager
$manager, array $paths, $type, Status
$status
153 $this->manager
= $manager;
154 $this->paths
= $paths;
155 $this->status
= $status;
160 * Get a ScopedLock object representing a lock on resource paths.
161 * Any locks are released once this object goes out of scope.
162 * The status object is updated with any errors or warnings.
164 * @param $manager LockManager
165 * @param $paths Array List of storage paths
166 * @param $type integer LockManager::LOCK_* constant
167 * @param $status Status
168 * @return ScopedLock|null Returns null on failure
170 public static function factory(
171 LockManager
$manager, array $paths, $type, Status
$status
173 $lockStatus = $manager->lock( $paths, $type );
174 $status->merge( $lockStatus );
175 if ( $lockStatus->isOK() ) {
176 return new self( $manager, $paths, $type, $status );
181 function __destruct() {
182 $wasOk = $this->status
->isOK();
183 $this->status
->merge( $this->manager
->unlock( $this->paths
, $this->type
) );
185 // Make sure status is OK, despite any unlockFiles() fatals
186 $this->status
->setResult( true, $this->status
->value
);
192 * Simple version of LockManager that does nothing
195 class NullLockManager
extends LockManager
{
197 * @see LockManager::doLock()
198 * @param $paths array
202 protected function doLock( array $paths, $type ) {
203 return Status
::newGood();
207 * @see LockManager::doUnlock()
208 * @param $paths array
212 protected function doUnlock( array $paths, $type ) {
213 return Status
::newGood();