3 * Resource locking handling.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup LockManager
22 * @author Aaron Schulz
26 * Self-releasing locks
28 * LockManager helper class to handle scoped locks, which
29 * release when an object is destroyed or goes out of scope.
31 * @ingroup LockManager
35 /** @var LockManager */
38 /** @var StatusValue */
41 /** @var array Map of lock types to resource paths */
42 protected $pathsByType;
45 * @param LockManager $manager
46 * @param array $pathsByType Map of lock types to path lists
47 * @param StatusValue $status
49 protected function __construct(
50 LockManager
$manager, array $pathsByType, StatusValue
$status
52 $this->manager
= $manager;
53 $this->pathsByType
= $pathsByType;
54 $this->status
= $status;
58 * Get a ScopedLock object representing a lock on resource paths.
59 * Any locks are released once this object goes out of scope.
60 * The StatusValue object is updated with any errors or warnings.
62 * @param LockManager $manager
63 * @param array $paths List of storage paths or map of lock types to path lists
64 * @param int|string $type LockManager::LOCK_* constant or "mixed" and $paths
65 * can be a map of types to paths (since 1.22). Otherwise $type should be an
66 * integer and $paths should be a list of paths.
67 * @param StatusValue $status
68 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.22)
69 * @return ScopedLock|null Returns null on failure
71 public static function factory(
72 LockManager
$manager, array $paths, $type, StatusValue
$status, $timeout = 0
74 $pathsByType = is_integer( $type ) ?
[ $type => $paths ] : $paths;
75 $lockStatus = $manager->lockByType( $pathsByType, $timeout );
76 $status->merge( $lockStatus );
77 if ( $lockStatus->isOK() ) {
78 return new self( $manager, $pathsByType, $status );
85 * Release a scoped lock and set any errors in the attatched StatusValue object.
86 * This is useful for early release of locks before function scope is destroyed.
87 * This is the same as setting the lock object to null.
89 * @param ScopedLock $lock
92 public static function release( ScopedLock
&$lock = null ) {
97 * Release the locks when this goes out of scope
99 function __destruct() {
100 $wasOk = $this->status
->isOK();
101 $this->status
->merge( $this->manager
->unlockByType( $this->pathsByType
) );
103 // Make sure StatusValue is OK, despite any unlockFiles() fatals
104 $this->status
->setResult( true, $this->status
->value
);