-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "ll/api/thread/TickSyncTaskPool.h" | ||
#include "ll/api/memory/Hook.h" | ||
|
||
#include "mc/server/ServerLevel.h" | ||
#include "mc/world/level/Tick.h" | ||
|
||
namespace ll::thread { | ||
std::vector<std::function<void()>> TickSyncTaskPool::tasks; | ||
std::atomic_bool TickSyncTaskPool::hasTask; | ||
std::mutex TickSyncTaskPool::mutex; | ||
|
||
LL_TYPED_INSTANCE_HOOK(TickSyncTaskPoolWorker, HookPriority::Normal, ServerLevel, &ServerLevel::_subTick, void) { | ||
origin(); | ||
if (TickSyncTaskPool::hasTask) { | ||
std::lock_guard lock{TickSyncTaskPool::mutex}; | ||
for (auto& task : TickSyncTaskPool::tasks) { | ||
task(); | ||
} | ||
TickSyncTaskPool::tasks.clear(); | ||
TickSyncTaskPool::hasTask = false; | ||
} | ||
} | ||
|
||
void TickSyncTaskPool::notify() { | ||
static memory::HookAutoRegister<TickSyncTaskPoolWorker> reg{}; | ||
hasTask = true; | ||
} | ||
} // namespace ll::thread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <future> | ||
#include <memory> | ||
#include <mutex> | ||
#include <queue> | ||
#include <stdexcept> | ||
#include <thread> | ||
#include <vector> | ||
|
||
#include "ll/api/base/Macro.h" | ||
#include "ll/api/base/StdInt.h" | ||
|
||
namespace ll::thread { | ||
struct TickSyncTaskPoolWorker; | ||
class TickSyncTaskPool { | ||
friend TickSyncTaskPoolWorker; | ||
|
||
private: | ||
LLAPI static std::vector<std::function<void()>> tasks; | ||
LLAPI static std::atomic_bool hasTask; | ||
LLAPI static std::mutex mutex; | ||
|
||
LLAPI static void notify(); | ||
|
||
public: | ||
explicit TickSyncTaskPool(size_t) {} | ||
~TickSyncTaskPool() = default; | ||
|
||
template <class F, class... Args> | ||
decltype(auto) addTask(F&& f, Args&&... args) { | ||
|
||
auto task = std::move(std::make_shared<std::packaged_task<std::invoke_result_t<F, Args...>()>>( | ||
[f = std::forward<F>(f), args...] { return f(args...); } | ||
)); | ||
auto res = task->get_future(); | ||
{ | ||
std::lock_guard lock{mutex}; | ||
tasks.emplace_back([task] { (*task)(); }); | ||
} | ||
notify(); | ||
|
||
return res; | ||
} | ||
}; | ||
} // namespace ll::thread |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters