File: scheduler.h

package info (click to toggle)
etlcpp 20.39.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 18,232 kB
  • sloc: cpp: 245,721; ansic: 10,254; sh: 1,481; asm: 301; python: 281; makefile: 24
file content (406 lines) | stat: -rw-r--r-- 12,226 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2017 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_SCHEDULER_INCLUDED
#define ETL_SCHEDULER_INCLUDED

#include "platform.h"
#include "vector.h"
#include "nullptr.h"
#include "error_handler.h"
#include "exception.h"
#include "task.h"
#include "type_traits.h"
#include "function.h"

#include <stdint.h>

namespace etl
{
  //***************************************************************************
  /// Base exception class for scheduler.
  //***************************************************************************
  class scheduler_exception : public etl::exception
  {
  public:

    scheduler_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
      : etl::exception(reason_, file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// 'No tasks' exception.
  //***************************************************************************
  class scheduler_no_tasks_exception : public etl::scheduler_exception
  {
  public:

    scheduler_no_tasks_exception(string_type file_name_, numeric_type line_number_)
      : etl::scheduler_exception(ETL_ERROR_TEXT("scheduler:no tasks", ETL_SCHEDULER_FILE_ID"A"), file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// 'Null tasks' exception.
  //***************************************************************************
  class scheduler_null_task_exception : public etl::scheduler_exception
  {
  public:

    scheduler_null_task_exception(string_type file_name_, numeric_type line_number_)
      : etl::scheduler_exception(ETL_ERROR_TEXT("scheduler:null task", ETL_SCHEDULER_FILE_ID"B"), file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// 'Too many tasks' exception.
  //***************************************************************************
  class scheduler_too_many_tasks_exception : public etl::scheduler_exception
  {
  public:

    scheduler_too_many_tasks_exception(string_type file_name_, numeric_type line_number_)
      : etl::scheduler_exception(ETL_ERROR_TEXT("scheduler:too many tasks", ETL_SCHEDULER_FILE_ID"C"), file_name_, line_number_)
    {
    }
  };

  //***************************************************************************
  /// Sequential Single.
  /// A policy the scheduler can use to decide what to do next.
  /// Only calls the task to process work once, if it has work to do.
  //***************************************************************************
  struct scheduler_policy_sequential_single
  {
    bool schedule_tasks(etl::ivector<etl::task*>& task_list)
    {
      bool idle = true;

      for (size_t index = 0UL; index < task_list.size(); ++index)
      {
        etl::task& task = *(task_list[index]);

        if (task.task_request_work() > 0)
        {
          task.task_process_work();
          idle = false;
        }
      }

      return idle;
    }
  };

  /// Typedef for backwards compatibility with miss-spelt struct name.
  ETL_DEPRECATED_REASON("Misspelt class name") typedef scheduler_policy_sequential_single scheduler_policy_sequencial_single;

  //***************************************************************************
  /// Sequential Multiple.
  /// A policy the scheduler can use to decide what to do next.
  /// Calls the task to process work until it reports that it has no more.
  //***************************************************************************
  struct scheduler_policy_sequential_multiple
  {
    bool schedule_tasks(etl::ivector<etl::task*>& task_list)
    {
      bool idle = true;

      for (size_t index = 0UL; index < task_list.size(); ++index)
      {
        etl::task& task = *(task_list[index]);

        while (task.task_request_work() > 0)
        {
          task.task_process_work();
          idle = false;
        }
      }

      return idle;
    }
  };

  /// Typedef for backwards compatibility with miss-spelt struct name.
  ETL_DEPRECATED typedef scheduler_policy_sequential_multiple scheduler_policy_sequencial_multiple;

  //***************************************************************************
  /// Highest Priority.
  /// A policy the scheduler can use to decide what to do next.
  /// Calls the highest priority task that has work.
  //***************************************************************************
  struct scheduler_policy_highest_priority
  {
    bool schedule_tasks(etl::ivector<etl::task*>& task_list)
    {
      bool idle = true;

      size_t index = 0UL;
      while (index < task_list.size())
      {
        etl::task& task = *(task_list[index]);

        if (task.task_request_work() > 0)
        {
          task.task_process_work();
          idle = false;
          break;
        }
        else
        {
          ++index;
        }
      }

      return idle;
    }
  };

  //***************************************************************************
  /// Most Work.
  /// A policy the scheduler can use to decide what to do next.
  /// Calls the task that has the most work.
  /// Starts looking from the task with the highest priority.
  //***************************************************************************
  struct scheduler_policy_most_work
  {
    bool schedule_tasks(etl::ivector<etl::task*>& task_list)
    {
      bool idle = true;

      size_t most_index = 0UL;
      uint32_t most_work = 0UL;

      for (size_t index = 0UL; index < task_list.size(); ++index)
      {
        etl::task& task = *(task_list[index]);

        uint32_t n_work = task.task_request_work();

        if (n_work > most_work)
        {
          most_index = index;
          most_work = n_work;
          idle = false;
        }
      }

      if (!idle)
      {
        task_list[most_index]->task_process_work();
      }

      return idle;
    }
  };

  //***************************************************************************
  /// Scheduler base.
  //***************************************************************************
  class ischeduler
  {
  public:

    //*******************************************
    // Virtuals.
    //*******************************************
    virtual void start() = 0;

    virtual ~ischeduler()
    {
    }

    //*******************************************
    /// Set the idle callback.
    //*******************************************
    void set_idle_callback(etl::ifunction<void>& callback)
    {
      p_idle_callback = &callback;
    }

    //*******************************************
    /// Set the watchdog callback.
    //*******************************************
    void set_watchdog_callback(etl::ifunction<void>& callback)
    {
      p_watchdog_callback = &callback;
    }

    //*******************************************
    /// Set the running state for the scheduler.
    //*******************************************
    void set_scheduler_running(bool scheduler_running_)
    {
      scheduler_running = scheduler_running_;
    }

    //*******************************************
    /// Get the running state for the scheduler.
    //*******************************************
    bool scheduler_is_running() const
    {
      return scheduler_running;
    }

    //*******************************************
    /// Force the scheduler to exit.
    //*******************************************
    void exit_scheduler()
    {
      scheduler_exit = true;
    }

    //*******************************************
    /// Add a task.
    /// Add to the task list in priority order.
    //*******************************************
    void add_task(etl::task& task)
    {
      ETL_ASSERT(!task_list.full(), ETL_ERROR(etl::scheduler_too_many_tasks_exception));

      if (!task_list.full())
      {
        typename task_list_t::iterator itask = etl::upper_bound(task_list.begin(),
                                                                   task_list.end(),
                                                                   task.get_task_priority(),
                                                                   compare_priority());

        task_list.insert(itask, &task);

        task.on_task_added();
      }
    }

    //*******************************************
    /// Add a task list.
    /// Adds to the tasks to the internal task list in priority order.
    /// Input order is ignored.
    //*******************************************
    template <typename TSize>
    void add_task_list(etl::task** p_tasks, TSize size)
    {
      for (TSize i = 0; i < size; ++i)
      {
        ETL_ASSERT((p_tasks[i] != ETL_NULLPTR), ETL_ERROR(etl::scheduler_null_task_exception));
        add_task(*(p_tasks[i]));
      }
    }

  protected:

    //*******************************************
    /// Constructor.
    //*******************************************
    ischeduler(etl::ivector<etl::task*>& task_list_)
      : scheduler_running(false),
        scheduler_exit(false),
        p_idle_callback(ETL_NULLPTR),
        p_watchdog_callback(ETL_NULLPTR),
        task_list(task_list_)
    {
    }

    bool scheduler_running;
    bool scheduler_exit;
    etl::ifunction<void>* p_idle_callback;
    etl::ifunction<void>* p_watchdog_callback;

  private:

    //*******************************************
    // Used to order tasks in descending priority.
    //*******************************************
    struct compare_priority
    {
      bool operator()(etl::task_priority_t priority, etl::task* ptask) const
      {
        return priority > ptask->get_task_priority();
      }
    };

    typedef etl::ivector<etl::task*> task_list_t;
    task_list_t& task_list;
  };

  //***************************************************************************
  /// Scheduler.
  //***************************************************************************
  template <typename TSchedulerPolicy, size_t MAX_TASKS_>
  class scheduler : public etl::ischeduler, protected TSchedulerPolicy
  {
  public:

    enum
    {
      MAX_TASKS = MAX_TASKS_,
    };

    scheduler()
      : ischeduler(task_list)
    {
    }

    //*******************************************
    /// Start the scheduler.
    //*******************************************
    void start()
    {
      ETL_ASSERT(task_list.size() > 0, ETL_ERROR(etl::scheduler_no_tasks_exception));

      scheduler_running = true;

      while (!scheduler_exit)
      {
        if (scheduler_running)
        {
          bool idle = TSchedulerPolicy::schedule_tasks(task_list);

          if (p_watchdog_callback)
          {
            (*p_watchdog_callback)();
          }

          if (idle && p_idle_callback)
          {
            (*p_idle_callback)();
          }
        }
      }
    }

  private:

    typedef etl::vector<etl::task*, MAX_TASKS> task_list_t;
    task_list_t task_list;
  };
}

#endif