1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-29 09:34:03 +08:00

updated goprocess (SetTeardown fix)

License: MIT
Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
This commit is contained in:
Juan Batiz-Benet
2015-07-29 01:18:04 -07:00
parent 0b1d36966c
commit 7a41dcb620
4 changed files with 15 additions and 19 deletions

2
Godeps/Godeps.json generated
View File

@ -204,7 +204,7 @@
},
{
"ImportPath": "github.com/jbenet/goprocess",
"Rev": "788dcf5ca3517f243d276394545ca6b3b4ac32d5"
"Rev": "4562d0c5780b8f060df2b84a8945bb8678bfc023"
},
{
"ImportPath": "github.com/kardianos/osext",

View File

@ -145,8 +145,6 @@ type Process interface {
// lifecycle of a Process.
type TeardownFunc func() error
var nilTeardownFunc = func() error { return nil }
// ProcessFunc is a function that takes a process. Its main use case is goprocess.Go,
// which spawns a ProcessFunc in its own goroutine, and returns a corresponding
// Process object.

View File

@ -300,13 +300,11 @@ func TestAddChild(t *testing.T) {
testNone(t, Q)
go b.Close()
testNone(t, Q)
d.Close()
testStrs(t, Q, "b", "d")
testStrs(t, Q, "b", "d")
go a.Close()
testNone(t, Q)
c.Close()
testStrs(t, Q, "a", "c")
testStrs(t, Q, "a", "c")

View File

@ -25,10 +25,6 @@ type process struct {
// **after** entering <-Closing(), and
// **before** <-Closed().
func newProcess(tf TeardownFunc) *process {
if tf == nil {
tf = nilTeardownFunc
}
return &process{
teardown: tf,
closed: make(chan struct{}),
@ -123,17 +119,19 @@ func (p *process) Go(f ProcessFunc) Process {
// SetTeardown to assign a teardown function
func (p *process) SetTeardown(tf TeardownFunc) {
if tf == nil {
tf = nilTeardownFunc
panic("cannot set nil TeardownFunc")
}
p.Lock()
if p.teardown == nil {
select {
case <-p.Closed():
p.teardown = tf
p.closeErr = tf()
default:
}
if p.teardown != nil {
panic("cannot SetTeardown twice")
}
p.teardown = tf
select {
case <-p.Closed():
p.closeErr = tf()
default:
}
p.Unlock()
}
@ -196,8 +194,10 @@ func (p *process) doClose() {
}
}
p.closeErr = p.teardown() // actually run the close logic (ok safe to teardown)
close(p.closed) // signal that we're shut down (Closed)
if p.teardown != nil {
p.closeErr = p.teardown() // actually run the close logic (ok safe to teardown)
}
close(p.closed) // signal that we're shut down (Closed)
// go remove all the parents from the process links. optimization.
go func(waiters []*processLink) {