Bug #24342 » 16749-v3.diff

Administrator Admin, 2010-12-20 10:34

View differences:

t3lib/class.t3lib_lock.php (working copy)
46 46
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
47 47
 */
48 48
class t3lib_lock {
49
	protected $method;
50
	protected $id; // Identifier used for this lock
51
	protected $resource; // Resource used for this lock (can be a file or a semaphore resource)
52
	protected $filepointer;
53
	protected $isAcquired = FALSE;
54

  
55
	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.
56
	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.
57

  
58

  
49
	private $lockInstance; // the real lock instance
50
	
59 51
	/**
60 52
	 * Constructor:
61 53
	 * initializes locking, check input parameters and set variables accordingly.
......
68 60
	 */
69 61
	public function __construct($id, $method = '', $loops = 0, $step = 0) {
70 62

  
71
			// Input checks
72
		$id = (string) $id; // Force ID to be string
73
		if (intval($loops)) {
74
			$this->loops = intval($loops);
75
		}
76
		if (intval($step)) {
77
			$this->step = intval($step);
78
		}
79

  
80 63
			// Detect locking method
81
		if (in_array($method, array('disable', 'simple', 'flock', 'semaphore'))) {
82
			$this->method = $method;
83
		} else {
64
		if (!in_array($method, array('disable', 'simple', 'flock', 'semaphore'))) {
84 65
			throw new Exception('No such method "' . $method . '"');
85 66
		}
86 67

  
87 68
		$success = FALSE;
88
		switch ($this->method) {
69
		switch ($method) {
89 70
			case 'simple':
71
				$success=t3lib_div::makeInstance('t3lib_lock_SimpleLock',$id,$loops,$step);
72
				break;
90 73
			case 'flock':
91
				$path = PATH_site . 'typo3temp/locks/';
92
				if (!is_dir($path)) {
93
					t3lib_div::mkdir($path);
94
				}
95
				$this->id = md5($id);
96
				$this->resource = $path . $this->id;
97
				$success = TRUE;
74
				$success=t3lib_div::makeInstance('t3lib_lock_FlockLock',$id,$loops,$step);
98 75
			break;
99 76
			case 'semaphore':
100
				$this->id = abs(crc32($id));
101
				if (($this->resource = sem_get($this->id, 1)) == TRUE) {
102
					$success = TRUE;
103
				}
77
				$success=t3lib_div::makeInstance('t3lib_lock_SeamphoreLock',$id,$loops,$step);
104 78
			break;
105 79
			case 'disable':
106 80
				return FALSE;
107 81
			break;
108 82
		}
109

  
83
		if ($success) {
84
			$this->lockInstance = $success;
85
			$success=TRUE;
86
		}
87
		
110 88
		return $success;
111 89
	}
112 90

  
113 91
	/**
114
	 * Destructor:
115
	 * Releases lock automatically when instance is destroyed.
116
	 *
117
	 * @return	void
118
	 */
119
	function __destruct() {
120
		$this->release();
121
	}
122

  
123
	/**
124 92
	 * Acquire a lock and return when successful. If the lock is already open, the client will be
125 93
	 *
126 94
	 * 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.
......
128 96
	 * @return	boolean		Returns true if lock could be acquired without waiting, false otherwise.
129 97
	 */
130 98
	public function acquire() {
131
		$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.
132
		$isAcquired = TRUE;
133

  
134
		switch ($this->method) {
135
			case 'simple':
136
				if (is_file($this->resource)) {
137
					$this->sysLog('Waiting for a different process to release the lock');
138
					$maxExecutionTime = ini_get('max_execution_time');
139
					$maxAge = time() - ($maxExecutionTime ? $maxExecutionTime : 120);
140
					if (@filectime($this->resource) < $maxAge) {
141
						@unlink($this->resource);
142
						$this->sysLog('Unlink stale lockfile');
143
					}
144
				}
145

  
146
				$isAcquired = FALSE;
147
				for ($i = 0; $i < $this->loops; $i++) {
148
					$filepointer = @fopen($this->resource, 'x');
149
					if ($filepointer !== FALSE) {
150
						fclose($filepointer);
151
						$this->sysLog('Lock aquired');
152
						$noWait = ($i === 0);
153
						$isAcquired = TRUE;
154
						break;
155
					}
156
					usleep($this->step * 1000);
157
				}
158

  
159
				if (!$isAcquired) {
160
					throw new Exception('Lock file could not be created');
161
				}
162
			break;
163
			case 'flock':
164
				if (($this->filepointer = fopen($this->resource, 'w+')) == FALSE) {
165
					throw new Exception('Lock file could not be opened');
166
				}
167

  
168
				if (flock($this->filepointer, LOCK_EX | LOCK_NB) == TRUE) { // Lock without blocking
169
					$noWait = TRUE;
170
				} elseif (flock($this->filepointer, LOCK_EX) == TRUE) { // Lock with blocking (waiting for similar locks to become released)
171
					$noWait = FALSE;
172
				} else {
173
					throw new Exception('Could not lock file "' . $this->resource . '"');
174
				}
175
			break;
176
			case 'semaphore':
177
				if (sem_acquire($this->resource)) {
178
						// 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.
179
					$noWait = FALSE;
180
				}
181
			break;
182
			case 'disable':
183
				$noWait = FALSE;
184
				$isAcquired = FALSE;
185
			break;
186
		}
187

  
188
		$this->isAcquired = $isAcquired;
189
		return $noWait;
99
		return $this->lockInstance->acquire();
190 100
	}
191 101

  
192 102
	/**
......
195 105
	 * @return	boolean		Returns TRUE on success or FALSE on failure
196 106
	 */
197 107
	public function release() {
198
		if (!$this->isAcquired) {
199
			return TRUE;
200
		}
201

  
202
		$success = TRUE;
203
		switch ($this->method) {
204
			case 'simple':
205
				if (unlink($this->resource) == FALSE) {
206
					$success = FALSE;
207
				}
208
			break;
209
			case 'flock':
210
				if (flock($this->filepointer, LOCK_UN) == FALSE) {
211
					$success = FALSE;
212
				}
213
				fclose($this->filepointer);
214
				unlink($this->resource);
215
			break;
216
			case 'semaphore':
217
				if (@sem_release($this->resource)) {
218
					sem_remove($this->resource);
219
				} else {
220
					$success = FALSE;
221
				}
222
			break;
223
			case 'disable':
224
				$success = FALSE;
225
			break;
226
		}
227

  
228
		$this->isAcquired = FALSE;
229
		return $success;
108
		return $this->lockInstance->release();
230 109
	}
231 110

  
232 111
	/**
......
235 114
	 * @return	string		Locking method
236 115
	 */
237 116
	public function getMethod() {
238
		return $this->method;
117
		return $this->lockInstance->getMethod();
239 118
	}
240 119

  
241 120
	/**
......
244 123
	 * @return	string		Locking ID
245 124
	 */
246 125
	public function getId() {
247
		return $this->id;
126
		return $this->lockInstance->getId();
248 127
	}
249 128

  
250 129
	/**
......
254 133
	 * @return	mixed		Locking resource (filename as string or semaphore as resource)
255 134
	 */
256 135
	public function getResource() {
257
		return $this->resource;
136
		return $this->lockInstance->getResource();
258 137
	}
259 138

  
260 139
	/**
......
263 142
	 * @return	string		Returns TRUE if lock is acquired, FALSE otherwise
264 143
	 */
265 144
	public function getLockStatus() {
266
		return $this->isAcquired;
145
		return $this->lockInstance->getLockStatus();
267 146
	}
268 147

  
269 148
	/**
......
275 154
	 * @return	void
276 155
	 */
277 156
	public function sysLog($message, $severity = 0) {
278
		t3lib_div::sysLog('Locking [' . $this->method . '::' . $this->id . ']: ' . trim($message), 'cms', $severity);
157
		return $this->lockInstance->ysLog($message, $severity);
279 158
	}
280 159
}
281 160

  
......
283 162
if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_lock.php'])) {
284 163
	include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_lock.php']);
285 164
}
286
?>
165
?>
t3lib/lock/class.t3lib_lock_simplelock.php (revision 0)
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2008-2010 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: class.t3lib_lock.php 9758 2010-12-05 11:25:36Z stephenking $
31
 *
32
 * @author	Michael Stucki <michael@typo3.org>
33
 */
34

  
35

  
36
/**
37
 * TYPO3 locking class
38
 * This class provides a simple lock using a lock file.
39
 *
40
 * It is intended to blocks requests until some data has been generated.
41
 * 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.
42
 *
43
 * @author	Michael Stucki <michael@typo3.org>
44
 * @package TYPO3
45
 * @subpackage t3lib
46
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
47
 */
48
class t3lib_lock_SimpleLock extends t3lib_lock_AbstractLock {
49

  
50
	/**
51
	 * Constructor:
52
	 * initializes locking, check input parameters and set variables accordingly.
53
	 *
54
	 * @param	string		ID to identify this lock in the system
55
	 * @param	string		Define which locking method to use. Defaults to "simple".
56
	 * @param	integer		Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
57
	 * @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.
58
	 * @return	boolean		Returns true unless something went wrong
59
	 */
60
	public function __construct($id, $loops = 0, $step = 0, $static = FALSE) {
61

  
62
		$this->method='simple';
63
			// Input checks
64
		parent::__construct($id, $loops, $step, $static);
65

  
66
		$success = FALSE;
67
		$path = PATH_site . 'typo3temp/locks/';
68
		if (!is_dir($path)) {
69
			t3lib_div::mkdir($path);
70
		}
71
		$this->id = md5($id);
72
		$this->resource = $path . $this->id;
73
		$success = TRUE;
74
		return $success;
75
	}
76

  
77
	/**
78
	 * Destructor:
79
	 * Releases lock automatically when instance is destroyed.
80
	 *
81
	 * @return	void
82
	 */
83
	function __destruct() {
84
		$this->destruct=TRUE;
85
		$this->release();
86
	}
87

  
88
	/**
89
	 * Acquire a lock and return when successful. If the lock is already open, the client will be
90
	 *
91
	 * 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.
92
	 *
93
	 * @return	boolean		Returns true if lock could be acquired without waiting, false otherwise.
94
	 */
95
	public function acquire() {
96

  
97
		$this->isAcquired = FALSE;
98
		if (is_file($this->resource)) {
99
			$this->sysLog('Waiting for a different process to release the lock');
100
			$maxExecutionTime = ini_get('max_execution_time');
101
			$maxAge = time() - ($maxExecutionTime ? $maxExecutionTime : 120);
102
			if (@filectime($this->resource) < $maxAge) {
103
				@unlink($this->resource);
104
				$this->sysLog('Unlink stale lockfile');
105
			}
106
		}
107

  
108
		$isAcquired = FALSE;
109
		for ($i = 0; $i < $this->loops; $i++) {
110
			$filepointer = @fopen($this->resource, 'x');
111
			if ($filepointer !== FALSE) {
112
				fclose($filepointer);
113
				$this->sysLog('Lock aquired');
114
				$noWait = ($i === 0);
115
				$isAcquired = TRUE;
116
				break;
117
			}
118
			usleep($this->step * 1000);
119
		}
120

  
121
		if (!$isAcquired) {
122
			throw new Exception('Lock file could not be created');
123
		}
124
		
125
		$this->isAcquired = $isAcquired;
126
		return $noWait;
127
	}
128

  
129
	/**
130
	 * Release the lock
131
	 *
132
	 * @return	boolean		Returns TRUE on success or FALSE on failure
133
	 */
134
	public function release() {
135

  
136
		if (!$this->isAcquired && !$this->destruct) {
137
			return TRUE;
138
		}
139

  
140
		$success = TRUE;
141
		if (unlink($this->resource) == FALSE) {
142
			$success = FALSE;
143
		}
144
		return $success;
145
	}
146

  
147
}
148
	
149

  
150

  
151
if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_simplelock.php'])) {
152
	include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_simplelock.php']);
153
}
154
?>
t3lib/lock/class.t3lib_lock_flocklock.php (revision 0)
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2008-2010 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: class.t3lib_lock.php 9758 2010-12-05 11:25:36Z stephenking $
31
 *
32
 * @author	Michael Stucki <michael@typo3.org>
33
 */
34

  
35

  
36
/**
37
 * TYPO3 locking class
38
 * This class provides an flock lock.
39
 *
40
 * It is intended to blocks requests until some data has been generated.
41
 * 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.
42
 *
43
 * @author	Michael Stucki <michael@typo3.org>
44
 * @package TYPO3
45
 * @subpackage t3lib
46
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
47
 */
48
class t3lib_lock_FlockLock extends t3lib_lock_AbstractLock {
49

  
50
	/**
51
	 * Constructor:
52
	 * initializes locking, check input parameters and set variables accordingly.
53
	 *
54
	 * @param	string		ID to identify this lock in the system
55
	 * @param	string		Define which locking method to use. Defaults to "simple".
56
	 * @param	integer		Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
57
	 * @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.
58
	 * @return	boolean		Returns true unless something went wrong
59
	 */
60
	public function __construct($id, $loops = 0, $step = 0, $static = FALSE) {
61

  
62
		$this->method='flock';
63
			// Input checks
64
		parent::__construct($id, $loops, $step, $static);
65

  
66
		$success = FALSE;
67
		$path = PATH_site . 'typo3temp/locks/';
68
		if (!is_dir($path)) {
69
			t3lib_div::mkdir($path);
70
		}
71
		$this->id = md5($id);
72
		$this->resource = $path . $this->id;
73
		$success = TRUE;
74
		return $success;
75
	}
76

  
77
	/**
78
	 * Destructor:
79
	 * Releases lock automatically when instance is destroyed.
80
	 *
81
	 * @return	void
82
	 */
83
	function __destruct() {
84
		$this->destruct=TRUE;
85
		$this->release();
86
	}
87

  
88
	/**
89
	 * Acquire a lock and return when successful. If the lock is already open, the client will be
90
	 *
91
	 * 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.
92
	 *
93
	 * @return	boolean		Returns true if lock could be acquired without waiting, false otherwise.
94
	 */
95
	public function acquire() {
96

  
97
		$isAcquired = TRUE;
98
		if (($this->filepointer = fopen($this->resource, 'w+')) == FALSE) {
99
			throw new Exception('Lock file could not be opened');
100
		}
101

  
102
		if (flock($this->filepointer, LOCK_EX | LOCK_NB) == TRUE) { // Lock without blocking
103
			$noWait = TRUE;
104
		} elseif (flock($this->filepointer, LOCK_EX) == TRUE) { // Lock with blocking (waiting for similar locks to become released)
105
			$noWait = FALSE;
106
		} else {
107
			throw new Exception('Could not lock file "' . $this->resource . '"');
108
		}
109
				
110
		$this->isAcquired = $isAcquired;
111
		return $noWait;
112
	}
113

  
114
	/**
115
	 * Release the lock
116
	 *
117
	 * @return	boolean		Returns TRUE on success or FALSE on failure
118
	 */
119
	public function release() {
120

  
121
		if (!$this->isAcquired && !$this->destruct) {
122
			return TRUE;
123
		}
124

  
125
		$success = TRUE;
126
		if (flock($this->filepointer, LOCK_UN) == FALSE) {
127
			$success = FALSE;
128
		}
129
		fclose($this->filepointer);
130
		unlink($this->resource);
131
		return $success;
132
	}
133

  
134
}
135
	
136

  
137

  
138
if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_flocklock.php'])) {
139
	include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_flocklock.php']);
140
}
141
?>
t3lib/lock/class.t3lib_lock_abstractlock.php (revision 0)
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2008-2010 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: class.t3lib_lock.php 9758 2010-12-05 11:25:36Z stephenking $
31
 *
32
 * @author	Michael Stucki <michael@typo3.org>
33
 */
34

  
35

  
36
/**
37
 * TYPO3 locking class
38
 * This class provides an abstract layer to various locking features for TYPO3
39
 *
40
 * It is intended to blocks requests until some data has been generated.
41
 * 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.
42
 *
43
 * @author	Michael Stucki <michael@typo3.org>
44
 * @package TYPO3
45
 * @subpackage t3lib
46
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
47
 */
48
class t3lib_lock_AbstractLock {
49
	protected $method; // used for the log message
50
	protected $id; // Identifier used for this lock
51
	protected $resource; // Resource used for this lock (can be a file or a semaphore resource)
52
	protected $isAcquired = FALSE;
53
	protected $destruct=FALSE;  // destroy all resources on destruct
54
	protected $static=FALSE;
55

  
56
	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.
57
	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.
58

  
59

  
60
	/**
61
	 * Constructor:
62
	 * initializes locking, check input parameters and set variables accordingly.
63
	 *
64
	 * @param	string		ID to identify this lock in the system
65
	 * @param	integer		Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
66
	 * @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.
67
	 * @param  boolean     if TRUE, ressources are not deleted on release/destruction, used cuurently for the global lock lock. Default: FALSE.
68
	 * @return	boolean		Returns true unless something went wrong
69
	 */
70
	public function __construct($id, $loops = 0, $step = 0, $static=FALSE) {
71

  
72
			// Input checks
73
		$id = (string) $id; // Force ID to be string
74
		if (intval($loops)) {
75
			$this->loops = intval($loops);
76
		}
77
		if (intval($step)) {
78
			$this->step = intval($step);
79
		}
80

  
81
		$this->static=$static;
82

  
83
		$success = FALSE;
84
		return $success;
85
	}
86

  
87
	/**
88
	 * Destructor:
89
	 * Releases lock automatically when instance is destroyed.
90
	 *
91
	 * @return	void
92
	 */
93
	function __destruct() {
94
		$this->destruct=TRUE;
95
		$this->release();
96
	}
97

  
98
	/**
99
	 * Acquire a lock and return when successful. If the lock is already open, the client will be
100
	 *
101
	 * 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.
102
	 *
103
	 * @return	boolean		Returns true if lock could be acquired without waiting, false otherwise.
104
	 */
105
	public function acquire() {
106

  
107
		$this->isAcquired = FALSE;
108
		return FALSE;
109
	}
110

  
111
	/**
112
	 * Release the lock
113
	 *
114
	 * @return	boolean		Returns TRUE on success or FALSE on failure
115
	 */
116
	public function release() {
117
		return TRUE;
118
	}
119

  
120
	/**
121
	 * Return the locking method which is currently used
122
	 *
123
	 * @return	string		Locking method
124
	 */
125
	public function getMethod() {
126
		return $this->method;
127
	}
128

  
129
	/**
130
	 * Return the ID which is currently used
131
	 *
132
	 * @return	string		Locking ID
133
	 */
134
	public function getId() {
135
		return $this->id;
136
	}
137

  
138
	/**
139
	 * Return the resource which is currently used.
140
	 * Depending on the locking method this can be a filename or a semaphore resource.
141
	 *
142
	 * @return	mixed		Locking resource (filename as string or semaphore as resource)
143
	 */
144
	public function getResource() {
145
		return $this->resource;
146
	}
147

  
148
	/**
149
	 * Return the status of a lock
150
	 *
151
	 * @return	string		Returns TRUE if lock is acquired, FALSE otherwise
152
	 */
153
	public function getLockStatus() {
154
		return $this->isAcquired;
155
	}
156

  
157
	/**
158
	 * Adds a common log entry for this locking API using t3lib_div::sysLog().
159
	 * Example: 25-02-08 17:58 - cms: Locking [simple::0aeafd2a67a6bb8b9543fb9ea25ecbe2]: Acquired
160
	 *
161
	 * @param	string		$message: The message to be logged
162
	 * @param	integer		$severity: Severity - 0 is info (default), 1 is notice, 2 is warning, 3 is error, 4 is fatal error
163
	 * @return	void
164
	 */
165
	public function sysLog($message, $severity = 0) {
166
		t3lib_div::sysLog('Locking [' . $this->method . '::' . $this->id . ']: ' . trim($message), 'cms', $severity);
167
	}
168
}
169
	
170

  
171

  
172
if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_abstractlock.php'])) {
173
	include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_abstractlock.php']);
174
}
175
?>
t3lib/lock/class.t3lib_lock_semaphorelock.php (revision 0)
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2008-2010 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: class.t3lib_lock.php 9758 2010-12-05 11:25:36Z stephenking $
31
 *
32
 * @author	Michael Stucki <michael@typo3.org>
33
 */
34

  
35

  
36
/**
37
 * TYPO3 locking class
38
 * This class provides an SYSV semaphore lock.
39
 *
40
 * It is intended to blocks requests until some data has been generated.
41
 * 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.
42
 *
43
 * @author	Michael Stucki <michael@typo3.org>
44
 * @package TYPO3
45
 * @subpackage t3lib
46
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
47
 */
48
class t3lib_lock_SemaphoreLock extends t3lib_lock_AbstractLock {
49

  
50
	/**
51
	 * Constructor:
52
	 * initializes locking, check input parameters and set variables accordingly.
53
	 *
54
	 * @param	string		ID to identify this lock in the system
55
	 * @param	string		Define which locking method to use. Defaults to "simple".
56
	 * @param	integer		Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
57
	 * @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.
58
	 * @return	boolean		Returns true unless something went wrong
59
	 */
60
	public function __construct($id, $loops = 0, $step = 0, $static = FALSE) {
61

  
62
		$this->method='semaphore';
63
			// Input checks
64
		parent::__construct($id, $loops, $step, $static);
65
		$success=FALSE;
66
		$this->id = abs(crc32($id));
67
		if (($this->resource = sem_get($this->id, 1)) == TRUE) {
68
			$success = TRUE;
69
		}
70
		
71
		return $success;
72
	}
73

  
74
	/**
75
	 * Destructor:
76
	 * Releases lock automatically when instance is destroyed.
77
	 *
78
	 * @return	void
79
	 */
80
	function __destruct() {
81
		$this->destruct=TRUE;
82
		$this->release();
83
	}
84

  
85
	/**
86
	 * Acquire a lock and return when successful. If the lock is already open, the client will be
87
	 *
88
	 * 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.
89
	 *
90
	 * @return	boolean		Returns true if lock could be acquired without waiting, false otherwise.
91
	 */
92
	public function acquire() {
93

  
94
		$isAcquired = TRUE;
95
		if (sem_acquire($this->resource)) {
96
		// 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.
97
			$noWait = FALSE;
98
		}
99
		
100
		$this->isAcquired = $isAcquired;
101
		return $noWait;
102
	}
103

  
104
	/**
105
	 * Release the lock
106
	 *
107
	 * @return	boolean		Returns TRUE on success or FALSE on failure
108
	 */
109
	public function release() {
110

  
111
		if (!$this->isAcquired && !$this->destruct) {
112
			return TRUE;
113
		}
114

  
115
		$success = TRUE;
116
		if (@sem_release($this->resource)) {
117
			sem_remove($this->resource);
118
		} else {
119
			$success = FALSE;
120
		}
121
		return $success;
122
	}
123

  
124
}
125
	
126

  
127

  
128
if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_simplelock.php'])) {
129
	include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_simplelock.php']);
130
}
131
?>
t3lib/lock/class.t3lib_lock_DisableLock.php (revision 0)
1
<?php
2
/***************************************************************
3
 *  Copyright notice
4
 *
5
 *  (c) 2008-2010 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: class.t3lib_lock.php 9758 2010-12-05 11:25:36Z stephenking $
31
 *
32
 * @author	Michael Stucki <michael@typo3.org>
33
 */
34

  
35

  
36
/**
37
 * TYPO3 disabled locking class
38
 *
39
 *	does nothing
40
 *
41
 * @author	Michael Stucki <michael@typo3.org>
42
 * @package TYPO3
43
 * @subpackage t3lib
44
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
45
 */
46
class t3lib_lock_DisabledLock extends t3lib_lock_AbstractLock {
47

  
48

  
49

  
50
}
51
	
52

  
53

  
54
if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_disabledlock.php'])) {
55
	include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/lock/class.t3lib_lock_disabledlock.php']);
56
}
57
?>
(3-3/4)