Fixed bug #7700: Fatal error if caching is disabled and locking enabled (follow-up...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_lock.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 Michael Stucki (michael@typo3.org)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Class for providing locking features in TYPO3
29 *
30 * $Id$
31 *
32 * @author Michael Stucki <michael@typo3.org>
33 */
34
35 require_once(PATH_t3lib.'class.t3lib_div.php');
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 /**
51 * TYPO3 locking class
52 * This class provides an abstract layer to various locking features for TYPO3
53 *
54 * It is intended to blocks requests until some data has been generated.
55 * This is especially useful if two clients are requesting the same website short after each other. While the request of client 1 triggers building and caching of the website, client 2 will be waiting at this lock.
56 *
57 * @author Michael Stucki <michael@typo3.org>
58 * @package TYPO3
59 * @subpackage t3lib
60 * @see class.t3lib_tstemplate.php, class.tslib_fe.php
61 */
62 class t3lib_lock {
63 private $method;
64 private $id; // Identifier used for this lock
65 private $resource; // Resource used for this lock (can be a file or a semaphore resource)
66 private $filepointer;
67 private $isAcquired = false;
68
69 private $loops = 150; // Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
70 private $step = 200; // Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock. Only used by manual locks like the "simple" method.
71
72
73
74
75
76 /**
77 * Constructor:
78 * initializes locking, check input parameters and set variables accordingly.
79 *
80 * @param string ID to identify this lock in the system
81 * @param string Define which locking method to use. Defaults to "simple".
82 * @param integer Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
83 * @param integer Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock. Only used by manual locks like the "simple" method.
84 * @return boolean Returns true unless something went wrong
85 */
86 public function __construct($id, $method='', $loops=0, $steps=0) {
87
88 // Input checks
89 $id = (string)$id; // Force ID to be string
90 if (intval($loops)) {
91 $this->loops = intval($loops);
92 }
93 if (intval($step)) {
94 $this->step = intval($step);
95 }
96
97 // Detect locking method
98 if (in_array($method, array('disable', 'simple', 'flock', 'semaphore'))) {
99 $this->method = $method;
100 } else {
101 throw new Exception('No such method "'.$method.'"');
102 }
103
104 $success = false;
105 switch ($this->method) {
106 case 'simple':
107 case 'flock':
108 $path = PATH_site.'typo3temp/locks/';
109 if (!is_dir($path)) {
110 t3lib_div::mkdir($path);
111 }
112 $this->id = md5($id);
113 $this->resource = $path.$this->id;
114 $success = true;
115 break;
116 case 'semaphore':
117 $this->id = abs(crc32($id));
118 if (($this->resource = sem_get($this->id, 1))==true) {
119 $success = true;
120 }
121 break;
122 case 'disable':
123 return false;
124 break;
125 }
126
127 return $success;
128 }
129
130 /**
131 * Acquire a lock and return when successful. If the lock is already open, the client will be
132 *
133 * It is important to know that the lock will be acquired in any case, even if the request was blocked first. Therefore, the lock needs to be released in every situation.
134 *
135 * @return boolean Returns true if lock could be acquired without waiting, false otherwise.
136 */
137 public function acquire() {
138 $noWait = true; // Default is true, which means continue without caring for other clients. In the case of TYPO3s cache management, this has no negative effect except some resource overhead.
139 $isAcquired = true;
140
141 switch ($this->method) {
142 case 'simple':
143 if (is_file($this->resource)) {
144 $this->sysLog('Waiting for a different process to release the lock');
145 $i = 0;
146 while ($i<$this->loops) {
147 $i++;
148 usleep($this->step*1000);
149 clearstatcache();
150 if (!is_file($this->resource)) { // Lock became free, leave the loop
151 $this->sysLog('Different process released the lock');
152 $noWait = false;
153 break;
154 }
155 }
156 } else {
157 $noWait = true;
158 }
159
160 if (($this->filepointer = touch($this->resource)) == false) {
161 throw new Exception('Lock file could not be created');
162 }
163 break;
164 case 'flock':
165 if (($this->filepointer = fopen($this->resource, 'w+')) == false) {
166 throw new Exception('Lock file could not be opened');
167 }
168
169 if (flock($this->filepointer, LOCK_EX|LOCK_NB) == true) { // Lock without blocking
170 $noWait = true;
171 } elseif (flock($this->filepointer, LOCK_EX) == true) { // Lock with blocking (waiting for similar locks to become released)
172 $noWait = false;
173 } else {
174 throw new Exception('Could not lock file "'.$this->resource.'"');
175 }
176 break;
177 case 'semaphore':
178 if (sem_acquire($this->resource)) {
179 // Unfortunately it seems not possible to find out if the request was blocked, so we return false in any case to make sure the operation is tried again.
180 $noWait = false;
181 }
182 break;
183 case 'disable':
184 $noWait = false;
185 $isAcquired = false;
186 break;
187 }
188
189 $this->isAcquired = $isAcquired;
190 return $noWait;
191 }
192
193 /**
194 * Release the lock
195 *
196 * @return boolean Returns true on success or false on failure
197 */
198 public function release() {
199 if (!$this->isAcquired) {
200 return true;
201 }
202
203 $success = true;
204 switch ($this->method) {
205 case 'simple':
206 if (unlink($this->resource) == false) {
207 $success = false;
208 }
209 break;
210 case 'flock':
211 if (flock($this->filepointer, LOCK_UN) == false) {
212 $success = false;
213 }
214 fclose($this->filepointer);
215 unlink($this->resource);
216 break;
217 case 'semaphore':
218 if (@sem_release($this->resource)) {
219 sem_remove($this->resource);
220 } else {
221 $success = false;
222 }
223 break;
224 case 'disable':
225 $success = false;
226 break;
227 }
228
229 $this->isAcquired = false;
230 return $success;
231 }
232
233 /**
234 * Return the locking method which is currently used
235 *
236 * @return string Locking method
237 */
238 public function getMethod() {
239 return $this->method;
240 }
241
242 /**
243 * Return the ID which is currently used
244 *
245 * @return string Locking ID
246 */
247 public function getId() {
248 return $this->id;
249 }
250
251 /**
252 * Return the resource which is currently used.
253 * Depending on the locking method this can be a filename or a semaphore resource.
254 *
255 * @return mixed Locking resource (filename as string or semaphore as resource)
256 */
257 public function getResource() {
258 return $this->resource;
259 }
260
261 /**
262 * Return the status of a lock
263 *
264 * @return string Returns true if lock is acquired, false otherwise
265 */
266 public function getLockStatus() {
267 return $this->isAcquired;
268 }
269
270 /**
271 * Adds a common log entry for this locking API using t3lib_div::sysLog().
272 * Example: 25-02-08 17:58 - cms: Locking [simple::0aeafd2a67a6bb8b9543fb9ea25ecbe2]: Acquired
273 *
274 * @param string $message: The message to be logged
275 * @param integer $severity: Severity - 0 is info (default), 1 is notice, 2 is warning, 3 is error, 4 is fatal error
276 * @return void
277 */
278 public function sysLog($message, $severity=0) {
279 t3lib_div::sysLog('Locking ['.$this->method.'::'.$this->id.']: '.trim($message), 'cms', $severity);
280 }
281 }
282
283
284 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_lock.php']) {
285 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_lock.php']);
286 }
287 ?>