-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththink.go
126 lines (107 loc) · 2.73 KB
/
think.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
package glua
/*
#include "c/think_queue.h"
#include "c/glua.h"
*/
import "C"
import (
"math/rand"
"strconv"
"sync"
"time"
)
// The usage for C as a Think callback is because CGO is slow, so we need to only call it ONLY when we need to.
var (
thinkQueue chan GoFunc
thinkFuncs []GoFunc
thinkFuncsMu sync.Mutex
)
func InitThinkQueue(L State) {
thinkQueue = make(chan GoFunc, 100) // Make a buffered channel for the think queue
thinkFuncs = make([]GoFunc, 0) // Make a slice for the think functions
thinkFuncsMu = sync.Mutex{}
C.reset_tasks_count()
L.GetGlobal("timer")
{
L.GetField(-1, "Create")
{
randomUniqueName := strconv.FormatInt(int64(rand.Int()), 10) + strconv.FormatInt(time.Now().UnixNano(), 10)
L.PushString("GoLuaThinkQueue" + randomUniqueName)
L.PushNumber(0) // Delay (0 = next frame)
L.PushNumber(0) // Repetitions (0 = infinite)
L.PushCFunc(C.think_queue_think)
}
L.TryCall(4, 0)
}
L.Pop()
}
//export thinkQueueProcess
func thinkQueueProcess(L State) {
count := 0
thinkFuncsMu.Lock()
snapshot := thinkFuncs[:len(thinkFuncs):len(thinkFuncs)]
thinkFuncsMu.Unlock()
toRemove := map[int]struct{}{}
for i, fn := range snapshot {
L.SetTop(0) // completely empty the lua stack
res, err := callGoFunc(L, fn) // we use callGoFunc to safely handle panics
if err != nil {
L.ErrorNoHalt(err.Error())
}
if res == 1 {
count++
toRemove[i] = struct{}{}
}
}
if len(toRemove) > 0 {
thinkFuncsMu.Lock()
newThinkFuncs := make([]GoFunc, 0, len(thinkFuncs)-len(toRemove))
for i, fn := range thinkFuncs {
if _, ok := toRemove[i]; !ok {
newThinkFuncs = append(newThinkFuncs, fn)
}
}
thinkFuncs = newThinkFuncs
thinkFuncsMu.Unlock()
}
loop:
for i := 0; i < 3; i++ { // Process 3 tasks per frame to prevent lag spikes
select {
case task := <-thinkQueue:
L.SetTop(0) // completely empty the lua stack
_, err := callGoFunc(L, task) // we use callGoFunc to safely handle panics
if err != nil {
L.ErrorNoHalt(err.Error())
}
count++
default:
// No more tasks to process
break loop
}
}
C.decrement_tasks_count_by(C.int(count))
}
func WaitLuaThink(fn GoFunc) {
if IS_STATE_OPEN.Load() == false {
return
}
thinkQueue <- fn
C.increment_tasks_count() // concurrent increment
}
// LuaThink is a function that will be called every frame
// This function is thread-safe
// Return 1 to stop calling the function
func LuaThink(fn GoFunc) {
if IS_STATE_OPEN.Load() == false {
return
}
thinkFuncsMu.Lock()
{
thinkFuncs = append(thinkFuncs, fn) // Add the function to the think functions
}
thinkFuncsMu.Unlock()
C.increment_tasks_count() // concurrent increment
}
func (L State) PollThinkQueue() {
thinkQueueProcess(L)
}