e41c7770a7f3f5d479c112f43b79eb8f767f4133
[lhc/web/wiklou.git] / includes / filerepo / backend / lockmanager / LockManager.php
1 <?php
2 /**
3 * @defgroup LockManager Lock management
4 * @ingroup FileBackend
5 */
6
7 /**
8 * Resource locking handling.
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 * @ingroup LockManager
27 * @author Aaron Schulz
28 */
29
30 /**
31 * @brief Class for handling resource locking.
32 *
33 * Locks on resource keys can either be shared or exclusive.
34 *
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.
39 *
40 * Subclasses should avoid throwing exceptions at all costs.
41 *
42 * @ingroup LockManager
43 * @since 1.19
44 */
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
51 );
52
53 /** @var Array Map of (resource path => lock type => count) */
54 protected $locksHeld = array();
55
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)
60
61 /**
62 * Construct a new instance from configuration
63 *
64 * @param $config Array
65 */
66 public function __construct( array $config ) {}
67
68 /**
69 * Lock the resources at the given abstract paths
70 *
71 * @param $paths Array List of resource names
72 * @param $type integer LockManager::LOCK_* constant
73 * @return Status
74 */
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__ );
79 return $status;
80 }
81
82 /**
83 * Unlock the resources at the given abstract paths
84 *
85 * @param $paths Array List of storage paths
86 * @param $type integer LockManager::LOCK_* constant
87 * @return Status
88 */
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__ );
93 return $status;
94 }
95
96 /**
97 * Get the base 36 SHA-1 of a string, padded to 31 digits
98 *
99 * @param $path string
100 * @return string
101 */
102 final protected static function sha1Base36( $path ) {
103 return wfBaseConvert( sha1( $path ), 16, 36, 31 );
104 }
105
106 /**
107 * Lock resources with the given keys and lock type
108 *
109 * @param $paths Array List of storage paths
110 * @param $type integer LockManager::LOCK_* constant
111 * @return string
112 */
113 abstract protected function doLock( array $paths, $type );
114
115 /**
116 * Unlock resources with the given keys and lock type
117 *
118 * @param $paths Array List of storage paths
119 * @param $type integer LockManager::LOCK_* constant
120 * @return string
121 */
122 abstract protected function doUnlock( array $paths, $type );
123 }
124
125 /**
126 * Self releasing locks
127 *
128 * LockManager helper class to handle scoped locks, which
129 * release when an object is destroyed or goes out of scope.
130 *
131 * @ingroup LockManager
132 * @since 1.19
133 */
134 class ScopedLock {
135 /** @var LockManager */
136 protected $manager;
137 /** @var Status */
138 protected $status;
139 /** @var Array List of resource paths*/
140 protected $paths;
141
142 protected $type; // integer lock type
143
144 /**
145 * @param $manager LockManager
146 * @param $paths Array List of storage paths
147 * @param $type integer LockManager::LOCK_* constant
148 * @param $status Status
149 */
150 protected function __construct(
151 LockManager $manager, array $paths, $type, Status $status
152 ) {
153 $this->manager = $manager;
154 $this->paths = $paths;
155 $this->status = $status;
156 $this->type = $type;
157 }
158
159 /**
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.
163 *
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
169 */
170 public static function factory(
171 LockManager $manager, array $paths, $type, Status $status
172 ) {
173 $lockStatus = $manager->lock( $paths, $type );
174 $status->merge( $lockStatus );
175 if ( $lockStatus->isOK() ) {
176 return new self( $manager, $paths, $type, $status );
177 }
178 return null;
179 }
180
181 function __destruct() {
182 $wasOk = $this->status->isOK();
183 $this->status->merge( $this->manager->unlock( $this->paths, $this->type ) );
184 if ( $wasOk ) {
185 // Make sure status is OK, despite any unlockFiles() fatals
186 $this->status->setResult( true, $this->status->value );
187 }
188 }
189 }
190
191 /**
192 * Simple version of LockManager that does nothing
193 * @since 1.19
194 */
195 class NullLockManager extends LockManager {
196 /**
197 * @see LockManager::doLock()
198 * @param $paths array
199 * @param $type int
200 * @return Status
201 */
202 protected function doLock( array $paths, $type ) {
203 return Status::newGood();
204 }
205
206 /**
207 * @see LockManager::doUnlock()
208 * @param $paths array
209 * @param $type int
210 * @return Status
211 */
212 protected function doUnlock( array $paths, $type ) {
213 return Status::newGood();
214 }
215 }