Files
llgo/x/io/io.go

160 lines
2.9 KiB
Go
Raw Normal View History

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"
"time"
)
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 {
Resume()
Call()
Done() bool
}
2024-07-23 15:58:37 +08:00
type AsyncCall[OutT any] interface {
2024-07-25 08:50:49 +08:00
Call()
2024-07-23 15:58:37 +08:00
Await(timeout ...time.Duration) (ret OutT, err error)
Chan() <-chan OutT
2024-07-25 08:50:49 +08:00
Done() bool
2024-07-22 19:34:49 +08:00
}
2024-07-25 08:50:49 +08:00
type executor struct {
ac asyncCall
mu sync.Mutex
cond *sync.Cond
susp bool
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 08:50:49 +08:00
func (e *executor) Resume() {
e.mu.Lock()
defer e.mu.Unlock()
e.susp = false
e.cond.Signal()
2024-07-24 01:01:09 +08:00
}
2024-07-25 08:50:49 +08:00
func Run[OutT any](ac AsyncCall[OutT]) (OutT, error) {
e := newExecutor()
p := ac.(*PromiseImpl[OutT])
p.Exec = e
2024-07-24 01:01:09 +08:00
2024-07-25 08:50:49 +08:00
for {
e.mu.Lock()
for e.susp {
e.cond.Wait()
}
e.mu.Unlock()
e.susp = true
if ac.Done() {
return p.Value, p.Err
}
ac.Call()
}
2024-07-24 01:01:09 +08:00
}
2024-07-22 19:34:49 +08:00
// -----------------------------------------------------------------------------
2024-07-23 17:02:40 +08:00
type Promise[OutT any] func(OutT, error)
2024-07-23 15:58:37 +08:00
2024-07-23 17:02:40 +08:00
// llgo:link Promise.Await llgo.await
func (p Promise[OutT]) Await(timeout ...time.Duration) (ret OutT, err error) {
2024-07-23 15:58:37 +08:00
return
}
2024-07-25 08:50:49 +08:00
func (p Promise[OutT]) Call() {
}
2024-07-23 17:02:40 +08:00
func (p Promise[OutT]) Chan() <-chan OutT {
2024-07-23 15:58:37 +08:00
return nil
}
2024-07-22 19:34:49 +08:00
2024-07-25 08:50:49 +08:00
func (p Promise[OutT]) Done() bool {
return false
2024-07-24 01:01:09 +08:00
}
// -----------------------------------------------------------------------------
type PromiseImpl[TOut any] struct {
Prev int
Next int
2024-07-25 08:50:49 +08:00
Exec *executor
Debug string
2024-07-24 01:01:09 +08:00
2024-07-25 08:50:49 +08:00
Func func(resolve func(TOut, error))
Err error
Value TOut
c chan TOut
2024-07-24 01:01:09 +08:00
}
func (p *PromiseImpl[TOut]) Resume() {
2024-07-25 08:50:49 +08:00
p.Exec.Resume()
}
func (p *PromiseImpl[TOut]) Done() bool {
return p.Next == -1
}
func (p *PromiseImpl[TOut]) Call() {
2024-07-24 01:01:09 +08:00
p.Func(func(v TOut, err error) {
2024-07-25 08:50:49 +08:00
if debugAsync {
log.Printf("Resolve task: %+v, %+v, %+v\n", p, v, err)
}
2024-07-24 01:01:09 +08:00
p.Value = v
p.Err = err
2024-07-25 08:50:49 +08:00
p.Resume()
2024-07-24 01:01:09 +08:00
})
}
func (p *PromiseImpl[TOut]) Chan() <-chan TOut {
if p.c == nil {
p.c = make(chan TOut, 1)
p.Func(func(v TOut, err error) {
p.Value = v
p.Err = err
p.c <- v
})
}
return p.c
}
func (p *PromiseImpl[TOut]) Await(timeout ...time.Duration) (ret TOut, err error) {
panic("should not called")
}
2024-07-22 19:34:49 +08:00
// -----------------------------------------------------------------------------