2024-07-22 19:34:49 +08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package io
|
|
|
|
|
|
|
|
|
|
import (
|
2024-07-25 08:50:49 +08:00
|
|
|
"log"
|
|
|
|
|
"sync"
|
2024-07-22 19:34:49 +08:00
|
|
|
_ "unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
LLGoPackage = "decl"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-25 08:50:49 +08:00
|
|
|
var debugAsync = false
|
|
|
|
|
|
2024-07-22 19:34:49 +08:00
|
|
|
type Void = [0]byte
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-25 08:50:49 +08:00
|
|
|
type asyncCall interface {
|
2024-07-25 10:49:02 +08:00
|
|
|
parent() asyncCall
|
2024-07-25 08:50:49 +08:00
|
|
|
Resume()
|
|
|
|
|
Call()
|
|
|
|
|
Done() bool
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-23 15:58:37 +08:00
|
|
|
type AsyncCall[OutT any] interface {
|
2024-07-28 17:56:30 +08:00
|
|
|
Resume()
|
2024-07-22 19:34:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 08:50:49 +08:00
|
|
|
type executor struct {
|
2024-07-25 10:49:02 +08:00
|
|
|
acs []asyncCall
|
2024-07-25 08:50:49 +08:00
|
|
|
mu sync.Mutex
|
|
|
|
|
cond *sync.Cond
|
2024-07-23 17:02:40 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 08:50:49 +08:00
|
|
|
func newExecutor() *executor {
|
|
|
|
|
e := &executor{}
|
|
|
|
|
e.cond = sync.NewCond(&e.mu)
|
|
|
|
|
return e
|
2024-07-22 19:34:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 10:49:02 +08:00
|
|
|
func (e *executor) schedule(ac asyncCall) {
|
2024-07-25 08:50:49 +08:00
|
|
|
e.mu.Lock()
|
2024-07-25 10:49:02 +08:00
|
|
|
e.acs = append(e.acs, ac)
|
|
|
|
|
e.mu.Unlock()
|
2024-07-25 08:50:49 +08:00
|
|
|
e.cond.Signal()
|
2024-07-24 01:01:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func Run[OutT any](ac AsyncCall[OutT]) OutT {
|
2024-07-25 08:50:49 +08:00
|
|
|
e := newExecutor()
|
2024-07-28 17:56:30 +08:00
|
|
|
p := ac.(*Promise[OutT])
|
2024-07-25 08:50:49 +08:00
|
|
|
p.Exec = e
|
2024-07-25 10:49:02 +08:00
|
|
|
var rootAc asyncCall = p
|
|
|
|
|
e.schedule(rootAc)
|
2024-07-24 01:01:09 +08:00
|
|
|
|
2024-07-25 08:50:49 +08:00
|
|
|
for {
|
|
|
|
|
e.mu.Lock()
|
2024-07-25 10:49:02 +08:00
|
|
|
for len(e.acs) == 0 {
|
2024-07-25 08:50:49 +08:00
|
|
|
e.cond.Wait()
|
|
|
|
|
}
|
|
|
|
|
e.mu.Unlock()
|
2024-07-25 10:49:02 +08:00
|
|
|
ac := e.acs[0]
|
|
|
|
|
e.acs = e.acs[1:]
|
2024-07-25 11:04:40 +08:00
|
|
|
ac.Call()
|
|
|
|
|
if ac.Done() && ac == rootAc {
|
2024-07-28 17:56:30 +08:00
|
|
|
return p.value
|
2024-07-25 08:50:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-24 01:01:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 19:34:49 +08:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
type R1[T any] struct {
|
|
|
|
|
V1 T
|
|
|
|
|
}
|
2024-07-23 15:58:37 +08:00
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (r R1[T]) Values() T {
|
|
|
|
|
return r.V1
|
2024-07-23 15:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
type R2[T1 any, T2 any] struct {
|
|
|
|
|
V1 T1
|
|
|
|
|
V2 T2
|
2024-07-25 08:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (r R2[T1, T2]) Values() (T1, T2) {
|
|
|
|
|
return r.V1, r.V2
|
2024-07-23 15:58:37 +08:00
|
|
|
}
|
2024-07-22 19:34:49 +08:00
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
type R3[T1 any, T2 any, T3 any] struct {
|
|
|
|
|
V1 T1
|
|
|
|
|
V2 T2
|
|
|
|
|
V3 T3
|
2024-07-27 15:08:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (r R3[T1, T2, T3]) Values() (T1, T2, T3) {
|
|
|
|
|
return r.V1, r.V2, r.V3
|
2024-07-27 15:08:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
type R4[T1 any, T2 any, T3 any, T4 any] struct {
|
|
|
|
|
V1 T1
|
|
|
|
|
V2 T2
|
|
|
|
|
V3 T3
|
|
|
|
|
V4 T4
|
2024-07-24 01:01:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (r R4[T1, T2, T3, T4]) Values() (T1, T2, T3, T4) {
|
|
|
|
|
return r.V1, r.V2, r.V3, r.V4
|
|
|
|
|
}
|
2024-07-24 01:01:09 +08:00
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
type Promise[TOut any] struct {
|
2024-07-27 15:08:09 +08:00
|
|
|
Debug string
|
2024-07-25 10:49:02 +08:00
|
|
|
Next int
|
|
|
|
|
Exec *executor
|
|
|
|
|
Parent asyncCall
|
2024-07-24 01:01:09 +08:00
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
Func func()
|
2024-07-27 15:08:09 +08:00
|
|
|
value TOut
|
2024-07-25 08:50:49 +08:00
|
|
|
c chan TOut
|
2024-07-24 01:01:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func NewPromise[TOut any](fn func()) *Promise[TOut] {
|
|
|
|
|
return &Promise[TOut]{Func: fn}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Promise[TOut]) parent() asyncCall {
|
2024-07-25 10:49:02 +08:00
|
|
|
return p.Parent
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (p *Promise[TOut]) Resume() {
|
2024-07-25 10:49:02 +08:00
|
|
|
if debugAsync {
|
|
|
|
|
log.Printf("Resume task: %+v\n", p)
|
|
|
|
|
}
|
|
|
|
|
p.Exec.schedule(p)
|
2024-07-25 08:50:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (p *Promise[TOut]) Done() bool {
|
2024-07-25 08:50:49 +08:00
|
|
|
return p.Next == -1
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (p *Promise[TOut]) Call() {
|
|
|
|
|
p.Func()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
2024-07-24 01:01:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
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()
|
|
|
|
|
}
|
2024-07-27 15:08:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (p *Promise[TOut]) Value() TOut {
|
2024-07-27 15:08:09 +08:00
|
|
|
return p.value
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (p *Promise[TOut]) Chan() <-chan TOut {
|
2024-07-24 01:01:09 +08:00
|
|
|
if p.c == nil {
|
|
|
|
|
p.c = make(chan TOut, 1)
|
2024-07-28 17:56:30 +08:00
|
|
|
p.Func()
|
2024-07-24 01:01:09 +08:00
|
|
|
}
|
|
|
|
|
return p.c
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 17:56:30 +08:00
|
|
|
func (p *Promise[TOut]) Await() (ret TOut) {
|
2024-07-24 01:01:09 +08:00
|
|
|
panic("should not called")
|
|
|
|
|
}
|