new future IO and demo
This commit is contained in:
308
x/async/_demo/all/all.go
Normal file
308
x/async/_demo/all/all.go
Normal file
@@ -0,0 +1,308 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/net"
|
||||
"github.com/goplus/llgo/x/async"
|
||||
"github.com/goplus/llgo/x/async/timeout"
|
||||
"github.com/goplus/llgo/x/io"
|
||||
"github.com/goplus/llgo/x/tuple"
|
||||
)
|
||||
|
||||
func ReadFile(fileName string) async.Future[tuple.Tuple2[[]byte, error]] {
|
||||
return async.Async(func(resolve func(tuple.Tuple2[[]byte, error])) {
|
||||
go func() {
|
||||
println(async.Gettid(), "read file", fileName)
|
||||
bytes, err := os.ReadFile(fileName)
|
||||
resolve(tuple.T2(bytes, err))
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func WriteFile(fileName string, content []byte) async.Future[error] {
|
||||
return async.Async(func(resolve func(error)) {
|
||||
go func() {
|
||||
err := os.WriteFile(fileName, content, 0644)
|
||||
resolve(err)
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func sleep(i int, d time.Duration) async.Future[int] {
|
||||
return async.Async(func(resolve func(int)) {
|
||||
timeout.Timeout(d)(func(async.Void) {
|
||||
resolve(i)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
RunIO()
|
||||
RunAllAndRace()
|
||||
RunTimeout()
|
||||
RunSocket()
|
||||
}
|
||||
|
||||
func RunIO() {
|
||||
println("RunIO with Await")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
println("read file")
|
||||
defer resolve(async.Void{})
|
||||
content, err := async.Await(ReadFile("1.txt")).Get()
|
||||
if err != nil {
|
||||
fmt.Printf("read err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("read content: %s\n", content)
|
||||
err = async.Await(WriteFile("2.txt", content))
|
||||
if err != nil {
|
||||
fmt.Printf("write err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("write done\n")
|
||||
}))
|
||||
|
||||
// Translated Await to BindIO in Go+:
|
||||
println("RunIO with BindIO")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
ReadFile("1.txt")(func(v tuple.Tuple2[[]byte, error]) {
|
||||
content, err := v.Get()
|
||||
if err != nil {
|
||||
fmt.Printf("read err: %v\n", err)
|
||||
resolve(async.Void{})
|
||||
return
|
||||
}
|
||||
fmt.Printf("read content: %s\n", content)
|
||||
WriteFile("2.txt", content)(func(v error) {
|
||||
err = v
|
||||
if err != nil {
|
||||
fmt.Printf("write err: %v\n", err)
|
||||
resolve(async.Void{})
|
||||
return
|
||||
}
|
||||
println("write done")
|
||||
resolve(async.Void{})
|
||||
})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func RunAllAndRace() {
|
||||
ms100 := 100 * time.Millisecond
|
||||
ms200 := 200 * time.Millisecond
|
||||
ms300 := 300 * time.Millisecond
|
||||
|
||||
println("Run All with Await")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
async.All(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))(func(v []int) {
|
||||
fmt.Printf("All: %v\n", v)
|
||||
resolve(async.Void{})
|
||||
})
|
||||
}))
|
||||
|
||||
println("Run Race with Await")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
first := async.Race(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))
|
||||
v := async.Await(first)
|
||||
fmt.Printf("Race: %v\n", v)
|
||||
resolve(async.Void{})
|
||||
}))
|
||||
|
||||
// Translated to in Go+:
|
||||
|
||||
println("Run All with BindIO")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
async.All(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))(func(v []int) {
|
||||
fmt.Printf("All: %v\n", v)
|
||||
resolve(async.Void{})
|
||||
})
|
||||
}))
|
||||
|
||||
println("Run Race with BindIO")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
async.Race(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))(func(v int) {
|
||||
fmt.Printf("Race: %v\n", v)
|
||||
resolve(async.Void{})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func RunTimeout() {
|
||||
println("Run Timeout with Await")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
fmt.Printf("Start 100 ms timeout\n")
|
||||
async.Await(timeout.Timeout(100 * time.Millisecond))
|
||||
fmt.Printf("timeout\n")
|
||||
resolve(async.Void{})
|
||||
}))
|
||||
|
||||
// Translated to in Go+:
|
||||
|
||||
println("Run Timeout with BindIO")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
fmt.Printf("Start 100 ms timeout\n")
|
||||
timeout.Timeout(100 * time.Millisecond)(func(async.Void) {
|
||||
fmt.Printf("timeout\n")
|
||||
resolve(async.Void{})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func RunSocket() {
|
||||
println("Run Socket")
|
||||
|
||||
async.Run(async.Async(func(resolve func(async.Void)) {
|
||||
println("RunServer")
|
||||
|
||||
RunServer()(func(async.Void) {
|
||||
println("RunServer done")
|
||||
resolve(async.Void{})
|
||||
})
|
||||
|
||||
println("RunClient")
|
||||
|
||||
RunClient()(func(async.Void) {
|
||||
println("RunClient done")
|
||||
resolve(async.Void{})
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func RunClient() async.Future[async.Void] {
|
||||
return async.Async(func(resolve func(async.Void)) {
|
||||
bindAddr := "127.0.0.1:3927"
|
||||
io.ParseAddr(bindAddr)(func(v tuple.Tuple2[*net.SockAddr, error]) {
|
||||
addr, err := v.Get()
|
||||
println("Connect to", addr, err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
io.Connect(addr)(func(v tuple.Tuple2[*io.Tcp, error]) {
|
||||
client, err := v.Get()
|
||||
println("Connected", client, err)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var loop func(client *io.Tcp)
|
||||
loop = func(client *io.Tcp) {
|
||||
client.Write([]byte("Hello"))(func(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
client.Read()(func(v tuple.Tuple2[[]byte, error]) {
|
||||
data, err := v.Get()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
println("Read:", string(data))
|
||||
timeout.Timeout(1 * time.Second)(func(async.Void) {
|
||||
loop(client)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
loop(client)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func RunServer() async.Future[async.Void] {
|
||||
return async.Async(func(resolve func(async.Void)) {
|
||||
server, err := io.NewTcp()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bindAddr := "0.0.0.0:3927"
|
||||
io.ParseAddr(bindAddr)(func(v tuple.Tuple2[*net.SockAddr, error]) {
|
||||
addr, err := v.Get()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err = server.Bind(addr, 0); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Printf(c.Str("Listening on %s\n"), c.AllocaCStr(bindAddr))
|
||||
|
||||
err = server.Listen(128, func(server *io.Tcp, err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
client, err := server.Accept()
|
||||
println("Accept", client, err)
|
||||
|
||||
var loop func(client *io.Tcp)
|
||||
loop = func(client *io.Tcp) {
|
||||
client.Read()(func(v tuple.Tuple2[[]byte, error]) {
|
||||
data, err := v.Get()
|
||||
if err != nil {
|
||||
println("Read error", err)
|
||||
} else {
|
||||
println("Read:", string(data))
|
||||
client.Write(data)(func(err error) {
|
||||
if err != nil {
|
||||
println("Write error", err)
|
||||
} else {
|
||||
println("Write done")
|
||||
loop(client)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
loop(client)
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func RunServer1() async.Future[async.Void] {
|
||||
return async.Async(func(resolve func(async.Void)) {
|
||||
io.Listen("tcp", "0.0.0.0:3927")(func(v tuple.Tuple2[*io.Tcp, error]) {
|
||||
server, err := v.Get()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
client, err := server.Accept()
|
||||
println("Accept", client, err)
|
||||
|
||||
var loop func(client *io.Tcp)
|
||||
loop = func(client *io.Tcp) {
|
||||
client.Read()(func(v tuple.Tuple2[[]byte, error]) {
|
||||
data, err := v.Get()
|
||||
if err != nil {
|
||||
println("Read error", err)
|
||||
} else {
|
||||
println("Read:", string(data))
|
||||
client.Write(data)(func(err error) {
|
||||
if err != nil {
|
||||
println("Write error", err)
|
||||
} else {
|
||||
println("Write done")
|
||||
loop(client)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
loop(client)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/goplus/llgo/x/async"
|
||||
"github.com/goplus/llgo/x/async/timeout"
|
||||
"github.com/goplus/llgo/x/tuple"
|
||||
)
|
||||
|
||||
func ReadFile(fileName string) async.IO[tuple.Tuple2[[]byte, error]] {
|
||||
return async.Async(func(resolve func(tuple.Tuple2[[]byte, error])) {
|
||||
go func() {
|
||||
bytes, err := os.ReadFile(fileName)
|
||||
resolve(tuple.T2(bytes, err))
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func WriteFile(fileName string, content []byte) async.IO[error] {
|
||||
return async.Async(func(resolve func(error)) {
|
||||
go func() {
|
||||
err := os.WriteFile(fileName, content, 0644)
|
||||
resolve(err)
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
func sleep(i int, d time.Duration) async.IO[int] {
|
||||
return async.Async(func(resolve func(int)) {
|
||||
async.BindIO(timeout.Timeout(d), func(async.Void) {
|
||||
resolve(i)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
RunIO()
|
||||
RunAllAndRace()
|
||||
RunTimeout()
|
||||
RunSocket()
|
||||
}
|
||||
|
||||
func RunIO() {
|
||||
println("RunIO with Await")
|
||||
|
||||
async.Run(func() {
|
||||
content, err := async.Await(ReadFile("1.txt")).Get()
|
||||
if err != nil {
|
||||
fmt.Printf("read err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("read content: %s\n", content)
|
||||
err = async.Await(WriteFile("2.txt", content))
|
||||
if err != nil {
|
||||
fmt.Printf("write err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("write done\n")
|
||||
})
|
||||
|
||||
// Translated to in Go+:
|
||||
println("RunIO with BindIO")
|
||||
|
||||
async.Run(func() {
|
||||
async.BindIO(ReadFile("1.txt"), func(v tuple.Tuple2[[]byte, error]) {
|
||||
content, err := v.Get()
|
||||
if err != nil {
|
||||
fmt.Printf("read err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("read content: %s\n", content)
|
||||
async.BindIO(WriteFile("2.txt", content), func(v error) {
|
||||
err = v
|
||||
if err != nil {
|
||||
fmt.Printf("write err: %v\n", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("write done\n")
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func RunAllAndRace() {
|
||||
ms100 := 100 * time.Millisecond
|
||||
ms200 := 200 * time.Millisecond
|
||||
ms300 := 300 * time.Millisecond
|
||||
|
||||
println("Run All with Await")
|
||||
|
||||
async.Run(func() {
|
||||
all := async.All(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))
|
||||
async.BindIO(all, func(v []int) {
|
||||
fmt.Printf("All: %v\n", v)
|
||||
})
|
||||
})
|
||||
|
||||
println("Run Race with Await")
|
||||
|
||||
async.Run(func() {
|
||||
first := async.Race(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))
|
||||
v := async.Await(first)
|
||||
fmt.Printf("Race: %v\n", v)
|
||||
})
|
||||
|
||||
// Translated to in Go+:
|
||||
|
||||
println("Run All with BindIO")
|
||||
|
||||
async.Run(func() {
|
||||
all := async.All(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))
|
||||
async.BindIO(all, func(v []int) {
|
||||
fmt.Printf("All: %v\n", v)
|
||||
})
|
||||
})
|
||||
|
||||
println("Run Race with BindIO")
|
||||
|
||||
async.Run(func() {
|
||||
first := async.Race(sleep(1, ms200), sleep(2, ms100), sleep(3, ms300))
|
||||
async.BindIO(first, func(v int) {
|
||||
fmt.Printf("Race: %v\n", v)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func RunTimeout() {
|
||||
println("Run Timeout with Await")
|
||||
|
||||
async.Run(func() {
|
||||
fmt.Printf("Start 100 ms timeout\n")
|
||||
async.Await(timeout.Timeout(100 * time.Millisecond))
|
||||
fmt.Printf("timeout\n")
|
||||
})
|
||||
|
||||
// Translated to in Go+:
|
||||
|
||||
println("Run Timeout with BindIO")
|
||||
|
||||
async.Run(func() {
|
||||
fmt.Printf("Start 100 ms timeout\n")
|
||||
async.BindIO(timeout.Timeout(100*time.Millisecond), func(async.Void) {
|
||||
fmt.Printf("timeout\n")
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func RunSocket() {
|
||||
// async.Run(func() {
|
||||
// tcp := io.NewTcp()
|
||||
// tcp.
|
||||
// })
|
||||
}
|
||||
@@ -17,38 +17,45 @@
|
||||
package async
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c/libuv"
|
||||
)
|
||||
|
||||
type Void = [0]byte
|
||||
|
||||
type Future[T any] func() T
|
||||
type Future[T any] func(func(T))
|
||||
|
||||
type IO[T any] func(e *AsyncContext) Future[T]
|
||||
|
||||
type AsyncContext struct {
|
||||
*Executor
|
||||
complete func()
|
||||
type asyncBind[T any] struct {
|
||||
libuv.Async
|
||||
result T
|
||||
chain func(T)
|
||||
}
|
||||
|
||||
func (ctx *AsyncContext) Complete() {
|
||||
ctx.complete()
|
||||
func asyncCb[T any](a *libuv.Async) {
|
||||
a.Close(nil)
|
||||
aa := (*asyncBind[T])(unsafe.Pointer(a))
|
||||
aa.chain(aa.result)
|
||||
}
|
||||
|
||||
func Async[T any](fn func(resolve func(T))) IO[T] {
|
||||
return func(ctx *AsyncContext) Future[T] {
|
||||
var result T
|
||||
var done bool
|
||||
fn(func(t T) {
|
||||
result = t
|
||||
done = true
|
||||
ctx.Complete()
|
||||
func Async[T any](fn func(func(T))) Future[T] {
|
||||
return func(chain func(T)) {
|
||||
loop := Exec().L
|
||||
// var result T
|
||||
// var a *libuv.Async
|
||||
// var cb libuv.AsyncCb
|
||||
// a, cb = cbind.BindF[libuv.Async, libuv.AsyncCb](func() {
|
||||
// a.Close(nil)
|
||||
// chain(result)
|
||||
// })
|
||||
// loop.Async(a, cb)
|
||||
|
||||
aa := &asyncBind[T]{chain: chain}
|
||||
loop.Async(&aa.Async, asyncCb[T])
|
||||
fn(func(v T) {
|
||||
aa.result = v
|
||||
aa.Send()
|
||||
})
|
||||
return func() T {
|
||||
if !done {
|
||||
panic("async.Async: Future accessed before completion")
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build !llgo
|
||||
// +build !llgo
|
||||
//go:build llgo11
|
||||
// +build llgo11
|
||||
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build llgo
|
||||
// +build llgo
|
||||
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
@@ -21,92 +18,39 @@ package async
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c/libuv"
|
||||
)
|
||||
|
||||
type bindAsync struct {
|
||||
libuv.Async
|
||||
cb func()
|
||||
}
|
||||
|
||||
func BindIO[T any](call IO[T], callback func(T)) {
|
||||
loop := Exec().L
|
||||
a := &bindAsync{}
|
||||
loop.Async(&a.Async, func(p *libuv.Async) {
|
||||
(*bindAsync)(unsafe.Pointer(p)).cb()
|
||||
})
|
||||
done := atomic.Bool{}
|
||||
ctx := &AsyncContext{
|
||||
Executor: Exec(),
|
||||
complete: func() {
|
||||
done.Store(true)
|
||||
a.Async.Send()
|
||||
},
|
||||
}
|
||||
f := call(ctx)
|
||||
called := false
|
||||
a.cb = func() {
|
||||
if called {
|
||||
return
|
||||
}
|
||||
a.Async.Close(nil)
|
||||
result := f()
|
||||
callback(result)
|
||||
}
|
||||
// don't delay the callback if the future is already done
|
||||
if done.Load() {
|
||||
called = true
|
||||
a.cb()
|
||||
}
|
||||
}
|
||||
|
||||
func Await[T1 any](call IO[T1]) (ret T1) {
|
||||
BindIO(call, func(v T1) {
|
||||
ret = v
|
||||
})
|
||||
return
|
||||
func Await[T1 any](call Future[T1]) (ret T1) {
|
||||
return Run(call)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func Race[T1 any](calls ...IO[T1]) IO[T1] {
|
||||
func Race[T1 any](futures ...Future[T1]) Future[T1] {
|
||||
return Async(func(resolve func(T1)) {
|
||||
done := false
|
||||
for _, call := range calls {
|
||||
var f Future[T1]
|
||||
f = call(&AsyncContext{
|
||||
Executor: Exec(),
|
||||
complete: func() {
|
||||
if done {
|
||||
return
|
||||
}
|
||||
done = true
|
||||
resolve(f())
|
||||
},
|
||||
done := atomic.Bool{}
|
||||
for _, future := range futures {
|
||||
future(func(v T1) {
|
||||
if !done.Swap(true) {
|
||||
resolve(v)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func All[T1 any](calls ...IO[T1]) IO[[]T1] {
|
||||
func All[T1 any](futures ...Future[T1]) Future[[]T1] {
|
||||
return Async(func(resolve func([]T1)) {
|
||||
n := len(calls)
|
||||
n := len(futures)
|
||||
results := make([]T1, n)
|
||||
done := 0
|
||||
for i, call := range calls {
|
||||
var done uint32
|
||||
for i, future := range futures {
|
||||
i := i
|
||||
var f Future[T1]
|
||||
f = call(&AsyncContext{
|
||||
Executor: Exec(),
|
||||
complete: func() {
|
||||
results[i] = f()
|
||||
done++
|
||||
if done == n {
|
||||
resolve(results)
|
||||
}
|
||||
},
|
||||
future(func(v T1) {
|
||||
results[i] = v
|
||||
if atomic.AddUint32(&done, 1) == uint32(n) {
|
||||
resolve(results)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build !llgo
|
||||
// +build !llgo
|
||||
//go:build llgo11
|
||||
// +build llgo11
|
||||
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build llgo
|
||||
// +build llgo
|
||||
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
@@ -22,10 +19,14 @@ package async
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/libuv"
|
||||
"github.com/goplus/llgo/c/pthread"
|
||||
)
|
||||
|
||||
//go:linkname Gettid C.pthread_self
|
||||
func Gettid() c.Pointer
|
||||
|
||||
var execKey pthread.Key
|
||||
|
||||
func init() {
|
||||
@@ -44,20 +45,25 @@ func Exec() *Executor {
|
||||
return (*Executor)(v)
|
||||
}
|
||||
|
||||
func setExec(e *Executor) {
|
||||
func setExec(e *Executor) (old *Executor) {
|
||||
old = (*Executor)(execKey.Get())
|
||||
execKey.Set(unsafe.Pointer(e))
|
||||
return
|
||||
}
|
||||
|
||||
func (e *Executor) Run() {
|
||||
e.L.Run(libuv.RUN_DEFAULT)
|
||||
}
|
||||
|
||||
func Run(fn func()) {
|
||||
func Run[T any](future Future[T]) (ret T) {
|
||||
loop := libuv.LoopNew()
|
||||
exec := &Executor{loop}
|
||||
setExec(exec)
|
||||
fn()
|
||||
oldExec := setExec(exec)
|
||||
future(func(v T) {
|
||||
ret = v
|
||||
})
|
||||
exec.Run()
|
||||
loop.Close()
|
||||
setExec(nil)
|
||||
setExec(oldExec)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build !llgo
|
||||
// +build !llgo
|
||||
//go:build llgo11
|
||||
// +build llgo11
|
||||
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
//go:build llgo
|
||||
// +build llgo
|
||||
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
@@ -27,16 +24,16 @@ import (
|
||||
"github.com/goplus/llgo/x/cbind"
|
||||
)
|
||||
|
||||
func Timeout(d time.Duration) async.IO[async.Void] {
|
||||
func Timeout(d time.Duration) async.Future[async.Void] {
|
||||
return async.Async(func(resolve func(async.Void)) {
|
||||
t, _ := cbind.BindF[libuv.Timer, libuv.TimerCb](func() {
|
||||
t, cb := cbind.BindF[libuv.Timer, libuv.TimerCb](func(t *libuv.Timer) {
|
||||
resolve(async.Void{})
|
||||
})
|
||||
r := libuv.InitTimer(async.Exec().L, t)
|
||||
if r != 0 {
|
||||
panic("InitTimer failed")
|
||||
}
|
||||
r = t.Start(cbind.Callback[libuv.Timer], uint64(d/time.Millisecond), 0)
|
||||
r = t.Start(cb, uint64(d/time.Millisecond), 0)
|
||||
if r != 0 {
|
||||
panic("Start failed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user