LkEngine 0.1.2
 
Loading...
Searching...
No Matches
ThreadManager.h
1#pragma once
2
4#if 0
5
6#include "LkEngine/Core/Thread.h"
7
8/* Windows Platform */
9#if defined(LK_PLATFORM_WINDOWS)
10# include "LkEngine/Platform/Windows/WindowsThread.h"
11namespace LkEngine { using TThread = LThread<LWindowsThread>; }
12#elif defined(LK_ENGINE_GCC) || defined(LK_ENGINE_CLANG)
13#error
14#endif
15
16namespace LkEngine {
17
18 struct FThreadStartArgs
19 {
20 bool bRunAfterCreation = false;
21 };
22
23 class LThreadManager
24 {
25 public:
26 ~LThreadManager() = default;
27
28 template<typename TCallable, typename... TArgs>
29 void CreateThread(const FThreadStartArgs& ThreadStartArgs, TCallable&& Func, TArgs&&... Args);
30
31 void UpdateThreads();
32
33 int GetThreadPoolSize() const
34 {
35 return static_cast<int>(ThreadPool.size());
36 }
37
38 void StartThread(const uint8_t ThreadIndex)
39 {
40 ThreadPool.at(ThreadIndex)->Run();
41 }
42
43 static void SubmitFunctionToThread(FThreadData& ThreadData, const std::function<void()> Function)
44 {
45 LK_CORE_DEBUG_TAG("ThreadManager", "Adding function to queue, new queue size: {}", ThreadData.CommandQueue.size() + 1);
46 ThreadData.CommandQueue.push(Function);
47 }
48
49 static LThreadManager& Get();
50
51 private:
52 std::vector<std::shared_ptr<TThread>> ThreadPool{};
53 };
54
55 template<typename TCallable, typename... TArgs>
56 inline void LThreadManager::CreateThread(const FThreadStartArgs& ThreadStartArgs, TCallable&& Function, TArgs&&... Args)
57 {
58 LK_CORE_DEBUG_TAG("ThreadManager", "Creating new thread, indexed={}", ThreadPool.size());
59
60 std::shared_ptr<TThread> Thread = std::make_shared<TThread>(std::forward<TCallable>(Function), std::forward<TArgs>(Args)...);
61 ThreadPool.push_back(Thread);
62
63 if (ThreadStartArgs.bRunAfterCreation)
64 {
65 Thread->Run();
66 }
67 }
68
69 static void Thread_SubmitCommand(FThreadData& ThreadData, const std::function<void()> Function)
70 {
71 ThreadData.CommandQueue.push(Function);
72 LK_CORE_DEBUG_TAG("ThreadManager", "Added command to command queue, new queue size: {}", ThreadData.CommandQueue.size());
73 }
74
75 static void ThreadFunction_TestCommandQueue(FThreadData& ThreadData)
76 {
77 std::queue<std::function<void()>>& CommandQueue = ThreadData.CommandQueue;
78
79 while (ThreadData.bIsRunning)
80 {
81 /* Handle commands if there are any. */
82 while (CommandQueue.size() > 0)
83 {
84 // LK_CORE_DEBUG_TAG("ThreadFunction_1", "Executing function in queue, indexed={}", CommandQueue.size());
85 std::function<void()> Function = CommandQueue.front();
86 Function();
87 CommandQueue.pop();
88 }
89 }
90 }
91
92}
93
94#endif
Definition Asset.h:11