forked from scottkiss/grtm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grchannel.go
51 lines (45 loc) · 1.03 KB
/
grchannel.go
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
package grtm
import (
"fmt"
"math/rand"
"sync"
)
const (
STOP = "__P:"
)
type GoroutineChannel struct {
gid uint64
name string
msg chan string
fcmsg chan interface{}
}
type GoroutineChannelMap struct {
mutex sync.Mutex
grchannels map[string]*GoroutineChannel
}
func (m *GoroutineChannelMap) unregister(name string) error {
m.mutex.Lock()
defer m.mutex.Unlock()
if _, ok := m.grchannels[name]; !ok {
return fmt.Errorf("goroutine channel not find: %q", name)
}
delete(m.grchannels, name)
return nil
}
func (m *GoroutineChannelMap) register(name string) error {
gchannel := &GoroutineChannel{
gid: uint64(rand.Int63()),
name: name,
}
gchannel.msg = make(chan string, 100)
gchannel.fcmsg = make(chan interface{}, 100)
m.mutex.Lock()
defer m.mutex.Unlock()
if m.grchannels == nil {
m.grchannels = make(map[string]*GoroutineChannel)
} else if _, ok := m.grchannels[gchannel.name]; ok {
return fmt.Errorf("goroutine channel already defined: %q", gchannel.name)
}
m.grchannels[gchannel.name] = gchannel
return nil
}