2 /***************************************************************
5 * (c) 2008 Michael Stucki (michael@typo3.org)
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.
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.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
28 * Class for providing locking features in TYPO3
32 * @author Michael Stucki <michael@typo3.org>
35 require_once(PATH_t3lib
.'class.t3lib_div.php');
52 * This class provides an abstract layer to various locking features for TYPO3
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.
57 * @author Michael Stucki <michael@typo3.org>
60 * @see class.t3lib_tstemplate.php, class.tslib_fe.php
64 protected $id; // Identifier used for this lock
65 protected $resource; // Resource used for this lock (can be a file or a semaphore resource)
66 protected $filepointer;
67 protected $isAcquired = false
;
69 protected $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 protected $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.
78 * initializes locking, check input parameters and set variables accordingly.
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
86 public function __construct($id, $method='', $loops=0, $steps=0) {
89 $id = (string)$id; // Force ID to be string
91 $this->loops
= intval($loops);
94 $this->step
= intval($step);
97 // Detect locking method
98 if (in_array($method, array('disable', 'simple', 'flock', 'semaphore'))) {
99 $this->method
= $method;
101 throw new Exception('No such method "'.$method.'"');
105 switch ($this->method
) {
108 $path = PATH_site
.'typo3temp/locks/';
109 if (!is_dir($path)) {
110 t3lib_div
::mkdir($path);
112 $this->id
= md5($id);
113 $this->resource
= $path.$this->id
;
117 $this->id
= abs(crc32($id));
118 if (($this->resource
= sem_get($this->id
, 1))==true
) {
132 * Releases lock automatically when instance is destroyed.
136 function __destruct() {
141 * Acquire a lock and return when successful. If the lock is already open, the client will be
143 * 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.
145 * @return boolean Returns true if lock could be acquired without waiting, false otherwise.
147 public function acquire() {
148 $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.
151 switch ($this->method
) {
153 if (is_file($this->resource
)) {
154 $this->sysLog('Waiting for a different process to release the lock');
156 while ($i<$this->loops
) {
158 usleep($this->step
*1000);
160 if (!is_file($this->resource
)) { // Lock became free, leave the loop
161 $this->sysLog('Different process released the lock');
170 if (($this->filepointer
= touch($this->resource
)) == false
) {
171 throw new Exception('Lock file could not be created');
175 if (($this->filepointer
= fopen($this->resource
, 'w+')) == false
) {
176 throw new Exception('Lock file could not be opened');
179 if (flock($this->filepointer
, LOCK_EX|LOCK_NB
) == true
) { // Lock without blocking
181 } elseif (flock($this->filepointer
, LOCK_EX
) == true
) { // Lock with blocking (waiting for similar locks to become released)
184 throw new Exception('Could not lock file "'.$this->resource
.'"');
188 if (sem_acquire($this->resource
)) {
189 // 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.
199 $this->isAcquired
= $isAcquired;
206 * @return boolean Returns true on success or false on failure
208 public function release() {
209 if (!$this->isAcquired
) {
214 switch ($this->method
) {
216 if (unlink($this->resource
) == false
) {
221 if (flock($this->filepointer
, LOCK_UN
) == false
) {
224 fclose($this->filepointer
);
225 unlink($this->resource
);
228 if (@sem_release
($this->resource
)) {
229 sem_remove($this->resource
);
239 $this->isAcquired
= false
;
244 * Return the locking method which is currently used
246 * @return string Locking method
248 public function getMethod() {
249 return $this->method
;
253 * Return the ID which is currently used
255 * @return string Locking ID
257 public function getId() {
262 * Return the resource which is currently used.
263 * Depending on the locking method this can be a filename or a semaphore resource.
265 * @return mixed Locking resource (filename as string or semaphore as resource)
267 public function getResource() {
268 return $this->resource
;
272 * Return the status of a lock
274 * @return string Returns true if lock is acquired, false otherwise
276 public function getLockStatus() {
277 return $this->isAcquired
;
281 * Adds a common log entry for this locking API using t3lib_div::sysLog().
282 * Example: 25-02-08 17:58 - cms: Locking [simple::0aeafd2a67a6bb8b9543fb9ea25ecbe2]: Acquired
284 * @param string $message: The message to be logged
285 * @param integer $severity: Severity - 0 is info (default), 1 is notice, 2 is warning, 3 is error, 4 is fatal error
288 public function sysLog($message, $severity=0) {
289 t3lib_div
::sysLog('Locking ['.$this->method
.'::'.$this->id
.']: '.trim($message), 'cms', $severity);
294 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_lock.php']) {
295 include_once($TYPO3_CONF_VARS[TYPO3_MODE
]['XCLASS']['t3lib/class.t3lib_lock.php']);