4 * Simple version of LockManager based on using FS lock files.
5 * All locks are non-blocking, which avoids deadlocks.
7 * This should work fine for small sites running off one server.
8 * Do not use this with 'lockDirectory' set to an NFS mount unless the
9 * NFS client is at least version 2.6.12. Otherwise, the BSD flock()
10 * locks will be ignored; see http://nfs.sourceforge.net/#section_d.
12 * @ingroup LockManager
15 class FSLockManager
extends LockManager
{
16 /** @var Array Mapping of lock types to the type actually used */
17 protected $lockTypeMap = array(
18 self
::LOCK_SH
=> self
::LOCK_SH
,
19 self
::LOCK_UW
=> self
::LOCK_SH
,
20 self
::LOCK_EX
=> self
::LOCK_EX
23 protected $lockDir; // global dir for all servers
25 /** @var Array Map of (locked key => lock type => lock file handle) */
26 protected $handles = array();
29 * Construct a new instance from configuration.
32 * 'lockDirectory' : Directory containing the lock files
34 * @param array $config
36 function __construct( array $config ) {
37 parent
::__construct( $config );
38 $this->lockDir
= $config['lockDirectory'];
42 * @see LockManager::doLock()
45 protected function doLock( array $paths, $type ) {
46 $status = Status
::newGood();
48 $lockedPaths = array(); // files locked in this attempt
49 foreach ( $paths as $path ) {
50 $status->merge( $this->doSingleLock( $path, $type ) );
51 if ( $status->isOK() ) {
52 $lockedPaths[] = $path;
54 // Abort and unlock everything
55 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
64 * @see LockManager::doUnlock()
67 protected function doUnlock( array $paths, $type ) {
68 $status = Status
::newGood();
70 foreach ( $paths as $path ) {
71 $status->merge( $this->doSingleUnlock( $path, $type ) );
78 * Lock a single resource key
81 * @param $type integer
84 protected function doSingleLock( $path, $type ) {
85 $status = Status
::newGood();
87 if ( isset( $this->locksHeld
[$path][$type] ) ) {
88 ++
$this->locksHeld
[$path][$type];
89 } elseif ( isset( $this->locksHeld
[$path][self
::LOCK_EX
] ) ) {
90 $this->locksHeld
[$path][$type] = 1;
93 $handle = fopen( $this->getLockPath( $path ), 'a+' );
95 if ( !$handle ) { // lock dir missing?
96 wfMkdirParents( $this->lockDir
);
97 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
100 // Either a shared or exclusive lock
101 $lock = ( $type == self
::LOCK_SH
) ? LOCK_SH
: LOCK_EX
;
102 if ( flock( $handle, $lock | LOCK_NB
) ) {
103 // Record this lock as active
104 $this->locksHeld
[$path][$type] = 1;
105 $this->handles
[$path][$type] = $handle;
108 $status->fatal( 'lockmanager-fail-acquirelock', $path );
111 $status->fatal( 'lockmanager-fail-openlock', $path );
119 * Unlock a single resource key
121 * @param $path string
122 * @param $type integer
125 protected function doSingleUnlock( $path, $type ) {
126 $status = Status
::newGood();
128 if ( !isset( $this->locksHeld
[$path] ) ) {
129 $status->warning( 'lockmanager-notlocked', $path );
130 } elseif ( !isset( $this->locksHeld
[$path][$type] ) ) {
131 $status->warning( 'lockmanager-notlocked', $path );
133 $handlesToClose = array();
134 --$this->locksHeld
[$path][$type];
135 if ( $this->locksHeld
[$path][$type] <= 0 ) {
136 unset( $this->locksHeld
[$path][$type] );
137 // If a LOCK_SH comes in while we have a LOCK_EX, we don't
138 // actually add a handler, so check for handler existence.
139 if ( isset( $this->handles
[$path][$type] ) ) {
140 // Mark this handle to be unlocked and closed
141 $handlesToClose[] = $this->handles
[$path][$type];
142 unset( $this->handles
[$path][$type] );
145 // Unlock handles to release locks and delete
146 // any lock files that end up with no locks on them...
147 if ( wfIsWindows() ) {
148 // Windows: for any process, including this one,
149 // calling unlink() on a locked file will fail
150 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
151 $status->merge( $this->pruneKeyLockFiles( $path ) );
153 // Unix: unlink() can be used on files currently open by this
154 // process and we must do so in order to avoid race conditions
155 $status->merge( $this->pruneKeyLockFiles( $path ) );
156 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
163 private function closeLockHandles( $path, array $handlesToClose ) {
164 $status = Status
::newGood();
165 foreach ( $handlesToClose as $handle ) {
166 wfSuppressWarnings();
167 if ( !flock( $handle, LOCK_UN
) ) {
168 $status->fatal( 'lockmanager-fail-releaselock', $path );
170 if ( !fclose( $handle ) ) {
171 $status->warning( 'lockmanager-fail-closelock', $path );
178 private function pruneKeyLockFiles( $path ) {
179 $status = Status
::newGood();
180 if ( !count( $this->locksHeld
[$path] ) ) {
181 wfSuppressWarnings();
182 # No locks are held for the lock file anymore
183 if ( !unlink( $this->getLockPath( $path ) ) ) {
184 $status->warning( 'lockmanager-fail-deletelock', $path );
187 unset( $this->locksHeld
[$path] );
188 unset( $this->handles
[$path] );
194 * Get the path to the lock file for a key
195 * @param $path string
198 protected function getLockPath( $path ) {
199 $hash = self
::sha1Base36( $path );
200 return "{$this->lockDir}/{$hash}.lock";
203 function __destruct() {
204 // Make sure remaining locks get cleared for sanity
205 foreach ( $this->locksHeld
as $path => $locks ) {
206 $this->doSingleUnlock( $path, self
::LOCK_EX
);
207 $this->doSingleUnlock( $path, self
::LOCK_SH
);