https://www.mediawiki.org/wiki/Special:Code/MediaWiki/106298
Revision: 106298
Author: aaron
Date: 2011-12-15 01:13:29 +0000 (Thu, 15 Dec 2011)
Log Message:
-----------
Refactored FileBackendScopedLock class into more general ScopedLock class
Modified Paths:
--------------
branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
branches/FileBackend/phase3/includes/filerepo/backend/LockManager.php
Modified: branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
===================================================================
--- branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
2011-12-15 01:02:25 UTC (rev 106297)
+++ branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
2011-12-15 01:13:29 UTC (rev 106298)
@@ -409,7 +409,7 @@
* @return FileBackendScopedLock|null Returns null on failure
*/
final public function getScopedFileLocks( array $paths, $type, Status
$status ) {
- return FileBackendScopedLock::factory( $this, $paths, $type,
$status );
+ return ScopedLock::factory( $this->lockManager, $paths, $type,
$status );
}
}
@@ -782,65 +782,3 @@
return $relStoragePath;
}
}
-
-/**
- * Class to handle scoped locks, which release when the object is destroyed
- */
-class FileBackendScopedLock {
- /** @var FileBackendBase */
- protected $backend;
- /** @var Status */
- protected $status;
- /** @var Array List of storage paths*/
- protected $paths;
- protected $type; // integer lock type
-
- /**
- * @param $backend FileBackendBase
- * @param $paths Array List of storage paths
- * @param $type integer LockManager::LOCK_EX, LockManager::LOCK_SH
- * @param $status Status
- */
- protected function __construct(
- FileBackendBase $backend, array $paths, $type, Status $status
- ) {
- $this->backend = $backend;
- $this->paths = $paths;
- $this->status = $status;
- $this->type = $type;
- }
-
- protected function __clone() {}
-
- /**
- * Get a status object resulting from an attempt to lock files.
- * If the attempt is sucessful, the value of the status will be
- * FileBackendScopedLock object which releases the locks when
- * it goes out of scope. Otherwise, the value will be null.
- *
- * @param $backend FileBackendBase
- * @param $files Array List of storage paths
- * @param $type integer LockManager::LOCK_EX, LockManager::LOCK_SH
- * @param $status Status
- * @return FileBackendScopedLock|null
- */
- public static function factory(
- FileBackendBase $backend, array $files, $type, Status $status
- ) {
- $lockStatus = $backend->lockFiles( $files, $type );
- $status->merge( $lockStatus );
- if ( $lockStatus->isOK() ) {
- return new self( $backend, $files, $type, $status );
- }
- return null;
- }
-
- function __destruct() {
- $wasOk = $this->status->isOK();
- $this->status->merge( $this->backend->unlockFiles(
$this->paths, $this->type ) );
- if ( $wasOk ) {
- // Make sure status is OK, despite any unlockFiles()
fatals
- $this->status->setResult( true, $this->status->value );
- }
- }
-}
Modified: branches/FileBackend/phase3/includes/filerepo/backend/LockManager.php
===================================================================
--- branches/FileBackend/phase3/includes/filerepo/backend/LockManager.php
2011-12-15 01:02:25 UTC (rev 106297)
+++ branches/FileBackend/phase3/includes/filerepo/backend/LockManager.php
2011-12-15 01:13:29 UTC (rev 106298)
@@ -1,6 +1,6 @@
<?php
/**
- * FileBackend helper class for handling file locking.
+ * Class for handling resource locking.
* Locks on resource keys can either be shared or exclusive.
*
* Implementations must keep track of what is locked by this proccess
@@ -76,6 +76,69 @@
}
/**
+ * LockManager helper class to handle scoped locks, which
+ * release when an object is destroyed or goes out of scope.
+ */
+class ScopedLock {
+ /** @var LockManager */
+ protected $manager;
+ /** @var Status */
+ protected $status;
+ /** @var Array List of resource paths*/
+ protected $paths;
+
+ protected $type; // integer lock type
+
+ /**
+ * @param $manager LockManager
+ * @param $paths Array List of storage paths
+ * @param $type integer LockManager::LOCK_* constant
+ * @param $status Status
+ */
+ protected function __construct(
+ LockManager $manager, array $paths, $type, Status $status
+ ) {
+ $this->manager = $manager;
+ $this->paths = $paths;
+ $this->status = $status;
+ $this->type = $type;
+ }
+
+ protected function __clone() {}
+
+ /**
+ * Get a ScopedLock object representing a lock on resource paths.
+ * Any locks are released once this object goes out of scope.
+ * The status object is updated with any errors or warnings.
+ *
+ * @param $manager LockManager
+ * @param $paths Array List of storage paths
+ * @param $type integer LockManager::LOCK_* constant
+ * @param $status Status
+ * @return ScopedLock|null Returns null on failure
+ */
+ public static function factory(
+ LockManager $manager, array $paths, $type, Status $status
+ ) {
+ $lockStatus = $manager->lock( $paths, $type );
+ $status->merge( $lockStatus );
+ if ( $lockStatus->isOK() ) {
+ return new self( $manager, $paths, $type, $status );
+ }
+ return null;
+ }
+
+ function __destruct() {
+ $wasOk = $this->status->isOK();
+ $this->status->merge( $this->manager->unlock( $this->paths,
$this->type ) );
+ if ( $wasOk ) {
+ // Make sure status is OK, despite any unlockFiles()
fatals
+ $this->status->setResult( true, $this->status->value );
+ }
+ }
+}
+
+/**
* Simple version of LockManager based on using FS lock files.
* All locks are non-blocking, which avoids deadlocks.
*
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs