Main Page   Modules   Data Structures   File List   Globals  

mutex.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 <avr/interrupt.h>
00042 #include <stddef.h>
00043 
00044 #include "otos_cfg.h"
00045 #include "otos_def.h"
00046 #include "types.h"
00047 #include "task.h"
00048 #include "mutex.h"
00049 #include "time.h"
00050 
00051 
00052 
00066 uint8_t otosTestAndSet(uint8_t* pLock)
00067 {
00068     uint8_t stat;
00069     uint8_t iStat;    
00070 
00071 
00072     iStat = SREG;           /* save cpu status register         */
00073     cli();
00074     stat = *pLock;
00075     *pLock = 1;
00076     SREG = iStat;           /* restore status register (i-bit)  */
00077     
00078     return stat;
00079 }
00080 
00081 
00104 uint8_t otosLockMutex(OtosMutex* pMutex, uint32_t timeout)
00105 {
00106     uint8_t lock;
00107 
00108         
00109     /* test and set mutex */
00110     lock = otosTestAndSet(&pMutex->lock);       /* lock = 0: we got the mutex. lock = 1: mutex is locked */
00111 
00112     if (lock == 0)                              /* ok, we got the mutex */
00113     {
00114         pMutex->pOwner = g_pRunningTask;        /* mark that this is our mutex */
00115         return MUTEX_SUCCESS;          
00116     }
00117     
00118     if (timeout == 0)                           /* don't wait */
00119     {
00120         return MUTEX_ALREADY_LOCKED;            /* could not lock mutex, don't block task */
00121     }
00122     else                                        /* wait for mutex */
00123     {
00124         cli();
00125         g_pRunningTask->pWaitMutex = pMutex;
00126         g_pRunningTask->sleepTicks = (timeout == INFINITE) ? 0 : timeout;
00127         otosBlock();
00128         cli();
00129         if (g_pRunningTask->pWaitMutex)         /* otosUnlockMutex must clear this value to indicate mutex was unlocked */
00130         {
00131             g_pRunningTask->pWaitMutex = NULL;
00132             sei();
00133             return MUTEX_TIMEOUT;
00134         }
00135         else
00136         {
00137             pMutex->lock = 1;                   /* lock the mutex */
00138             pMutex->pOwner = g_pRunningTask;    /* mark that this is our mutex */
00139             sei();
00140             return MUTEX_SUCCESS;
00141         }
00142     }           
00143 }
00144 
00145 
00161 uint8_t otosUnlockMutex(OtosMutex* pMutex)
00162 {
00163     OtosTask*   pTmp;
00164     OtosTask*   pNextTask;
00165     uint8_t     dispatch;
00166     uint8_t     unlock = 1;
00167 
00168     if (pMutex->pOwner != g_pRunningTask)   /* task must own the mutex to unlock it */
00169         return MUTEX_NOT_OWNER;
00170 
00171     dispatch = ISR_NOT_ACTIVE();            /* call dispatcher only if not called from an isr */
00172     cli();  
00173 
00174     /* wake first waiting task */
00175     pTmp = g_pBlockedQueue;
00176     while (pTmp != NULL)
00177     {
00178         pNextTask = pTmp->pNext;            /* remember next task, because after wakeup it is not valid any more */
00179 
00180         if (pTmp->pWaitMutex == pMutex)     /* if task waits on this mutex */
00181         {
00182             pTmp->pWaitMutex = NULL;        /* task doesn't wait for a mutex anymore */
00183             pTmp->sleepTicks = 0;           /* deactivate sleep counter */
00184             pMutex->pOwner = pTmp;          /* give mutex to this task */
00185             unlock = 0;                     /* don't unlock mutex, because it is now locked by the next task */
00186             otosWakeup(pTmp);
00187             break;
00188         }        
00189         
00190         pTmp = pNextTask;
00191     }
00192 
00193     if (unlock)
00194         pMutex->lock = 0;                   /* unlock mutex */
00195     
00196     if (dispatch)
00197         otosScheduler(SCHED_NORM);
00198     else
00199         g_performDispatch = TRUE;
00200 
00201     return MUTEX_SUCCESS;
00202 }
00203 

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