Better handling of concurrent calls to observers

We now call wg.Add(n) before we loop over n observers.  We also run the
loop inside a goroutine and therefore more quickly handle all (pid,
offset) pairs.
This commit is contained in:
Özgür Kesim 2020-01-16 13:30:41 +01:00
parent 8033525f3f
commit 4ba4442100

View File

@ -122,7 +122,8 @@ func (S *Scanner) Run() error {
proc.Close()
var wg sync.WaitGroup
var wg sync.WaitGroup // To be able to wait for all the observers to finish
for _, pinfo := range infos {
var pid_s = pinfo.Name()
@ -134,9 +135,11 @@ func (S *Scanner) Run() error {
} else if offset, found := S.searchSymbolIn(pid); !found {
continue
} else {
// Notify the observers.
// Call the observers with (pid, offset), in the
// background.
wg.Add(len(S.observers)) // Wait for this many goroutines
go func() {
for n, observer := range S.observers {
wg.Add(1)
go func() {
err = observer(pid, offset)
if err != nil {
@ -146,10 +149,11 @@ func (S *Scanner) Run() error {
wg.Done()
}()
}
}()
}
}
wg.Wait()
wg.Wait() // Wait for all observers to finish
return S.err
}