Lockless Task Scheduler  v1.0a
A lockless task scheduler
taskgraphhelper.h
1 // ***********************************************************************
2 // Assembly : task_scheduler
3 // Author : viknash
4 // ***********************************************************************
5 // <copyright file="taskgraphhelper.h" >
6 // Copyright (c) viknash. All rights reserved.
7 // </copyright>
8 // <summary></summary>
9 // ***********************************************************************
10 #pragma once
11 
12 #include <algorithm>
13 #include <cinttypes>
14 #include <fstream>
15 #include <iostream>
16 #include <iterator>
17 #include <set>
18 #include <sstream>
19 #include <unordered_map>
20 
21 #include "containers.h"
22 
23 namespace task_scheduler
24 {
25 
26  template < class TMemInterface > class base_thread_pool;
27 
28  template < class TMemInterface > class base_task_graph_helper : public TMemInterface
29  {
30  public:
33 
34  typedef std::basic_string< tchar_t, std::char_traits< tchar_t >, stl_allocator< tchar_t, TMemInterface > > string_type;
35  typedef std::vector< task_type *, stl_allocator< task_type *, TMemInterface > > task_vector;
37  typedef task_type *task_list;
38  typedef std::function< void(task_type *, void *&) > traversal_function_type;
40 
41  public:
46  base_task_graph_helper(task_graph_type &_task_graph);
47 
52  void load(string_type _file_name);
59  void setup_task(task_type *_task, uint32_t _task_file_field, string_type str);
60 
64  task_graph_type &task_graph;
65  };
66 
67  template < class TMemInterface >
69  : task_graph(_task_graph)
70  {
71  }
72 
73  template < class TMemInterface > void base_task_graph_helper< TMemInterface >::load(string_type _file_name)
74  {
75  using namespace std::placeholders;
76 
77  string_type line;
78  tifstream task_file(_file_name.c_str(), std::ios::in);
79  assert(task_file.is_open()); // File was not found
80  while (getline(task_file, line))
81  {
82  typedef std::basic_istringstream< tchar_t, std::char_traits< tchar_t >, stl_allocator< tchar_t, TMemInterface > >
83  istringstream_type;
84  istringstream_type iss(line);
85  string_type token;
86  auto new_task = new worker_task_type(task_graph);
87  unsigned int _task_file_field = 0;
88  while (getline(iss, token, _t(',')))
89  {
90  tcout << token << std::endl;
91  setup_task(new_task, _task_file_field, token);
92  _task_file_field++;
93  }
94  task_graph.debug.task_name_to_task.insert({new_task->debug.task_name, new_task});
95  task_graph.debug.task_list.push_back(new_task);
96  }
97 
98  for (auto &task : task_graph.debug.task_list)
99  {
100  for (auto &task_name : task->debug.dependent_task_names)
101  {
102  auto dependent_task = task_graph.debug.task_name_to_task.find(task_name);
103  if (dependent_task != task_graph.debug.task_name_to_task.end())
104  {
105  task->persistent.dependent_tasks.push_back(dependent_task->second);
106  dependent_task->second->persistent.parent_tasks.push_back(task);
107  }
108  else
109  {
110  tcout << "Cannot link" << task->debug.task_name << " to " << task_name << '\n';
111  }
112  }
113  }
114  }
115 
116  template < class TMemInterface >
117  void base_task_graph_helper< TMemInterface >::setup_task(task_type *_task, uint32_t _task_file_field,
118  string_type _str)
119  {
120  enum TaskFileField
121  {
122  TaskName,
123  TaskPriority,
124  DependentTask1,
125  DependentTask2,
126  DependentTask3,
127  DependentTask4,
128  DependentTask5,
129  };
130 
131  switch (_task_file_field)
132  {
133  case TaskName:
134  _task->debug.task_name = _str;
135  break;
136  case TaskPriority:
137  {
138  transform(_str.begin(), _str.end(), _str.begin(), ::toupper);
139  for (auto i = 0; i < task_type::num_priority; i++)
140  {
141  if (_str.compare(_task->debug.priority_to_string(
142  static_cast< typename task_type::priority_selector >(i))) == 0)
143  {
144  _task->persistent.task_priority = static_cast< typename task_type::priority_selector >(i);
145  }
146  }
147  break;
148  }
149  default:
150  _task->debug.dependent_task_names.push_back(_str);
151  break;
152  }
153  }
154 };
Definition: workertask.h:23
base_task_graph_helper(task_graph_type &_task_graph)
Initializes a new instance of the base_task_graph_helper class.
Definition: taskgraphhelper.h:68
task_graph_type & task_graph
The task graph
Definition: taskgraphhelper.h:64
Class stl_allocator.
Definition: allocator.h:16
void setup_task(task_type *_task, uint32_t _task_file_field, string_type str)
Setups the task.
Definition: taskgraphhelper.h:117
Definition: allocator.h:17
string_vector dependent_task_names
The dependent task names
Definition: task.h:98
task_vector task_list
The task list
Definition: taskgraph.h:152
priority_selector task_priority
The task priority
Definition: task.h:162
void load(string_type _file_name)
Loads the specified file name.
Definition: taskgraphhelper.h:73
const tchar_t * priority_to_string(priority_selector _priority) const
Priorities to string.
Definition: task.h:293
string_type task_name
The task name
Definition: task.h:94
debug_container debug
The debug
Definition: taskgraph.h:281
Class base_task.
Definition: task.h:44
Class base_task_graph.
Definition: task.h:35
debug_container debug
The debug
Definition: task.h:255
Definition: taskgraphhelper.h:28
Class base_thread_pool.
Definition: task.h:36
task_name_to_task_map task_name_to_task
The task name to task
Definition: taskgraph.h:148
persistent_container persistent
The persistent
Definition: task.h:263
priority_selector
Enum priority_selector
Definition: task.h:70