original_string: public function lock($key, $ownerid, $block = false) {
          // Get the name of the lock file we want to use.
          $lockfile = $this->get_lock_file($key);
  
          // Attempt to create a handle to the lock file.
          // Mode xb is the secret to this whole function.
          //   x = Creates the file and opens it for writing. If the file already exists fopen returns false and a warning is thrown.
          //   b = Forces binary mode.
          $result = @fopen($lockfile, 'xb');
  
          // Check if we could create the file or not.
          if ($result === false) {
              // Lock exists already.
              if ($this->maxlife !== null && !array_key_exists($key, $this->locks)) {
                  $mtime = filemtime($lockfile);
                  if ($mtime < time() - $this->maxlife) {
                      $this->unlock($key, true);
                      $result = $this->lock($key, false);
                      if ($result) {
                          return true;
                      }
                  }
              }
              if ($block) {
                  // OK we are blocking. We had better sleep and then retry to lock.
                  $iterations = 0;
                  $maxiterations = $this->blockattempts;
                  while (($result = $this->lock($key, false)) === false) {
                      // Usleep causes the application to cleep to x microseconds.
                      // Before anyone asks there are 1'000'000 microseconds to a second.
                      usleep(rand(1000, 50000)); // Sleep between 1 and 50 milliseconds.
                      $iterations++;
                      if ($iterations > $maxiterations) {
                          // BOOM! We've exceeded the maximum number of iterations we want to block for.
                          throw new cache_exception('ex_unabletolock');
                      }
                  }
              }
  
              return false;
          } else {
              // We have the lock.
              fclose($result);
              $this->locks[$key] = $lockfile;
              return true;
          }
      }
code: public function lock($key, $ownerid, $block = false) {
          // Get the name of the lock file we want to use.
          $lockfile = $this->get_lock_file($key);
  
          // Attempt to create a handle to the lock file.
          // Mode xb is the secret to this whole function.
          //   x = Creates the file and opens it for writing. If the file already exists fopen returns false and a warning is thrown.
          //   b = Forces binary mode.
          $result = @fopen($lockfile, 'xb');
  
          // Check if we could create the file or not.
          if ($result === false) {
              // Lock exists already.
              if ($this->maxlife !== null && !array_key_exists($key, $this->locks)) {
                  $mtime = filemtime($lockfile);
                  if ($mtime < time() - $this->maxlife) {
                      $this->unlock($key, true);
                      $result = $this->lock($key, false);
                      if ($result) {
                          return true;
                      }
                  }
              }
              if ($block) {
                  // OK we are blocking. We had better sleep and then retry to lock.
                  $iterations = 0;
                  $maxiterations = $this->blockattempts;
                  while (($result = $this->lock($key, false)) === false) {
                      // Usleep causes the application to cleep to x microseconds.
                      // Before anyone asks there are 1'000'000 microseconds to a second.
                      usleep(rand(1000, 50000)); // Sleep between 1 and 50 milliseconds.
                      $iterations++;
                      if ($iterations > $maxiterations) {
                          // BOOM! We've exceeded the maximum number of iterations we want to block for.
                          throw new cache_exception('ex_unabletolock');
                      }
                  }
              }
  
              return false;
          } else {
              // We have the lock.
              fclose($result);
              $this->locks[$key] = $lockfile;
              return true;
          }
      }
code_tokens: public function lock ( $ key , $ ownerid , $ block = false ) { // Get the name of the lock file we want to use. $ lockfile = $ this -> get_lock_file ( $ key ) ; // Attempt to create a handle to the lock file. // Mode xb is the secret to this whole function. //   x = Creates the file and opens it for writing. If the file already exists fopen returns false and a warning is thrown. //   b = Forces binary mode. $ result = @ fopen ( $ lockfile , 'xb' ) ; // Check if we could create the file or not. if ( $ result === false ) { // Lock exists already. if ( $ this -> maxlife !== null && ! array_key_exists ( $ key , $ this -> locks ) ) { $ mtime = filemtime ( $ lockfile ) ; if ( $ mtime < time ( ) - $ this -> maxlife ) { $ this -> unlock ( $ key , true ) ; $ result = $ this -> lock ( $ key , false ) ; if ( $ result ) { return true ; } } } if ( $ block ) { // OK we are blocking. We had better sleep and then retry to lock. $ iterations = 0 ; $ maxiterations = $ this -> blockattempts ; while ( ( $ result = $ this -> lock ( $ key , false ) ) === false ) { // Usleep causes the application to cleep to x microseconds. // Before anyone asks there are 1'000'000 microseconds to a second. usleep ( rand ( 1000 , 50000 ) ) ; // Sleep between 1 and 50 milliseconds. $ iterations ++ ; if ( $ iterations > $ maxiterations ) { // BOOM! We've exceeded the maximum number of iterations we want to block for. throw new cache_exception ( 'ex_unabletolock' ) ; } } } return false ; } else { // We have the lock. fclose ( $ result ) ; $ this -> locks [ $ key ] = $ lockfile ; return true ; } }
docstring: Acquire a lock.
  
  If the lock can be acquired:
  This function will return true.
  
  If the lock cannot be acquired the result of this method is determined by the block param:
  $block = true (default)
  The function will block any further execution unti the lock can be acquired.
  This involves the function attempting to acquire the lock and the sleeping for a period of time. This process
  will be repeated until the lock is required or until a limit is hit (100 by default) in which case a cache
  exception will be thrown.
  $block = false
  The function will return false immediately.
  
  If a max life has been specified and the lock can not be acquired then the lock file will be checked against this time.
  In the case that the file exceeds that max time it will be forcefully deleted.
  Because this can obviously be a dangerous thing it is not used by default. If it is used it should be set high enough that
  we can be as sure as possible that the executing code has completed.
  
  @param string $key The key that we want to lock
  @param string $ownerid A unique identifier for the owner of this lock. Not used by default.
  @param bool $block True if we want the program block further execution until the lock has been acquired.
  @return bool
  @throws cache_exception If block is set to true and more than 100 attempts have been made to acquire a lock.
docstring_tokens: Acquire a lock .