Lock file for writing. You need to manually unlock().

@param bool $block  For non-blocking lock, set the parameter to false.
@return bool
@throws \RuntimeException
339489
public function lock($block = true)
    {
        if (!$this->handle) {
            if (!$this->mkdir(\dirname($this->filename))) {
                throw new \RuntimeException('Creating directory failed for ' . $this->filename);
            }
            $this->handle = @fopen($this->filename, 'cb+');
            if (!$this->handle) {
                $error = error_get_last();

                throw new \RuntimeException("Opening file for writing failed on error {$error['message']}");
            }
        }
        $lock = $block ? LOCK_EX : LOCK_EX | LOCK_NB;

        // Some filesystems do not support file locks, only fail if another process holds the lock.
        $this->locked = flock($this->handle, $lock, $wouldblock) || !$wouldblock;

        return $this->locked;
    }
1230088
public function lock($lockType, $nonBlockingLock = false)
    {
        if ($nonBlockingLock) {
            return flock($this->_fileHandle, $lockType | LOCK_NB);
        } else {
            return flock($this->_fileHandle, $lockType);
        }
    }
126620
def lock(self, block=True):
		
		self._locked = True
		return self._lock.acquire(block)
430881
public function unlock()
    {
        $this->storage->openWriter();
        $object = $this->getObjectSafely('openWriter');
        $object->setLocked(false);
        $this->lock = false;
        $this->storage->setObject($object);
        $this->storage->close();
        return $this;
    }
80465
public function addBlock(Block $block)
	{
		if ($this->lock('block')) {
			$this->matchDiscriminator($block);
			if ($this->addUnique($this->blocks, $block)) {
				$block->setPlaceHolder($this);
			}
			$this->unlock('block');
		}
	}
824163
public function lock($mode = true)
    {
        $this->shouldLock = $mode;

        if (!$handle = $this->resource()) {
            return false;
        }

        return $this->applyLock($handle);

    }
1199810
public function lock($key, $timeout = self::EXPIRE): bool
    {
        $wait = 20000;
        $totalWait = 0;
        $time = $timeout * 1000000;
        $key = self::PREFIX . $key;

        while ($totalWait < $time && false === $this->mem->add($key, 1, $timeout)) {
            usleep($wait);
            $totalWait += $wait;
        }

        if ($totalWait >= $time) {
            throw new \RuntimeException('cannot get lock for waiting ' . $timeout . 's.', __LINE__);
        }

        return true;
    }
1333162
def write(self, block):
        
        self.fileobj.write(block)
        self.status.update(len(block))
1266933
private function finishBlock($block)
    {
        if (!array_key_exists($block, $this->blocks)) {
            throw new \Exception('Block '.$block.' not defined');
        }
        $this->blocks[$block]['total'] = $this->blocks[$block]['stop'] - $this->blocks[$block]['start'];

        return $this->blocks[$block];
    }
575080
def lock_file(f, block=False):
    
    try:
        flags = fcntl.LOCK_EX
        if not block:
            flags |= fcntl.LOCK_NB
        fcntl.flock(f.fileno(), flags)
    except IOError as e:
        if e.errno in (errno.EACCES, errno.EAGAIN):
            raise SystemExit("ERROR: %s is locked by another process." %
                             f.name)
        raise
657915
private function lock($name = null, $blocking = false)
    {
        if (!class_exists(SemaphoreStore::class)) {
            throw new LogicException('To enable the locking feature you must install the symfony/lock component.');
        }

        if (null !== $this->lock) {
            throw new LogicException('A lock is already in place.');
        }

        if (SemaphoreStore::isSupported()) {
            $store = new SemaphoreStore();
        } else {
            $store = new FlockStore();
        }

        $this->lock = (new Factory($store))->createLock($name ?: $this->getName());
        if (!$this->lock->acquire($blocking)) {
            $this->lock = null;

            return false;
        }

        return true;
    }
722923
private function acquireLock()
    {
        $this->logger->info('Acquire lock file.');

        if (!$this->lock->lock()) {
            $locked = false;
            $retry  = 3;
            // Try up to 3 times to acquire with short delay in between.
            while ($retry > 0) {
                usleep(1000);
                if ($locked = $this->lock->lock()) {
                    break;
                }
                $retry--;
            }
            if (!$locked) {
                $this->logger->error('Could not acquire lock file.');
                throw new \RuntimeException(
                    'Another task appears to be running. If this is not the case, please remove the lock file.'
                );
            }
        }
    }
842750
public function stop($block)
    {
        if ($this->enabled) {
            if (!isset($this->blocks[$block])) {
                throw new \Exception('Block '.$block.' has not been started!');
            }

            $this->blocks[$block]['stop'] = microtime(true);
            $this->blocks[$block]['stop-line'] = $this->getLineNumber();
        }
    }
1305500
private function printAvgBlock($block)
    {
        if (!array_key_exists($block, $this->avgs)) {
            throw new \Exception('Average block '.$block.' not defined');
        }
        $this->finishAvgBlock($block);

        $output = "";
        $output .= "    $block";
        $output .= " [".$this->avgs[$block]['count']."]";
        $output .= " (".$this->avgs[$block]['start-line']."-".$this->avgs[$block]['stop-line'].")";
        $output .= ": ";
        $output .= round($this->avgs[$block]['avg'], 4);
        $output .= ' seconds';
        $output .= PHP_EOL;

        // Output max and min time
        $output .= "        max time: ".round($this->avgs[$block]['max-time'], 4).PHP_EOL;
        $output .= "        min time: ".round($this->avgs[$block]['min-time'], 4).PHP_EOL;

        return $output;
    }
1009849
protected function block(OutputBlock $block)
    {
        if (empty($block->lines) && empty($block->children)) {
            return;
        }

        $this->currentBlock = $block;

        $pre = $this->indentStr();

        if (! empty($block->selectors)) {
            $this->blockSelectors($block);

            $this->indentLevel++;
        }

        if (! empty($block->lines)) {
            $this->blockLines($block);
        }

        if (! empty($block->children)) {
            $this->blockChildren($block);
        }

        if (! empty($block->selectors)) {
            $this->indentLevel--;

            if (empty($block->children)) {
                $this->write($this->break);
            }

            $this->write($pre . $this->close . $this->break);
        }
    }
938603
public function release()
    {
        if (!$this->locked) {
            throw new \RuntimeException('release a non lock');
        }

        $unlock = flock($this->fp, LOCK_UN);
        fclose($this->fp);
        if ($unlock !== true) {
            return false;
        }
        $this->locked = false;

        return true;
    }
1078073
public function setBlock($block)
    {
        if (!empty($block) && $block <= 4 && $block !== 0 && !($block < 0)) {
            $this->block = $block;
        }
    }