// wrapper around the map to get a ReadLock for concurrency. This could easily
// be done with Go 1.9 sync.Map, but we want to support versions below.
497577
func (c *Client) getCachedProjectID(label string) (*manifold.ID, bool) {
	c.RLock()
	defer c.RUnlock()

	v, ok := c.projectIDs[label]
	return v, ok
}
832249
func (m *lockingMap) Set(key string, val interface{}) {
	m.keyLocker.Lock(key)
	defer m.keyLocker.Unlock(key)

	m.lock.Lock()
	defer m.lock.Unlock()

	m.data[key] = val
}
761219
func ReadMapNoNest(rd io.Reader) (m Map, err error) {
	// Just in case of programming mistake. Not intentionally used.
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprint(r))
		}
	}()

	return decodeMap(rd, "", false)
}
755226
func (tp *DocumentsWriterPerThreadPool) lockAny() (res *ThreadState) {
	for res == nil {
		if res = tp.findNextAvailableThreadState(); res == nil {
			// ThreadState is already locked before return by this method:
			if res = tp.newThreadState(); res == nil {
				// Wait until a thread state freez up:
				func() {
					tp.hasMoreStates.L.Lock()
					defer tp.hasMoreStates.L.Unlock()

					tp.hasMoreStates.Wait()
				}()
			}
		}
	}
	return
}
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
}
922358
func (w *MapFieldWriter) Map() map[string]string {
	w.lock.Lock()
	defer w.lock.Unlock()
	if w.result == nil {
		w.result = make(map[string]string)
	}

	return w.result
}
230525
func (sm *SyncManager) limitMap(m map[chainhash.Hash]struct{}, limit int) {
	if len(m)+1 > limit {
		// Remove a random entry from the map.  For most compilers, Go's
		// range statement iterates starting at a random item although
		// that is not 100% guaranteed by the spec.  The iteration order
		// is not important here because an adversary would have to be
		// able to pull off preimage attacks on the hashing function in
		// order to target eviction of specific entries anyways.
		for txHash := range m {
			delete(m, txHash)
			return
		}
	}
}
425428
func (s *SharedStore) SharedKeysMap() map[string]Key {
	s.mutex.RLock()
	defer s.mutex.RUnlock()
	sharedKeysCopy := make(map[string]Key, len(s.sharedKeys))

	for k, v := range s.sharedKeys {
		sharedKeysCopy[k] = v
	}
	return sharedKeysCopy
}
532155
func (m *mmapAccessor) readAll(key []byte) ([]Value, error) {
	m.incAccess()

	blocks, err := m.index.ReadEntries(key, nil)
	if len(blocks) == 0 || err != nil {
		return nil, err
	}

	tombstones := m.index.TombstoneRange(key, nil)

	m.mu.RLock()
	defer m.mu.RUnlock()

	var temp []Value
	var values []Value
	for _, block := range blocks {
		var skip bool
		for _, t := range tombstones {
			// Should we skip this block because it contains points that have been deleted
			if t.Min <= block.MinTime && t.Max >= block.MaxTime {
				skip = true
				break
			}
		}

		if skip {
			continue
		}
		//TODO: Validate checksum
		temp = temp[:0]
		// The +4 is the 4 byte checksum length
		temp, err = DecodeBlock(m.b[block.Offset+4:block.Offset+int64(block.Size)], temp)
		if err != nil {
			return nil, err
		}

		// Filter out any values that were deleted
		for _, t := range tombstones {
			temp = Values(temp).Exclude(t.Min, t.Max)
		}

		values = append(values, temp...)
	}

	return values, nil
}
386243
func (m *RWMutex) RUnlock() {
	m.mutex.RUnlock()
	atomic.AddInt64(&m.readers, -1)
}
684311
func (m ConcurrentMap) MarshalJSON() ([]byte, error) {
	// Create a temporary map, which will hold all item spread across shards.
	tmp := make(map[string]interface{})

	// Insert items to temporary map.
	for item := range m.IterBuffered() {
		tmp[item.Key] = item.Val
	}
	return json.Marshal(tmp)
}
1097783
func (delimited *Delimited) ReadNSRecursive(key string) golhashmap.HashMap {
	var hmap golhashmap.HashMap
	hmap = make(golhashmap.HashMap)

	keyname := "key::" + key
	valname := "val::" + key
	keynameVal := delimited.db.GetVal(keyname)
	valnameVal := delimited.db.GetVal(valname)
	if valnameVal != "" {
		hmap[key] = valnameVal
	}
	if keynameVal == "" {
		return hmap
	}
	children := strings.Split(keynameVal, ",")

	for _, childValAsKey := range children {
		childKey := strings.Split(childValAsKey, "key::")[1]
		inhmap := delimited.ReadNSRecursive(childKey)
		for inHmapKey, inHmapVal := range inhmap {
			hmap[inHmapKey] = inHmapVal
		}
	}

	return hmap
}
997649
func (m *concurrentMap) Get(w string) (Word, bool) {
	m.RLock()
	result, ok := m.words[w]
	m.RUnlock()
	return result, ok
}
439208
func (i *InmemHABackend) LockWith(key, value string) (physical.Lock, error) {
	l := &InmemLock{
		in:    i,
		key:   key,
		value: value,
	}
	return l, nil
}
362562
func (nm *NamedMutex) TryLock(name string) bool {
	nm.localMutex.Lock()
	mc, ok := nm.mutexMap[name]
	if !ok {
		mc = make(chan struct{}, 1)
		nm.mutexMap[name] = mc
	}
	nm.localMutex.Unlock()
	select {
	case mc <- struct{}{}:
		return true
	default:
		return false
	}
}
158349
func (m *RWMutex) RLock() {
	atomic.AddInt64(&m.readers, 1)
	m.mutex.RLock()
}
1163985
func (e *Endpoint) DeleteMapsLocked() []error {
	var errors []error

	maps := map[string]string{
		"config": e.BPFConfigMapPath(),
		"policy": e.PolicyMapPathLocked(),
		"calls":  e.CallsMapPathLocked(),
		"egress": e.BPFIpvlanMapPath(),
	}
	for name, path := range maps {
		if err := os.RemoveAll(path); err != nil {
			errors = append(errors, fmt.Errorf("unable to remove %s map file %s: %s", name, path, err))
		}
	}

	if e.ConntrackLocalLocked() {
		// Remove local connection tracking maps
		for _, m := range ctmap.LocalMaps(e, option.Config.EnableIPv4, option.Config.EnableIPv6) {
			ctPath, err := m.Path()
			if err == nil {
				err = os.RemoveAll(ctPath)
			}
			if err != nil {
				errors = append(errors, fmt.Errorf("unable to remove CT map %s: %s", ctPath, err))
			}
		}
	}

	// Remove handle_policy() tail call entry for EP
	if err := policymap.RemoveGlobalMapping(uint32(e.ID)); err != nil {
		errors = append(errors, fmt.Errorf("unable to remove endpoint from global policy map: %s", err))
	}

	return errors
}