src/xPDO/Cache/xPDOCacheManager.php
public function lockFile($file, array $options = array()) {
        $locked = false;
        $lockDir = $this->getOption('lock_dir', $options, $this->getCachePath() . 'locks' . DIRECTORY_SEPARATOR);
        if ($this->writeTree($lockDir, $options)) {
            $lockFile = $this->lockFileName($file, $options);
            if (!file_exists($lockFile)) {
                $myPID = (php_sapi_name() == 'cli' || !isset($_SERVER['SERVER_ADDR']) ? gethostname() : $_SERVER['SERVER_ADDR']) . '.' . getmypid();
                $myPID .= mt_rand();
                $tmpLockFile = "{$lockFile}.{$myPID}";
                if (file_put_contents($tmpLockFile, $myPID)) {
                    if (link($tmpLockFile, $lockFile)) {
                        $locked = true;
                    }
                    @unlink($tmpLockFile);
                }
            }
        } else {
            $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "The lock_dir at {$lockDir} is not writable and could not be created");
        }
        if (!$locked) $this->xpdo->log(xPDO::LOG_LEVEL_WARN, "Attempt to lock file {$file} failed");
        return $locked;
    }
public function lockFile($file, array $options = array()) {
        $locked = false;
        $lockDir = $this->getOption('lock_dir', $options, $this->getCachePath() . 'locks' . DIRECTORY_SEPARATOR);
        if ($this->writeTree($lockDir, $options)) {
            $lockFile = $this->lockFileName($file, $options);
            if (!file_exists($lockFile)) {
                $myPID = (php_sapi_name() == 'cli' || !isset($_SERVER['SERVER_ADDR']) ? gethostname() : $_SERVER['SERVER_ADDR']) . '.' . getmypid();
                $myPID .= mt_rand();
                $tmpLockFile = "{$lockFile}.{$myPID}";
                if (file_put_contents($tmpLockFile, $myPID)) {
                    if (link($tmpLockFile, $lockFile)) {
                        $locked = true;
                    }
                    @unlink($tmpLockFile);
                }
            }
        } else {
            $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "The lock_dir at {$lockDir} is not writable and could not be created");
        }
        if (!$locked) $this->xpdo->log(xPDO::LOG_LEVEL_WARN, "Attempt to lock file {$file} failed");
        return $locked;
    }
public function lockFile ( $ file , array $ options = array ( ) ) { $ locked = false ; $ lockDir = $ this -> getOption ( 'lock_dir' , $ options , $ this -> getCachePath ( ) . 'locks' . DIRECTORY_SEPARATOR ) ; if ( $ this -> writeTree ( $ lockDir , $ options ) ) { $ lockFile = $ this -> lockFileName ( $ file , $ options ) ; if ( ! file_exists ( $ lockFile ) ) { $ myPID = ( php_sapi_name ( ) == 'cli' || ! isset ( $ _SERVER [ 'SERVER_ADDR' ] ) ? gethostname ( ) : $ _SERVER [ 'SERVER_ADDR' ] ) . '.' . getmypid ( ) ; $ myPID .= mt_rand ( ) ; $ tmpLockFile = "{$lockFile}.{$myPID}" ; if ( file_put_contents ( $ tmpLockFile , $ myPID ) ) { if ( link ( $ tmpLockFile , $ lockFile ) ) { $ locked = true ; } @ unlink ( $ tmpLockFile ) ; } } } else { $ this -> xpdo -> log ( xPDO :: LOG_LEVEL_ERROR , "The lock_dir at {$lockDir} is not writable and could not be created" ) ; } if ( ! $ locked ) $ this -> xpdo -> log ( xPDO :: LOG_LEVEL_WARN , "Attempt to lock file {$file} failed" ) ; return $ locked ; }
Add an exclusive lock to a file for atomic write operations in multi-threaded environments.

xPDO::OPT_USE_FLOCK must be set to false (or 0) or xPDO will assume flock is reliable.

@param string $file The name of the file to lock.
@param array $options An array of options for the process.
@return boolean True only if the current process obtained an exclusive lock for writing.
Add an exclusive lock to a file for atomic write operations in multi - threaded environments .