DDraceNetwork Docs
jobs.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SHARED_JOBS_H
4#define ENGINE_SHARED_JOBS_H
5
6#include <base/lock.h>
7#include <base/system.h>
8
9#include <atomic>
10#include <deque>
11#include <memory>
12#include <vector>
13
19class IJob
20{
21 friend class CJobPool;
22
23public:
28 {
33
38
43
51 };
52
53private:
54 std::shared_ptr<IJob> m_pNext;
55 std::atomic<EJobState> m_State;
56 std::atomic<bool> m_Abortable;
57
58protected:
62 virtual void Run() = 0;
63
71 void Abortable(bool Abortable);
72
73public:
74 IJob();
75 virtual ~IJob();
76
77 IJob(const IJob &Other) = delete;
78 IJob &operator=(const IJob &Other) = delete;
79
89 EJobState State() const;
90
97 bool Done() const;
98
107 virtual bool Abort();
108
119 bool IsAbortable() const;
120};
121
128{
129 std::vector<void *> m_vpThreads;
130 std::atomic<bool> m_Shutdown;
131
134 std::shared_ptr<IJob> m_pFirstJob GUARDED_BY(m_Lock);
135 std::shared_ptr<IJob> m_pLastJob GUARDED_BY(m_Lock);
136
138 std::deque<std::shared_ptr<IJob>> m_RunningJobs GUARDED_BY(m_LockRunning);
139
140 static void WorkerThread(void *pUser) NO_THREAD_SAFETY_ANALYSIS;
142
143public:
144 CJobPool();
145 ~CJobPool();
146
154 void Init(int NumThreads) REQUIRES(!m_Lock);
155
163
172 void Add(std::shared_ptr<IJob> pJob) REQUIRES(!m_Lock);
173};
174#endif
Definition: jobs.h:128
CLock m_Lock
Definition: jobs.h:132
void Add(std::shared_ptr< IJob > pJob) REQUIRES(!m_Lock)
Definition: jobs.cpp:209
std::shared_ptr< IJob > m_pFirstJob GUARDED_BY(m_Lock)
static void WorkerThread(void *pUser) NO_THREAD_SAFETY_ANALYSIS
Definition: jobs.cpp:58
std::shared_ptr< IJob > m_pLastJob GUARDED_BY(m_Lock)
void RunLoop() NO_THREAD_SAFETY_ANALYSIS
Definition: jobs.cpp:63
void Shutdown() REQUIRES(!m_Lock) REQUIRES(!m_LockRunning)
Definition: jobs.cpp:149
SEMAPHORE m_Semaphore
Definition: jobs.h:133
void Init(int NumThreads) REQUIRES(!m_Lock)
Definition: jobs.cpp:129
std::deque< std::shared_ptr< IJob > > m_RunningJobs GUARDED_BY(m_LockRunning)
std::vector< void * > m_vpThreads
Definition: jobs.h:129
std::atomic< bool > m_Shutdown
Definition: jobs.h:130
CLock m_LockRunning
Definition: jobs.h:137
Definition: lock.h:88
Definition: jobs.h:20
virtual void Run()=0
bool IsAbortable() const
Definition: jobs.cpp:40
bool Done() const
Definition: jobs.cpp:20
EJobState
Definition: jobs.h:28
@ STATE_QUEUED
Definition: jobs.h:32
@ STATE_RUNNING
Definition: jobs.h:37
@ STATE_DONE
Definition: jobs.h:42
@ STATE_ABORTED
Definition: jobs.h:50
virtual ~IJob()
void Abortable(bool Abortable)
Definition: jobs.cpp:35
EJobState State() const
Definition: jobs.cpp:15
IJob()
Definition: jobs.cpp:6
virtual bool Abort()
Definition: jobs.cpp:26
std::atomic< bool > m_Abortable
Definition: jobs.h:56
std::atomic< EJobState > m_State
Definition: jobs.h:55
std::shared_ptr< IJob > m_pNext
Definition: jobs.h:54
IJob(const IJob &Other)=delete
IJob & operator=(const IJob &Other)=delete
#define REQUIRES(...)
Definition: lock.h:32
#define NO_THREAD_SAFETY_ANALYSIS
Definition: lock.h:71
sem_t SEMAPHORE
Definition: system.h:681