Skip to content

Commit 930024a

Browse files
committed
fix: fix TimeTaskSystem.cpp
1 parent be656eb commit 930024a

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

src/legacy/engine/TimeTaskSystem.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@
33
#include "api/APIHelp.h"
44
#include "engine/EngineManager.h"
55
#include "engine/EngineOwnData.h"
6-
#include "engine/MessageSystem.h"
76
#include "ll/api/service/GamingStatus.h"
87
#include "ll/api/thread/ThreadPoolExecutor.h"
8+
#include "ll/api/coro/CoroTask.h"
99

10-
#include <chrono>
11-
#include <ll/api/chrono/GameChrono.h>
1210
#include <ll/api/service/ServerInfo.h>
13-
#include <map>
1411
#include <mutex>
15-
#include <shared_mutex>
1612
#include <vector>
1713

18-
std::atomic_uint timeTaskId = 0;
19-
std::mutex locker;
20-
ll::thread::ThreadPoolExecutor taskScheduler{"LSE_TASK", 1};
14+
std::atomic_uint timeTaskId = 0;
15+
std::mutex locker;
2116

2217
struct TimeTaskData {
2318
uint64 taskId;
@@ -63,25 +58,26 @@ int NewTimeout(Local<Function> func, std::vector<Local<Value>> paras, int timeou
6358
data.engine = EngineScope::currentEngine();
6459
for (auto& para : paras) data.paras.emplace_back(std::move(para));
6560

66-
taskScheduler.execute([timeout, tid, data = std::move(data)]() mutable {
67-
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
61+
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
62+
co_await std::chrono::milliseconds(timeout);
6863
try {
69-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) return;
64+
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine))
65+
co_return;
7066

7167
EngineScope scope(data.engine);
7268

7369
if (!data.func.isEmpty()) {
7470
std::vector<Local<Value>> args;
7571
for (auto& para : data.paras) {
76-
if (para.isEmpty()) return;
72+
if (para.isEmpty()) co_return;
7773
args.emplace_back(para.get());
7874
}
7975
data.func.get().call({}, args);
8076
}
8177
}
8278
TIMETASK_CATCH("setTimeout-Function");
8379
ClearTimeTask(tid);
84-
});
80+
}).launch(ll::thread::ThreadPoolExecutor::getDefault());
8581

8682
std::lock_guard lock(locker);
8783
timeTaskMap[tid] = std::move(data);
@@ -95,16 +91,17 @@ int NewTimeout(Local<String> func, int timeout) {
9591
data.code = func;
9692
data.engine = EngineScope::currentEngine();
9793

98-
taskScheduler.execute([timeout, tid, data = std::move(data)]() mutable {
99-
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
94+
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
95+
co_await std::chrono::milliseconds(timeout);
10096
try {
101-
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) return;
97+
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine))
98+
co_return;
10299

103100
EngineScope scope(data.engine);
104101
{
105102
std::lock_guard lock(locker);
106103
auto it = timeTaskMap.find(tid);
107-
if (it == timeTaskMap.end()) return;
104+
if (it == timeTaskMap.end()) co_return;
108105

109106
auto& taskData = it->second;
110107

@@ -118,7 +115,7 @@ int NewTimeout(Local<String> func, int timeout) {
118115
}
119116
}
120117
TIMETASK_CATCH("setTimeout-String");
121-
});
118+
}).launch(ll::thread::ThreadPoolExecutor::getDefault());
122119

123120
std::lock_guard lock(locker);
124121
timeTaskMap[tid] = std::move(data);
@@ -133,29 +130,29 @@ int NewInterval(Local<Function> func, std::vector<Local<Value>> paras, int timeo
133130
data.engine = EngineScope::currentEngine();
134131
for (auto& para : paras) data.paras.emplace_back(std::move(para));
135132

136-
taskScheduler.execute([timeout, tid, data = std::move(data)]() mutable {
133+
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
137134
while (true) {
138-
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
135+
co_await std::chrono::milliseconds(timeout);
139136
try {
140137
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping) || !EngineManager::isValid(data.engine)) {
141138
ClearTimeTask(tid);
142-
return;
139+
co_return;
143140
}
144141

145142
EngineScope scope(data.engine);
146143

147144
if (!data.func.isEmpty()) {
148145
std::vector<Local<Value>> args;
149146
for (auto& para : data.paras) {
150-
if (para.isEmpty()) return;
147+
if (para.isEmpty()) co_return;
151148
args.emplace_back(para.get());
152149
}
153150
data.func.get().call({}, args);
154151
}
155152
}
156153
TIMETASK_CATCH("setInterval-Function");
157154
}
158-
});
155+
}).launch(ll::thread::ThreadPoolExecutor::getDefault());
159156

160157
std::lock_guard lock(locker);
161158
timeTaskMap[tid] = std::move(data);
@@ -169,18 +166,18 @@ int NewInterval(Local<String> func, int timeout) {
169166
data.code = func;
170167
data.engine = EngineScope::currentEngine();
171168

172-
taskScheduler.execute([timeout, tid, data = std::move(data)]() mutable {
169+
ll::coro::keepThis([timeout, tid, data = std::move(data)]() -> ll::coro::CoroTask<> {
173170
while (true) {
174-
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
171+
co_await std::chrono::milliseconds(timeout);
175172
try {
176173
if ((ll::getGamingStatus() == ll::GamingStatus::Stopping)) {
177174
ClearTimeTask(tid);
178-
return;
175+
co_return;
179176
}
180177

181178
if (!EngineManager::isValid(data.engine)) {
182179
ClearTimeTask(tid);
183-
return;
180+
co_return;
184181
}
185182

186183
EngineScope scope(data.engine);
@@ -189,11 +186,11 @@ int NewInterval(Local<String> func, int timeout) {
189186
std::lock_guard lock(locker);
190187

191188
auto it = timeTaskMap.find(tid);
192-
if (it == timeTaskMap.end()) return;
189+
if (it == timeTaskMap.end()) co_return;
193190

194191
auto& taskData = it->second;
195192

196-
if (taskData.code.isEmpty()) return;
193+
if (taskData.code.isEmpty()) co_return;
197194
code = taskData.code.get().toString();
198195
}
199196
if (!code.empty()) {
@@ -202,7 +199,7 @@ int NewInterval(Local<String> func, int timeout) {
202199
}
203200
TIMETASK_CATCH("setInterval-String");
204201
}
205-
});
202+
}).launch(ll::thread::ThreadPoolExecutor::getDefault());
206203

207204
std::lock_guard lock(locker);
208205
timeTaskMap[tid] = std::move(data);

0 commit comments

Comments
 (0)