Skip to content

Commit

Permalink
Merge pull request #531 from xushiwei/q
Browse files Browse the repository at this point in the history
c/neco; neco demo: gen
  • Loading branch information
xushiwei authored Jul 17, 2024
2 parents ae87cb0 + c4775dd commit 9daa77c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 118 deletions.
2 changes: 1 addition & 1 deletion c/neco/_demo/cgen/cgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ func main() {
}

//go:linkname runMain C.run_main
func runMain() {}
func runMain()
67 changes: 17 additions & 50 deletions c/neco/_demo/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,31 @@ import (
"github.com/goplus/llgo/c/neco"
)

func main() {
// c.Printf(c.Str("main"))
run_main()
}

func run_main() {
// c.Printf(c.Str("run_main"))
// c.Fflush(c.Stdout)
neco.EnvSetpaniconerror(true)
neco.EnvSetcanceltype(neco.CANCEL_ASYNC)
ret := neco.Start(main2, 0)
c.Fprintf(c.Stderr, c.Str("neco_start: %s (code %d)\n"), neco.Strerror(ret), ret)
}

func main2(argc c.Int, argv ...any) {
// c.Printf(c.Str("main2"))
// c.Fflush(c.Stdout)
neco.ExitProg(main3())
func coroutine(argc c.Int, argv *c.Pointer) {
// Yield each int to the caller, one at a time.
for i := 0; i < 10; i++ {
neco.GenYield(unsafe.Pointer(&i))
}
}

func main3() c.Int {

// c.Printf(c.Str("main3"))
// c.Fflush(c.Stdout)

func necoMain(argc c.Int, argv *c.Pointer) {
// Create a new generator coroutine that is used to send ints.
gen := new(neco.Gen)
neco.GenStart(&gen, unsafe.Sizeof(int(0)), coroutine, 0)
var gen *neco.Gen
neco.GenStart(&gen, unsafe.Sizeof(0), coroutine, 0)

// Iterate over each int until the generator is closed.
var i c.Int
for {
ret := neco.GenNext(gen, c.Pointer(&i))

// c.Printf(c.Str("gen [%d, %d] "), ret, c.Int(neco.NECO_CLOSED))
// c.Fflush(c.Stdout)

if ret != c.Int(neco.CLOSED) {
c.Printf(c.Str("%d\n"), i)
} else {
break
}
var i int
for neco.GenNext(gen, unsafe.Pointer(&i)) != neco.CLOSED {
c.Printf(c.Str("%d\n"), i)
}

// This coroutine no longer needs the generator.
neco.GenRelease(gen)

// c.Printf(c.Str("main3 end"))
// c.Fflush(c.Stdout)

return 0
neco.ExitProg(0)
}

func coroutine(argc c.Int, argv ...any) {
// Yield each int to the caller, one at a time.
for i := 0; i < 10; i++ {
neco.GenYield(c.Pointer(&i))
}
// c.Printf(c.Str("coroutine end"))
// c.Fflush(c.Stdout)
func main() {
neco.EnvSetpaniconerror(true)
neco.EnvSetcanceltype(neco.CANCEL_ASYNC)
var ret = neco.Start(necoMain, 0)
c.Fprintf(c.Stderr, c.Str("neco_start: %s (code %d)\n"), neco.Strerror(ret), ret)
}
48 changes: 0 additions & 48 deletions c/neco/_demo/gen2/gen2.go

This file was deleted.

24 changes: 5 additions & 19 deletions c/neco/neco.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ const (
// generator
type Gen struct{}

// llgo:type C
type Coro = func(argc c.Int, argv *c.Pointer)

//go:linkname GenYield C.neco_gen_yield
func GenYield(data c.Pointer) c.Int

//go:linkname GenNext C.neco_gen_next
func GenNext(gen *Gen, data c.Pointer) c.Int

//go:linkname GenStart C.neco_gen_start
func GenStart(gen **Gen, dataSize uintptr, co Coro, argc c.Int, __llgo_va_list /* argv */ ...any) c.Int
func GenStart(gen **Gen, dataSize uintptr, co Coro, argc c.Int, __llgo_va_list ...any) c.Int

//go:linkname GenRelease C.neco_gen_release
func GenRelease(gen *Gen) c.Int
Expand All @@ -75,21 +78,4 @@ func EnvSetcanceltype(type_ c.Int)
func Strerror(errcode c.Int) *c.Char

//go:linkname Start C.neco_start
func Start(co Coro, argc c.Int, __llgo_va_list /* argv */ ...any) c.Int

// llgo:type C
type Coro = func(argc c.Int, __llgo_va_list /* argv */ ...any)

var mainfn func() c.Int

func Main(fn func() c.Int) {
mainfn = fn

EnvSetpaniconerror(true)
EnvSetcanceltype(CANCEL_ASYNC)
ret := Start(func(argc c.Int, args ...any) {
ExitProg(mainfn())
}, 0)
c.Fprintf(c.Stderr, c.Str("llgo/c/neco.RunMain: Start: %s (code %d)\n"), Strerror(ret), ret)
c.Exit(1)
}
func Start(co Coro, argc c.Int, __llgo_va_list ...any) c.Int

0 comments on commit 9daa77c

Please sign in to comment.