From be7812ed684addce6a8928489399cbed9ccc20e0 Mon Sep 17 00:00:00 2001 From: MahnoKropotkinvich Date: Mon, 23 Dec 2024 15:43:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=9C=A8=E6=9E=84=E5=BB=BAMC=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E7=8A=B6=E6=80=81=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- state.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 state.go diff --git a/state.go b/state.go new file mode 100644 index 0000000..885992f --- /dev/null +++ b/state.go @@ -0,0 +1,76 @@ +package main + +import ( + "reflect" + "runtime/debug" +) + +type MCState int + +const ( + STOPPED MCState = iota + RUNNING + BOOTING + WAITING + STOPPING +) +const QUERY = 114514 + +type CntEvent int + +const ( + INCREASE CntEvent = 1 + DECREASE CntEvent = -1 +) + +type StateEvent int + +const ( + INCOMING StateEvent = iota + EMPTY +) + +type QueryChan chan MCState + +var ( + state MCState = STOPPED + // we don't need mutex for cnt because with channel it's guaranteed to be processed sequently + CntChan = make(chan CntEvent) + eventChan = make(chan StateEvent) + // the channel used to pass channel to build directional communication + ChanChan = make(chan QueryChan) +) +var cnt int + +func InitState() { + go handleCnt() + go handleEvent() +} +func handleCnt() { + for { + cntEvent := <-CntChan + switch cntEvent { + case INCREASE: + cnt++ + if cnt == 1 { //which means it is 0->1 + eventChan <- INCOMING + } + case DECREASE: + cnt-- + if cnt < 0 { + debug.Stack() + panic("cnt processor was written wrong!") + } else if cnt == 0 { + eventChan <- EMPTY + } + } + } +} +func handleEvent() { //handles both write and read + var chanList []QueryChan + // TODO: create chanList vector + for { + + } + +}