1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Core; |
4 | | | |
5 | | | /* |
6 | | | CLASS ExclusiveLock |
7 | | | Description |
8 | | | ================================================================== |
9 | | | This is a pseudo implementation of mutex since php does not have |
10 | | | any thread synchronization objects |
11 | | | This class uses flock() as a base to provide locking functionality. |
12 | | | Lock will be released in following cases |
13 | | | 1 - user calls unlock |
14 | | | 2 - when this lock object gets deleted |
15 | | | 3 - when request or script ends |
16 | | | ================================================================== |
17 | | | Usage: |
18 | | | |
19 | | | //get the lock |
20 | | | $lock = new ExclusiveLock( "mylock", FALSE); |
21 | | | |
22 | | | //lock |
23 | | | if( $lock->lock( ) == FALSE ) |
24 | | | error("Locking failed"); |
25 | | | //-- |
26 | | | //Do your work here |
27 | | | //-- |
28 | | | |
29 | | | //unlock |
30 | | | $lock->unlock(); |
31 | | | =================================================================== |
32 | | | */ |
33 | | | class ExclusiveLock |
34 | | | { |
35 | | | protected $key = null; //user given value |
36 | | | protected $file = null; //resource to lock |
37 | | | protected $own = FALSE; //have we locked resource |
38 | | | |
39 | | | function __construct( $key ) |
40 | | | { |
41 | | | $this->key = $key; |
42 | | | //create a new resource or get exisitng with same key |
43 | | | $this->file = fopen(RUN_DIR.DS."Lock".DS."$key.lockfile", 'w+'); |
44 | | | } |
45 | | | |
46 | | | |
47 | | | function __destruct() |
48 | | | { |
49 | | | if( $this->own == TRUE ) |
50 | | | $this->unlock( ); |
51 | | | } |
52 | | | |
53 | | | |
54 | | | function lock( ) |
55 | | | { |
56 | | | if( !flock($this->file, LOCK_EX)) |
57 | | | { //failed |
58 | | | $key = $this->key; |
59 | | | error_log("ExclusiveLock::acquire_lock FAILED to acquire lock [$key]"); |
60 | | | return FALSE; |
61 | | | } |
62 | | | ftruncate($this->file, 0); // truncate file |
63 | | | //write something to just help debugging |
64 | | | fwrite( $this->file, "Locked\n"); |
65 | | | fflush( $this->file ); |
66 | | | |
67 | | | $this->own = TRUE; |
68 | | | return $this->own; |
69 | | | } |
70 | | | |
71 | | | |
72 | | | function unlock( ) |
73 | | | { |
74 | | | $key = $this->key; |
75 | | | if( $this->own == TRUE ) |
76 | | | { |
77 | | | if( !flock($this->file, LOCK_UN) ) |
78 | | | { //failed |
79 | | | error_log("ExclusiveLock::lock FAILED to release lock [$key]"); |
80 | | | return FALSE; |
81 | | | } |
82 | | | ftruncate($this->file, 0); // truncate file |
83 | | | //write something to just help debugging |
84 | | | fwrite( $this->file, "Unlocked\n"); |
85 | | | fflush( $this->file ); |
86 | | | } |
87 | | | else |
88 | | | { |
89 | | | error_log("ExclusiveLock::unlock called on [$key] but its not acquired by caller"); |
90 | | | } |
91 | | | $this->own = FALSE; |
92 | | | return $this->own; |
93 | | | } |
94 | | | } |