@param object|ObjectID|string $id
@param callable $lockCallback
@param bool $blocking
@param int $limit

@return mixed
344254
public function lock($id, callable $lockCallback, bool $blocking = true, int $limit = 300)
    {
        if ($id instanceof ObjectID) {
            // do nothing
        } elseif (is_object($id)) {
            $id = get_object_id($id);
        } elseif (is_string($id)) {
            $id = new ObjectID((string) $id);
        } else {
            throw new \LogicException('The given id is not supported. Could be a model object, Mongo\'s ObjectID or a string model id.');
        }

        if (false == $this->pessimisticLock) {
            throw new \LogicException('Cannot lock. The PessimisticLock instance is not injected');
        }

        $this->pessimisticLock->lock($id, $blocking, $limit);
        $result = null;
        try {
            if ($model = $this->findOne(['_id' => $id])) {
                $result = call_user_func($lockCallback, $model, $this);
            }

            return $result;
        } finally {
            $this->pessimisticLock->unlock($id);
        }
    }
593556
private function executeInLock($lockName, callable $callback)
    {
        $this->res->getManagementLock()->lock($lockName);

        try {
            return call_user_func($callback);
        } finally {
            $this->res->getManagementLock()->unlock($lockName);
        }
    }
484322
public function extend($id, callable $callback)
    {
        if (is_callable($callback) || method_exists($callback, '__invoke')) {
            $this->callables[$id] = $callback;
        }
    }
1143265
public function find($id, $lockMode = null, $lockVersion = null)
    {
        $qb = $this->createQueryBuilder('n');

        if (preg_match('/^\d+$/', $id)) {
            $qb->where('n.id = :id');
        } else {
            $qb->where('n.uuid = :id');
        }

        return $qb
            ->getQuery()
            ->setParameter('id', $id)
            ->getOneOrNullResult();
    }
953198
public function block($id)
    {
        $user = AppUser::find($id);

        if (!$user) {
            return response()->json(null, 404);
        }

        if ($user->isBlock()) {
            return response()->json(null, 204);
        }

        if (!$user->block()) {
            return response()->json(null, 500); // @codeCoverageIgnore
        }

        return response()->json(null, 204);
    }
1198387
public void lockContainerpage(final I_CmsSimpleCallback<Boolean> callback) {

        if (m_lockStatus == LockStatus.locked) {
            callback.execute(Boolean.TRUE);
        } else if (m_lockStatus == LockStatus.failed) {
            callback.execute(Boolean.FALSE);
        } else {
            I_CmsSimpleCallback<String> call = new I_CmsSimpleCallback<String>() {

                public void execute(String lockError) {

                    if (lockError == null) {
                        onLockSuccess();
                        callback.execute(Boolean.TRUE);
                    } else {
                        onLockFail(lockError);
                        callback.execute(Boolean.FALSE);
                    }
                }
            };

            if (getData().getDetailContainerPage() != null) {
                CmsCoreProvider.get().lockOrReturnError(getData().getDetailContainerPage(), getLoadTime(), call);
            } else {
                CmsCoreProvider.get().lockOrReturnError(CmsCoreProvider.get().getStructureId(), getLoadTime(), call);
            }
        }
    }
97097
public function checkNodeVersionLockAction(Request $request, $id, $public)
    {
        $nodeVersionIsLocked = false;
        $message = '';
        $this->init($request);

        /* @var Node $node */
        $node = $this->em->getRepository('KunstmaanNodeBundle:Node')->find($id);

        try {
            $this->checkPermission($node, PermissionMap::PERMISSION_EDIT);

            /** @var NodeVersionLockHelper $nodeVersionLockHelper */
            $nodeVersionLockHelper = $this->get('kunstmaan_node.admin_node.node_version_lock_helper');
            $nodeTranslation = $node->getNodeTranslation($this->locale, true);

            if ($nodeTranslation) {
                $nodeVersionIsLocked = $nodeVersionLockHelper->isNodeVersionLocked($this->getUser(), $nodeTranslation, $public);

                if ($nodeVersionIsLocked) {
                    $users = $nodeVersionLockHelper->getUsersWithNodeVersionLock($nodeTranslation, $public, $this->getUser());
                    $message = $this->get('translator')->trans('kuma_node.admin.edit.flash.locked', array('%users%' => implode(', ', $users)));
                }
            }
        } catch (AccessDeniedException $ade) {
        }

        return new JsonResponse(['lock' => $nodeVersionIsLocked, 'message' => $message]);
    }
676289
public function then(callable $callback, callable $failure = null)
    {
        try {
            return (new ConcurrencyLimiter(
                $this->connection, $this->name, $this->maxLocks, $this->releaseAfter
            ))->block($this->timeout, $callback);
        } catch (LimiterTimeoutException $e) {
            if ($failure) {
                return $failure($e);
            }

            throw $e;
        }
    }
909436
public function extend($id, $callable)
    {
        try {
            return parent::extend($id, $callable);
        } catch (UnknownIdentifierException $e) {
            throw new ContainerNotFoundException(
                $e->getMessage(),
                $e->getCode(),
                $e
            );
        } catch (PimpleFrozenServiceException $e) {
            throw new FrozenServiceException($id);
        } catch (PimpleInvalidServiceIdentifierException $e) {
            throw new InvalidServiceIdentifierException($id);
        } catch (PimpleExpectedInvokableException $e) {
            throw new ExpectedInvokableException(
                $e->getMessage(),
                $e->getCode(),
                $e
            );
        } catch (\Throwable $e) {
            throw new ContainerException(
                $e->getMessage(),
                $e->getCode(),
                $e
            );
        }
    }
787841
public void queryLock (NodeObject.Lock lock, ResultListener<String> listener)
    {
        // if it's being resolved, add the listener to the list
        LockHandler handler = _locks.get(lock);
        if (handler != null) {
            handler.listeners.add(listener);
            return;
        }

        // otherwise, return its present value
        listener.requestCompleted(queryLock(lock));
    }
211071
private static function createOnBlockHandler(LoggerInterface $logger): callable
    {
        return static function(int $blockInterval) use ($logger): void
        {
            $trace     = \debug_backtrace();
            $traceData = ['info' => []];

            /** skip first since it's always the current method */
            \array_shift($trace);
            /** the call_user_func call is also skipped */
            \array_shift($trace);

            $i = 0;

            while (isset($trace[$i]['class']))
            {
                /** @noinspection SlowArrayOperationsInLoopInspection */
                $traceData['info'] = \array_merge(
                    $traceData['info'],
                    [
                        'file'     => $trace[$i - 1]['file'] ?? null,
                        'line'     => $trace[$i - 1]['line'] ?? null,
                        'class'    => $trace[$i]['class'] ?? null,
                        'function' => $trace[$i]['function'] ?? null,
                    ]
                );

                $i++;
            }

            $traceData['lockTime'] = 0 !== $blockInterval ? $blockInterval / 1000 : 0;

            $logger->error('A lock event loop has been detected. Blocking time: {lockTime} seconds', $traceData);

            unset($traceData, $trace, $i);
        };
    }
1374582
public function byId($callerClass, $id, $cache = true, $requiredPerm = 'View')
    {
        $id = (int) $id;
        $item = DataObject::get_by_id($callerClass, $id, $cache);
        if ($item && $item->hasExtension('Restrictable') && $item->checkPerm($requiredPerm)) {
            return $item;
        }
        
        if ($item && !$item->hasExtension('Restrictable') && $item->canView()) {
            return $item;
        }
    }
398570
public function doLock($blocking = false)
    {
        if ($this->lockAcquired) {
            return $this;
        }

        $this->mode   = $this->mode ?: (is_file($this->file) ? 'rb' : 'wb');
        $this->handle = fopen($this->file, $this->mode) ?: null;
        if (
            $this->lockType === self::LOCK_EXTERNAL &&
            $this->mode === 'wb' &&
            !$this->handle
        ) {
            // if another process won the race at creating lock file
            $this->mode   = 'rb';
            $this->handle = fopen($this->file, $this->mode) ?: null;
        }

        $this->lockAcquired = $this->handle ? flock($this->handle, $blocking ? LOCK_EX : LOCK_EX | LOCK_NB) : false;

        if (!$this->lockAcquired) {
            $this->unLock();
        }

        return $this;
    }
483719
private function isAllowed($blockId, ModelInterface $block)
    {
        $result = $this->getEventManager()->trigger(
            __FUNCTION__,
            $this,
            [
                'block_id' => $blockId,
                'block' => $block
            ]
        );
        if ($result->stopped()) {
            return $result->last();
        }
        return true;
    }
475671
public function update($id)
    {
        $block = $this->repository->getById($id);
        if (!empty($block)) {
            $this->authorize('update', $block);
            $input = $this->validator->validate('update');
            $block = $this->repository->update($block, $input, auth()->user());
            return $this->respondWithSuccess($block, new BlockTransformer);
        }
        return $this->respondNotFound();
    }
1308629
public function getBlock(?string $blockId = null): GetBlockResponse
    {
        $command = new GetBlockCommand($blockId);
        $response = $this->driver->executeCommand($command);

        return new GetBlockResponse($response->getRawData());
    }
646223
public function get($id, ?int $lockMode = null, ?int $lockVersion = null)
    {
        $entity = $this->find($id, $lockMode, $lockVersion);

        if (null === $entity) {
            if (!\is_array($id)) {
                $id = [$this->_class->identifier[0] => $id];
            }

            throw EntityNotFoundException::fromClassNameAndIdentifier(
                $this->getClassName(),
                $this->identifierFlattener->flattenIdentifier($this->_class, $id)
            );
        }

        return $entity;
    }