Simple round-robin scheduler for mbed
Overview
This library provides simple round-robin scheduling based on the mbed-os.
Why would you want to use this?
- Tasks are not run from within interrupt sub-routines (as they are using Tickers)
- Ordering/priority of tasks is deterministic (rate monotonic)
It's not rocket science, just a handy wrapper around the RTOS functions.
Example
#define BASE_RATE 0.1f #define TASK1_MULTIPLIER 1 #define TASK2_MULTIPLIER 2 .... RoundRobin::instance()->SetBaseRate( BASE_RATE ); RoundRobin::instance()->addTask( TASK1_MULTIPLIER, lcdRefresh ); RoundRobin::instance()->addTask( TASK2_MULTIPLIER, watchButtons );
RoundRobin.hpp@0:a8c603b939a7, 2017-07-29 (annotated)
- Committer:
- johnb
- Date:
- Sat Jul 29 11:53:34 2017 +0000
- Revision:
- 0:a8c603b939a7
Initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:a8c603b939a7 | 1 | /* @brief Round Robin scheduler for mbed |
johnb | 0:a8c603b939a7 | 2 | |
johnb | 0:a8c603b939a7 | 3 | @author John Bailey |
johnb | 0:a8c603b939a7 | 4 | |
johnb | 0:a8c603b939a7 | 5 | @copyright Copyright 2017 John Bailey |
johnb | 0:a8c603b939a7 | 6 | |
johnb | 0:a8c603b939a7 | 7 | @section LICENSE |
johnb | 0:a8c603b939a7 | 8 | |
johnb | 0:a8c603b939a7 | 9 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:a8c603b939a7 | 10 | you may not use this file except in compliance with the License. |
johnb | 0:a8c603b939a7 | 11 | You may obtain a copy of the License at |
johnb | 0:a8c603b939a7 | 12 | |
johnb | 0:a8c603b939a7 | 13 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:a8c603b939a7 | 14 | |
johnb | 0:a8c603b939a7 | 15 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:a8c603b939a7 | 16 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:a8c603b939a7 | 17 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:a8c603b939a7 | 18 | See the License for the specific language governing permissions and |
johnb | 0:a8c603b939a7 | 19 | limitations under the License. |
johnb | 0:a8c603b939a7 | 20 | */ |
johnb | 0:a8c603b939a7 | 21 | |
johnb | 0:a8c603b939a7 | 22 | #if !defined ROUNDROBIN_HPP |
johnb | 0:a8c603b939a7 | 23 | #define ROUNDROBIN_HPP |
johnb | 0:a8c603b939a7 | 24 | |
johnb | 0:a8c603b939a7 | 25 | #include "rtos.h" |
johnb | 0:a8c603b939a7 | 26 | #include <map> |
johnb | 0:a8c603b939a7 | 27 | |
johnb | 0:a8c603b939a7 | 28 | class RoundRobin |
johnb | 0:a8c603b939a7 | 29 | { |
johnb | 0:a8c603b939a7 | 30 | protected: |
johnb | 0:a8c603b939a7 | 31 | EventQueue eventQueue; |
johnb | 0:a8c603b939a7 | 32 | Thread* thread; |
johnb | 0:a8c603b939a7 | 33 | Ticker ticker; |
johnb | 0:a8c603b939a7 | 34 | static RoundRobin *p_instance; |
johnb | 0:a8c603b939a7 | 35 | |
johnb | 0:a8c603b939a7 | 36 | RoundRobin(); |
johnb | 0:a8c603b939a7 | 37 | static void EventTrigger( void ); |
johnb | 0:a8c603b939a7 | 38 | |
johnb | 0:a8c603b939a7 | 39 | class TaskEntry |
johnb | 0:a8c603b939a7 | 40 | { |
johnb | 0:a8c603b939a7 | 41 | void (*fn)( void ); |
johnb | 0:a8c603b939a7 | 42 | unsigned counter; |
johnb | 0:a8c603b939a7 | 43 | |
johnb | 0:a8c603b939a7 | 44 | public: |
johnb | 0:a8c603b939a7 | 45 | TaskEntry( void (*p_fn)( void ) ) : fn(p_fn),counter(0) {} |
johnb | 0:a8c603b939a7 | 46 | void tick( void ) { counter++; } |
johnb | 0:a8c603b939a7 | 47 | void triggerIfNeeded( unsigned multiplier ) |
johnb | 0:a8c603b939a7 | 48 | { |
johnb | 0:a8c603b939a7 | 49 | if( multiplier == counter ) |
johnb | 0:a8c603b939a7 | 50 | { |
johnb | 0:a8c603b939a7 | 51 | fn(); |
johnb | 0:a8c603b939a7 | 52 | counter = 0; |
johnb | 0:a8c603b939a7 | 53 | } |
johnb | 0:a8c603b939a7 | 54 | } |
johnb | 0:a8c603b939a7 | 55 | }; |
johnb | 0:a8c603b939a7 | 56 | |
johnb | 0:a8c603b939a7 | 57 | static std::multimap<unsigned,TaskEntry> p_taskList; |
johnb | 0:a8c603b939a7 | 58 | |
johnb | 0:a8c603b939a7 | 59 | public: |
johnb | 0:a8c603b939a7 | 60 | virtual ~RoundRobin(); |
johnb | 0:a8c603b939a7 | 61 | |
johnb | 0:a8c603b939a7 | 62 | static RoundRobin* instance( void ); |
johnb | 0:a8c603b939a7 | 63 | |
johnb | 0:a8c603b939a7 | 64 | static bool addTask( unsigned p_multiplier, void (*p_fn)(void) ); |
johnb | 0:a8c603b939a7 | 65 | |
johnb | 0:a8c603b939a7 | 66 | void SetBaseRate( const float p_rate ); |
johnb | 0:a8c603b939a7 | 67 | }; |
johnb | 0:a8c603b939a7 | 68 | |
johnb | 0:a8c603b939a7 | 69 | #endif |