github.com/Cloud-Foundations/Dominator@v0.3.4/lib/goroutine/api.go (about) 1 package goroutine 2 3 // Function defines a simple function with no arguments or return values. This 4 // is typically used as a closure. 5 type Function func() 6 7 // Goroutine represents a goroutine. A Goroutine is useful for running functions 8 // in a specific environment (such as a pinned OS thread with modified 9 // attributes). 10 type Goroutine struct { 11 start chan<- Function 12 } 13 14 // New creates a Goroutine with an underlying goroutine which can run functions. 15 func New() *Goroutine { 16 return newGoroutine() 17 } 18 19 // Quit causes the underlying goroutine to exit. If there is a currently running 20 // function, it will wait for it to complete. 21 func (g *Goroutine) Quit() { 22 close(g.start) 23 } 24 25 // Run will run a function in the underlying goroutine and wait for it to 26 // complete. If there is a currently running function, it will first wait for it 27 // to complete. 28 func (g *Goroutine) Run(fn Function) { 29 g.start <- fn 30 g.start <- nil 31 } 32 33 // Start will run a function in the underlying goroutine. If there is a 34 // currently running function, it will first wait for it to complete. 35 func (g *Goroutine) Start(fn Function) { 36 g.start <- fn 37 } 38 39 // Wait will wait for a running function to complete. 40 func (g *Goroutine) Wait() { 41 g.start <- nil 42 }