Skip to content

Commit

Permalink
asynchronous call support
Browse files Browse the repository at this point in the history
  • Loading branch information
name5566 committed Jan 23, 2015
1 parent 33c1fe4 commit 2be0550
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions go/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g_test

import (
"fmt"
"github.com/name5566/leaf/go"
)

func Example() {
d := g.New(10)

d.Go(func() {
fmt.Print("My name is ")
}, func() {
fmt.Println("Leaf")
})

d.Close()

// Output:
// My name is Leaf
}
34 changes: 34 additions & 0 deletions go/go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g

// one Go per goroutine
type Go struct {
ChanCb chan func()
pendingGo int
}

func New(l int) *Go {
g := new(Go)
g.ChanCb = make(chan func(), l)
return g
}

func (g *Go) Go(f func(), cb func()) {
g.pendingGo++

go func() {
f()
g.ChanCb <- cb
}()
}

func (g *Go) Cb(cb func()) {
cb()

g.pendingGo--
}

func (g *Go) Close() {
for g.pendingGo > 0 {
g.Cb(<-g.ChanCb)
}
}

0 comments on commit 2be0550

Please sign in to comment.