3 * @defgroup LockManager Lock management
10 * @author Aaron Schulz
14 * @brief Class for handling resource locking.
16 * Locks on resource keys can either be shared or exclusive.
18 * Implementations must keep track of what is locked by this proccess
19 * in-memory and support nested locking calls (using reference counting).
20 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
21 * Locks should either be non-blocking or have low wait timeouts.
23 * Subclasses should avoid throwing exceptions at all costs.
25 * @ingroup LockManager
28 abstract class LockManager
{
29 /** @var Array Mapping of lock types to the type actually used */
30 protected $lockTypeMap = array(
31 self
::LOCK_SH
=> self
::LOCK_SH
,
32 self
::LOCK_UW
=> self
::LOCK_EX
, // subclasses may use self::LOCK_SH
33 self
::LOCK_EX
=> self
::LOCK_EX
36 /** @var Array Map of (resource path => lock type => count) */
37 protected $locksHeld = array();
39 /* Lock types; stronger locks have higher values */
40 const LOCK_SH
= 1; // shared lock (for reads)
41 const LOCK_UW
= 2; // shared lock (for reads used to write elsewhere)
42 const LOCK_EX
= 3; // exclusive lock (for writes)
45 * Construct a new instance from configuration
47 * @param $config Array
49 public function __construct( array $config ) {}
52 * Lock the resources at the given abstract paths
54 * @param $paths Array List of resource names
55 * @param $type integer LockManager::LOCK_* constant
58 final public function lock( array $paths, $type = self
::LOCK_EX
) {
59 wfProfileIn( __METHOD__
);
60 $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap
[$type] );
61 wfProfileOut( __METHOD__
);
66 * Unlock the resources at the given abstract paths
68 * @param $paths Array List of storage paths
69 * @param $type integer LockManager::LOCK_* constant
72 final public function unlock( array $paths, $type = self
::LOCK_EX
) {
73 wfProfileIn( __METHOD__
);
74 $status = $this->doUnlock( array_unique( $paths ), $this->lockTypeMap
[$type] );
75 wfProfileOut( __METHOD__
);
80 * Get the base 36 SHA-1 of a string, padded to 31 digits
85 final protected static function sha1Base36( $path ) {
86 return wfBaseConvert( sha1( $path ), 16, 36, 31 );
90 * Lock resources with the given keys and lock type
92 * @param $paths Array List of storage paths
93 * @param $type integer LockManager::LOCK_* constant
96 abstract protected function doLock( array $paths, $type );
99 * Unlock resources with the given keys and lock type
101 * @param $paths Array List of storage paths
102 * @param $type integer LockManager::LOCK_* constant
105 abstract protected function doUnlock( array $paths, $type );
109 * Self releasing locks
111 * LockManager helper class to handle scoped locks, which
112 * release when an object is destroyed or goes out of scope.
114 * @ingroup LockManager
118 /** @var LockManager */
122 /** @var Array List of resource paths*/
125 protected $type; // integer lock type
128 * @param $manager LockManager
129 * @param $paths Array List of storage paths
130 * @param $type integer LockManager::LOCK_* constant
131 * @param $status Status
133 protected function __construct(
134 LockManager
$manager, array $paths, $type, Status
$status
136 $this->manager
= $manager;
137 $this->paths
= $paths;
138 $this->status
= $status;
142 protected function __clone() {}
145 * Get a ScopedLock object representing a lock on resource paths.
146 * Any locks are released once this object goes out of scope.
147 * The status object is updated with any errors or warnings.
149 * @param $manager LockManager
150 * @param $paths Array List of storage paths
151 * @param $type integer LockManager::LOCK_* constant
152 * @param $status Status
153 * @return ScopedLock|null Returns null on failure
155 public static function factory(
156 LockManager
$manager, array $paths, $type, Status
$status
158 $lockStatus = $manager->lock( $paths, $type );
159 $status->merge( $lockStatus );
160 if ( $lockStatus->isOK() ) {
161 return new self( $manager, $paths, $type, $status );
166 function __destruct() {
167 $wasOk = $this->status
->isOK();
168 $this->status
->merge( $this->manager
->unlock( $this->paths
, $this->type
) );
170 // Make sure status is OK, despite any unlockFiles() fatals
171 $this->status
->setResult( true, $this->status
->value
);
177 * Simple version of LockManager that does nothing
180 class NullLockManager
extends LockManager
{
182 * @see LockManager::doLock()
185 protected function doLock( array $paths, $type ) {
186 return Status
::newGood();
190 * @see LockManager::doUnlock()
193 protected function doUnlock( array $paths, $type ) {
194 return Status
::newGood();