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