// This assumes that lb.lock is already held.
487626
func (lb *LoadBalancerRR) newServiceInternal(svcPort proxy.ServicePortName, affinityType v1.ServiceAffinity, ttlSeconds int) *balancerState {
	if ttlSeconds == 0 {
		ttlSeconds = int(v1.DefaultClientIPServiceAffinitySeconds) //default to 3 hours if not specified.  Should 0 be unlimited instead????
	}

	if _, exists := lb.services[svcPort]; !exists {
		lb.services[svcPort] = &balancerState{affinity: *newAffinityPolicy(affinityType, ttlSeconds)}
		klog.V(4).Infof("LoadBalancerRR service %q did not exist, created", svcPort)
	} else if affinityType != "" {
		lb.services[svcPort].affinity.affinityType = affinityType
	}
	return lb.services[svcPort]
}
419640
func (m *TryMutex) Lock() {
	//log.Debug("TryMutex: lock")
	ctx := m.LockCtx
	if ctx == nil {
		ctx = m.ctx
	}
	if err := m.mtx.Lock(ctx); err != nil {
		log.Debugf("TryMutex: lock err: %v", err)
		if err != context.Canceled && err != context.DeadlineExceeded {
			log.Panicf("TryMutex: lock panic: %v", err)
		}
	}
}
451798
func (l *lockfile) lock(l_type int16) {
	lk := unix.Flock_t{
		Type:   l_type,
		Whence: int16(os.SEEK_SET),
		Start:  0,
		Len:    0,
	}
	switch l_type {
	case unix.F_RDLCK:
		l.rwMutex.RLock()
	case unix.F_WRLCK:
		l.rwMutex.Lock()
	default:
		panic(fmt.Sprintf("attempted to acquire a file lock of unrecognized type %d", l_type))
	}
	l.stateMutex.Lock()
	defer l.stateMutex.Unlock()
	if l.counter == 0 {
		// If we're the first reference on the lock, we need to open the file again.
		fd, err := openLock(l.file, l.ro)
		if err != nil {
			panic(fmt.Sprintf("error opening %q", l.file))
		}
		unix.CloseOnExec(fd)
		l.fd = uintptr(fd)

		// Optimization: only use the (expensive) fcntl syscall when
		// the counter is 0.  In this case, we're either the first
		// reader lock or a writer lock.
		for unix.FcntlFlock(l.fd, unix.F_SETLKW, &lk) != nil {
			time.Sleep(10 * time.Millisecond)
		}
	}
	l.locktype = l_type
	l.locked = true
	l.counter++
}
1302765
func (l *LockFile) Lock(timeout time.Duration) error {
	end := time.Now().Add(timeout)
	for {
		if l.TryLock() {
			return nil
		}
		if time.Now().Before(end) {
			time.Sleep(l.resolution)
			continue
		}
		return ErrTimeout
	}

	return fmt.Errorf("not reached")
}
1082258
func (l *lockfile) Lock() {
	if l.ro {
		l.RLock()
	} else {
		l.lock(unix.F_WRLCK)
	}
}
608302
func (surface *Surface) Lock() error {
	if C.SDL_LockSurface(surface.cptr()) != 0 {
		return GetError()
	}
	return nil
}
1315488
func (n *nsLockMap) Lock(volume, path, opsID string, timeout time.Duration) (locked bool) {
	readLock := false // This is a write lock.

	lockSource := getSource() // Useful for debugging
	return n.lock(volume, path, lockSource, opsID, readLock, timeout)
}
736698
func (l *AddrLocker) lock(address common.Address) *sync.Mutex {
	l.mu.Lock()
	defer l.mu.Unlock()
	if l.locks == nil {
		l.locks = make(map[common.Address]*sync.Mutex)
	}
	if _, ok := l.locks[address]; !ok {
		l.locks[address] = new(sync.Mutex)
	}
	return l.locks[address]
}
437408
func (mutex *Mutex) Lock() error {
	ret := int(C.SDL_LockMutex(mutex.cptr()))
	if ret != 0 {
		return GetError()
	}
	return nil
}
579033
func (bc *BlockChanges) AddRefBlock(ptr data.BlockPointer) {
	if bc.sizeEstimate != 0 {
		panic("Can't alter block changes after the size is estimated")
	}
	bc.Ops[len(bc.Ops)-1].AddRefBlock(ptr)
}
176632
func (li *localLockInstance) RUnlock() {
	readLock := true
	li.ns.unlock(li.volume, li.path, li.opsID, readLock)
}
45655
func (s *Filesystem) Lock(info *LockInfo) (string, error) {
	defer s.mutex()()

	if s.stateFileOut == nil {
		if err := s.createStateFiles(); err != nil {
			return "", err
		}
	}

	if s.lockID != "" {
		return "", fmt.Errorf("state %q already locked", s.stateFileOut.Name())
	}

	if err := s.lock(); err != nil {
		info, infoErr := s.lockInfo()
		if infoErr != nil {
			err = multierror.Append(err, infoErr)
		}

		lockErr := &LockError{
			Info: info,
			Err:  err,
		}

		return "", lockErr
	}

	s.lockID = info.ID
	return s.lockID, s.writeLockInfo(info)
}
1323978
func (nom *NamedOnceMutex) Lock(useMutexKey interface{}) bool {
	nom.mutex.Lock()
	m, ok := nom.lockMap[useMutexKey]
	if ok {
		nom.mutex.Unlock()
		return m.Lock()
	}

	m = &OnceMutex{}
	m.Lock()
	nom.lockMap[useMutexKey] = m
	nom.mutex.Unlock()
	return true
}
479019
func (lk *etcdLock) acquireLock() {
	keyName := "/contiv.io/lock/" + lk.name

	// Start a watch on the lock first so that we dont loose any notifications
	go lk.watchLock()

	// Wait in this loop forever till lock times out or released
	for {
		log.Infof("Getting the lock %s to see if its acquired", keyName)
		// Get the key and see if we or someone else has already acquired the lock
		resp, err := lk.kapi.Get(context.Background(), keyName, &client.GetOptions{Quorum: true})
		if err != nil {
			if !client.IsKeyNotFound(err) {
				log.Errorf("Error getting the key %s. Err: %v", keyName, err)
				// Retry after a second in case of error
				time.Sleep(time.Second)
				continue
			} else {
				log.Infof("Lock %s does not exist. trying to acquire it", keyName)
			}

			// Try to acquire the lock
			resp, err := lk.kapi.Set(context.Background(), keyName, lk.myID, &client.SetOptions{PrevExist: client.PrevNoExist, TTL: lk.ttl})
			if err != nil {
				if _, ok := err.(client.Error); ok && err.(client.Error).Code != client.ErrorCodeNodeExist {
					log.Errorf("Error creating key %s. Err: %v", keyName, err)
				} else {
					log.Infof("Lock %s acquired by someone else", keyName)
				}
			} else {
				log.Debugf("Acquired lock %s. Resp: %#v, Node: %+v", keyName, resp, resp.Node)

				lk.mutex.Lock()
				// Successfully acquired the lock
				lk.isAcquired = true
				lk.holderID = lk.myID
				lk.mutex.Unlock()

				// Send acquired message to event channel
				lk.eventChan <- LockEvent{EventType: LockAcquired}

				// refresh it
				lk.refreshLock()

				lk.mutex.Lock()
				// If lock is released, we are done, else go back and try to acquire it
				if lk.isReleased {
					lk.mutex.Unlock()
					return
				}
				lk.mutex.Unlock()
			}
		} else if resp.Node.Value == lk.myID {
			log.Debugf("Already Acquired key %s. Resp: %#v, Node: %+v", keyName, resp, resp.Node)

			lk.mutex.Lock()
			// We have already acquired the lock. just keep refreshing it
			lk.isAcquired = true
			lk.holderID = lk.myID
			lk.mutex.Unlock()

			// Send acquired message to event channel
			lk.eventChan <- LockEvent{EventType: LockAcquired}

			// Refresh lock
			lk.refreshLock()

			lk.mutex.Lock()
			// If lock is released, we are done, else go back and try to acquire it
			if lk.isReleased {
				lk.mutex.Unlock()
				return
			}
			lk.mutex.Unlock()
		} else if resp.Node.Value != lk.myID {
			log.Debugf("Lock already acquired by someone else. Resp: %+v, Node: %+v", resp, resp.Node)

			lk.mutex.Lock()
			// Set the current holder's ID
			lk.holderID = resp.Node.Value
			lk.mutex.Unlock()

			// Wait for changes on the lock
			lk.waitForLock()

			lk.mutex.Lock()
			if lk.isReleased {
				lk.mutex.Unlock()
				return
			}
			lk.mutex.Unlock()
		}
	}
}
1352762
func (m LeveledMutex) Lock(lockState *LockState) {
	err := lockState.doLock(m.level, writeExclusion, m.locker)
	if err != nil {
		panic(err)
	}
}
689650
func (l *Lock) Lock(stopCh <-chan struct{}) (<-chan struct{}, error) {
	defer metrics.MeasureSince(metricLockLock, time.Now())

	l.lock.Lock()
	defer l.lock.Unlock()
	if l.held {
		return nil, errors.New("lock already held")
	}

	// Attempt to lock - this function blocks until a lock is acquired or an error
	// occurs.
	acquired, err := l.attemptLock(stopCh)
	if err != nil {
		return nil, errwrap.Wrapf("lock: {{err}}", err)
	}
	if !acquired {
		return nil, nil
	}

	// We have the lock now
	l.held = true

	// Build the locks
	l.stopLock.Lock()
	l.stopCh = make(chan struct{})
	l.stopped = false
	l.stopLock.Unlock()

	// Periodically renew and watch the lock
	go l.renewLock()
	go l.watchLock()

	return l.stopCh, nil
}
339152
func (b *BlockGen) PrevBlock(index int) *types.Block {
	if index >= b.i {
		panic(fmt.Errorf("block index %d out of range (%d,%d)", index, -1, b.i))
	}
	if index == -1 {
		return b.parent
	}
	return b.chain[index]
}