Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c372dc3

Browse files
authored
Get rid of std::function in AsyncTaskSched (#2522)
1 parent e1d0993 commit c372dc3

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

‎Client/mods/deathmatch/logic/luadefs/CLuaVectorGraphicDefs.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ CClientVectorGraphic* CLuaVectorGraphicDefs::SVGCreate(lua_State* luaVM, CVector
151151
{
152152
CLuaFunctionRef funcRef = luaFunctionRef.value();
153153

154-
CLuaShared::GetAsyncTaskScheduler()->PushTask<bool>(
154+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
155155
[funcRef, pVectorGraphic, strRawData] { return LoadFromData(funcRef.GetLuaVM(), pVectorGraphic, strRawData); },
156156
[funcRef](const bool didLoad) {
157157
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(funcRef.GetLuaVM());
@@ -186,7 +186,7 @@ CClientVectorGraphic* CLuaVectorGraphicDefs::SVGCreate(lua_State* luaVM, CVector
186186
CLuaFunctionRef funcRef = luaFunctionRef.value();
187187
std::string path = pathOrRawData.value();
188188

189-
CLuaShared::GetAsyncTaskScheduler()->PushTask<bool>(
189+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
190190
[funcRef, pFile, pVectorGraphic, path] {
191191
lua_State* luaVM = funcRef.GetLuaVM();
192192

@@ -248,7 +248,7 @@ bool CLuaVectorGraphicDefs::SVGSetDocumentXML(CClientVectorGraphic* pVectorGraph
248248
{
249249
CLuaFunctionRef funcRef = luaFunctionRef.value();
250250

251-
CLuaShared::GetAsyncTaskScheduler()->PushTask<bool>([pVectorGraphic, pXMLNode] { return SetDocument(pVectorGraphic, pXMLNode); },
251+
CLuaShared::GetAsyncTaskScheduler()->PushTask([pVectorGraphic, pXMLNode] { return SetDocument(pVectorGraphic, pXMLNode); },
252252
[funcRef](const bool didLoad) {
253253
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(funcRef.GetLuaVM());
254254
if (pLuaMain)
@@ -282,7 +282,7 @@ bool CLuaVectorGraphicDefs::SVGSetSize(CClientVectorGraphic* pVectorGraphic, CVe
282282
{
283283
CLuaFunctionRef funcRef = luaFunctionRef.value();
284284

285-
CLuaShared::GetAsyncTaskScheduler()->PushTask<bool>([pVectorGraphic, size] { return SetSize(pVectorGraphic, size); },
285+
CLuaShared::GetAsyncTaskScheduler()->PushTask([pVectorGraphic, size] { return SetSize(pVectorGraphic, size); },
286286
[funcRef](const bool didLoad) {
287287
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(funcRef.GetLuaVM());
288288
if (pLuaMain)

‎Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ std::variant<std::string, bool> CLuaCryptDefs::PasswordHash(lua_State* luaVM, st
108108
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
109109
if (pLuaMain)
110110
{
111-
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
111+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
112112
[password, salt = options["salt"], cost] {
113113
// Execute time-consuming task
114114
return SharedUtil::BcryptHash(password, salt, cost);
@@ -199,7 +199,7 @@ int CLuaCryptDefs::PasswordVerify(lua_State* luaVM)
199199
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
200200
if (pLuaMain)
201201
{
202-
CLuaShared::GetAsyncTaskScheduler()->PushTask<bool>(
202+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
203203
[password, hash] {
204204
// Execute time-consuming task
205205
return SharedUtil::BcryptVerify(password, hash);
@@ -253,7 +253,7 @@ std::variant<bool, CLuaMultiReturn<SString, SString>> CLuaCryptDefs::GenerateKey
253253
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
254254
if (pLuaMain)
255255
{
256-
CLuaShared::GetAsyncTaskScheduler()->PushTask<std::variant<KeyPair, SString>>(
256+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
257257
[size]() -> std::variant<KeyPair, SString> {
258258
// Execute time-consuming task
259259
try
@@ -346,7 +346,7 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
346346
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
347347
if (pLuaMain)
348348
{
349-
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
349+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
350350
[data, key] {
351351
// Execute time-consuming task
352352
SString result;
@@ -391,7 +391,7 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
391391
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
392392
if (pLuaMain)
393393
{
394-
CLuaShared::GetAsyncTaskScheduler()->PushTask<std::pair<SString, SString>>(
394+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
395395
[data, key] {
396396
std::pair<SString, SString> result;
397397
try
@@ -459,7 +459,7 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
459459
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
460460
if (pLuaMain)
461461
{
462-
CLuaShared::GetAsyncTaskScheduler()->PushTask<std::pair<SString, bool>>(
462+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
463463
[data, key] {
464464
try
465465
{
@@ -558,7 +558,7 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
558558
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
559559
if (pLuaMain)
560560
{
561-
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
561+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
562562
[data, key] {
563563
// Execute time-consuming task
564564
SString result;
@@ -611,7 +611,7 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
611611
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
612612
if (pLuaMain)
613613
{
614-
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
614+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
615615
[data, key, iv] {
616616
// Execute time-consuming task
617617
SString result;
@@ -678,7 +678,7 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
678678
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
679679
if (pLuaMain)
680680
{
681-
CLuaShared::GetAsyncTaskScheduler()->PushTask<std::pair<SString, bool>>(
681+
CLuaShared::GetAsyncTaskScheduler()->PushTask(
682682
[data, key] {
683683
try
684684
{

‎Shared/sdk/SharedUtil.AsyncTaskScheduler.h‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#pragma once
2+
3+
#include <version>
4+
5+
// Workaround MultiplayerSA including this header for whatever reason..
6+
#ifdef __cpp_lib_is_invocable
7+
#define HAS_ASYNC_TASK_SCHED
8+
29
#include <queue>
310
#include <functional>
411
#include <memory>
512
#include <thread>
613
#include <mutex>
14+
#include <type_traits>
715

816
namespace SharedUtil
917
{
@@ -24,17 +32,19 @@ namespace SharedUtil
2432
virtual void ProcessResult() = 0;
2533
};
2634

27-
template <typename ResultType>
35+
template <typename TaskFn, typename ReadyFn>
2836
struct STask : public SBaseTask
2937
{
30-
using TaskFunction_t = std::function<ResultType()>;
31-
using ReadyFunction_t = std::function<void(const ResultType&)>;
38+
using Result = std::invoke_result_t<TaskFn>;
3239

33-
TaskFunction_t m_TaskFunction;
34-
ReadyFunction_t m_ReadyFunction;
35-
ResultType m_Result;
40+
TaskFn m_TaskFunction;
41+
ReadyFn m_ReadyFunction;
42+
Result m_Result;
3643

37-
STask(const TaskFunction_t& taskFunc, const ReadyFunction_t& readyFunc) : m_TaskFunction(taskFunc), m_ReadyFunction(readyFunc) {}
44+
STask(TaskFn&& task, ReadyFn&& ready) :
45+
m_TaskFunction(std::move(task)),
46+
m_ReadyFunction(std::move(ready))
47+
{}
3848

3949
void Execute() override { m_Result = std::move(m_TaskFunction()); }
4050

@@ -60,13 +70,13 @@ namespace SharedUtil
6070
// taskFunc: Time-consuming function that is executed on the secondary thread (be aware of thread safety!)
6171
// readyFunc: Function that is called once the result is ready (called on the main thread)
6272
//
63-
template <typename ResultType>
64-
void PushTask(const std::function<ResultType()>& taskFunc, const std::function<void(const ResultType&)>& readyFunc)
73+
template <typename TaskFn, typename ReadyFn>
74+
void PushTask(TaskFn&& task, ReadyFn&& ready)
6575
{
66-
std::unique_ptr<SBaseTask> pTask{new STask<ResultType>{taskFunc, readyFunc}};
76+
std::unique_ptr<SBaseTask> pTask{new STask{std::move(task), std::move(ready)}};
6777

68-
std::lock_guard<std::mutex> lock{m_TasksMutex};
69-
m_Tasks.push(std::move(pTask));
78+
std::scoped_lock<std::mutex> lock{m_TasksMutex};
79+
m_Tasks.emplace(std::move(pTask));
7080
}
7181

7282
//
@@ -90,3 +100,4 @@ namespace SharedUtil
90100
std::mutex m_TaskResultsMutex;
91101
};
92102
} // namespace SharedUtil
103+
#endif

‎Shared/sdk/SharedUtil.AsyncTaskScheduler.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "SharedUtil.AsyncTaskScheduler.h"
22

3+
// Workaround MultiplayerSA including this header for whatever reason..
4+
#ifdef HAS_ASYNC_TASK_SCHED
5+
36
namespace SharedUtil
47
{
58
CAsyncTaskScheduler::CAsyncTaskScheduler(std::size_t numWorkers)
@@ -65,3 +68,4 @@ namespace SharedUtil
6568
}
6669
}
6770
} // namespace SharedUtil
71+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /