pgraph: semaphore: Add lock around semaphore map

I forgot about the `concurrent map write` race, but now it's fixed. I
suppose we could probably pre-create all semaphores in the graph at once
before Start, and remove this lock, but that's an optimization for a
later day.
This commit is contained in:
James Shubin
2017-03-11 09:06:18 -05:00
parent b740e0b78a
commit 6d9be15035
2 changed files with 5 additions and 0 deletions

View File

@@ -46,11 +46,13 @@ func (g *Graph) SemaLock(semas []string) error {
}
}
g.slock.Lock() // semaphore creation lock
sema, ok := g.semas[id] // lookup
if !ok {
g.semas[id] = semaphore.NewSemaphore(size)
sema = g.semas[id]
}
g.slock.Unlock()
if err := sema.P(1); err != nil { // lock!
reterr = multierr.Append(reterr, err) // list of errors