// Assumes that lock is already acquired.
979114
func (cache *schedulerCache) addPod(pod *v1.Pod) {
	n, ok := cache.nodes[pod.Spec.NodeName]
	if !ok {
		n = newNodeInfoListItem(schedulernodeinfo.NewNodeInfo())
		cache.nodes[pod.Spec.NodeName] = n
	}
	n.info.AddPod(pod)
	cache.moveNodeInfoToHead(pod.Spec.NodeName)
}
38177
func (b *Backend) LockWith(key, value string) (physical.Lock, error) {
	identity, err := uuid.GenerateUUID()
	if err != nil {
		return nil, errwrap.Wrapf("lock with: {{err}}", err)
	}
	return &Lock{
		backend:  b,
		key:      key,
		value:    value,
		identity: identity,
		stopped:  true,

		renewInterval:      LockRenewInterval,
		retryInterval:      LockRetryInterval,
		ttl:                LockTTL,
		watchRetryInterval: LockWatchRetryInterval,
		watchRetryMax:      LockWatchRetryMax,
	}, nil
}
893884
func (s *Store) Lock() (err error) {
	s.mtx.Lock()
	defer s.mtx.Unlock()

	if s.flags.watchingOnly {
		return ErrWatchingOnly
	}

	// Remove clear text passphrase from key store.
	if s.isLocked() {
		err = ErrLocked
	} else {
		zero(s.passphrase)
		s.passphrase = nil
		zero(s.secret)
		s.secret = nil
	}

	// Remove clear text private keys from all address entries.
	for _, addr := range s.addrMap {
		if baddr, ok := addr.(*btcAddress); ok {
			_ = baddr.lock()
		}
	}

	return err
}
294387
func (lock *etcd3Lock) Acquire() error {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	// this is a blocking call; if we receive DeadlineExceeded
	// the lock is most likely already taken
	if err := lock.Mutex.Lock(ctx); err != nil {
		if err == context.DeadlineExceeded {
			return tusd.ErrFileLocked
		} else {
			return err
		}
	}
	return nil
}
1336811
func (c *controllerImpl) addExpectationLocked(
	o MockObject,
	methodName string,
	exp *InternalExpectation) {
	// Get the existing list.
	existing := c.getExpectationsLocked(o, methodName)

	// Store a modified list.
	id := o.Oglemock_Id()
	c.expectationsByObject[id][methodName] = append(existing, exp)
}
457871
func (ks *KeyStore) Lock(addr common.Address) error {
	ks.mu.Lock()
	if unl, found := ks.unlocked[addr]; found {
		ks.mu.Unlock()
		ks.expire(addr, unl, time.Duration(0)*time.Nanosecond)
	} else {
		ks.mu.Unlock()
	}
	return nil
}
282100
func acquireLockForSocket(addr *net.UnixAddr) *flock.Flock {
	lockname := fmt.Sprintf("%s.lock", addr.String())
	lock := flock.NewFlock(lockname)
	locked, err := lock.TryLock()
	if err != nil {
		panic(fmt.Sprintf("Could not acquire the lock %q to listen on %v: %v", lockname, addr, err))
	}
	if !locked {
		panic(fmt.Sprintf("Lock file %q for %v is in use by another process already", lockname, addr))
	}
	// We have the exclusive use of the socket, clear away any old sockets and listen:
	_ = os.Remove(addr.String())
	return lock
}
940196
func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime {
	seconds, hasExpiration, found := db.redis.TTL(sid)
	if !found {
		// not found, create an entry with ttl and return an empty lifetime, session manager will do its job.
		if err := db.redis.Set(sid, sid, int64(expires.Seconds())); err != nil {
			golog.Debug(err)
		}

		return sessions.LifeTime{} // session manager will handle the rest.
	}

	if !hasExpiration {
		return sessions.LifeTime{}

	}

	return sessions.LifeTime{Time: time.Now().Add(time.Duration(seconds) * time.Second)}
}
600123
func (s *GlobalStore) lock(addr common.Address, key []byte) (unlock func(), err error) {
	start := time.Now()
	nodeLockKey := addr.Hex()
	for {
		_, loaded := s.nodesLocks.LoadOrStore(nodeLockKey, struct{}{})
		if !loaded {
			break
		}
		time.Sleep(lockCheckDelay)
		if time.Since(start) > lockTimeout {
			return nil, errLockTimeout
		}
	}
	start = time.Now()
	keyLockKey := common.Bytes2Hex(key)
	for {
		_, loaded := s.keysLocks.LoadOrStore(keyLockKey, struct{}{})
		if !loaded {
			break
		}
		time.Sleep(lockCheckDelay)
		if time.Since(start) > lockTimeout {
			return nil, errLockTimeout
		}
	}
	return func() {
		s.nodesLocks.Delete(nodeLockKey)
		s.keysLocks.Delete(keyLockKey)
	}, nil
}
1147212
func (m *MockLock) Acquire(arg0 machinelock.Spec) (func(), error) {
	ret := m.ctrl.Call(m, "Acquire", arg0)
	ret0, _ := ret[0].(func())
	ret1, _ := ret[1].(error)
	return ret0, ret1
}
657168
func (m *MockLocker) Acquire(arg0 string, arg1 int) (lock.Lock, error) {
	ret := m.ctrl.Call(m, "Acquire", arg0, arg1)
	ret0, _ := ret[0].(lock.Lock)
	ret1, _ := ret[1].(error)
	return ret0, ret1
}
1059096
func (r *lockRepositoryImpl) AcquireLock(ctx context.Context, owner, name string, opts ...pglock.ClientOption) (*pglock.Lock, error) {
	log.Info(ctx, map[string]interface{}{
		"lock":  name,
		"owner": owner,
	}, "acquiring lock...")
	// obtain a lock to prevent other pods to perform this task
	var clnOpts []pglock.ClientOption
	if opts != nil {
		// Useful for testing
		clnOpts = opts
	} else {
		// Use default
		clnOpts = []pglock.ClientOption{
			pglock.WithCustomTable("worker_lock"),
			pglock.WithLeaseDuration(30 * time.Second),
			pglock.WithHeartbeatFrequency(10 * time.Second),
			pglock.WithLogger(log.Logger()),
		}
	}
	if owner != "" {
		// use a specific owner name, otherwise it will be a random value (default behaviour)
		clnOpts = append(clnOpts, pglock.WithOwner(owner))
	}
	c, err := pglock.New(r.db, clnOpts...)
	if err != nil {
		return nil, errs.Wrap(err, "cannot create worker lock client")
	}
	l, err := c.Acquire(name) // will wait until it succeeds
	if err != nil {
		// Try to get the name of the owner who holds the lock if any
		var holdByOwner string
		existingLock, nerr := c.Get(name)
		if nerr != nil {
			log.Error(ctx, map[string]interface{}{
				"err":   nerr,
				"lock":  name,
				"owner": owner,
			}, "cannot obtain the existing lock when trying to acquire a new one")
		} else {
			holdByOwner = existingLock.Owner()
		}
		return nil, errs.Wrapf(err, "cannot acquire worker lock '%s' which is currently hold by '%s'", name, holdByOwner)
	}
	log.Info(ctx, map[string]interface{}{
		"lock":  name,
		"owner": owner,
	}, "acquired lock")
	return l, nil
}
1030341
func (c *Pool) Acquire(w http.ResponseWriter, r *http.Request) Context {
	ctx := c.pool.Get().(Context)
	ctx.BeginRequest(w, r)
	return ctx
}
877451
func (c *lock) Acquire(spec Spec) (func(), error) {
	if err := spec.Validate(); err != nil {
		return nil, errors.Trace(err)
	}
	current := &info{
		worker:    spec.Worker,
		comment:   spec.Comment,
		stack:     string(debug.Stack()),
		requested: c.clock.Now(),
	}
	c.mu.Lock()

	id := c.next
	c.next++
	c.waiting[id] = current

	mSpec := c.spec
	mSpec.Cancel = spec.Cancel

	c.mu.Unlock()
	c.logger.Debugf("acquire machine lock for %s (%s)", spec.Worker, spec.Comment)
	releaser, err := c.acquire(mSpec)
	c.mu.Lock()
	defer c.mu.Unlock()
	// Remove from the waiting map.
	delete(c.waiting, id)

	if err != nil {
		return nil, errors.Trace(err)
	}
	c.logger.Debugf("machine lock acquired for %s (%s)", spec.Worker, spec.Comment)
	c.holder = current
	current.acquired = c.clock.Now()
	return func() {
		// We need to acquire the mutex before we call the releaser
		// to ensure that we move the current to the history before
		// another pending acquisition overwrites the current value.
		c.mu.Lock()
		defer c.mu.Unlock()
		// We write the log file entry before we release the execution
		// lock to ensure that no other agent is attempting to write to the
		// log file.
		current.released = c.clock.Now()
		c.writeLogEntry()
		c.logger.Debugf("machine lock released for %s (%s)", spec.Worker, spec.Comment)
		releaser.Release()
		c.history.PushFront(current)
		c.holder = nil
	}, nil
}
206111
func (c *ZooKeeperBackend) LockWith(key, value string) (physical.Lock, error) {
	l := &ZooKeeperHALock{
		in:     c,
		key:    key,
		value:  value,
		logger: c.logger,
	}
	return l, nil
}
271214
func (d *DynamoDBBackend) LockWith(key, value string) (physical.Lock, error) {
	identity, err := uuid.GenerateUUID()
	if err != nil {
		return nil, err
	}
	return &DynamoDBLock{
		backend:            d,
		key:                pkgPath.Join(pkgPath.Dir(key), DynamoDBLockPrefix+pkgPath.Base(key)),
		value:              value,
		identity:           identity,
		renewInterval:      DynamoDBLockRenewInterval,
		ttl:                DynamoDBLockTTL,
		watchRetryInterval: DynamoDBWatchRetryInterval,
	}, nil
}
970175
func AcquireWithContext(ctx context.Context, lock sync.Locker) (err error) {
	if err = ctx.Err(); err != nil {
		return err
	}
	acquiredCh := make(chan struct{})
	shouldReleaseCh := make(chan bool, 1)
	go func() {
		lock.Lock()
		close(acquiredCh)
		shouldRelease := <-shouldReleaseCh
		if shouldRelease {
			lock.Unlock()
		}
	}()
	select {
	case <-acquiredCh:
		err = nil
	case <-ctx.Done():
		err = ctx.Err()
	}
	shouldReleaseCh <- err != nil
	return err
}