new future IO and demo

This commit is contained in:
Li Jie
2024-09-05 16:00:26 +08:00
parent 6e0a9b2b48
commit a2d4e79c20
14 changed files with 706 additions and 507 deletions

View File

@@ -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
}