EventFramework library allows the creation of an event-driven infrastructure in which small "threads" can handle events in a multithreaded execution context. The EventFramework can be configured to act as a cooperative or a fully-preemptive kernel with fixed-priority scheduling. Furthermore, this kernel matches run-to-completion semantics, and hence a single-stack configuration is enough to keep running this multithreaded execution environment. As running threads shares global stack, a huge quantity of RAM is saved in contrast with traditional RTOSes.

Dependents:   sensors_KL46Z_xmn

Committer:
raulMrello
Date:
Mon Oct 01 10:38:45 2012 +0000
Revision:
0:9d09acc8f9d9
Release 1.00; EventFramework library allows the creation of an event-driven infrastructure for lightweight multithreaded applications with cooperative or fully-preemptive scheduling.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
raulMrello 0:9d09acc8f9d9 1 /* mbed EventFramework Library
raulMrello 0:9d09acc8f9d9 2 * Copyright (c) 2012 raulMrello
raulMrello 0:9d09acc8f9d9 3 *
raulMrello 0:9d09acc8f9d9 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
raulMrello 0:9d09acc8f9d9 5 * of this software and associated documentation files (the "Software"), to deal
raulMrello 0:9d09acc8f9d9 6 * in the Software without restriction, including without limitation the rights
raulMrello 0:9d09acc8f9d9 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
raulMrello 0:9d09acc8f9d9 8 * copies of the Software, and to permit persons to whom the Software is
raulMrello 0:9d09acc8f9d9 9 * furnished to do so, subject to the following conditions:
raulMrello 0:9d09acc8f9d9 10 *
raulMrello 0:9d09acc8f9d9 11 * The above copyright notice and this permission notice shall be included in
raulMrello 0:9d09acc8f9d9 12 * all copies or substantial portions of the Software.
raulMrello 0:9d09acc8f9d9 13 *
raulMrello 0:9d09acc8f9d9 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
raulMrello 0:9d09acc8f9d9 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
raulMrello 0:9d09acc8f9d9 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
raulMrello 0:9d09acc8f9d9 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
raulMrello 0:9d09acc8f9d9 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
raulMrello 0:9d09acc8f9d9 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
raulMrello 0:9d09acc8f9d9 20 * THE SOFTWARE.
raulMrello 0:9d09acc8f9d9 21 */
raulMrello 0:9d09acc8f9d9 22
raulMrello 0:9d09acc8f9d9 23 #include "Event.h"
raulMrello 0:9d09acc8f9d9 24
raulMrello 0:9d09acc8f9d9 25 Event::Event(uint16_t prio){
raulMrello 0:9d09acc8f9d9 26 this->prio = prio;
raulMrello 0:9d09acc8f9d9 27 base = NULL;
raulMrello 0:9d09acc8f9d9 28 data = NULL;
raulMrello 0:9d09acc8f9d9 29 hlist = NULL;
raulMrello 0:9d09acc8f9d9 30 }
raulMrello 0:9d09acc8f9d9 31
raulMrello 0:9d09acc8f9d9 32 Event::~Event(void){
raulMrello 0:9d09acc8f9d9 33 prio = 0;
raulMrello 0:9d09acc8f9d9 34 data = NULL;
raulMrello 0:9d09acc8f9d9 35 base = NULL;
raulMrello 0:9d09acc8f9d9 36 if(hlist){
raulMrello 0:9d09acc8f9d9 37 delete hlist;
raulMrello 0:9d09acc8f9d9 38 }
raulMrello 0:9d09acc8f9d9 39 }
raulMrello 0:9d09acc8f9d9 40
raulMrello 0:9d09acc8f9d9 41 void Event::SetData(void* data){
raulMrello 0:9d09acc8f9d9 42 this->data = data;
raulMrello 0:9d09acc8f9d9 43 }
raulMrello 0:9d09acc8f9d9 44
raulMrello 0:9d09acc8f9d9 45 void Event::SetBase(Event* ev){
raulMrello 0:9d09acc8f9d9 46 base = ev;
raulMrello 0:9d09acc8f9d9 47 }
raulMrello 0:9d09acc8f9d9 48
raulMrello 0:9d09acc8f9d9 49 void* Event::GetData(void){
raulMrello 0:9d09acc8f9d9 50 return data;
raulMrello 0:9d09acc8f9d9 51 }
raulMrello 0:9d09acc8f9d9 52
raulMrello 0:9d09acc8f9d9 53 Event* Event::GetBase(void){
raulMrello 0:9d09acc8f9d9 54 return base;
raulMrello 0:9d09acc8f9d9 55 }
raulMrello 0:9d09acc8f9d9 56
raulMrello 0:9d09acc8f9d9 57 List* Event::GetList(void){
raulMrello 0:9d09acc8f9d9 58 return hlist;
raulMrello 0:9d09acc8f9d9 59 }
raulMrello 0:9d09acc8f9d9 60
raulMrello 0:9d09acc8f9d9 61 uint16_t Event::GetPrio(void){
raulMrello 0:9d09acc8f9d9 62 return prio;
raulMrello 0:9d09acc8f9d9 63 }
raulMrello 0:9d09acc8f9d9 64
raulMrello 0:9d09acc8f9d9 65 void Event::Subscribe(EventHandler * hnd){
raulMrello 0:9d09acc8f9d9 66 // if EventHandler list is not created yet, then creates and add handler
raulMrello 0:9d09acc8f9d9 67 if(!hlist){
raulMrello 0:9d09acc8f9d9 68 hlist = new List(hnd);
raulMrello 0:9d09acc8f9d9 69 return;
raulMrello 0:9d09acc8f9d9 70 }
raulMrello 0:9d09acc8f9d9 71 // else insert the handler according with its priority
raulMrello 0:9d09acc8f9d9 72 Node* node = hlist->GetFirstNode();
raulMrello 0:9d09acc8f9d9 73 EventHandler* data = (EventHandler*)node->GetData();
raulMrello 0:9d09acc8f9d9 74 while(data->GetPrio() <= hnd->GetPrio()){
raulMrello 0:9d09acc8f9d9 75 Node* next = node->GetNext();
raulMrello 0:9d09acc8f9d9 76 if(!next){
raulMrello 0:9d09acc8f9d9 77 hlist->AddAfter(node, hnd);
raulMrello 0:9d09acc8f9d9 78 return;
raulMrello 0:9d09acc8f9d9 79 }
raulMrello 0:9d09acc8f9d9 80 data = (EventHandler*)next->GetData();
raulMrello 0:9d09acc8f9d9 81 }
raulMrello 0:9d09acc8f9d9 82 Node* newHead = hlist->AddBefore(node, hnd);
raulMrello 0:9d09acc8f9d9 83 if(newHead){
raulMrello 0:9d09acc8f9d9 84 hlist->SetFirstNode(newHead);
raulMrello 0:9d09acc8f9d9 85 }
raulMrello 0:9d09acc8f9d9 86 }
raulMrello 0:9d09acc8f9d9 87
raulMrello 0:9d09acc8f9d9 88 void Event::Unsubscribe(EventHandler* hnd){
raulMrello 0:9d09acc8f9d9 89 hlist->RemoveItem(hnd);
raulMrello 0:9d09acc8f9d9 90 }