-
Notifications
You must be signed in to change notification settings - Fork 297
/
TaskScheduler.h
217 lines (129 loc) · 5.37 KB
/
TaskScheduler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, development version *
* (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef TaskScheduler_std_h__
#define TaskScheduler_std_h__
#include <MultiThreading/config.h>
#include "Tasks.h"
#include "Locks.h"
#include <thread>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <map>
#include <deque>
namespace sofa
{
namespace simulation
{
class TaskScheduler;
class WorkerThread;
class TasksAllocators;
class SOFA_MULTITHREADING_PLUGIN_API WorkerThread
{
public:
WorkerThread(TaskScheduler* const& taskScheduler);
~WorkerThread();
static WorkerThread* getCurrent();
// queue task if there is space, and run it otherwise
bool addTask(Task* pTask);
void workUntilDone(Task::Status* status);
Task::Status* getCurrentStatus() const {return _currentStatus;}
const std::thread::id getId();
const std::deque<Task*>* getTasksQueue() {return &_tasks;}
std::uint64_t getTaskCount() {return _tasks.size(); }
int GetWorkerIndex();
void* allocate();
void free(void* ptr);
private:
bool start(TaskScheduler* const& taskScheduler);
std::thread* create_and_attach(TaskScheduler* const&);
// queue task if there is space (or do nothing)
bool pushTask(Task* pTask);
// pop task from queue
bool popTask(Task** ppTask);
// steal and queue some task from another thread
bool stealTasks();
// give an idle thread some work
bool giveUpSomeWork(WorkerThread* pIdleThread);
void doWork(Task::Status* status);
// boost thread main loop
void run(void);
//void ThreadProc(void);
void Idle(void);
bool isFinished();
private:
enum
{
Max_TasksPerThread = 256
};
SpinLock _taskMutex;
std::deque<Task*> _tasks;
std::thread _stdThread;
Task::Status* _currentStatus;
TaskScheduler* _taskScheduler;
// The following members may be accessed by _multiple_ threads at the same time:
volatile bool _finished;
friend class TaskScheduler;
};
class SOFA_MULTITHREADING_PLUGIN_API TaskScheduler
{
enum
{
MAX_THREADS = 16,
STACKSIZE = 64*1024 /* 64K */,
};
public:
static TaskScheduler& getInstance();
void init(const unsigned int NbThread = 0);
bool isInitialized() { return _isInitialized; }
// void start(unsigned int NbThread);
void stop(void);
bool isClosing(void) const { return _isClosing; }
unsigned int getThreadCount(void) const { return _threadCount; }
void WaitForWorkersToBeReady();
void wakeUpWorkers();
static unsigned GetHardwareThreadsCount();
unsigned size() const;
const WorkerThread* getWorkerThread(const std::thread::id id);
private:
//static thread_local WorkerThread* _workerThreadIndex;
static std::map< std::thread::id, WorkerThread*> _threads;
Task::Status* _mainTaskStatus;
std::mutex _wakeUpMutex;
std::condition_variable _wakeUpEvent;
private:
TaskScheduler();
TaskScheduler(const TaskScheduler& ) {}
~TaskScheduler();
void start(unsigned int NbThread);
bool _isInitialized;
unsigned _workerThreadCount;
bool _workerThreadsIdle;
bool _isClosing;
unsigned _threadCount;
friend class WorkerThread;
};
SOFA_MULTITHREADING_PLUGIN_API bool runThreadSpecificTask(WorkerThread* pThread, const Task *pTask );
SOFA_MULTITHREADING_PLUGIN_API bool runThreadSpecificTask(const Task *pTask );
} // namespace simulation
} // namespace sofa
#endif // TaskScheduler_std_h__