goProject/.svn/pristine/a8/a83cd5ad09cf59aa3349a43bb74edf81e509216e.svn-base

63 lines
1.3 KiB
Plaintext
Raw Normal View History

2025-01-06 16:21:36 +08:00
package initMgr
import (
"fmt"
"sync"
"goutil/debugUtil"
"goutil/logUtil"
)
// 初始化成功对象
type InitSuccess struct {
name string
registerMap map[string]chan bool
mutex sync.RWMutex
}
// 注册需要被通知的对象
// name:唯一标识
// ch:用于通知的通道
func (this *InitSuccess) Register(name string, ch chan bool) {
this.mutex.Lock()
defer this.mutex.Unlock()
if _, exists := this.registerMap[name]; exists {
panic(fmt.Errorf("InitSuccess.Register-%s-%s已经存在请检查", this.name, name))
}
this.registerMap[name] = ch
logUtil.DebugLog(fmt.Sprintf("InitSuccess.Register-%s当前共有%d个注册", this.name, len(this.registerMap)))
}
// 取消启动成功通知注册
// name:唯一标识
func (this *InitSuccess) Unregister(name string) {
this.mutex.Lock()
defer this.mutex.Unlock()
delete(this.registerMap, name)
}
// 通知所有已注册的对象
func (this *InitSuccess) Notify() {
this.mutex.RLock()
defer this.mutex.RUnlock()
for name, ch := range this.registerMap {
ch <- true
msg := fmt.Sprintf("通知:%s-%s初始化成功", this.name, name)
debugUtil.Println(msg)
logUtil.DebugLog(msg)
}
}
// 创建初始化成功对象
func NewInitSuccess(name string) *InitSuccess {
return &InitSuccess{
name: name,
registerMap: make(map[string]chan bool),
}
}