-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
41 lines (35 loc) · 962 Bytes
/
cache.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
38
39
40
41
package main
import (
"fmt"
"os"
"sync"
)
type cache struct {
sync.Mutex
folder string
Locks map[interface{}]*sync.Mutex
}
func newCache(cacheFolder string) (*cache, error) {
if _, err := os.Stat(cacheFolder); err != nil {
if err = os.MkdirAll(cacheFolder, 0775); err != nil {
return &cache{}, fmt.Errorf("Failed creating cache folder %s: %s", cacheFolder, err.Error())
}
}
return &cache{folder: cacheFolder, Locks: make(map[interface{}]*sync.Mutex)}, nil
}
func (cache *cache) lockModule(o interface{}) { // FIXME that was a bad improvement, we want to lock on target folder name
cache.Lock()
if _, ok := cache.Locks[o]; !ok {
cache.Locks[o] = new(sync.Mutex)
}
l := (*cache).Locks[o]
// We need to unlock cache before waiting for the
// module lock, otherwise UnlockModule can not unlock it
cache.Unlock()
l.Lock()
}
func (cache *cache) unlockModule(o interface{}) {
cache.Lock()
(*cache).Locks[o].Unlock()
cache.Unlock()
}