Leviathan  0.8.0.0
Leviathan game engine
ThreadingManager.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2018 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "QueuedTask.h"
7 #include "TaskThread.h"
8 
9 #ifdef _WIN32
10 #include "WindowsInclude.h"
11 #endif //_WIN32
12 
13 #include <list>
14 #include <vector>
15 
16 #define DEFAULT_THREADS_PER_CORE 1
17 
18 namespace Leviathan {
19 
20 #ifdef LEVIATHAN_USING_OGRE
23 #endif // LEVIATHAN_USING_OGRE
24 
27 
28 #ifdef _WIN32
29 
30 void SetThreadName(TaskThread* thread, const std::string& name);
31 void SetThreadNameImpl(DWORD threadid, const std::string& name);
32 #else
33 void SetThreadName(TaskThread* thread, const std::string& name);
34 
35 #endif // _WIN32
36 
37 
39 class ThreadingManager : public ThreadSafe {
40  friend void RunTaskQueuerThread(ThreadingManager* manager);
41 
42 public:
43  DLLEXPORT ThreadingManager(int basethreadspercore = DEFAULT_THREADS_PER_CORE);
44  DLLEXPORT virtual ~ThreadingManager();
45 
47  DLLEXPORT virtual bool Init();
49  DLLEXPORT virtual bool CheckInit();
50 
53  DLLEXPORT virtual void Release();
54 
56  DLLEXPORT void QueueTask(std::shared_ptr<QueuedTask> task);
57 
62  DLLEXPORT bool RemoveFromQueue(std::shared_ptr<QueuedTask> task);
63 
69  DLLEXPORT void RemoveTasksFromQueue(std::vector<std::shared_ptr<QueuedTask>>& tasklist);
70 
74  {
75  QueueTask(std::shared_ptr<QueuedTask>(newdtask));
76  }
77 
80 
87 
88 
91 
92 
95 
96 
99  DLLEXPORT void SetDisallowRepeatingTasks(bool disallow);
100 
103  DLLEXPORT void SetDiscardConditionalTasks(bool discard);
104 
106  DLLEXPORT void NotifyTaskFinished(std::shared_ptr<QueuedTask> task);
107 
110 
113 
114  DLLEXPORT static ThreadingManager* Get();
115 
116 protected:
119 
121 
125 
128 
132 
133 
135  std::list<std::shared_ptr<QueuedTask>> WaitingTasks;
136  std::condition_variable_any TaskQueueNotify;
137  std::vector<std::shared_ptr<TaskThread>> UsableThreads;
138 
140  std::thread WorkQueueHandler;
141 
143 };
144 }
145 
146 #ifdef LEAK_INTO_GLOBAL
154 #endif
DLLEXPORT void WaitForWorkersToEmpty(Lock &guard)
Blocks until all threads are empty.
DLLEXPORT void SetDiscardConditionalTasks(bool discard)
Sets the task queuer to discard all conditional tasks.
virtual DLLEXPORT bool Init()
Sets up the work queue.
DLLEXPORT void FlushActiveThreads()
This function waits for all tasks to complete.
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
void RunTaskQueuerThread(ThreadingManager *manager)
DLLEXPORT void QueueTask(std::shared_ptr< QueuedTask > task)
Adds a task to the queue.
void SetThreadName(TaskThread *thread, const std::string &name)
Encapsulates a function that can later be ran in a free thread.
Definition: QueuedTask.h:82
Encapsulates a function that is ran after a time period.
Definition: QueuedTask.h:130
virtual DLLEXPORT ~ThreadingManager()
std::condition_variable_any TaskQueueNotify
DLLEXPORT void RemoveTasksFromQueue(std::vector< std::shared_ptr< QueuedTask >> &tasklist)
Removes specific tasks from the queue.
DLLEXPORT void WaitForAllTasksToFinish()
Blocks until all queued tasks are finished.
static DLLEXPORT ThreadingManager * Get()
DLLEXPORT ThreadingManager(int basethreadspercore=DEFAULT_THREADS_PER_CORE)
#define FORCE_INLINE
Definition: Include.h:95
std::vector< std::shared_ptr< TaskThread > > UsableThreads
DLLEXPORT void MakeThreadsWorkWithOgre()
Makes the threads work with Ogre.
Encapsulates a function that is ran after a time period and repeated certain amount.
Definition: QueuedTask.h:238
DLLEXPORT void NotifyQueuerThread()
Notifies the queuer thread to check task setting.
Manages delayed execution of functions through use of QueuedTask and subclasses.
Encapsulates a function that can later be ran in a free thread.
Definition: QueuedTask.h:34
std::list< std::shared_ptr< QueuedTask > > WaitingTasks
List of the tasks queued by the application.
Encapsulates a function that is ran after a time period.
Definition: QueuedTask.h:160
DLLEXPORT void RegisterOgreOnThread()
DLLEXPORT bool RemoveFromQueue(std::shared_ptr< QueuedTask > task)
Removes a task from the queue.
virtual DLLEXPORT bool CheckInit()
Checks has Init worked.
DLLEXPORT void NotifyTaskFinished(std::shared_ptr< QueuedTask > task)
Called by work threads when they are done.
#define DEFAULT_THREADS_PER_CORE
virtual DLLEXPORT void Release()
DLLEXPORT void SetDisallowRepeatingTasks(bool disallow)
Disallows repeating tasks to occur again.
DLLEXPORT void UnregisterOgreOnThread()
bool AllowRepeats
Can tasks be repeated.
friend void RunTaskQueuerThread(ThreadingManager *manager)
Encapsulates a function that can later be ran in a free thread.
Definition: QueuedTask.h:102
#define DLLEXPORT
Definition: Include.h:84
std::thread WorkQueueHandler
Thread used to set tasks to threads.
DLLEXPORT void UnregisterGraphics()
Must be called if MakeThreadsWorkWithOgre has been called, BEFORE releasing graphics.
static ThreadingManager * staticaccess
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT FORCE_INLINE void QueueTask(QueuedTask *newdtask)
Adds a task to the queue.
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18