1 #include "thread/Scheduler.h"
2 #include "math/MathUtils.h"
3 #include "objects/wrappers/Optional.h"
4
5 NAMESPACE_SPH_BEGIN
6
7 class SequentialTaskHandle : public ITask {
8 public:
wait()9 virtual void wait() override {
10 // when we return this handle, the task has been already finished, so we do not have to wait
11 }
12
completed() const13 virtual bool completed() const override {
14 // when we return this handle, the task has been already finished
15 return true;
16 }
17 };
18
submit(const Function<void ()> & task)19 SharedPtr<ITask> SequentialScheduler::submit(const Function<void()>& task) {
20 task();
21 return makeShared<SequentialTaskHandle>();
22 }
23
getThreadIdx() const24 Optional<Size> SequentialScheduler::getThreadIdx() const {
25 // imitating ThreadPool with 1 thread, so that we can use ThreadLocal with this scheduler
26 return 0;
27 }
28
getThreadCnt() const29 Size SequentialScheduler::getThreadCnt() const {
30 // imitating ThreadPool with 1 thread, so that we can use ThreadLocal with this scheduler
31 return 1;
32 }
33
getRecommendedGranularity() const34 Size SequentialScheduler::getRecommendedGranularity() const {
35 return 1;
36 }
37
parallelFor(const Size from,const Size to,const Size UNUSED (granularity),const RangeFunctor & functor)38 void SequentialScheduler::parallelFor(const Size from,
39 const Size to,
40 const Size UNUSED(granularity),
41 const RangeFunctor& functor) {
42 functor(from, to);
43 }
44
parallelInvoke(const Functor & func1,const Functor & func2)45 void SequentialScheduler::parallelInvoke(const Functor& func1, const Functor& func2) {
46 func1();
47 func2();
48 }
49
getGlobalInstance()50 SharedPtr<SequentialScheduler> SequentialScheduler::getGlobalInstance() {
51 static SharedPtr<SequentialScheduler> scheduler = makeShared<SequentialScheduler>();
52 return scheduler;
53 }
54
55 SequentialScheduler SEQUENTIAL;
56
57 NAMESPACE_SPH_END
58