• David Vorick's avatar
    separate ThreadGroup locking by function · 3ffc5b35
    David Vorick authored
    The threadgroup has two separate things that it is protecting when it
    locks the mutex. One group of things is to block the execution of other
    threads, in the case of 'Add', 'Flush', and 'Stop'. The other group of
    things is to protect the ThreadGroup state variables, namely 'onStopFns'
    and 'afterStopFns'.
    
    Previously, you would get a deadlock with the following:
    
    tg.Add()
    go func {
    	tg.Stop()
    }
    time.Sleep(1 * time.Second)
    tg.OnStop()
    tg.Done()
    
    The deadlock happens because tg.Stop() is blocking the OnStop call while
    it waits for the tg.Add to return, but the tg.Add will not return until
    tg.OnStop can get the lock.
    
    I don't see any reason for this behavior to be forbidden. You want to be
    able to do something like:
    
    tg.Add()
    tg.RegisterRPC()
    tg.OnStop(func() {
    	tg.UnregisterRPC()
    })
    tg.Done()
    
    and currently that's not safe code if you are assuming that Stop() could
    be called at any time.
    3ffc5b35
threadgroup.go 4.33 KB