From c4775dd3132f1bdd4a08f532593310a06580233a Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 18 Jul 2024 00:54:31 +0800 Subject: [PATCH] c/neco; neco demo: gen --- c/neco/_demo/cgen/cgen.go | 2 +- c/neco/_demo/gen/gen.go | 67 ++++++++++----------------------------- c/neco/_demo/gen2/gen2.go | 48 ---------------------------- c/neco/neco.go | 24 +++----------- 4 files changed, 23 insertions(+), 118 deletions(-) delete mode 100644 c/neco/_demo/gen2/gen2.go diff --git a/c/neco/_demo/cgen/cgen.go b/c/neco/_demo/cgen/cgen.go index 87c3eb49d..d00b04f4b 100644 --- a/c/neco/_demo/cgen/cgen.go +++ b/c/neco/_demo/cgen/cgen.go @@ -16,4 +16,4 @@ func main() { } //go:linkname runMain C.run_main -func runMain() {} +func runMain() diff --git a/c/neco/_demo/gen/gen.go b/c/neco/_demo/gen/gen.go index cd5771f0b..0de37f01b 100644 --- a/c/neco/_demo/gen/gen.go +++ b/c/neco/_demo/gen/gen.go @@ -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) } diff --git a/c/neco/_demo/gen2/gen2.go b/c/neco/_demo/gen2/gen2.go deleted file mode 100644 index fcac1094f..000000000 --- a/c/neco/_demo/gen2/gen2.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "unsafe" - - "github.com/goplus/llgo/c" - "github.com/goplus/llgo/c/neco" -) - -func main() { - neco.Main(func() c.Int { - // Create a new generator coroutine that is used to send ints. - gen := new(neco.Gen) - neco.GenStart(&gen, unsafe.Sizeof(int(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 - } - } - - // This coroutine no longer needs the generator. - neco.GenRelease(gen) - - // c.Printf(c.Str("main3 end")) - // c.Fflush(c.Stdout) - - return 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) -} diff --git a/c/neco/neco.go b/c/neco/neco.go index 87fb17477..1c4d8ccc7 100644 --- a/c/neco/neco.go +++ b/c/neco/neco.go @@ -50,6 +50,9 @@ 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 @@ -57,7 +60,7 @@ func GenYield(data c.Pointer) c.Int 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 @@ -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