DPsim
ThreadScheduler.h
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #pragma once
10 
11 #include <dpsim/Scheduler.h>
12 
13 #include <thread>
14 #include <vector>
15 
16 namespace DPsim {
17 class ThreadScheduler : public Scheduler {
18 public:
19  ThreadScheduler(Int threads, String outMeasurementFile,
20  Bool useConditionVariable);
21  virtual ~ThreadScheduler();
22 
23  void step(Real time, Int timeStepCount);
24  virtual void stop();
25 
26 protected:
27  void finishSchedule(const Edges &inEdges);
28  void scheduleTask(int thread, CPS::Task::Ptr task);
29 
30  Int mNumThreads;
31 
32 private:
33  void doStep(Int scheduleIdx);
34  static void threadFunction(ThreadScheduler *sched, Int idx);
35 
36  String mOutMeasurementFile;
37  Barrier mStartBarrier;
38 
39  std::vector<std::thread> mThreads;
40 
41  std::vector<CPS::Task::List> mTempSchedules;
42  struct ScheduleEntry {
43  CPS::Task *task;
44  Counter endCounter;
45  std::vector<Counter *> reqCounters;
46  };
47  std::vector<ScheduleEntry *> mSchedules;
48 
49  Bool mJoining = false;
50  Real mTime = 0;
51  Int mTimeStepCount = 0;
52 };
53 } // namespace DPsim
Tasks to be defined by every component.
Definition: Task.h:25
std::unordered_map< CPS::Task::Ptr, std::deque< CPS::Task::Ptr > > Edges
Definition: Scheduler.h:31
virtual void stop()
Called on simulation stop to reliably clean up e.g. running helper threads.
void step(Real time, Int timeStepCount)
Performs a single simulation step.