1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package x
import (
"container/list"
"fmt"
"os"
"sync"
)
type in struct {
requestExpected SeqNum
requestRead SeqNum
requestCompleted SeqNum
currentReply *list.List
replies map[SeqNum]*list.List
pendingReplies *list.List
readers *list.List
events *list.List
eventsCond *sync.Cond
eventChans []chan<- GenericEvent
chansMu sync.Mutex
}
func (in *in) init(ioMu *sync.Mutex) {
in.replies = make(map[SeqNum]*list.List)
in.pendingReplies = list.New()
in.readers = list.New()
in.events = list.New()
in.eventsCond = sync.NewCond(ioMu)
}
func (in *in) addEventChan(eventChan chan<- GenericEvent) {
in.chansMu.Lock()
for _, ch := range in.eventChans {
if ch == eventChan {
// exist
in.chansMu.Unlock()
return
}
}
// COW
newEventChans := make([]chan<- GenericEvent, len(in.eventChans)+1)
copy(newEventChans, in.eventChans)
newEventChans[len(newEventChans)-1] = eventChan
in.eventChans = newEventChans
in.chansMu.Unlock()
}
func (in *in) removeEventChan(eventChan chan<- GenericEvent) {
in.chansMu.Lock()
chans := in.eventChans
var found bool
for _, ch := range chans {
if ch == eventChan {
found = true
break
}
}
if !found {
// not found
in.chansMu.Unlock()
return
}
// COW
newEventChans := make([]chan<- GenericEvent, 0, len(in.eventChans)-1)
for _, ch := range chans {
if ch != eventChan {
newEventChans = append(newEventChans, ch)
}
}
in.eventChans = newEventChans
in.chansMu.Unlock()
}
func (in *in) addEvent(e GenericEvent) {
logPrintln("add event", e)
if in.events.Len() > 0xfff {
_, _ = fmt.Fprintf(os.Stderr, "<warning> too many events are not processed, len: %d\n",
in.events.Len())
}
in.events.PushBack(e)
in.eventsCond.Signal()
}
func (in *in) sendEvent(e GenericEvent) {
in.chansMu.Lock()
eventChans := in.eventChans
in.chansMu.Unlock()
for _, ch := range eventChans {
ch <- e
}
}
func (in *in) closeEventChans() {
in.chansMu.Lock()
for _, ch := range in.eventChans {
close(ch)
}
in.chansMu.Unlock()
}
type ReplyReader struct {
request SeqNum
cond *sync.Cond
}
func (in *in) insertNewReader(request SeqNum, cond *sync.Cond) *ReplyReader {
r := &ReplyReader{
request: request,
cond: cond,
}
var mark *list.Element
l := in.readers
for e := l.Front(); e != nil; e = e.Next() {
reader := e.Value.(*ReplyReader)
if reader.request >= request {
mark = e
break
}
}
if mark != nil {
logPrintf("insertNewReader %d before %d\n", request, mark.Value.(*ReplyReader).request)
l.InsertBefore(r, mark)
} else {
logPrintf("insertNewReader %d at end\n", request)
l.PushBack(r)
}
return r
}
func (in *in) removeReader(r *ReplyReader) {
l := in.readers
for e := l.Front(); e != nil; e = e.Next() {
reader := e.Value.(*ReplyReader)
if reader.request == r.request {
logPrintln("remove reader", reader.request)
l.Remove(e)
break
}
}
}
func (in *in) removeFinishedReaders() {
l := in.readers
e := l.Front()
for e != nil {
reader := e.Value.(*ReplyReader)
if reader.request <= in.requestCompleted {
reader.cond.Signal()
logPrintln("remove finished reader", reader.request)
tmp := e
e = e.Next()
l.Remove(tmp)
} else {
break
}
}
}
func (in *in) wakeUpAllReaders() {
l := in.readers
for e := l.Front(); e != nil; e = e.Next() {
reader := e.Value.(*ReplyReader)
reader.cond.Signal()
}
}
func (in *in) wakeUpNextReader() {
if in.readers.Front() != nil {
reader := in.readers.Front().Value.(*ReplyReader)
logPrintln("wake up next reader", reader.request)
reader.cond.Signal()
}
}
type PendingReply struct {
firstRequest SeqNum
lastRequest SeqNum
workaround uint
flags uint
}
func (in *in) expectReply(request SeqNum, workaround uint, flags uint) {
pend := &PendingReply{
firstRequest: request,
lastRequest: request,
workaround: workaround,
flags: flags,
}
in.pendingReplies.PushBack(pend)
}
func (in *in) removeFinishedPendingReplies() {
l := in.pendingReplies
e := l.Front()
for e != nil {
pend := e.Value.(*PendingReply)
if pend.lastRequest <= in.requestCompleted {
// remove pend from list
tmp := e
e = e.Next()
l.Remove(tmp)
} else {
break
}
}
}
|