Lockless Task Scheduler  v1.0a
A lockless task scheduler
profilebase.h
1 // ***********************************************************************
2 // Assembly : task_scheduler
3 // Author : viknash
4 // ***********************************************************************
5 // <copyright file="profilebase.h" >
6 // Copyright (c) viknash. All rights reserved.
7 // </copyright>
8 // <summary></summary>
9 // ***********************************************************************
10 #pragma once
11 #include "../platform.h"
12 
16 namespace task_scheduler
17 {
18  namespace profile
19  {
20  typedef std::chrono::time_point< std::chrono::high_resolution_clock > time_point;
21  typedef std::chrono::high_resolution_clock clock;
22  typedef std::chrono::microseconds time;
23 
27  struct timer
28  {
33  timer(std::chrono::microseconds &_elapsedTime)
34  : start(clock::now())
35  , elapsedTime(_elapsedTime)
36  {
37  }
38 
43  {
44  elapsedTime += std::chrono::duration_cast<std::chrono::microseconds>(clock::now() - start);
45  }
46 
50  time_point start;
54  time &elapsedTime;
55  };
56 
65  template < typename TReturnType, typename TClassType, typename TMemFunc, typename... TArgs >
66  TReturnType instrument(time &_profileTime, TClassType *_classType, TMemFunc _func, TArgs... _params)
67  {
68  timer profile_timer(_profileTime);
69  return (_classType->*_func)(std::forward< TArgs >(_params)...);
70  }
71 
76  {
77 
78  };
79 
80  class errors
81  {
82  public:
83  enum type {
84  threads = 0,
85  memory,
86  all
87  };
88 
89  virtual void suppress(enum type _error) = 0;
90  virtual void unsuppress(enum type _error) = 0;
91 
92  template<class TInterface, class TKey = TInterface>
93  friend TInterface* get();
94 
95  private:
96 
97  static errors* instance();
98  };
99 
100  template <class TErrorImplementation>
101  class error_stack : public errors
102  {
105 
106  public:
107 
108  void suppress(enum type _error) override
109  {
110  error_implementation.suppress(_error);
111  stack.push_front(_error);
112  }
113 
114  void unsuppress(enum type _error) override
115  {
116  enum type stack_item;
117  stack.pop_front(stack_item);
118  ts_assert(stack_item == _error);
119  error_implementation.unsuppress(_error);
120  }
121 
122  private:
123  stack_type stack;
124  memory_allocator_type dispenser;
125  TErrorImplementation error_implementation;
126  };
127 
128  class memory
129  {
130  public:
131  ts_declare_attribute_and_callbacks(memory, const tchar_t*, name);
132 
133  memory()
134  : ts_init_attribute_and_callbacks(memory, name, _t(""))
135  {}
136 
137  void suppress()
138  {
139  get<errors>()->suppress(errors::type::memory);
140  }
141 
142  void unsuppress()
143  {
144  get<errors>()->unsuppress(errors::memory);
145  }
146 
147  virtual void allocate_begin(size_t _size, bool _initialized) {
148  ts_unused(_size); ts_unused(_initialized);
149  }
150 
151  virtual void allocate_end(void** _memory_allocation, size_t _size, bool _initialized) {
152  ts_unused(_memory_allocation); ts_unused(_size); ts_unused(_initialized);
153  }
154 
155  template <class TKey>
156  static memory* instance();
157  };
158 
159 
160  void memory::set_name(const tchar_t* _name)
161  {
162  *&name = _name;
163  }
164 
165  const tchar_t* memory::get_name()
166  {
167  return (&name)->c_str();
168  }
169 
170  namespace thread
171  {
172 
173  inline void suppress()
174  {
175  get<errors>()->suppress(errors::threads);
176  }
177 
178  inline void unsuppress()
179  {
180  get<errors>()->unsuppress(errors::threads);
181  }
182 
183  }
184 
185  inline void suppress()
186  {
187  get<errors>()->suppress(errors::all);
188  }
189 
190  inline void unsuppress()
191  {
192  get<errors>()->unsuppress(errors::all);
193  }
194 
195  template <class TStringImplementation>
196  class basic_string : public TStringImplementation
197  {
198  public:
199 
200  basic_string(const tchar_t* _name)
201  : TStringImplementation(_name)
202  {
203  }
204 
205  typename TStringImplementation::handle& operator* ()
206  {
207  return TStringImplementation::operator*();
208  }
209 
210  };
211 
212  class domain
213  {
214  public:
215  typedef void* handle;
216 
217  ts_declare_attribute_and_callbacks(domain, const tchar_t*, name);
218 
219  domain()
220  : ts_init_attribute_and_callbacks(domain, name, _t(""))
221  , hdl(nullptr)
222  {}
223 
224  virtual handle operator* () { return hdl; }
225 
226  template <class TKey>
227  static domain* instance();
228  private:
229  handle hdl;
230  };
231 
232  void domain::set_name(const tchar_t* _name)
233  {
234  *&name = _name;
235  }
236 
237  const tchar_t* domain::get_name()
238  {
239  return (&name)->c_str();
240  }
241 
242  class function
243  {
244  typedef void* handle;
245  public:
246  template <class TFuncType>
247  function(TFuncType _func)
248  : function_ptr((void*)_func)
249  {
250  }
251 
252  typename handle& operator* ()
253  {
254  return function_ptr;
255  }
256 
257  private:
258  void* function_ptr;
259  };
260 
261 
262  }
263 
264 }
Definition: profilebase.h:242
Struct timer
Definition: profilebase.h:27
Class stl_allocator.
Definition: allocator.h:16
Definition: profilebase.h:212
time & elapsedTime
The elapsed time
Definition: profilebase.h:54
Definition: profilebase.h:101
Definition: profilebase.h:128
Class profile_mem_interface.
Definition: profilebase.h:75
Definition: profilebase.h:196
~timer()
Finalizes an instance of the timer class.
Definition: profilebase.h:42
Definition: profilebase.h:80
time_point start
The start
Definition: profilebase.h:50
timer(std::chrono::microseconds &_elapsedTime)
Initializes a new instance of the timer struct.
Definition: profilebase.h:33