Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FlockStore.php

function FlockStore::lock

4 calls to FlockStore::lock()
FlockStore::save in vendor/symfony/lock/Store/FlockStore.php
Stores the resource if it's not locked by someone else.
FlockStore::saveRead in vendor/symfony/lock/Store/FlockStore.php
Stores the resource if it's not locked for reading by someone else.
FlockStore::waitAndSave in vendor/symfony/lock/Store/FlockStore.php
Waits until a key becomes free, then stores the resource.
FlockStore::waitAndSaveRead in vendor/symfony/lock/Store/FlockStore.php

File

vendor/symfony/lock/Store/FlockStore.php, line 73

Class

FlockStore
FlockStore is a PersistingStoreInterface implementation using the FileSystem flock.

Namespace

Symfony\Component\Lock\Store

Code

private function lock(Key $key, bool $read, bool $blocking) : void {
    $handle = null;
    // The lock is maybe already acquired.
    if ($key->hasState(__CLASS__)) {
        [
            $stateRead,
            $handle,
        ] = $key->getState(__CLASS__);
        // Check for promotion or demotion
        if ($stateRead === $read) {
            return;
        }
    }
    if (!$handle) {
        $fileName = \sprintf('%s/sf.%s.%s.lock', $this->lockPath, substr(preg_replace('/[^a-z0-9\\._-]+/i', '-', $key), 0, 50), strtr(substr(base64_encode(hash('sha256', $key, true)), 0, 7), '/', '_'));
        // Silence error reporting
        set_error_handler(function ($type, $msg) use (&$error) {
            $error = $msg;
        });
        try {
            if (!($handle = fopen($fileName, 'r+') ?: fopen($fileName, 'r'))) {
                if ($handle = fopen($fileName, 'x')) {
                    chmod($fileName, 0666);
                }
                elseif (!($handle = fopen($fileName, 'r+') ?: fopen($fileName, 'r'))) {
                    usleep(100);
                    // Give some time for chmod() to complete
                    $handle = fopen($fileName, 'r+') ?: fopen($fileName, 'r');
                }
            }
        } finally {
            restore_error_handler();
        }
    }
    if (!$handle) {
        throw new LockStorageException($error, 0, null);
    }
    // On Windows, even if PHP doc says the contrary, LOCK_NB works, see
    // https://bugs.php.net/54129
    if (!flock($handle, ($read ? \LOCK_SH : \LOCK_EX) | ($blocking ? 0 : \LOCK_NB))) {
        fclose($handle);
        throw new LockConflictedException();
    }
    $key->setState(__CLASS__, [
        $read,
        $handle,
    ]);
    $key->markUnserializable();
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal