@@ -17,8 +17,6 @@ public function __construct(string $tag, bool $auto = true, bool $autoShared = f
17
17
$ hash = hash ("sha256 " , $ tag );
18
18
$ this ->fn = Config::getTmpDir () . DIRECTORY_SEPARATOR . $ hash . ".lock " ;
19
19
20
- $ this ->fd = fopen ($ this ->fn , "wb " );
21
-
22
20
if ($ auto ) {
23
21
if ($ autoShared ) {
24
22
$ this ->shared ();
@@ -28,6 +26,15 @@ public function __construct(string $tag, bool $auto = true, bool $autoShared = f
28
26
}
29
27
}/*}}}*/
30
28
29
+ public function __destruct ()
30
+ {/*{{{*/
31
+ $ this ->unlock ();
32
+ /* We don't really know no one else waits on the same lock yet.*/
33
+ /*if (file_exists($this->fn) && !$this->shared) {
34
+ @unlink($this->fn);
35
+ }*/
36
+ }/*}}}*/
37
+
31
38
public function shared (bool $ block = false ) : bool
32
39
{/*{{{*/
33
40
$ flags = LOCK_SH ;
@@ -55,16 +62,27 @@ protected function doLock(int $flags = LOCK_EX) : bool
55
62
return true ;
56
63
}
57
64
58
- $ this ->locked = flock ($ this ->fd , $ flags , $ this ->wouldBlock );
59
65
$ this ->shared = $ flags & LOCK_SH ;
66
+ if ($ this ->shared ) {
67
+ $ this ->fd = fopen ($ this ->fn , "rb " );
68
+ } else {
69
+ $ this ->fd = fopen ($ this ->fn , "wb " );
70
+ }
71
+ $ this ->locked = flock ($ this ->fd , $ flags , $ this ->wouldBlock );
60
72
return $ this ->locked ;
61
73
}/*}}}*/
62
74
63
75
public function unlock () : bool
64
76
{/*{{{*/
65
- if ($ this ->locked ) {
66
- return $ this -> doLock ( LOCK_UN ) ;
77
+ if (! $ this ->locked ) {
78
+ return true ;
67
79
}
80
+
81
+ $ this ->doLock (LOCK_UN );
82
+
83
+ fclose ($ this ->fd );
84
+ $ this ->fd = NULL ;
85
+
68
86
return $ this ->locked ;
69
87
}/*}}}*/
70
88
@@ -77,17 +95,6 @@ public function wouldBlock() : bool
77
95
{/*{{{*/
78
96
return 1 === $ this ->wouldBlock ;
79
97
}/*}}}*/
80
-
81
- public function __destruct ()
82
- {/*{{{*/
83
- $ this ->unlock ();
84
- fclose ($ this ->fd );
85
- /* We don't really know no one else waits on the same lock yet.*/
86
- /*if (file_exists($this->fn) && !$this->shared) {
87
- @unlink($this->fn);
88
- }*/
89
- }/*}}}*/
90
-
91
98
}
92
99
93
100
/*
0 commit comments