1515#include < shared_mutex>
1616#include < vector>
1717
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
2122struct TimeTaskData {
2223 uint64 task;
2324 script::Global<Function> func;
@@ -103,7 +104,7 @@ int NewTimeout(Local<Function> func, vector<Local<Value>> paras, int timeout) {
103104 EngineScope scope (engine);
104105 TimeTaskData taskData;
105106 {
106- std::unique_lock<std::shared_mutex> lock (locker);
107+ std::lock_guard lock (locker);
107108
108109 auto t = timeTaskMap.find (id);
109110 if (t == timeTaskMap.end ()) return ;
@@ -127,7 +128,7 @@ int NewTimeout(Local<Function> func, vector<Local<Value>> paras, int timeout) {
127128 }
128129 )
129130 ->getId ();
130- std::unique_lock<std::shared_mutex> lock (locker);
131+ std::lock_guard lock (locker);
131132 data.swap (timeTaskMap[tid]);
132133 return tid;
133134}
@@ -149,7 +150,7 @@ int NewTimeout(Local<String> func, int timeout) {
149150 EngineScope scope (engine);
150151 TimeTaskData taskData;
151152 {
152- std::unique_lock<std::shared_mutex> lock (locker);
153+ std::lock_guard lock (locker);
153154
154155 auto t = timeTaskMap.find (id);
155156 if (t == timeTaskMap.end ()) return ;
@@ -166,7 +167,7 @@ int NewTimeout(Local<String> func, int timeout) {
166167 )
167168 ->getId ();
168169
169- std::unique_lock<std::shared_mutex> lock (locker);
170+ std::lock_guard lock (locker);
170171 data.swap (timeTaskMap[tid]);
171172 return tid;
172173}
@@ -193,7 +194,7 @@ int NewInterval(Local<Function> func, vector<Local<Value>> paras, int timeout) {
193194 Local<Value> func = Local<Value>();
194195 vector<Local<Value>> args;
195196 {
196- std::unique_lock<std::shared_mutex> lock (locker);
197+ std::lock_guard lock (locker);
197198
198199 auto t = timeTaskMap.find (id);
199200 if (t == timeTaskMap.end ()) return ;
@@ -218,7 +219,7 @@ int NewInterval(Local<Function> func, vector<Local<Value>> paras, int timeout) {
218219 )
219220 ->getId ();
220221
221- std::unique_lock<std::shared_mutex> lock (locker);
222+ std::lock_guard lock (locker);
222223 data.swap (timeTaskMap[tid]);
223224 return tid;
224225}
@@ -243,7 +244,7 @@ int NewInterval(Local<String> func, int timeout) {
243244 EngineScope scope (engine);
244245 std::string code;
245246 {
246- std::unique_lock<std::shared_mutex> lock (locker);
247+ std::lock_guard lock (locker);
247248
248249 auto t = timeTaskMap.find (id);
249250 if (t == timeTaskMap.end ()) return ;
@@ -259,16 +260,16 @@ int NewInterval(Local<String> func, int timeout) {
259260 )
260261 ->getId ();
261262
262- std::unique_lock<std::shared_mutex> lock (locker);
263+ std::lock_guard lock (locker);
263264 data.swap (timeTaskMap[tid]);
264265 return tid;
265266}
266267
267268bool ClearTimeTask (int id) {
268269 assert (EngineScope::currentEngine () != nullptr );
269270 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);
272273 if (it != timeTaskMap.end ()) {
273274 taskScheduler.remove (timeTaskMap[id].task );
274275 timeTaskMap.erase (id);
@@ -285,7 +286,7 @@ void LLSERemoveTimeTaskData(ScriptEngine* engine) {
285286 // enter scope to prevent script::Global::~Global() from crashing
286287 EngineScope enter (engine);
287288 try {
288- std::unique_lock<std::shared_mutex> lock (locker);
289+ std::lock_guard lock (locker);
289290 for (auto it = timeTaskMap.begin (); it != timeTaskMap.end ();) {
290291 if (it->second .engine == engine) {
291292 taskScheduler.remove (it->second .task );
0 commit comments