/
lockmap.go
37 lines (30 loc) · 927 Bytes
/
lockmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright 2015 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package compilerutil
import "sync"
// LockMap defines a concurrent-safe map that returns a sync Mutex for each key. This is useful
// when multiple resources are being loaded concurrently and locking is needed, but only on a
// per-resource basis.
type LockMap struct {
locks map[string]*sync.Mutex
globalLock *sync.Mutex
}
// CreateLockMap returns a new LockMap.
func CreateLockMap() LockMap {
return LockMap{
locks: map[string]*sync.Mutex{},
globalLock: &sync.Mutex{},
}
}
// GetLock returns a lock for the given key.
func (lm LockMap) GetLock(key string) *sync.Mutex {
lm.globalLock.Lock()
defer lm.globalLock.Unlock()
if lock, ok := lm.locks[key]; ok {
return lock
}
lock := &sync.Mutex{}
lm.locks[key] = lock
return lock
}