Checks, whether the page is locked by current user or not,
will throw an exception if no, and update lock modified time if yes.

@param bool $createOnMiss
@throws ObjectLockedException if page is locked by another user
@return void
348206
protected function checkLock($createOnMiss = true)
	{
		$this->isPostRequest();

		$user = $this->getCurrentUser();

		if (! $user) {
			return;
		}

		$localization = $this->getPageLocalization();

		if ($localization->isLocked()) {

			$lock = $localization->getLock();
			
			if ($lock->getUserName() !== $user->getUsername()) {
				throw new ObjectLockedException('page.error.page_locked', 'Page is locked by another user');
			} else {

				$entityManager = $this->getEntityManager();

//				if ( ! $this->lockTransactionOpened) {
//					$entityManager->beginTransaction();
//
//					$this->lockTransactionOpened = true;
//				}
				
//				$entityManager->lock($lock, \Doctrine\DBAL\LockMode::PESSIMISTIC_READ);

				$lock->setModificationTime(new \DateTime('now'));
				
				$entityManager->flush($lock);
			}
		} elseif ($createOnMiss) {

//			$entityManager = $this->getEntityManager();

//			if ( ! $this->lockTransactionOpened) {
//				$entityManager->beginTransaction();
//				$this->lockTransactionOpened = true;
//			}

			// Creates lock if doesn't exist
//			$entityManager->lock(
//					$this->createLock(),
//					\Doctrine\DBAL\LockMode::PESSIMISTIC_READ
//			);
		}
	}
257675
public function getLocksForLockableEntity(LockableEntity $entity, $threshold, User $userToExclude = null)
    {
        $qb = $this->createQueryBuilder('evl')
            ->select('evl')
            ->join('evl.lockableEntity', 'le')
            ->where('le.id = :e')
            ->andWhere('evl.createdAt > :date')
            ->setParameter('e', $entity->getId())
            ->setParameter('date', new \DateTime(sprintf('-%s seconds', $threshold)))
        ;

        if (!is_null($userToExclude)) {
            $qb->andWhere('evl.owner <> :owner')
                ->setParameter('owner', $userToExclude->getUsername())
            ;
        }

        return $qb->getQuery()->getResult();
    }
841308
public function allowUser($permissions, $object, $user = null)
	{
		// if no user is specified, default to the Auth one
		if(is_null($user))
		{
			$user = app('auth')->user();
		}
		
		if(!is_array($permissions))
		{
			$permissions = array($permissions);
		}
		
		foreach($permissions as $permission)
		{
			$this->allowSinglePermission($permission, $object, $user);
		}
	}
427628
public function lock(Request $request)
    {
        $cacheKey = $this->getCacheKey($request);

        if (isset($this->locks[$cacheKey])) {
            return false;
        }

        $this->locks[$cacheKey] = $this->lockFactory
            ->createLock($cacheKey);

        return $this->locks[$cacheKey]->acquire();
    }
519832
function lock()
    {
        if ( $fp = $this->fp() )
        {
            if ( flock( $fp, LOCK_EX ) )
            {
                $this->clearMeta();
                $this->setMeta( 'timestamp', time() );
                return true;
            }
        }
        return false;
    }
980427
public function lock()
    {
        //Prevent lock take over, only an saved and unlocked row and be locked
        if(!$this->isNew() && !$this->isLocked())
        {
            $this->locked_by = (int) $this->getObject('user')->getId();
            $this->locked_on = gmdate('Y-m-d H:i:s');
            $this->save();
        }

        return true;
    }
1128193
private static void lockBoth(Lock sourceWriteLock, Lock destWriteLock) {
    while (true) {
      sourceWriteLock.lock();
      if (destWriteLock.tryLock()) {
        return;
      } else {
        sourceWriteLock.unlock();
      }

      destWriteLock.lock();
      if (sourceWriteLock.tryLock()) {
        return;
      } else {
        destWriteLock.unlock();
      }
    }
  }
808890
public function lock($member = null) {
		if (!$member) {
			$member = Member::currentUser();
		}

		// set the updated lock expiry based on now + lock timeout
		$this->WikiLastEditor = $member->Email;
		$this->WikiLockExpiry = date('Y-m-d H:i:s', time() + $this->config()->get('lock_time'));

		// save it with us as the editor
		$this->write();
	}
637708
public function lock_submission($userid) {
        global $USER, $DB;
        // Need grade permission.
        require_capability('mod/assign:grade', $this->context);

        // Give each submission plugin a chance to process the locking.
        $plugins = $this->get_submission_plugins();
        $submission = $this->get_user_submission($userid, false);

        $flags = $this->get_user_flags($userid, true);
        $flags->locked = 1;
        $this->update_user_flags($flags);

        foreach ($plugins as $plugin) {
            if ($plugin->is_enabled() && $plugin->is_visible()) {
                $plugin->lock($submission, $flags);
            }
        }

        $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
        \mod_assign\event\submission_locked::create_from_user($this, $user)->trigger();
        return true;
    }
105422
public function lock($seconds = null, $throw = true)
    {
        $this->ch = fopen($this->file, 'r');

        if ($seconds = abs((int) $seconds)) {
            $time = time();
            while ((time() - $time) < $seconds) {
                if (flock($this->ch, LOCK_EX | LOCK_NB)) {
                    return true;
                }

                sleep(1);
            }

            if ($throw) {
                throw new Exception("Can't lock critical section on file '".$this->file."'");
            }

            return false;
        }

        flock($this->ch, LOCK_EX);

        return $this;
    }
251364
protected function getLockableEntity(LockableEntityInterface $entity, $create = true)
    {
        /** @var LockableEntity $lockable */
        $lockable = $this->objectManager->getRepository('KunstmaanAdminListBundle:LockableEntity')->getOrCreate($entity->getId(), get_class($entity));

        if ($create === true && $lockable->getId() === null) {
            $this->objectManager->persist($lockable);
            $this->objectManager->flush();
        }

        return $lockable;
    }
533147
final public function lock($namespace)
    {
        //locks a namespace in this session to prevent editing
        if (empty($namespace)) {
            //@TODO throw an exception,
            //we don't know what namespace this is
            return false;
        }
        $session = $this;
        //unlocks a namespace
        if (isset($session->registry[$namespace]) && !$session->isLocked($namespace)) {
            $session->registry[$namespace]->lock();
            return true;
        }

        return false;
    }
434091
public function block($seconds, $callback = null)
    {
        $starting = $this->currentTime();

        while (! $this->acquire()) {
            usleep(250 * 1000);

            if ($this->currentTime() - $seconds >= $starting) {
                throw new LockTimeoutException;
            }
        }

        if (is_callable($callback)) {
            try {
                return $callback();
            } finally {
                $this->release();
            }
        }

        return true;
    }
640477
public function locked()
    {
        $this->defaultData();
	    $resource = $this->getResource($this->getRequestParameter('id'));
	    $lockData = LockManager::getImplementation()->getLockData($resource);

	    $this->setData('topclass-label',
	        $this->hasRequestParameter('topclass-label') ? $this->getRequestParameter('topclass-label') : __('Resource')
        );

	    if (AclProxy::hasAccess($this->getSession()->getUser(), __CLASS__, 'forceRelease', array('uri' => $resource->getUri()))) {
	        $this->setData('id', $resource->getUri());
            $this->setData('forceRelease', true);
	    }

	    $this->setData('lockDate', $lockData->getCreationTime());
	    $this->setData('ownerHtml', UserHelper::renderHtmlUser($lockData->getOwnerId()));

	    if ($this->hasRequestParameter('view') && $this->hasRequestParameter('ext')) {
	        $this->setView($this->getRequestParameter('view'), $this->getRequestParameter('ext'));
	    } else {
	        $this->setView('Lock/locked.tpl', 'tao');
	    }
	}
194915
private function init($maxLocks, $permissions)
    {
        $maxLocks = (int) $maxLocks;
        if ($maxLocks < 1) {
            $maxLocks = 1;
        }

        $this->key = abs(crc32(spl_object_hash($this)));
        $this->maxLocks = $maxLocks;

        $this->queue = msg_get_queue($this->key, $permissions);
        if (!$this->queue) {
            throw new SemaphoreException('Failed to create the semaphore.');
        }

        // Fill the semaphore with locks.
        while (--$maxLocks >= 0) {
            $this->release();
        }
    }
996311
protected function process_lock_submission($userid = 0) {

        require_sesskey();

        if (!$userid) {
            $userid = required_param('userid', PARAM_INT);
        }

        return $this->lock_submission($userid);
    }
477386
private function init(int $maxLocks, int $permissions) {
        if (\msg_queue_exists($this->key)) {
            throw new SyncException('A semaphore with that ID already exists');
        }

        $this->queue = \msg_get_queue($this->key, $permissions);
        if (!$this->queue) {
            throw new SyncException('Failed to create the semaphore.');
        }

        $this->initializer = \getmypid();

        // Fill the semaphore with locks.
        while (--$maxLocks >= 0) {
            $this->release($maxLocks);
        }
    }