1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Utils;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: final class CriticalSection
24: {
25:
26: private static $criticalSections;
27:
28:
29:
30: 31: 32:
33: final public function __construct()
34: {
35: throw new Nette\StaticClassException;
36: }
37:
38:
39:
40: 41: 42: 43:
44: public static function enter()
45: {
46: if (self::$criticalSections) {
47: throw new Nette\InvalidStateException('Critical section has already been entered.');
48: }
49:
50: $handle = substr(PHP_OS, 0, 3) === 'WIN'
51: ? @fopen(NETTE_DIR . '/lockfile', 'w')
52: : @fopen(__FILE__, 'r');
53:
54: if (!$handle) {
55: throw new Nette\InvalidStateException("Unable initialize critical section.");
56: }
57: flock(self::$criticalSections = $handle, LOCK_EX);
58: }
59:
60:
61:
62: 63: 64: 65:
66: public static function leave()
67: {
68: if (!self::$criticalSections) {
69: throw new Nette\InvalidStateException('Critical section has not been initialized.');
70: }
71: flock(self::$criticalSections, LOCK_UN);
72: fclose(self::$criticalSections);
73: self::$criticalSections = NULL;
74: }
75:
76: }
77: