9 * Class for handling resource locking.
10 * Locks on resource keys can either be shared or exclusive.
12 * Implementations must keep track of what is locked by this proccess
13 * in-memory and support nested locking calls (using reference counting).
14 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
15 * Locks should either be non-blocking or have low wait timeouts.
17 * Subclasses should avoid throwing exceptions at all costs.
19 * @ingroup LockManager
22 abstract class LockManager
{
23 /** @var Array Mapping of lock types to the type actually used */
24 protected $lockTypeMap = array(
25 self
::LOCK_SH
=> self
::LOCK_SH
,
26 self
::LOCK_UW
=> self
::LOCK_EX
, // subclasses may use self::LOCK_SH
27 self
::LOCK_EX
=> self
::LOCK_EX
30 /** @var Array Map of (resource path => lock type => count) */
31 protected $locksHeld = array();
33 /* Lock types; stronger locks have higher values */
34 const LOCK_SH
= 1; // shared lock (for reads)
35 const LOCK_UW
= 2; // shared lock (for reads used to write elsewhere)
36 const LOCK_EX
= 3; // exclusive lock (for writes)
39 * Construct a new instance from configuration
41 * @param $config Array
43 public function __construct( array $config ) {}
46 * Lock the resources at the given abstract paths
48 * @param $paths Array List of resource names
49 * @param $type integer LockManager::LOCK_* constant
52 final public function lock( array $paths, $type = self
::LOCK_EX
) {
53 return $this->doLock( array_unique( $paths ), $this->lockTypeMap
[$type] );
57 * Unlock the resources at the given abstract paths
59 * @param $paths Array List of storage paths
60 * @param $type integer LockManager::LOCK_* constant
63 final public function unlock( array $paths, $type = self
::LOCK_EX
) {
64 return $this->doUnlock( array_unique( $paths ), $this->lockTypeMap
[$type] );
68 * Get the base 36 SHA-1 of a string, padded to 31 digits
73 final protected static function sha1Base36( $path ) {
74 return wfBaseConvert( sha1( $path ), 16, 36, 31 );
78 * Lock resources with the given keys and lock type
80 * @param $paths Array List of storage paths
81 * @param $type integer LockManager::LOCK_* constant
84 abstract protected function doLock( array $paths, $type );
87 * Unlock resources with the given keys and lock type
89 * @param $paths Array List of storage paths
90 * @param $type integer LockManager::LOCK_* constant
93 abstract protected function doUnlock( array $paths, $type );
97 * LockManager helper class to handle scoped locks, which
98 * release when an object is destroyed or goes out of scope.
100 * @ingroup LockManager
104 /** @var LockManager */
108 /** @var Array List of resource paths*/
111 protected $type; // integer lock type
114 * @param $manager LockManager
115 * @param $paths Array List of storage paths
116 * @param $type integer LockManager::LOCK_* constant
117 * @param $status Status
119 protected function __construct(
120 LockManager
$manager, array $paths, $type, Status
$status
122 $this->manager
= $manager;
123 $this->paths
= $paths;
124 $this->status
= $status;
128 protected function __clone() {}
131 * Get a ScopedLock object representing a lock on resource paths.
132 * Any locks are released once this object goes out of scope.
133 * The status object is updated with any errors or warnings.
135 * @param $manager LockManager
136 * @param $paths Array List of storage paths
137 * @param $type integer LockManager::LOCK_* constant
138 * @param $status Status
139 * @return ScopedLock|null Returns null on failure
141 public static function factory(
142 LockManager
$manager, array $paths, $type, Status
$status
144 $lockStatus = $manager->lock( $paths, $type );
145 $status->merge( $lockStatus );
146 if ( $lockStatus->isOK() ) {
147 return new self( $manager, $paths, $type, $status );
152 function __destruct() {
153 $wasOk = $this->status
->isOK();
154 $this->status
->merge( $this->manager
->unlock( $this->paths
, $this->type
) );
156 // Make sure status is OK, despite any unlockFiles() fatals
157 $this->status
->setResult( true, $this->status
->value
);
163 * Simple version of LockManager that does nothing
166 class NullLockManager
extends LockManager
{
167 protected function doLock( array $paths, $type ) {
168 return Status
::newGood();
171 protected function doUnlock( array $paths, $type ) {
172 return Status
::newGood();