15
15
#include < shared_mutex>
16
16
#include < vector>
17
17
18
- std::atomic_uint timeTaskId = 0 ;
19
- std::shared_mutex locker;
20
- ll::schedule::GameTickScheduler taskScheduler;
18
+ std::atomic_uint timeTaskId = 0 ;
19
+ std::mutex locker;
20
+ ll::schedule::GameTickAsyncScheduler
21
+ taskScheduler; // This should be GameTickScheduler or ServerTimeScheduler after fix dead lock problem
21
22
struct TimeTaskData {
22
23
uint64 task;
23
24
script::Global<Function> func;
@@ -103,7 +104,7 @@ int NewTimeout(Local<Function> func, vector<Local<Value>> paras, int timeout) {
103
104
EngineScope scope (engine);
104
105
TimeTaskData taskData;
105
106
{
106
- std::unique_lock<std::shared_mutex> lock (locker);
107
+ std::lock_guard lock (locker);
107
108
108
109
auto t = timeTaskMap.find (id);
109
110
if (t == timeTaskMap.end ()) return ;
@@ -127,7 +128,7 @@ int NewTimeout(Local<Function> func, vector<Local<Value>> paras, int timeout) {
127
128
}
128
129
)
129
130
->getId ();
130
- std::unique_lock<std::shared_mutex> lock (locker);
131
+ std::lock_guard lock (locker);
131
132
data.swap (timeTaskMap[tid]);
132
133
return tid;
133
134
}
@@ -149,7 +150,7 @@ int NewTimeout(Local<String> func, int timeout) {
149
150
EngineScope scope (engine);
150
151
TimeTaskData taskData;
151
152
{
152
- std::unique_lock<std::shared_mutex> lock (locker);
153
+ std::lock_guard lock (locker);
153
154
154
155
auto t = timeTaskMap.find (id);
155
156
if (t == timeTaskMap.end ()) return ;
@@ -166,7 +167,7 @@ int NewTimeout(Local<String> func, int timeout) {
166
167
)
167
168
->getId ();
168
169
169
- std::unique_lock<std::shared_mutex> lock (locker);
170
+ std::lock_guard lock (locker);
170
171
data.swap (timeTaskMap[tid]);
171
172
return tid;
172
173
}
@@ -193,7 +194,7 @@ int NewInterval(Local<Function> func, vector<Local<Value>> paras, int timeout) {
193
194
Local<Value> func = Local<Value>();
194
195
vector<Local<Value>> args;
195
196
{
196
- std::unique_lock<std::shared_mutex> lock (locker);
197
+ std::lock_guard lock (locker);
197
198
198
199
auto t = timeTaskMap.find (id);
199
200
if (t == timeTaskMap.end ()) return ;
@@ -218,7 +219,7 @@ int NewInterval(Local<Function> func, vector<Local<Value>> paras, int timeout) {
218
219
)
219
220
->getId ();
220
221
221
- std::unique_lock<std::shared_mutex> lock (locker);
222
+ std::lock_guard lock (locker);
222
223
data.swap (timeTaskMap[tid]);
223
224
return tid;
224
225
}
@@ -243,7 +244,7 @@ int NewInterval(Local<String> func, int timeout) {
243
244
EngineScope scope (engine);
244
245
std::string code;
245
246
{
246
- std::unique_lock<std::shared_mutex> lock (locker);
247
+ std::lock_guard lock (locker);
247
248
248
249
auto t = timeTaskMap.find (id);
249
250
if (t == timeTaskMap.end ()) return ;
@@ -259,16 +260,16 @@ int NewInterval(Local<String> func, int timeout) {
259
260
)
260
261
->getId ();
261
262
262
- std::unique_lock<std::shared_mutex> lock (locker);
263
+ std::lock_guard lock (locker);
263
264
data.swap (timeTaskMap[tid]);
264
265
return tid;
265
266
}
266
267
267
268
bool ClearTimeTask (int id) {
268
269
assert (EngineScope::currentEngine () != nullptr );
269
270
try {
270
- std::unique_lock<std::shared_mutex> lock (locker);
271
- auto it = timeTaskMap.find (id);
271
+ std::lock_guard lock (locker);
272
+ auto it = timeTaskMap.find (id);
272
273
if (it != timeTaskMap.end ()) {
273
274
taskScheduler.remove (timeTaskMap[id].task );
274
275
timeTaskMap.erase (id);
@@ -285,7 +286,7 @@ void LLSERemoveTimeTaskData(ScriptEngine* engine) {
285
286
// enter scope to prevent script::Global::~Global() from crashing
286
287
EngineScope enter (engine);
287
288
try {
288
- std::unique_lock<std::shared_mutex> lock (locker);
289
+ std::lock_guard lock (locker);
289
290
for (auto it = timeTaskMap.begin (); it != timeTaskMap.end ();) {
290
291
if (it->second .engine == engine) {
291
292
taskScheduler.remove (it->second .task );
0 commit comments