Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmutex.php
Go to the documentation of this file.
1 <?php
17 class eZMutex
18 {
19 const STEAL_STRING = '_eZMutex_Steal';
20
25 public function __construct( $name )
26 {
27 $this->Name = md5( $name );
28 $mutexPath = eZDir::path( array( eZSys::cacheDirectory(),
29 'ezmutex' ) );
30 eZDir::mkdir( $mutexPath, false, true );
31 $this->FileName = eZDir::path( array( $mutexPath,
32 $this->Name ) );
33 $this->MetaFileName = eZDir::path( array( $mutexPath,
34 $this->Name . '_meta' ) );
35 }
36
41 function fp()
42 {
43 if ( !isset( $GLOBALS['eZMutex_FP_' . $this->FileName] ) ||
44 $GLOBALS['eZMutex_FP_' . $this->FileName] === false )
45 {
46 $GLOBALS['eZMutex_FP_' . $this->FileName] = fopen( $this->FileName, 'w' );
47 if ( $GLOBALS['eZMutex_FP_' . $this->FileName] === false )
48 {
49 eZDebug::writeError( 'Failed to open file: ' . $this->FileName );
50 }
51 }
52 return $GLOBALS['eZMutex_FP_' . $this->FileName];
53 }
54
60 function test()
61 {
62 if ( $fp = $this->fp() )
63 {
64 if ( flock( $fp, LOCK_EX | LOCK_NB ) )
65 {
66 flock( $fp, LOCK_UN );
67 return false;
68 }
69 }
70 return true;
71 }
72
79 function lock()
80 {
81 if ( $fp = $this->fp() )
82 {
83 if ( flock( $fp, LOCK_EX ) )
84 {
85 $this->clearMeta();
86 $this->setMeta( 'timestamp', time() );
87 return true;
88 }
89 }
90 return false;
91 }
92
99 function setMeta( $key, $value )
100 {
101 $content = array();
102 if ( file_exists( $this->MetaFileName ) )
103 {
104 $content = unserialize( file_get_contents( $this->MetaFileName ) );
105 }
106 $content[$key] = $value;
107 eZFile::create( $this->MetaFileName, false, serialize( $content), true );
108 }
109
117 function meta( $key )
118 {
119 $content = array();
120 if ( file_exists( $this->MetaFileName ) )
121 {
122 $content = unserialize( file_get_contents( $this->MetaFileName ) );
123 }
124 return isset( $content[$key] ) ? $content[$key] : null;
125 }
126
130 function clearMeta()
131 {
132 $content = array();
133 eZFile::create( $this->MetaFileName, false, serialize( $content), true );
134 }
135
141 function unlock()
142 {
143 if ( $fp = $this->fp() )
144 {
145 fclose( $fp );
146
147 if ( file_exists( $this->MetaFileName ) )
148 {
149 @unlink( $this->MetaFileName );
150 }
151
152 if ( file_exists( $this->FileName ) )
153 {
154 @unlink( $this->FileName );
155 }
156
157 $GLOBALS['eZMutex_FP_' . $this->FileName] = false;
158 }
159 return false;
160 }
161
168 function lockTS()
169 {
170 return $this->test() ? $this->meta( 'timestamp' ) : false;
171 }
172
190 function steal( $force = false )
191 {
192 $stealMutex = new eZMutex( $this->Name . eZMutex::STEAL_STRING );
193 if ( !$force )
194 {
195 // Aquire a steal mutex, and steal the mutex.
196 if ( $stealMutex->test() )
197 {
198 return false;
199 }
200 if ( $stealMutex->lock() )
201 {
202 $stealMutex->setMeta( 'pid', getmypid() );
203 if ( $this->lock() )
204 {
205 // sleep for 1 second in case lock has only been granted beacause a larger
206 // cleanup is in progress.
207 sleep( 1 );
208 $stealMutex->unlock();
209 return true;
210 }
211 }
212 }
213 else
214 {
215 $stealPid = $stealMutex->meta( 'pid' );
216 if ( is_numeric( $stealPid ) &&
217 $stealPid != 0 &&
218 function_exists( 'posix_kill' ) )
219 {
220 eZDebug::writeNotice( 'Killing steal mutex process: ' . $stealPid );
221 posix_kill( $stealPid, 9 );
222 }
223
224 // If other steal mutex exists, kill it, and create your own.
225 $this->unlock();
226 return $this->lock();
227 }
228 return false;
229 }
230
231 public $Name;
232 public $FileName;
233 public $MetaFileName;
234
235 }
236
237 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZDebug\writeNotice
static writeNotice( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:459
eZDir\mkdir
static mkdir( $dir, $perm=false, $recursive=false)
Definition: ezdir.php:73
eZDir\path
static path( $names, $includeEndSeparator=false, $type=self::SEPARATOR_UNIX)
Definition: ezdir.php:237
eZFile\create
static create( $filename, $directory=false, $data=false, $atomic=false)
Definition: ezfile.php:41
eZMutex
The class eZMutex provides a file based mutex. The mutex works across processes.
Definition: ezmutex.php:18
eZMutex\meta
meta( $key)
Definition: ezmutex.php:117
eZMutex\lock
lock()
Definition: ezmutex.php:79
eZMutex\fp
fp()
Definition: ezmutex.php:41
eZMutex\$Name
$Name
Definition: ezmutex.php:231
eZMutex\clearMeta
clearMeta()
Definition: ezmutex.php:130
eZMutex\STEAL_STRING
const STEAL_STRING
Definition: ezmutex.php:19
eZMutex\__construct
__construct( $name)
Definition: ezmutex.php:25
eZMutex\setMeta
setMeta( $key, $value)
Definition: ezmutex.php:99
eZMutex\$FileName
$FileName
Definition: ezmutex.php:232
eZMutex\lockTS
lockTS()
Definition: ezmutex.php:168
eZMutex\steal
steal( $force=false)
Definition: ezmutex.php:190
eZMutex\test
test()
Definition: ezmutex.php:60
eZMutex\$MetaFileName
$MetaFileName
Definition: ezmutex.php:233
eZMutex\unlock
unlock()
Definition: ezmutex.php:141
eZSys\cacheDirectory
static cacheDirectory()
Definition: ezsys.php:527
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.