package memory import ( "sync" ) /////////////////////////////////////////////////////////////////////////// // 轨旁仿真模型状态 type VerifyStatus struct { //道岔状态,key为道岔id即索引 state.SwitchState SwitchStateMap sync.Map //轨道状态,key为轨道id即索引 state.LinkState LinkStateMap sync.Map //列车状态,key为列车id即索引 state.TrainState TrainStateMap sync.Map //计轴区段状态,key为计轴区段的id即索引 state.SectionState AxleSectionStateMap sync.Map //物理区段状态,key为物理区段id即索引 state.SectionState PhysicalSectionStateMap sync.Map //逻辑区段状态,key为逻辑区段id即索引 state.SectionState LogicSectionStateMap sync.Map //信号机状态,key为信号机id,即索引 state.SignalState SignalStateMap sync.Map } // 轨旁仿真模型状态(变更) type ChangeVerifyStatus struct { VerifyStatus //删除的列车ID列表 RemoveTrainId []string } ////////////////////////////////////////////////////////////////////////// // 轨旁仿真内存模型 type WaysideMemory struct { //可变状态数据:轨旁仿真模型状态(全量数据) Status *VerifyStatus // 要变更的状态:用户操作过的状态记录在这里,增量推送次数据 ChangeStatus *ChangeVerifyStatus //状态保护锁 rwLock *sync.RWMutex } func NewWaysideMemory() *WaysideMemory { return &WaysideMemory{ Status: &VerifyStatus{}, ChangeStatus: &ChangeVerifyStatus{}, rwLock: &sync.RWMutex{}, } } // 状态读保护操作 func (me *WaysideMemory) SafeOptRead(opt func(memory *WaysideMemory) any) any { me.rwLock.RLock() defer me.rwLock.RUnlock() var rt any = opt(me) return rt } // 状态写保护操作 func (me *WaysideMemory) SafeOptWrite(opt func(memory *WaysideMemory) any) any { me.rwLock.Lock() defer me.rwLock.Unlock() var rt any = opt(me) return rt }