-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathergo.go
111 lines (101 loc) · 2.08 KB
/
ergo.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ergo
import (
"reflect"
"sync"
)
var pids = make(map[chan interface{}]([](chan interface{})))
var mutex = &sync.Mutex{}
// Spawn
func Spawn(f interface{}) (chan interface{}, *sync.WaitGroup) {
mutex.Lock()
defer mutex.Unlock()
pid := make(chan interface{})
wg := sync.WaitGroup{}
wg.Add(1)
fn := reflect.ValueOf(f)
fnType := fn.Type()
if fnType.Kind() != reflect.Func || fnType.NumIn() != 2 || fnType.NumOut() != 1 {
panic("Expected a binary function returning a single value")
}
go func() {
fn.Call([]reflect.Value{reflect.ValueOf(pid), reflect.ValueOf(&wg)})
Kill(pid)
wg.Done()
}()
pids[pid] = make([](chan interface{}), 0)
return pid, &wg
}
// SpawnLink
func SpawnLink(partner chan interface{}, f interface{}) (chan interface{}, *sync.WaitGroup) {
// register the new channel under a list for the parent channel
mutex.Lock()
v, ok := pids[partner]
if ok {
mutex.Unlock()
pid, wg := Spawn(f)
mutex.Lock()
pids[partner] = append(v, pid)
w, k := pids[pid]
if k {
pids[pid] = append(w, partner)
} else {
panic("Process pid does not exist")
}
mutex.Unlock()
return pid, wg
} else {
mutex.Unlock()
panic("Partner pid does not exist")
}
}
// Send
func Send(pid chan interface{}, message interface{}) {
// we could use a lock here, but that might not be a good idea
_, ok := pids[pid]
if ok {
pid <- message
} else {
//fmt.Println("Process pid does not exist")
}
}
// Receive
func Receive(pid chan interface{}, f func(bool, interface{}) int) bool {
select {
case message, ok := (<-pid):
if ok {
f(ok, message)
return true
} else {
// clean up and kill
Kill(pid)
f(ok, message)
return false
}
}
}
// Kill
func Kill(pid chan interface{}) bool {
mutex.Lock()
children, ok := pids[pid]
if ok {
close(pid)
delete(pids, pid)
mutex.Unlock()
for _, child := range children {
Kill(child)
}
return true
} else {
mutex.Unlock()
return false
}
}
// Monitor
// Noop
func Noop(interface{}) int {
return 0
}
// ListProcesses
func ListProcesses() map[chan interface{}]([](chan interface{})) {
return pids
}