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 );
Committer:
johnb
Date:
Sat Jul 29 12:17:31 2017 +0000
Revision:
1:549bc1cd1f3d
Parent:
0:a8c603b939a7
Remove mbed-os lib to prevent clashes when it's also included by the program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:a8c603b939a7 1 /* Copyright 2017 John Bailey
johnb 0:a8c603b939a7 2
johnb 0:a8c603b939a7 3 Licensed under the Apache License, Version 2.0 (the "License");
johnb 0:a8c603b939a7 4 you may not use this file except in compliance with the License.
johnb 0:a8c603b939a7 5 You may obtain a copy of the License at
johnb 0:a8c603b939a7 6
johnb 0:a8c603b939a7 7 http://www.apache.org/licenses/LICENSE-2.0
johnb 0:a8c603b939a7 8
johnb 0:a8c603b939a7 9 Unless required by applicable law or agreed to in writing, software
johnb 0:a8c603b939a7 10 distributed under the License is distributed on an "AS IS" BASIS,
johnb 0:a8c603b939a7 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 0:a8c603b939a7 12 See the License for the specific language governing permissions and
johnb 0:a8c603b939a7 13 limitations under the License.
johnb 0:a8c603b939a7 14 */
johnb 0:a8c603b939a7 15
johnb 0:a8c603b939a7 16 #include "RoundRobin.hpp"
johnb 0:a8c603b939a7 17
johnb 0:a8c603b939a7 18 RoundRobin* RoundRobin::p_instance = NULL;
johnb 0:a8c603b939a7 19 std::multimap<unsigned,RoundRobin::TaskEntry> RoundRobin::p_taskList;
johnb 0:a8c603b939a7 20
johnb 0:a8c603b939a7 21 RoundRobin* RoundRobin::instance( void )
johnb 0:a8c603b939a7 22 {
johnb 0:a8c603b939a7 23 if (!p_instance)
johnb 0:a8c603b939a7 24 {
johnb 0:a8c603b939a7 25 p_instance = new RoundRobin();
johnb 0:a8c603b939a7 26 }
johnb 0:a8c603b939a7 27 return p_instance;
johnb 0:a8c603b939a7 28 }
johnb 0:a8c603b939a7 29
johnb 0:a8c603b939a7 30 RoundRobin::RoundRobin()
johnb 0:a8c603b939a7 31 {
johnb 0:a8c603b939a7 32 thread = new Thread(osPriorityLow);
johnb 0:a8c603b939a7 33 thread->start(callback(&eventQueue, &EventQueue::dispatch_forever));
johnb 0:a8c603b939a7 34 }
johnb 0:a8c603b939a7 35
johnb 0:a8c603b939a7 36 void RoundRobin::SetBaseRate( const float p_rate )
johnb 0:a8c603b939a7 37 {
johnb 0:a8c603b939a7 38 ticker.attach( eventQueue.event( &EventTrigger ), p_rate );
johnb 0:a8c603b939a7 39 }
johnb 0:a8c603b939a7 40
johnb 0:a8c603b939a7 41 RoundRobin::~RoundRobin()
johnb 0:a8c603b939a7 42 {
johnb 0:a8c603b939a7 43 delete( thread );
johnb 0:a8c603b939a7 44 }
johnb 0:a8c603b939a7 45
johnb 0:a8c603b939a7 46 void RoundRobin::EventTrigger( void )
johnb 0:a8c603b939a7 47 {
johnb 0:a8c603b939a7 48 for( std::multimap<unsigned,TaskEntry>::iterator i = p_taskList.begin();
johnb 0:a8c603b939a7 49 i != p_taskList.end();
johnb 0:a8c603b939a7 50 i++ )
johnb 0:a8c603b939a7 51 {
johnb 0:a8c603b939a7 52 i->second.tick();
johnb 0:a8c603b939a7 53 i->second.triggerIfNeeded(i->first);
johnb 0:a8c603b939a7 54 }
johnb 0:a8c603b939a7 55 }
johnb 0:a8c603b939a7 56
johnb 0:a8c603b939a7 57 bool RoundRobin::addTask( unsigned p_multiplier, void (*p_fn)(void) )
johnb 0:a8c603b939a7 58 {
johnb 0:a8c603b939a7 59 bool success = false;
johnb 0:a8c603b939a7 60 if( p_multiplier > 0 )
johnb 0:a8c603b939a7 61 {
johnb 0:a8c603b939a7 62 TaskEntry newTask ( p_fn );
johnb 0:a8c603b939a7 63
johnb 0:a8c603b939a7 64 p_taskList.insert( std::pair<unsigned,TaskEntry>(p_multiplier, newTask ));
johnb 0:a8c603b939a7 65
johnb 0:a8c603b939a7 66 success = true;
johnb 0:a8c603b939a7 67 }
johnb 0:a8c603b939a7 68 return success;
johnb 0:a8c603b939a7 69 }