asyncio: refactor
This commit is contained in:
174
x/io/io.go
174
x/io/io.go
@@ -17,8 +17,7 @@
|
||||
package io
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"unsafe"
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
@@ -26,174 +25,49 @@ const (
|
||||
LLGoPackage = "decl"
|
||||
)
|
||||
|
||||
var debugAsync = false
|
||||
const debugAsync = false
|
||||
|
||||
type Void = [0]byte
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type asyncCall interface {
|
||||
parent() asyncCall
|
||||
Resume()
|
||||
Call()
|
||||
Done() bool
|
||||
}
|
||||
|
||||
type AsyncCall[OutT any] interface {
|
||||
Resume()
|
||||
}
|
||||
|
||||
type executor struct {
|
||||
acs []asyncCall
|
||||
mu sync.Mutex
|
||||
cond *sync.Cond
|
||||
}
|
||||
|
||||
func newExecutor() *executor {
|
||||
e := &executor{}
|
||||
e.cond = sync.NewCond(&e.mu)
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *executor) schedule(ac asyncCall) {
|
||||
e.mu.Lock()
|
||||
e.acs = append(e.acs, ac)
|
||||
e.mu.Unlock()
|
||||
e.cond.Signal()
|
||||
}
|
||||
|
||||
func Run[OutT any](ac AsyncCall[OutT]) OutT {
|
||||
e := newExecutor()
|
||||
p := ac.(*Promise[OutT])
|
||||
p.Exec = e
|
||||
var rootAc asyncCall = p
|
||||
e.schedule(rootAc)
|
||||
|
||||
for {
|
||||
e.mu.Lock()
|
||||
for len(e.acs) == 0 {
|
||||
e.cond.Wait()
|
||||
}
|
||||
e.mu.Unlock()
|
||||
ac := e.acs[0]
|
||||
e.acs = e.acs[1:]
|
||||
ac.Call()
|
||||
if ac.Done() && ac == rootAc {
|
||||
return p.value
|
||||
}
|
||||
}
|
||||
}
|
||||
type AsyncCall[TOut any] interface{}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type R1[T any] struct {
|
||||
V1 T
|
||||
}
|
||||
|
||||
func (r R1[T]) Values() T {
|
||||
return r.V1
|
||||
}
|
||||
|
||||
type R2[T1 any, T2 any] struct {
|
||||
V1 T1
|
||||
V2 T2
|
||||
}
|
||||
|
||||
func (r R2[T1, T2]) Values() (T1, T2) {
|
||||
return r.V1, r.V2
|
||||
}
|
||||
|
||||
type R3[T1 any, T2 any, T3 any] struct {
|
||||
V1 T1
|
||||
V2 T2
|
||||
V3 T3
|
||||
}
|
||||
|
||||
func (r R3[T1, T2, T3]) Values() (T1, T2, T3) {
|
||||
return r.V1, r.V2, r.V3
|
||||
}
|
||||
|
||||
type R4[T1 any, T2 any, T3 any, T4 any] struct {
|
||||
V1 T1
|
||||
V2 T2
|
||||
V3 T3
|
||||
V4 T4
|
||||
}
|
||||
|
||||
func (r R4[T1, T2, T3, T4]) Values() (T1, T2, T3, T4) {
|
||||
return r.V1, r.V2, r.V3, r.V4
|
||||
}
|
||||
|
||||
type Promise[TOut any] struct {
|
||||
Debug string
|
||||
Next int
|
||||
Exec *executor
|
||||
Parent asyncCall
|
||||
|
||||
Func func()
|
||||
hdl unsafe.Pointer
|
||||
value TOut
|
||||
c chan TOut
|
||||
}
|
||||
|
||||
func NewPromise[TOut any](fn func()) *Promise[TOut] {
|
||||
return &Promise[TOut]{Func: fn}
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) parent() asyncCall {
|
||||
return p.Parent
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Resume() {
|
||||
if debugAsync {
|
||||
log.Printf("Resume task: %+v\n", p)
|
||||
}
|
||||
p.Exec.schedule(p)
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Done() bool {
|
||||
return p.Next == -1
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Call() {
|
||||
p.Func()
|
||||
// llgo:link PromiseImpl llgo.coAwait
|
||||
func (p *Promise[TOut]) Await() TOut {
|
||||
panic("should not executed")
|
||||
}
|
||||
|
||||
// llgo:link Return llgo.coReturn
|
||||
func (p *Promise[TOut]) Return(v TOut) {
|
||||
// TODO(lijie): panic if already resolved
|
||||
p.value = v
|
||||
if p.c != nil {
|
||||
p.c <- v
|
||||
}
|
||||
if debugAsync {
|
||||
log.Printf("Return task: %+v\n", p)
|
||||
}
|
||||
if p.Parent != nil {
|
||||
p.Parent.Resume()
|
||||
}
|
||||
panic("should not executed")
|
||||
}
|
||||
|
||||
// llgo:link Yield llgo.coYield
|
||||
func (p *Promise[TOut]) Yield(v TOut) {
|
||||
p.value = v
|
||||
if debugAsync {
|
||||
log.Printf("Yield task: %+v\n", p)
|
||||
}
|
||||
if p.Parent != nil {
|
||||
p.Parent.Resume()
|
||||
}
|
||||
panic("should not executed")
|
||||
}
|
||||
|
||||
// llgo:link Suspend llgo.coSuspend
|
||||
func (p *Promise[TOut]) Suspend() {
|
||||
panic("should not executed")
|
||||
}
|
||||
|
||||
// llgo:link Resume llgo.coResume
|
||||
func (p *Promise[TOut]) Resume() {
|
||||
panic("should not executed")
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Value() TOut {
|
||||
return p.value
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Chan() <-chan TOut {
|
||||
if p.c == nil {
|
||||
p.c = make(chan TOut, 1)
|
||||
p.Func()
|
||||
}
|
||||
return p.c
|
||||
}
|
||||
|
||||
func (p *Promise[TOut]) Await() (ret TOut) {
|
||||
panic("should not called")
|
||||
// llgo:link Run llgo.coRun
|
||||
func Run[TOut any](f func() TOut) TOut {
|
||||
panic("should not executed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user