Newton Dynamics  4.00
dThreadPool.h
1 /* Copyright (c) <2003-2019> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef _D_THREAD_POOL_H_
23 #define _D_THREAD_POOL_H_
24 
25 #include "dCoreStdafx.h"
26 #include "dTypes.h"
27 #include "dArray.h"
28 #include "dThread.h"
29 #include "dSyncMutex.h"
30 #include "dSemaphore.h"
31 #include "dClassAlloc.h"
32 
33 #define D_MAX_THREADS_COUNT 16
34 
35 #define D_LOCK_FREE_THREADS_POOL
36 
38 {
39  public:
41  {
42  }
43 
44  virtual ~dThreadPoolJob()
45  {
46  }
47 
48  const dInt32 GetThreadId() const
49  {
50  return m_threadIndex;
51  }
52 
53  virtual void Execute() = 0;
54 
55  private:
56  dInt32 m_threadIndex;
57  friend class dThreadPool;
58 };
59 
60 class dThreadPool: public dSyncMutex, public dThread
61 {
62  class dWorkerThread: public dClassAlloc, public dThread
63  {
64  public:
65  D_CORE_API dWorkerThread();
66  D_CORE_API virtual ~dWorkerThread();
67 
68  private:
69  void ExecuteJob(dThreadPoolJob* const job);
70  virtual void ThreadFunction();
71 
72  dThreadPoolJob* m_job;
73  dThreadPool* m_owner;
74  dInt32 m_threadIndex;
75  friend class dThreadPool;
76  };
77 
78 #ifdef D_LOCK_FREE_THREADS_POOL
79  class dThreadLockFreeUpdate: public dThreadPoolJob
80  {
81  public:
82  dThreadLockFreeUpdate()
84  ,m_job(nullptr)
85  ,m_begin(false)
86  ,m_joindInqueue(nullptr)
87  {
88  }
89 
90  virtual void Execute();
91  private:
93  dAtomic<bool> m_begin;
94  dAtomic<dInt32>* m_joindInqueue;
95  friend class dThreadPool;
96 
97  };
98 #endif
99 
100  public:
101  D_CORE_API dThreadPool(const char* const baseName);
102  D_CORE_API virtual ~dThreadPool();
103 
104  D_CORE_API dInt32 GetCount() const;
105  D_CORE_API void SetCount(dInt32 count);
106 
107  D_CORE_API void TickOne();
108  D_CORE_API void ExecuteJobs(dThreadPoolJob** const jobs);
109 
110  D_CORE_API void Begin();
111  D_CORE_API void End();
112 
113  private:
114  D_CORE_API virtual void Release();
115 
116  dSyncMutex m_sync;
117  dWorkerThread* m_workers;
118  dInt32 m_count;
119  char m_baseName[32];
120 
121 #ifdef D_LOCK_FREE_THREADS_POOL
122  dAtomic<dInt32> m_joindInqueue;
123  dThreadLockFreeUpdate m_lockFreeJobs[D_MAX_THREADS_COUNT];
124 #endif
125 };
126 
127 #endif
dThreadPool
Definition: dThreadPool.h:61
dAtomic< dThreadPoolJob * >
dClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: dClassAlloc.h:29
dThreadPoolJob
Definition: dThreadPool.h:38
dSyncMutex
Generic counting mutex for synchronization of thread jobs.
Definition: dSyncMutex.h:29
dThread
Definition: dThread.h:36