0008010_v3.patch

Administrator Admin, 2010-07-21 16:40

Download (4.12 KB)

View differences:

t3lib/stddb/tables.sql (Arbeitskopie)
413 413
) ENGINE=InnoDB;
414 414

  
415 415
#
416
# Table structure for table 'sys_lock'
417
#
418
CREATE TABLE sys_lock (
419
  id varchar(64) DEFAULT '' NOT NULL,
420
  expires int(11) unsigned DEFAULT '0' NOT NULL,
421
  PRIMARY KEY (id)
422
) ENGINE=InnoDB;
423

  
424
#
416 425
# Table structure for table 'sys_language'
417 426
#
418 427
CREATE TABLE sys_language (
t3lib/class.t3lib_lock.php (Arbeitskopie)
59 59
 * @see	class.t3lib_tstemplate.php, class.tslib_fe.php
60 60
 */
61 61
class t3lib_lock {
62
	const METHOD_SIMPLE = 'simple';
63
	const METHOD_FLOCK = 'flock';
64
	const METHOD_SEMAPHORE = 'semaphore';
65
	const METHOD_DATABASE = 'database';
66
	const METHOD_DISABLE = 'disable';
67

  
62 68
	protected $method;
63 69
	protected $id;		// Identifier used for this lock
64 70
	protected $resource;	// Resource used for this lock (can be a file or a semaphore resource)
......
67 73

  
68 74
	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.
69 75
	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.
76
	protected $expiration = 300;	// Number of seconds after a resource shall expire
70 77

  
71 78

  
72 79

  
......
94 101
		}
95 102

  
96 103
			// Detect locking method
97
		if (in_array($method, array('disable', 'simple', 'flock', 'semaphore'))) {
104
		if (in_array($method, array('disable', 'simple', 'flock', 'semaphore', 'database'))) {
98 105
			$this->method = $method;
99 106
		} else {
100 107
			throw new Exception('No such method "' . $method . '"');
......
118 125
					$success = TRUE;
119 126
				}
120 127
			break;
128
			case 'database':
129
				$this->id = sha1($id);
130
				$this->resource = $GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'sys_lock');
131
				$success = TRUE;
132
				break;
121 133
			case 'disable':
122 134
				return FALSE;
123 135
			break;
......
150 162
		switch ($this->method) {
151 163
			case 'simple':
152 164
				if (is_file($this->resource)) {
165
					$noWait = FALSE;
153 166
					$this->sysLog('Waiting for a different process to release the lock');
154 167
					$i = 0;
155 168
					while ($i<$this->loops) {
......
158 171
						clearstatcache();
159 172
						if (!is_file($this->resource)) {	// Lock became free, leave the loop
160 173
							$this->sysLog('Different process released the lock');
161
							$noWait = FALSE;
162 174
							break;
163 175
						}
164 176
					}
......
189 201
					$noWait = FALSE;
190 202
				}
191 203
			break;
204
			case 'database':
205
				$GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_lock', 'expires<' . $GLOBALS['EXEC_TIME']);
206
				$noWait = FALSE;
207

  
208
				while ($i < $this->loops) {
209
					if ($this->isDatabaseLockActive()) {
210
						$i++;
211
						usleep($this->step * 1000);
212
					} else {
213
						$noWait = TRUE;
214
						break;
215
					}
216
				}
217

  
218
				$fields = array(
219
					'expires' => $GLOBALS['EXEC_TIME'] + $this->expiration,
220
					'id' => $this->id,
221
				);
222

  
223
				if ($noWait || !$this->isDatabaseLockActive()) {
224
					$GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_lock', $fields);
225
				} else {
226
					$GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_lock', 'id=' . $this->resource, $fields);
227
				}
228

  
229
				break;
192 230
			case 'disable':
193 231
				$noWait = FALSE;
194 232
				$isAcquired = FALSE;
......
230 268
					$success = FALSE;
231 269
				}
232 270
			break;
271
			case 'database':
272
				$GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_lock', 'id=' . $this->resource);
273
				$success = ($GLOBALS['TYPO3_DB']->sql_affected_rows() > 0);
233 274
			case 'disable':
234 275
				$success = FALSE;
235 276
			break;
......
287 328
	public function sysLog($message, $severity=0) {
288 329
		t3lib_div::sysLog('Locking [' . $this->method . '::' . $this->id.']: ' . trim($message), 'cms', $severity);
289 330
	}
331

  
332
	/**
333
	 * Determines whether a database lock is active.
334
	 *
335
	 * @return boolean
336
	 */
337
	protected function isDatabaseLockActive() {
338
		return ($GLOBALS['TYPO3_DB']->exec_SELECTcountRows('id', 'sys_lock', 'id=' . $this->resource) != 0);
339
	}
290 340
}
291 341

  
292 342