LCOV - code coverage report
Current view: top level - Src/Scheduler - SpPrioScheduler.hpp (source / functions) Hit Total Coverage
Test: Coverage example Lines: 24 25 96.0 %
Date: 2021-12-02 17:21:05 Functions: 6 6 100.0 %

          Line data    Source code
       1             : ///////////////////////////////////////////////////////////////////////////
       2             : // Spetabaru - Berenger Bramas MPCDF - 2017
       3             : // Under LGPL Licence, please you must read the LICENCE file.
       4             : ///////////////////////////////////////////////////////////////////////////
       5             : #ifndef SPPRIOSCHEDULER_HPP
       6             : #define SPPRIOSCHEDULER_HPP
       7             : 
       8             : #include <vector>
       9             : #include <queue>
      10             : #include <utility>
      11             : 
      12             : #include "Task/SpAbstractTask.hpp"
      13             : #include "Task/SpPriority.hpp"
      14             : #include "Utils/small_vector.hpp"
      15             : #include "Speculation/SpSpeculativeModel.hpp"
      16             : 
      17             : class SpPrioScheduler{
      18             :     struct ComparePrio{
      19        1127 :         bool operator()(const SpAbstractTask* lhs, const SpAbstractTask* rhs) const
      20             :         {
      21        1127 :             return lhs->getPriority() < rhs->getPriority();
      22             :         }
      23             :     };
      24             : 
      25             :     //! To protect the tasksReady list
      26             :     mutable std::mutex mutexReadyTasks;
      27             :     //! Contains the tasks that can be executed
      28             :     std::priority_queue<SpAbstractTask*, small_vector<SpAbstractTask*>, ComparePrio > tasksReady;
      29             :     
      30             :     std::atomic<int> nbReadyTasks;
      31             : 
      32             : public:
      33          52 :     explicit SpPrioScheduler() : mutexReadyTasks(), tasksReady(), nbReadyTasks(0) {
      34          52 :     }
      35             : 
      36             :     // No copy or move
      37             :     SpPrioScheduler(const SpPrioScheduler&) = delete;
      38             :     SpPrioScheduler(SpPrioScheduler&&) = delete;
      39             :     SpPrioScheduler& operator=(const SpPrioScheduler&) = delete;
      40             :     SpPrioScheduler& operator=(SpPrioScheduler&&) = delete;
      41             : 
      42        2772 :     int getNbTasks() const{
      43        2772 :         return nbReadyTasks;
      44             :     }
      45             : 
      46        1069 :     int push(SpAbstractTask* newTask){
      47        1069 :         std::unique_lock<std::mutex> locker(mutexReadyTasks);
      48        1069 :         nbReadyTasks++;
      49        1069 :         tasksReady.push(newTask);
      50        2138 :         return 1;
      51             :     }
      52             :     
      53          52 :     int pushTasks(small_vector_base<SpAbstractTask*>& tasks) {
      54          52 :         std::unique_lock<std::mutex> locker(mutexReadyTasks);
      55          52 :         nbReadyTasks += int(tasks.size());
      56          56 :         for(auto t : tasks) {
      57           4 :             tasksReady.push(t);
      58             :         }
      59         104 :         return int(tasks.size());
      60             :     }
      61             : 
      62        1073 :     SpAbstractTask* pop(){
      63        2146 :         std::unique_lock<std::mutex> locker(mutexReadyTasks);
      64        1073 :         if(tasksReady.size()){
      65        1073 :             nbReadyTasks--;
      66        1073 :             auto res = tasksReady.top();
      67        1073 :             tasksReady.pop();
      68        1073 :             return res;
      69             :         }
      70           0 :         return nullptr;
      71             :     }
      72             : };
      73             : 
      74             : 
      75             : #endif

Generated by: LCOV version 1.14