86 lines
1.4 KiB
Go
86 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type GManager struct {
|
|
sync.WaitGroup
|
|
tlock sync.RWMutex
|
|
tasks map[string]*GTask
|
|
}
|
|
|
|
const (
|
|
GMSG_EXIT string = "GManager.Exit"
|
|
)
|
|
|
|
func (my *GManager) Init() *GManager {
|
|
my.tasks = make(map[string]*GTask)
|
|
return my
|
|
}
|
|
|
|
func (my *GManager) RegistTask(task *GTask, name string) {
|
|
my.tlock.Lock()
|
|
defer my.tlock.Unlock()
|
|
my.tasks[name] = task
|
|
}
|
|
|
|
func (my *GManager) CreateTask(name string, qsize int) *GTask {
|
|
t := new(GTask).Init(my, qsize)
|
|
t.GManager = my
|
|
my.RegistTask(t, name)
|
|
|
|
return t
|
|
}
|
|
|
|
func (my *GManager) Enter() {
|
|
my.WaitGroup.Add(1)
|
|
}
|
|
|
|
func (my *GManager) Exit() {
|
|
my.WaitGroup.Done()
|
|
}
|
|
|
|
func (my *GManager) Join() {
|
|
my.WaitGroup.Wait()
|
|
}
|
|
|
|
func (my *GManager) Broadcast(event interface{}) {
|
|
my.tlock.RLock()
|
|
defer my.tlock.RUnlock()
|
|
// Last to first.
|
|
for name := range my.tasks {
|
|
t := my.tasks[name]
|
|
t.EnQueueSync(event)
|
|
}
|
|
}
|
|
|
|
func (my *GManager) BroadcastWithout(event interface{}, without string) {
|
|
my.tlock.RLock()
|
|
defer my.tlock.RUnlock()
|
|
// Last to first.
|
|
for name := range my.tasks {
|
|
if without == name {
|
|
continue
|
|
}
|
|
t := my.tasks[name]
|
|
t.EnQueueSync(event)
|
|
}
|
|
}
|
|
|
|
func (my *GManager) ReqExit() {
|
|
my.Broadcast(GMSG_EXIT)
|
|
}
|
|
|
|
func (my *GManager) ReqTaskExit(name string) {
|
|
t, f := my.tasks[name]
|
|
if f {
|
|
t.EnQueueSync(GMSG_EXIT)
|
|
}
|
|
}
|
|
|
|
func (my *GManager) GetQueue(name string) (GQueue, bool) {
|
|
t, f := my.tasks[name]
|
|
return t.GQueue, f
|
|
}
|