(root)/shared-lockableclass.php - Rev 407
Rev 287 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
<?PHP
class lockableClass
{
var $lockfile; ///< string The file which is used to lock the data against access
/**
* Lock the class' file access
*
\code
if( $this->lock() )
{
// Reload the class' data to really be on the safe side
// $this->load();
// Do some processing...
// $this->save();
// Remember to unlock before continuing
$this->unlock();
}
\endcode
* @return bool True if successful, false if locking was impossible
*/
function lock
()
{
$maxtime = 600; // Wait for a maximum of 60 seconds before failing
$i = 0;
while( file_exists( $this->lockfile ) )
{
usleep( 100 );
if( $i++ > $maxtime )
{
// If this lock is older than the possible execution time for PHP scripts on this server, something borked and we can safely unlock
if( filemtime( $this->lockfile ) < time() - ini_get( "max_execution_time" ) )
break;
else
return false;
}
}
touch( $this->lockfile );
return true;
}
/**
* Unlock the class' file access
*/
function unlock
()
{
unlink( $this->lockfile );
}
}
?>