]> Cypherpunks.ru repositories - goredo.git/commitdiff
Protect concurrent map r/w
authorSergey Matveev <stargrave@stargrave.org>
Sat, 7 Oct 2023 17:14:22 +0000 (20:14 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 7 Oct 2023 17:14:30 +0000 (20:14 +0300)
ifchange.go
tgt.go

index a55120e2e995b957c78dc46f44614229542f3488..374adcf4e811ed044d6eef5c39ef815c9233de95 100644 (file)
@@ -80,7 +80,7 @@ func buildDependants(tgts []*Tgt) map[string]*Tgt {
                        }
                }
        }
-       TgtCache = nil
+       TgtCache = make(map[string]*Tgt)
        IfchangeCache = nil
        if len(seen) == 0 {
                return seen
diff --git a/tgt.go b/tgt.go
index 3fa14a0cb142b67d403a437dd87e7f42b727c100..1cba1e625043f4c82c06161f5ece9357ba98120e 100644 (file)
--- a/tgt.go
+++ b/tgt.go
@@ -3,9 +3,13 @@ package main
 import (
        "path"
        "path/filepath"
+       "sync"
 )
 
-var TgtCache = make(map[string]*Tgt)
+var (
+       TgtCache  = make(map[string]*Tgt)
+       TgtCacheM sync.RWMutex
+)
 
 func mustAbs(pth string) string {
        pth, err := filepath.Abs(pth)
@@ -43,10 +47,11 @@ type Tgt struct {
 
 func NewTgt(tgt string) *Tgt {
        a := mustAbs(tgt)
-       if TgtCache != nil {
-               if t := TgtCache[a]; t != nil {
-                       return t
-               }
+       TgtCacheM.RLock()
+       cached := TgtCache[a]
+       TgtCacheM.RUnlock()
+       if cached != nil {
+               return cached
        }
        h, t := pathSplit(a)
        res := Tgt{
@@ -54,9 +59,9 @@ func NewTgt(tgt string) *Tgt {
                rel: mustRel(Cwd, a),
                dep: path.Join(h, RedoDir, t+DepSuffix),
        }
-       if TgtCache != nil {
-               TgtCache[a] = &res
-       }
+       TgtCacheM.Lock()
+       TgtCache[a] = &res
+       TgtCacheM.Unlock()
        return &res
 }