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

Breadcrumb

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

function Lock::acquireRead

Overrides SharedLockInterface::acquireRead

File

vendor/symfony/lock/Lock.php, line 120

Class

Lock
Lock is the default implementation of the LockInterface.

Namespace

Symfony\Component\Lock

Code

public function acquireRead(bool $blocking = false) : bool {
    $this->key
        ->resetLifetime();
    try {
        if (!$this->store instanceof SharedLockStoreInterface) {
            $this->logger?->debug('Store does not support ReadLocks, fallback to WriteLock.', [
                'resource' => $this->key,
            ]);
            return $this->acquire($blocking);
        }
        if ($blocking) {
            if (!$this->store instanceof BlockingSharedLockStoreInterface) {
                while (true) {
                    try {
                        $this->store
                            ->saveRead($this->key);
                        break;
                    } catch (LockConflictedException) {
                        usleep((100 + random_int(-10, 10)) * 1000);
                    }
                }
            }
            else {
                $this->store
                    ->waitAndSaveRead($this->key);
            }
        }
        else {
            $this->store
                ->saveRead($this->key);
        }
        $this->dirty = true;
        $this->logger?->debug('Successfully acquired the "{resource}" lock for reading.', [
            'resource' => $this->key,
        ]);
        if ($this->ttl) {
            $this->refresh();
        }
        if ($this->key
            ->isExpired()) {
            try {
                $this->release();
            } catch (\Exception) {
                // swallow exception to not hide the original issue
            }
            throw new LockExpiredException(\sprintf('Failed to store the "%s" lock.', $this->key));
        }
        return true;
    } catch (LockConflictedException $e) {
        $this->dirty = false;
        $this->logger?->info('Failed to acquire the "{resource}" lock. Someone else already acquired the lock.', [
            'resource' => $this->key,
        ]);
        if ($blocking) {
            throw $e;
        }
        return false;
    } catch (\Exception $e) {
        $this->logger?->notice('Failed to acquire the "{resource}" lock.', [
            'resource' => $this->key,
            'exception' => $e,
        ]);
        throw new LockAcquiringException(\sprintf('Failed to acquire the "%s" lock.', $this->key), 0, $e);
    }
}

API Navigation

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