neco: fix
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
#include "../../../_neco/neco.h"
|
#include "../../../_wrap/neco.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -7,13 +7,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LLGoFiles = "_c/generators.c; ../../_neco/neco.c"
|
LLGoFiles = "_c/gen.c; ../../_wrap/neco.c"
|
||||||
LLGoPackage = "link"
|
LLGoPackage = "link"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
run_main()
|
runMain()
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:linkname run_main C.run_main
|
//go:linkname runMain C.run_main
|
||||||
func run_main() {}
|
func runMain() {}
|
||||||
@@ -15,16 +15,16 @@ func main() {
|
|||||||
func run_main() {
|
func run_main() {
|
||||||
// c.Printf(c.Str("run_main"))
|
// c.Printf(c.Str("run_main"))
|
||||||
// c.Fflush(c.Stdout)
|
// c.Fflush(c.Stdout)
|
||||||
neco.Neco_env_setpaniconerror(true)
|
neco.EnvSetpaniconerror(true)
|
||||||
neco.Neco_env_setcanceltype(neco.NECO_CANCEL_ASYNC)
|
neco.EnvSetcanceltype(neco.CANCEL_ASYNC)
|
||||||
ret := neco.Neco_start(main2, 0)
|
ret := neco.Start(main2, 0)
|
||||||
c.Fprintf(c.Stderr, c.Str("neco_start: %s (code %d)\n"), neco.Neco_strerror(int(ret)), ret)
|
c.Fprintf(c.Stderr, c.Str("neco_start: %s (code %d)\n"), neco.Strerror(ret), ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main2(argc c.Int, argv ...any) {
|
func main2(argc c.Int, argv ...any) {
|
||||||
// c.Printf(c.Str("main2"))
|
// c.Printf(c.Str("main2"))
|
||||||
// c.Fflush(c.Stdout)
|
// c.Fflush(c.Stdout)
|
||||||
neco.Neco_exit_prog(main3())
|
neco.ExitProg(main3())
|
||||||
}
|
}
|
||||||
|
|
||||||
func main3() c.Int {
|
func main3() c.Int {
|
||||||
@@ -33,18 +33,18 @@ func main3() c.Int {
|
|||||||
// c.Fflush(c.Stdout)
|
// c.Fflush(c.Stdout)
|
||||||
|
|
||||||
// Create a new generator coroutine that is used to send ints.
|
// Create a new generator coroutine that is used to send ints.
|
||||||
gen := new(neco.Neco_gen)
|
gen := new(neco.Gen)
|
||||||
neco.Neco_gen_start(&gen, unsafe.Sizeof(int(0)), coroutine, 0)
|
neco.GenStart(&gen, unsafe.Sizeof(int(0)), coroutine, 0)
|
||||||
|
|
||||||
// Iterate over each int until the generator is closed.
|
// Iterate over each int until the generator is closed.
|
||||||
var i c.Int
|
var i c.Int
|
||||||
for {
|
for {
|
||||||
ret := neco.Neco_gen_next(gen, c.Pointer(&i))
|
ret := neco.GenNext(gen, c.Pointer(&i))
|
||||||
|
|
||||||
// c.Printf(c.Str("gen [%d, %d] "), ret, c.Int(neco.NECO_CLOSED))
|
// c.Printf(c.Str("gen [%d, %d] "), ret, c.Int(neco.NECO_CLOSED))
|
||||||
// c.Fflush(c.Stdout)
|
// c.Fflush(c.Stdout)
|
||||||
|
|
||||||
if ret != c.Int(neco.NECO_CLOSED) {
|
if ret != c.Int(neco.CLOSED) {
|
||||||
c.Printf(c.Str("%d\n"), i)
|
c.Printf(c.Str("%d\n"), i)
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@@ -52,7 +52,7 @@ func main3() c.Int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// This coroutine no longer needs the generator.
|
// This coroutine no longer needs the generator.
|
||||||
neco.Neco_gen_release(gen)
|
neco.GenRelease(gen)
|
||||||
|
|
||||||
// c.Printf(c.Str("main3 end"))
|
// c.Printf(c.Str("main3 end"))
|
||||||
// c.Fflush(c.Stdout)
|
// c.Fflush(c.Stdout)
|
||||||
@@ -63,7 +63,7 @@ func main3() c.Int {
|
|||||||
func coroutine(argc c.Int, argv ...any) {
|
func coroutine(argc c.Int, argv ...any) {
|
||||||
// Yield each int to the caller, one at a time.
|
// Yield each int to the caller, one at a time.
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
neco.Neco_gen_yield(c.Pointer(&i))
|
neco.GenYield(c.Pointer(&i))
|
||||||
}
|
}
|
||||||
// c.Printf(c.Str("coroutine end"))
|
// c.Printf(c.Str("coroutine end"))
|
||||||
// c.Fflush(c.Stdout)
|
// c.Fflush(c.Stdout)
|
||||||
48
c/neco/_demo/gen2/gen2.go
Normal file
48
c/neco/_demo/gen2/gen2.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
113
c/neco/neco.go
113
c/neco/neco.go
@@ -7,64 +7,89 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LLGoFiles = "_neco/neco.c"
|
LLGoFiles = "_wrap/neco.c"
|
||||||
LLGoPackage = "link"
|
LLGoPackage = "link"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NECO_OK = 0 ///< Successful result (no error)
|
OK = 0 ///< Successful result (no error)
|
||||||
NECO_ERROR = -1 ///< System error (check errno)
|
ERROR = -1 ///< System error (check errno)
|
||||||
NECO_INVAL = -2 ///< Invalid argument
|
INVAL = -2 ///< Invalid argument
|
||||||
NECO_PERM = -3 ///< Operation not permitted
|
PERM = -3 ///< Operation not permitted
|
||||||
NECO_NOMEM = -4 ///< Cannot allocate memory
|
NOMEM = -4 ///< Cannot allocate memory
|
||||||
NECO_EOF = -5 ///< End of file or stream (neco_stream_*)
|
EOF = -5 ///< End of file or stream (neco_stream_*)
|
||||||
NECO_NOTFOUND = -6 ///< No such coroutine (neco_cancel)
|
NOTFOUND = -6 ///< No such coroutine (neco_cancel)
|
||||||
NECO_NOSIGWATCH = -7 ///< Not watching on a signal
|
NOSIGWATCH = -7 ///< Not watching on a signal
|
||||||
NECO_CLOSED = -8 ///< Channel is closed
|
CLOSED = -8 ///< Channel is closed
|
||||||
NECO_EMPTY = -9 ///< Channel is empty (neco_chan_tryrecv)
|
EMPTY = -9 ///< Channel is empty (neco_chan_tryrecv)
|
||||||
NECO_TIMEDOUT = -10 ///< Deadline has elapsed (neco_*_dl)
|
TIMEDOUT = -10 ///< Deadline has elapsed (neco_*_dl)
|
||||||
NECO_CANCELED = -11 ///< Operation canceled (by neco_cancel)
|
CANCELED = -11 ///< Operation canceled (by neco_cancel)
|
||||||
NECO_BUSY = -12 ///< Resource busy (mutex_trylock)
|
BUSY = -12 ///< Resource busy (mutex_trylock)
|
||||||
NECO_NEGWAITGRP = -13 ///< Negative waitgroup counter
|
NEGWAITGRP = -13 ///< Negative waitgroup counter
|
||||||
NECO_GAIERROR = -14 ///< Error with getaddrinfo (check neco_gai_error)
|
GAIERROR = -14 ///< Error with getaddrinfo (check neco_gai_error)
|
||||||
NECO_UNREADFAIL = -15 ///< Failed to unread byte (neco_stream_unread_byte)
|
UNREADFAIL = -15 ///< Failed to unread byte (neco_stream_unread_byte)
|
||||||
NECO_PARTIALWRITE = -16 ///< Failed to write all data (neco_stream_flush)
|
PARTIALWRITE = -16 ///< Failed to write all data (neco_stream_flush)
|
||||||
NECO_NOTGENERATOR = -17 ///< Coroutine is not a generator (neco_gen_yield)
|
NOTGENERATOR = -17 ///< Coroutine is not a generator (neco_gen_yield)
|
||||||
NECO_NOTSUSPENDED = -18 ///< Coroutine is not suspended (neco_resume)
|
NOTSUSPENDED = -18 ///< Coroutine is not suspended (neco_resume)
|
||||||
|
|
||||||
NECO_CANCEL_ASYNC = 1
|
CANCEL_ASYNC = 1
|
||||||
NECO_CANCEL_INLINE = 2
|
CANCEL_INLINE = 2
|
||||||
NECO_CANCEL_ENABLE = 3
|
CANCEL_ENABLE = 3
|
||||||
NECO_CANCEL_DISABLE = 4
|
CANCEL_DISABLE = 4
|
||||||
|
|
||||||
|
EAI_SYSTEM = 11
|
||||||
|
|
||||||
|
NANOSECOND = int64(1)
|
||||||
|
MICROSECOND = int64(1000)
|
||||||
|
MILLISECOND = int64(1000000)
|
||||||
|
SECOND = int64(1000000000)
|
||||||
|
MINUTE = int64(60000000000)
|
||||||
|
HOUR = int64(3600000000000)
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:linkname Neco_gen_yield C.neco_gen_yield
|
// generator
|
||||||
func Neco_gen_yield(data c.Pointer) c.Int
|
type Gen struct{}
|
||||||
|
|
||||||
//go:linkname Neco_gen_next C.neco_gen_next
|
//go:linkname GenYield C.neco_gen_yield
|
||||||
func Neco_gen_next(gen *Neco_gen, data c.Pointer) c.Int
|
func GenYield(data c.Pointer) c.Int
|
||||||
|
|
||||||
//go:linkname Neco_gen_start C.neco_gen_start
|
//go:linkname GenNext C.neco_gen_next
|
||||||
func Neco_gen_start(gen **Neco_gen, data_size uintptr, co Neco_coro, argc c.Int, argv ...any) c.Int
|
func GenNext(gen *Gen, data c.Pointer) c.Int
|
||||||
|
|
||||||
//go:linkname Neco_gen_release C.neco_gen_release
|
//go:linkname GenStart C.neco_gen_start
|
||||||
func Neco_gen_release(gen *Neco_gen) c.Int
|
func GenStart(gen **Gen, dataSize uintptr, co Coro, argc c.Int, __llgo_va_list /* argv */ ...any) c.Int
|
||||||
|
|
||||||
//go:linkname Neco_exit_prog C.__neco_exit_prog
|
//go:linkname GenRelease C.neco_gen_release
|
||||||
func Neco_exit_prog(code c.Int)
|
func GenRelease(gen *Gen) c.Int
|
||||||
|
|
||||||
//go:linkname Neco_env_setpaniconerror C.neco_env_setpaniconerror
|
//go:linkname ExitProg C.__neco_exit_prog
|
||||||
func Neco_env_setpaniconerror(paniconerror bool)
|
func ExitProg(code c.Int)
|
||||||
|
|
||||||
//go:linkname Neco_env_setcanceltype C.neco_env_setcanceltype
|
//go:linkname EnvSetpaniconerror C.neco_env_setpaniconerror
|
||||||
func Neco_env_setcanceltype(type_ c.Int)
|
func EnvSetpaniconerror(paniconerror bool)
|
||||||
|
|
||||||
//go:linkname Neco_strerror C.neco_strerror
|
//go:linkname EnvSetcanceltype C.neco_env_setcanceltype
|
||||||
func Neco_strerror(errcode int) *c.Char
|
func EnvSetcanceltype(type_ c.Int)
|
||||||
|
|
||||||
//go:linkname Neco_start C.neco_start
|
//go:linkname Strerror C.neco_strerror
|
||||||
func Neco_start(co Neco_coro, argc c.Int, argv ...any) c.Int
|
func Strerror(errcode c.Int) *c.Char
|
||||||
|
|
||||||
type Neco_gen struct{}
|
//go:linkname Start C.neco_start
|
||||||
|
func Start(co Coro, argc c.Int, __llgo_va_list /* argv */ ...any) c.Int
|
||||||
|
|
||||||
type Neco_coro = func(argc c.Int, argv ...any)
|
// 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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user