Skip to content

Commit 5642218

Browse files
committed
fix: fix setTimeout dead lock completely
1 parent 1ffea04 commit 5642218

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

src/legacy/api/SystemAPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ClassDefine<void> SystemClassBuilder = defineClass("system")
2727
.function("newProcess", &SystemClass::newProcess)
2828
.build();
2929

30-
ll::schedule::GameTickScheduler systemScheduler;
30+
ll::schedule::GameTickAsyncScheduler systemScheduler;
3131
// From LiteLoaderBDSv2 llapi/utils/WinHelper.cpp
3232
bool NewProcess(
3333
const std::string& process,

src/legacy/engine/TimeTaskSystem.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
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
2122
struct 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

267268
bool 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);

src/legacy/main/NodeJsHelper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <uv/uv.h>
2222
#include <v8/v8.h>
2323

24-
ll::schedule::GameTickScheduler nodeScheduler;
24+
ll::schedule::GameTickAsyncScheduler nodeScheduler;
2525
using ll::chrono_literals::operator""_tick;
2626

2727
// pre-declare

0 commit comments

Comments
 (0)