Files
llgo/internal/runtime/z_chan.go

332 lines
5.9 KiB
Go
Raw Normal View History

2024-07-02 16:00:43 +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 runtime
import (
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/pthread/sync"
)
// -----------------------------------------------------------------------------
2024-07-02 20:23:48 +08:00
const (
2024-07-06 21:59:28 +08:00
chanNoSendRecv = 0
chanHasRecv = 1
2024-07-02 20:23:48 +08:00
)
2024-07-02 16:00:43 +08:00
type Chan struct {
mutex sync.Mutex
cond sync.Cond
data unsafe.Pointer
getp int
len int
cap int
sops []*selectOp
2024-07-07 16:03:41 +08:00
sends uint16
2024-07-02 20:23:48 +08:00
close bool
2024-07-02 16:00:43 +08:00
}
func NewChan(eltSize, cap int) *Chan {
ret := new(Chan)
if cap > 0 {
ret.data = AllocU(uintptr(cap * eltSize))
ret.cap = cap
}
ret.cond.Init(nil)
return ret
}
func ChanLen(p *Chan) (n int) {
2024-09-25 20:51:58 +08:00
if p == nil {
return 0
}
2024-07-02 16:00:43 +08:00
p.mutex.Lock()
n = p.len
p.mutex.Unlock()
return
}
func ChanCap(p *Chan) int {
2024-09-25 20:51:58 +08:00
if p == nil {
return 0
}
2024-07-02 16:00:43 +08:00
return p.cap
}
2024-07-06 21:59:28 +08:00
func notifyOps(p *Chan) {
for _, sop := range p.sops {
2024-07-06 21:59:28 +08:00
sop.notify()
}
}
2024-07-02 20:23:48 +08:00
func ChanClose(p *Chan) {
p.mutex.Lock()
p.close = true
2024-07-06 21:59:28 +08:00
notifyOps(p)
2024-07-02 20:23:48 +08:00
p.mutex.Unlock()
p.cond.Broadcast()
}
2024-07-02 16:00:43 +08:00
func ChanTrySend(p *Chan, v unsafe.Pointer, eltSize int) bool {
n := p.cap
p.mutex.Lock()
if n == 0 {
2024-07-06 21:59:28 +08:00
if p.getp != chanHasRecv || p.close {
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
return false
}
2024-07-06 21:59:28 +08:00
if p.data != nil {
c.Memcpy(p.data, v, uintptr(eltSize))
}
p.getp = chanNoSendRecv
2024-07-02 16:00:43 +08:00
} else {
2024-07-02 20:23:48 +08:00
if p.len == n || p.close {
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
return false
}
off := (p.getp + p.len) % n
c.Memcpy(c.Advance(p.data, off*eltSize), v, uintptr(eltSize))
p.len++
}
2024-07-06 21:59:28 +08:00
notifyOps(p)
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
p.cond.Broadcast()
return true
}
2024-07-02 20:23:48 +08:00
func ChanSend(p *Chan, v unsafe.Pointer, eltSize int) bool {
2024-07-02 16:00:43 +08:00
n := p.cap
p.mutex.Lock()
if n == 0 {
2024-07-06 21:59:28 +08:00
for p.getp != chanHasRecv && !p.close {
2024-07-07 16:03:41 +08:00
p.sends++
2024-07-02 16:00:43 +08:00
p.cond.Wait(&p.mutex)
2024-07-07 16:03:41 +08:00
p.sends--
2024-07-02 16:00:43 +08:00
}
2024-07-02 20:23:48 +08:00
if p.close {
p.mutex.Unlock()
return false
}
2024-07-06 21:59:28 +08:00
if p.data != nil {
c.Memcpy(p.data, v, uintptr(eltSize))
}
p.getp = chanNoSendRecv
2024-07-02 16:00:43 +08:00
} else {
for p.len == n {
p.cond.Wait(&p.mutex)
}
2024-07-02 20:23:48 +08:00
if p.close {
p.mutex.Unlock()
return false
}
2024-07-02 16:00:43 +08:00
off := (p.getp + p.len) % n
c.Memcpy(c.Advance(p.data, off*eltSize), v, uintptr(eltSize))
p.len++
}
2024-07-06 21:59:28 +08:00
notifyOps(p)
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
p.cond.Broadcast()
2024-07-02 20:23:48 +08:00
return true
2024-07-02 16:00:43 +08:00
}
2024-07-06 21:59:28 +08:00
func ChanTryRecv(p *Chan, v unsafe.Pointer, eltSize int) (recvOK bool, tryOK bool) {
2024-07-02 16:00:43 +08:00
n := p.cap
p.mutex.Lock()
if n == 0 {
2024-07-07 16:03:41 +08:00
if p.sends == 0 || p.getp == chanHasRecv || p.close {
tryOK = p.close
p.mutex.Unlock()
return
}
p.getp = chanHasRecv
p.data = v
2024-07-02 16:00:43 +08:00
} else {
if p.len == 0 {
2024-07-06 21:59:28 +08:00
tryOK = p.close
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
2024-07-06 21:59:28 +08:00
return
}
if v != nil {
c.Memcpy(v, c.Advance(p.data, p.getp*eltSize), uintptr(eltSize))
2024-07-02 16:00:43 +08:00
}
p.getp = (p.getp + 1) % n
p.len--
}
2024-07-06 21:59:28 +08:00
notifyOps(p)
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
p.cond.Broadcast()
2024-07-07 16:03:41 +08:00
if n == 0 {
p.mutex.Lock()
2024-07-07 17:23:21 +08:00
for p.getp == chanHasRecv && !p.close {
2024-07-07 16:03:41 +08:00
p.cond.Wait(&p.mutex)
}
2024-07-07 17:23:21 +08:00
recvOK = !p.close
2024-07-07 16:03:41 +08:00
tryOK = recvOK
p.mutex.Unlock()
} else {
recvOK, tryOK = true, true
}
return
2024-07-02 16:00:43 +08:00
}
2024-07-06 21:59:28 +08:00
func ChanRecv(p *Chan, v unsafe.Pointer, eltSize int) (recvOK bool) {
2024-07-02 16:00:43 +08:00
n := p.cap
p.mutex.Lock()
if n == 0 {
2024-07-06 21:59:28 +08:00
for p.getp == chanHasRecv && !p.close {
2024-07-02 16:00:43 +08:00
p.cond.Wait(&p.mutex)
}
2024-07-06 21:59:28 +08:00
if p.close {
p.mutex.Unlock()
return false
}
p.getp = chanHasRecv
p.data = v
2024-07-02 16:00:43 +08:00
} else {
for p.len == 0 {
2024-07-02 20:23:48 +08:00
if p.close {
p.mutex.Unlock()
return false
}
2024-07-02 16:00:43 +08:00
p.cond.Wait(&p.mutex)
}
2024-07-06 21:59:28 +08:00
if v != nil {
c.Memcpy(v, c.Advance(p.data, p.getp*eltSize), uintptr(eltSize))
}
2024-07-02 16:00:43 +08:00
p.getp = (p.getp + 1) % n
p.len--
}
2024-07-06 21:59:28 +08:00
notifyOps(p)
2024-07-02 16:00:43 +08:00
p.mutex.Unlock()
p.cond.Broadcast()
2024-07-06 21:59:28 +08:00
if n == 0 {
p.mutex.Lock()
for p.getp == chanHasRecv && !p.close {
p.cond.Wait(&p.mutex)
}
recvOK = !p.close
p.mutex.Unlock()
} else {
recvOK = true
}
return
}
// -----------------------------------------------------------------------------
type selectOp struct {
mutex sync.Mutex
cond sync.Cond
sem bool
2024-07-06 21:59:28 +08:00
}
func (p *selectOp) init() {
p.mutex.Init(nil)
p.cond.Init(nil)
p.sem = false
}
2024-07-27 06:58:40 +08:00
func (p *selectOp) end() {
p.mutex.Destroy()
p.cond.Destroy()
}
2024-07-06 21:59:28 +08:00
func (p *selectOp) notify() {
p.mutex.Lock()
p.sem = true
p.mutex.Unlock()
p.cond.Signal()
}
func (p *selectOp) wait() {
p.mutex.Lock()
if !p.sem {
p.cond.Wait(&p.mutex)
}
p.sem = false
p.mutex.Unlock()
}
// ChanOp represents a channel operation.
type ChanOp struct {
C *Chan
Val unsafe.Pointer
Size int32
Send bool
}
// TrySelect executes a non-blocking select operation.
func TrySelect(ops ...ChanOp) (isel int, recvOK, tryOK bool) {
for isel = range ops {
op := ops[isel]
if op.Send {
if tryOK = ChanTrySend(op.C, op.Val, int(op.Size)); tryOK {
return
}
} else {
if recvOK, tryOK = ChanTryRecv(op.C, op.Val, int(op.Size)); tryOK {
return
}
}
}
return
}
// Select executes a blocking select operation.
func Select(ops ...ChanOp) (isel int, recvOK bool) {
selOp := new(selectOp) // TODO(xsw): use c.AllocaNew[selectOp]()
selOp.init()
for _, op := range ops {
prepareSelect(op.C, selOp)
}
var tryOK bool
for {
if isel, recvOK, tryOK = TrySelect(ops...); tryOK {
break
}
selOp.wait()
}
for _, op := range ops {
endSelect(op.C, selOp)
}
2024-07-27 06:58:40 +08:00
selOp.end()
2024-07-06 21:59:28 +08:00
return
}
func prepareSelect(c *Chan, selOp *selectOp) {
c.mutex.Lock()
c.sops = append(c.sops, selOp)
2024-07-06 21:59:28 +08:00
c.mutex.Unlock()
}
func endSelect(c *Chan, selOp *selectOp) {
c.mutex.Lock()
for i, op := range c.sops {
if op == selOp {
c.sops = append(c.sops[:i], c.sops[i+1:]...)
break
}
2024-07-06 21:59:28 +08:00
}
c.mutex.Unlock()
2024-07-02 16:00:43 +08:00
}
// -----------------------------------------------------------------------------