00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 define( 'eZMutex_StealString', '_eZMutex_Steal' );
00048
00049 include_once( 'lib/ezutils/classes/ezsys.php' );
00050 include_once( 'lib/ezfile/classes/ezfile.php' );
00051
00052 class eZMutex
00053 {
00054
00055
00056
00057
00058
00059
00060
00061 function eZMutex( $name )
00062 {
00063 include_once( 'lib/ezutils/classes/ezdir.php' );
00064 $this->Name = md5( $name );
00065 $mutexPath = eZDir::path( array( eZSys::cacheDirectory(),
00066 'ezmutex' ) );
00067 eZDir::mkdir( $mutexPath, false, true );
00068 $this->FileName = eZDir::path( array( $mutexPath,
00069 $this->Name ) );
00070 $this->MetaFileName = eZDir::path( array( $mutexPath,
00071 $this->Name . '_meta' ) );
00072 }
00073
00074
00075
00076
00077
00078 function fp()
00079 {
00080 if ( !isset( $GLOBALS['eZMutex_FP_' . $this->FileName] ) ||
00081 $GLOBALS['eZMutex_FP_' . $this->FileName] === false )
00082 {
00083 $GLOBALS['eZMutex_FP_' . $this->FileName] = fopen( $this->FileName, 'w' );
00084 if ( $GLOBALS['eZMutex_FP_' . $this->FileName] === false )
00085 {
00086 eZDebug::writeError( 'Failed to open file: ' . $this->FileName );
00087 }
00088 }
00089 return $GLOBALS['eZMutex_FP_' . $this->FileName];
00090 }
00091
00092
00093
00094
00095
00096
00097 function test()
00098 {
00099 if ( $fp = $this->fp() )
00100 {
00101 if ( flock( $fp, LOCK_EX | LOCK_NB ) )
00102 {
00103 flock( $fp, LOCK_UN );
00104 return false;
00105 }
00106 }
00107 return true;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 function lock()
00117 {
00118 if ( $fp = $this->fp() )
00119 {
00120 if ( flock( $fp, LOCK_EX ) )
00121 {
00122 $this->clearMeta();
00123 $this->setMeta( 'timestamp', gmmktime() );
00124 return true;
00125 }
00126 }
00127 return false;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136 function setMeta( $key, $value )
00137 {
00138 $tmpFile = $this->MetaFileName . substr( md5( mt_rand() ), 0, 8 );
00139 $content = array();
00140 if ( file_exists( $this->MetaFileName ) )
00141 {
00142 $content = unserialize( eZFile::getContents( $this->MetaFileName ) );
00143 }
00144 $content[$key] = $value;
00145 eZFile::create( $tmpFile, false, serialize( $content) );
00146 eZFile::rename( $tmpFile, $this->MetaFileName );
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156 function meta( $key )
00157 {
00158 $content = array();
00159 if ( file_exists( $this->MetaFileName ) )
00160 {
00161 $content = unserialize( eZFile::getContents( $this->MetaFileName ) );
00162 }
00163 return isset( $content[$key] ) ? $content[$key] : null;
00164 }
00165
00166
00167
00168
00169 function clearMeta()
00170 {
00171 $tmpFile = $this->MetaFileName . substr( md5( mt_rand() ), 0, 8 );
00172 $content = array();
00173 eZFile::create( $tmpFile, false, serialize( $content) );
00174 eZFile::rename( $tmpFile, $this->MetaFileName );
00175 }
00176
00177
00178
00179
00180
00181
00182 function unlock()
00183 {
00184 if ( $fp = $this->fp() )
00185 {
00186 @unlink( $this->MetaFileName );
00187 @unlink( $this->FileName );
00188 $GLOBALS['eZMutex_FP_' . $this->FileName] = false;
00189 }
00190 return false;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199 function lockTS()
00200 {
00201 return $this->test() ? $this->meta( 'timestamp' ) : false;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 function steal( $force = false )
00222 {
00223 $stealMutex = new eZMutex( $this->Name . eZMutex_StealString );
00224 if ( !$force )
00225 {
00226
00227 if ( $stealMutex->test() )
00228 {
00229 return false;
00230 }
00231 if ( $stealMutex->lock() )
00232 {
00233 $stealMutex->setMeta( 'pid', getmypid() );
00234 if ( $this->lock() )
00235 {
00236
00237
00238 sleep( 1 );
00239 $stealMutex->unlock();
00240 return true;
00241 }
00242 }
00243 }
00244 else
00245 {
00246 $stealPid = $stealMutex->meta( 'pid' );
00247 if ( is_numeric( $stealPid ) &&
00248 $stealPid != 0 &&
00249 function_exists( 'posix_kill' ) )
00250 {
00251 eZDebug::writeNotice( 'Killing steal mutex process: ' . $stealPid );
00252 posix_kill( $stealPid, 9 );
00253 }
00254
00255
00256 $this->unlock();
00257 return $this->lock();
00258 }
00259 return false;
00260 }
00261
00262 var $Name;
00263 var $FileName;
00264 }
00265
00266 ?>