1 //------------------------------------------------------------------------------
2 // emRenderThreadPool.h
3 //
4 // Copyright (C) 2016-2017 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20
21 #ifndef emRenderThreadPool_h
22 #define emRenderThreadPool_h
23
24 #ifndef emCoreConfig_h
25 #include <emCore/emCoreConfig.h>
26 #endif
27
28 #ifndef emThread_h
29 #include <emCore/emThread.h>
30 #endif
31
32
33 //==============================================================================
34 //============================= emRenderThreadPool =============================
35 //==============================================================================
36
37 class emRenderThreadPool : public emModel {
38
39 public:
40
41 // Class for a thread pool which can call a function concurrently.
42 // Mainly, this is used for speeding up rendering of graphics. The
43 // number of threads is the number of threads the hardware can run
44 // concurrently, but limited through emCoreConfig::MaxRenderThreads.
45
46 static emRef<emRenderThreadPool> Acquire(emRootContext & rootContext);
47 // Acquire the instance in a root context.
48
49 int GetThreadCount() const;
50 // Get number of threads. This includes the calling thread.
51
52 typedef void (*Func) (void * data, int index);
53 // Data type for a function to be called concurrently.
54
55 void CallParallel(Func func, void * data, int count);
56 // Call a function concurrently. This is equivalent to:
57 // for (int i = 0; i < count; i++) func(data, i);
58 // But parallelized best possible.
59
60 void CallParallel(Func func, void * data);
61 // Same as CallParallel(func,data,GetThreadCount());
62
63 protected:
64
65 emRenderThreadPool(emContext & context, const emString & name);
66 virtual ~emRenderThreadPool();
67
68 virtual bool Cycle();
69
70 private:
71
72 void UpdateThreadCount();
73 void CreateChildThreads(int count);
74 void DestroyChildThreads();
75
76 static int ChildThreadFunc(void * arg);
77 int ChildThreadRun();
78
79 emRef<emCoreConfig> CoreConfig;
80 emArray<emThread*> ChildThreads;
81 bool TerminateChildThreads;
82 Func CurrentFunc;
83 void * CurrentData;
84 int CurrentCount;
85 int CurrentStarted;
86 emThreadMiniMutex Mutex;
87 emThreadEvent ActivateEvent;
88 emThreadEvent DoneEvent;
89 };
90
GetThreadCount()91 inline int emRenderThreadPool::GetThreadCount() const
92 {
93 return ChildThreads.GetCount() + 1;
94 }
95
CallParallel(Func func,void * data)96 inline void emRenderThreadPool::CallParallel(Func func, void * data)
97 {
98 CallParallel(func,data,GetThreadCount());
99 }
100
101
102 #endif
103