Lockless Task Scheduler  v1.0a
A lockless task scheduler
workertask.h
1 // ***********************************************************************
2 // Assembly : task_scheduler
3 // Author : viknash
4 // ***********************************************************************
5 // <copyright file="workertask.h" >
6 // Copyright (c) viknash. All rights reserved.
7 // </copyright>
8 // <summary></summary>
9 // ***********************************************************************
10 #pragma once
11 
12 #include <atomic>
13 #include <cinttypes>
14 #include <vector>
15 
16 #include "memory.h"
17 #include "meta.h"
18 #include "print.h"
19 #include "types.h"
20 #include "task.h"
21 namespace task_scheduler
22 {
23  template < class TMemInterface > class base_worker_task : public base_task<TMemInterface>
24  {
25  public:
28  typedef base_task<TMemInterface> super;
29 
34  base_worker_task(typename super::task_graph_type &_task_graph);
39 
40  protected:
44  bool run() override;
45 
46  private:
51  void run_internal(typename super::function_type* _work_function);
56  virtual thread_num_t get_recommended_num_workers() override;
57  };
58 
59  template < class TMemInterface >
61  : super(_task_graph)
62  {
63  }
64 
65  template < class TMemInterface >
67  {
68  }
69 
70  template < class TMemInterface >
72  {
73  typename super::function_type *work_function = nullptr;
74  int64_t last_num_runned = super::transient.num_runned;
75  while (!super::transient.work_queue->empty())
76  {
77  if (super::transient.work_queue->pop_front(work_function))
78  {
79  profile::instrument< void, worker_task_type, void (worker_task_type::*)(typename super::function_type*) >(super::transient.task_time, this, &worker_task_type::run_internal, work_function);
81  }
82  }
83  return super::transient.num_runned > last_num_runned ? true : false;
84  }
85 
86  template < class TMemInterface >
87  void base_worker_task< TMemInterface >::run_internal(typename super::function_type* _work_function)
88  {
89  (*_work_function)();
90  }
91 
92  template < class TMemInterface >
94  {
95  using namespace std;
96 
97  thread_num_t optimum_num_workers = thread_num_t(super::persistent.task_work.size() / super::transient.minimum_batch_size);
98  return min(optimum_num_workers, super::persistent.num_workers);
99  }
100 
101 }
102 
Definition: workertask.h:23
transient_container transient
The transient
Definition: task.h:259
profile::time task_time
Total time spent running all work functions in this task
Definition: task.h:134
Class stl_allocator.
Definition: allocator.h:16
base_worker_task(typename super::task_graph_type &_task_graph)
Initializes a new instance of the base_worker_task class.
Definition: workertask.h:60
std::atomic_int64_t num_runned
Total number of times work function was called
Definition: task.h:138
bool run() override
Callback to run task
Definition: workertask.h:71
uint32_t minimum_batch_size
Calculated minimum batch size
Definition: task.h:142
Class base_task.
Definition: task.h:44
Class base_task_graph.
Definition: task.h:35
~base_worker_task()
Finalizes an instance of the base_worker_task class.
Definition: workertask.h:66
persistent_container persistent
The persistent
Definition: task.h:263