-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrent.go
165 lines (162 loc) · 4.64 KB
/
concurrent.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package builtin_libs
import (
"sync"
"time"
. "github.com/zgg-lang/zgg-go/runtime"
)
func libConcurrent(*Context) ValueObject {
lib := NewObject()
lib.SetMember("start", NewNativeFunction("start", func(c *Context, this Value, args []Value) Value {
if len(args) < 1 {
c.RaiseRuntimeError("concurrent.start: requires at least 1 argument")
return nil
}
callee, isCallable := args[0].(ValueCallable)
if !isCallable {
c.RaiseRuntimeError("concurrent.start: argument 0 must callable")
return nil
}
c.StartThread(callee, nil, args[1:])
return c.RetVal
}), nil)
lib.SetMember("all", NewNativeFunction("all", func(c *Context, this Value, args []Value) Value {
rv := make([]Value, len(args))
for i, arg := range args {
callee, isCallable := arg.(ValueCallable)
if !isCallable {
c.RaiseRuntimeError("concurrent.all: argument %d must callable", i)
return nil
}
c.StartThread(callee, nil, []Value{})
rv[i] = c.RetVal
}
for i, t := range rv {
await, ok := t.GetMember("await", c).(ValueCallable)
if ok {
c.Invoke(await, nil, NoArgs)
rv[i] = c.RetVal
}
}
return NewArrayByValues(rv...)
}), nil)
{
objMutex := NewClassBuilder("Mutex").
Constructor(func(c *Context, this ValueObject, args []Value) {
lock := new(sync.Mutex)
this.SetMember("__lock", NewGoValue(lock), c)
}).
Method("lock", func(c *Context, this ValueObject, args []Value) Value {
lockVal := this.GetMember("__lock", c)
lockVal.ToGoValue().(*sync.Mutex).Lock()
return Undefined()
}).
Method("unlock", func(c *Context, this ValueObject, args []Value) Value {
lockVal := this.GetMember("__lock", c)
lockVal.ToGoValue().(*sync.Mutex).Unlock()
return Undefined()
}).
Method("run", func(c *Context, this ValueObject, args []Value) Value {
switch len(args) {
case 1:
if !c.IsCallable(args[0]) {
c.RaiseRuntimeError("run: first argument must callable")
return nil
}
default:
c.RaiseRuntimeError("run: requires 1 argument(s)")
return nil
}
lockVal := this.GetMember("__lock", c).ToGoValue().(*sync.Mutex)
lockVal.Lock()
defer lockVal.Unlock()
c.Invoke(args[0], nil, NoArgs)
rv := c.RetVal
return rv
}).
Build()
lib.SetMember("Mutex", objMutex, nil)
}
{
objChan := NewClassBuilder("Chan").
Constructor(func(c *Context, this ValueObject, args []Value) {
var n ValueInt
EnsureFuncParams(c, "Chan.__init__", args,
ArgRuleOptional{"n", TypeInt, &n, NewInt(1)},
)
ch := make(chan Value, n.AsInt())
this.SetMember("__ch", NewGoValue(&ch), c)
}).
Method("send", func(c *Context, this ValueObject, args []Value) Value {
ch := this.GetMember("__ch", c).ToGoValue().(*chan Value)
(*ch) <- args[0]
return Undefined()
}).
Method("recv", func(c *Context, this ValueObject, args []Value) Value {
var n ValueFloat
EnsureFuncParams(c, "Chan.recv", args,
ArgRuleOptional{"timeout", TypeFloat, &n, NewFloat(-1)},
)
ch := this.GetMember("__ch", c).ToGoValue().(*chan Value)
timeout := n.Value()
if timeout < 0 {
return <-*ch
}
select {
case <-time.After(time.Duration(timeout) * time.Second):
return Undefined()
case v := <-*ch:
return v
}
}).
Build()
lib.SetMember("Chan", objChan, nil)
}
{
objLimiter := NewClassBuilder("Limiter").
Constructor(func(c *Context, this ValueObject, args []Value) {
if len(args) > 1 {
c.RaiseRuntimeError("Excutor.__init__: requires 0 or 1 argument(s)")
}
max := 1
if len(args) == 1 {
max = int(c.MustInt(args[0], "Limiter.__init__(maxConcurrent): maxConcurrent"))
if max < 1 {
c.RaiseRuntimeError("Limiter.__init__(maxConcurrent): maxConcurrent must > 0")
}
}
ch := make(chan bool, max)
this.SetMember("__ch", NewGoValue(&ch), c)
}).
Method("run", func(c *Context, this ValueObject, args []Value) Value {
switch len(args) {
case 1:
if !c.IsCallable(args[0]) {
c.RaiseRuntimeError("run: first argument must callable")
return nil
}
default:
c.RaiseRuntimeError("run: requires 1 argument(s)")
return nil
}
ch := this.GetMember("__ch", c).ToGoValue().(*chan bool)
*ch <- true
joinFunc := c.StartThread(args[0], nil, []Value{})
rv := c.RetVal
go func() {
joinFunc()
<-*ch
}()
return rv
}).
Method("wait", func(c *Context, this ValueObject, args []Value) Value {
ch := this.GetMember("__ch", c).ToGoValue().(*chan bool)
for len(*ch) > 0 {
time.Sleep(10 * time.Millisecond)
}
return Undefined()
}).
Build()
lib.SetMember("Limiter", objLimiter, nil)
}
return lib
}