Main Page   Modules   Data Structures   File List   Globals  

event.c

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2002, 2003, Oliver Tscherwitschke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice,
00009    this list of conditions and the following disclaimer.
00010 2. Redistributions in binary form must reproduce the above copyright notice,
00011    this list of conditions and the following disclaimer in the documentation
00012    and/or other materials provided with the distribution.
00013 3. The name of the author may not be used to endorse or promote products
00014    derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
00017 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
00019 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00021 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00022 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00023 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00041 #include <stdlib.h>
00042 #include <avr/interrupt.h>
00043 
00044 #include "otos_cfg.h"
00045 #include "otos_def.h"
00046 #include "types.h"
00047 #include "event.h"
00048 #include "time.h"
00049 
00050 
00051 
00066 void otosSendEvent(OtosTask* pTask, uint16_t events)
00067 {
00068     OtosTask*   pTmp;
00069     OtosTask*   pNextTask;
00070     uint8_t     dispatch;
00071 
00072     
00073     dispatch = ISR_NOT_ACTIVE();    /* call dispatcher only if not called from an isr */
00074     cli();
00075 
00076     if (pTask != NULL)              /* send event to one tasks */
00077     {
00078         otosSendEventToOneTask(pTask, events);
00079     }
00080     else                            /* send event to all tasks */
00081     {
00082         otosSendEventToOneTask(g_pRunningTask, events);
00083 
00084         pTmp = g_pReadyQueue;
00085         while (pTmp != NULL)
00086         {
00087             otosSendEventToOneTask(pTmp, events);
00088             pTmp = pTmp->pNext;
00089         }
00090 
00091         pTmp = g_pBlockedQueue;
00092         while (pTmp != NULL)
00093         {
00094             pNextTask = pTmp->pNext;    /* remember next task, because after wakeup it is not valid any more */
00095             otosSendEventToOneTask(pTmp, events);
00096             pTmp = pNextTask;
00097         }      
00098     }
00099 
00100     if (dispatch)
00101         otosScheduler(SCHED_NORM);
00102     else
00103         g_performDispatch = TRUE;
00104 }
00105 
00106 
00122 void otosSendEventToOneTask(OtosTask* pTask, uint16_t events)
00123 {
00124     pTask->events |= events;
00125 
00126     /* if task is blocked we must wake it perhaps */
00127     if (pTask->state == TASK_BLOCKED)
00128     {
00129         /* if task is waiting for events */
00130         if (pTask->waitEvents)
00131         {
00132             /* all needed events received? */
00133             if ((pTask->waitEvents & pTask->events) == pTask->waitEvents)
00134             {
00135                 pTask->events -= pTask->waitEvents;     /* remove received events */
00136                 pTask->waitEvents = 0;                  /* task doesn't wait for evetns anymore */
00137                 pTask->sleepTicks = 0;                  /* deactivate sleep counter */               
00138                 otosWakeup(pTask);
00139             }
00140         }
00141     }
00142 }
00143 
00170 uint8_t otosReceiveEvent(uint16_t events, uint32_t timeout)
00171 {
00172 
00173     cli();
00174     
00175     /* events already received? */
00176     if ((events & g_pRunningTask->events) == events)
00177     {
00178         g_pRunningTask->events -= events;       /* remove received events */
00179         sei();
00180         return EVENT_SUCCESS;
00181     }
00182 
00183     if (timeout == 0)                           /* don't wait */
00184     {
00185         sei();
00186         return EVENT_NOT_RECEIVED;
00187     }
00188     else                                        /* wait */
00189     {            
00190         g_pRunningTask->waitEvents = events;
00191         g_pRunningTask->sleepTicks = (timeout == INFINITE) ? 0 : timeout;
00192         otosBlock();
00193         cli();
00194         if (g_pRunningTask->waitEvents)        /* otosSendEvent must clear this value to indicate event reception */
00195         {
00196             g_pRunningTask->waitEvents = 0;
00197             sei();
00198             return EVENT_TIMEOUT;
00199         }
00200         else
00201         {
00202             sei();
00203             return EVENT_SUCCESS;
00204         }
00205     }    
00206 }
00207 
00208 

Generated on Sat Jan 25 18:41:43 2003 for otOS by doxygen1.3-rc2