-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlua_yield.go
More file actions
49 lines (41 loc) · 930 Bytes
/
lua_yield.go
File metadata and controls
49 lines (41 loc) · 930 Bytes
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
package lua
import (
"errors"
"sync"
)
// #cgo LDFLAGS: -lluajit-5.1 -ldl -lm
//#include "glua.h"
import "C"
var (
yieldCache map[int64]*gLuaYieldContext
yieldRW sync.RWMutex
)
func init() {
yieldCache = make(map[int64]*gLuaYieldContext)
}
type gLuaYieldContext struct {
methodName string
args []interface{}
}
//yield method
func storeYieldContext(vm *C.struct_lua_State, methodName string, args ...interface{}) {
if vm == nil {
panic(errors.New("Invalid Lua VM"))
}
vmKey := generateLuaStateId(vm)
yieldRW.Lock()
defer yieldRW.Unlock()
yieldCache[vmKey] = &gLuaYieldContext{methodName: methodName, args: args}
}
func loadYieldContext(threadId int64) (*gLuaYieldContext, error) {
yieldRW.RLock()
defer func() {
delete(yieldCache, threadId)
yieldRW.RUnlock()
}()
target, ok := yieldCache[threadId]
if false == ok {
return nil, errors.New("Invalid Yield Contxt")
}
return target, nil
}