classes/SharedLock.php
static public function acquire( $token, $mode, $opts=array(), $autoCleanup=true /*, $task=''*/ )
    {
        // just in case (is_file results might be cached!)...
        clearstatcache();

        if ( $autoCleanup && !self::$cleanedUp )
        {
            self::cleanup( $opts );
            self::$cleanedUp = true;
        }

        $lockDir = self::lockDir( $opts );
        $wLockFile = "$lockDir/{$token}_W.lock";

        if ( file_exists( $wLockFile ) )
        {
            return false;
        }

        if ( $mode == LOCK_EX && count( glob( $lockDir . "/{$token}_R/*.lock" ) ) )
        {
            return false;
        }

        if ( $mode == LOCK_EX )
        {
            pake_mkdirs( $lockDir );
            if ( !file_put_contents( $wLockFile, getmypid() /*. ' ' . $task*/, LOCK_EX ) )
            {
                pake_echo_error( "Could not create W lock file '$wLockFile'" );
                return false;
            }
            return true;
        }

        // assume a read lock
        $rLockFile = "$lockDir/{$token}_R/" . getmypid() . ".lock";
        pake_mkdirs( "$lockDir/{$token}_R/" );
        // we assume to be running in single-thread mode: do not lock the file for writing
        if ( !file_put_contents( $rLockFile, getmypid() /*. ' ' . $task*/ ) )
        {
            // log some error?
            pake_echo_error( "Could not create R lock file '$wLockFile'" );
            return false;
        }
        return true;
    }
static public function acquire( $token, $mode, $opts=array(), $autoCleanup=true /*, $task=''*/ )
    {
        // just in case (is_file results might be cached!)...
        clearstatcache();

        if ( $autoCleanup && !self::$cleanedUp )
        {
            self::cleanup( $opts );
            self::$cleanedUp = true;
        }

        $lockDir = self::lockDir( $opts );
        $wLockFile = "$lockDir/{$token}_W.lock";

        if ( file_exists( $wLockFile ) )
        {
            return false;
        }

        if ( $mode == LOCK_EX && count( glob( $lockDir . "/{$token}_R/*.lock" ) ) )
        {
            return false;
        }

        if ( $mode == LOCK_EX )
        {
            pake_mkdirs( $lockDir );
            if ( !file_put_contents( $wLockFile, getmypid() /*. ' ' . $task*/, LOCK_EX ) )
            {
                pake_echo_error( "Could not create W lock file '$wLockFile'" );
                return false;
            }
            return true;
        }

        // assume a read lock
        $rLockFile = "$lockDir/{$token}_R/" . getmypid() . ".lock";
        pake_mkdirs( "$lockDir/{$token}_R/" );
        // we assume to be running in single-thread mode: do not lock the file for writing
        if ( !file_put_contents( $rLockFile, getmypid() /*. ' ' . $task*/ ) )
        {
            // log some error?
            pake_echo_error( "Could not create R lock file '$wLockFile'" );
            return false;
        }
        return true;
    }
static public function acquire ( $ token , $ mode , $ opts = array ( ) , $ autoCleanup = true /*, $task=''*/ ) { // just in case (is_file results might be cached!)...
 clearstatcache ( ) ; if ( $ autoCleanup && ! self :: $ cleanedUp ) { self :: cleanup ( $ opts ) ; self :: $ cleanedUp = true ; } $ lockDir = self :: lockDir ( $ opts ) ; $ wLockFile = "$lockDir/{$token}_W.lock" ; if ( file_exists ( $ wLockFile ) ) { return false ; } if ( $ mode == LOCK_EX && count ( glob ( $ lockDir . "/{$token}_R/*.lock" ) ) ) { return false ; } if ( $ mode == LOCK_EX ) { pake_mkdirs ( $ lockDir ) ; if ( ! file_put_contents ( $ wLockFile , getmypid ( ) /*. ' ' . $task*/ , LOCK_EX ) ) { pake_echo_error ( "Could not create W lock file '$wLockFile'" ) ; return false ; } return true ; } // assume a read lock
 $ rLockFile = "$lockDir/{$token}_R/" . getmypid ( ) . ".lock" ; pake_mkdirs ( "$lockDir/{$token}_R/" ) ; // we assume to be running in single-thread mode: do not lock the file for writing
 if ( ! file_put_contents ( $ rLockFile , getmypid ( ) /*. ' ' . $task*/ ) ) { // log some error?
 pake_echo_error ( "Could not create R lock file '$wLockFile'" ) ; return false ; } return true ; }
Returns true if the lock is acquired for the given token (no waiting here).
A Write lock can only be acquired as long as there are no W or R locks.
A Read lock can be acquired as long as there are no W locks.
Does not not complain if 2 Read locks are taken on the same token by the same php script.

@param string $token
@param int $mode LOCK_SH (reader) or LOCK_EX (writer)
@param array $opts
@param bool $autoCleanup when true, on first lock acquired we remove any stale locks found
@return bool
Returns true if the lock is acquired for the given token ( no waiting here ) . A Write lock can only be acquired as long as there are no W or R locks . A Read lock can be acquired as long as there are no W locks . Does not not complain if 2 Read locks are taken on the same token by the same php script .