condition.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 by EmbeddedIT, 
00003  * Ole Reinhardt <ole.reinhardt@embedded-it.de> 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
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
00022  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00025  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00027  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00028  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log: condition.c,v $
00036  * Revision 1.2  2008/04/29 01:51:35  thiagocorrea
00037  * Compile fix
00038  *
00039  * Revision 1.1  2008/04/21 22:24:53  olereinhardt
00040  * Implemented condition variables to use with NutOS as an application candy
00041  *
00042  */
00043 
00044 #include <sys/heap.h>
00045 #include <sys/event.h>
00046 #include <sys/atom.h>
00047 #include <sys/timer.h>
00048 #include <sys/thread.h>
00049 #include <sys/condition.h>
00050 #include <stddef.h> /* NULL definition */
00051 
00056 
00063 CONDITION NutConditionInit(void)
00064 {
00065     CONDITION cond;
00066     cond = NutHeapAlloc(sizeof(cond));
00067     if (cond == NULL) return NULL;
00068     NutMutexInit(&cond->mutex);
00069     return cond;
00070 }
00071 
00082 void NutConditionLock(CONDITION cond)
00083 {
00084     if (cond == NULL) return;
00085     NutMutexLock(&cond->mutex);
00086 }
00087 
00098 void NutConditionUnlock(CONDITION cond)
00099 {
00100     if (cond == NULL) return;
00101     NutMutexUnlock(&cond->mutex);
00102 }
00103 
00127 int NutConditionWait(CONDITION cond)
00128 {
00129     if (cond == NULL) return -1;
00130     NutMutexUnlock(&cond->mutex);
00131     NutEventWait(cond->event, NUT_WAIT_INFINITE);
00132     NutMutexLock(&cond->mutex);
00133     return 0;
00134 }
00135 
00164 int NutConditionTimedWait(CONDITION cond, u_long abs_ms)
00165 {
00166     u_long ms;
00167     
00168     if (cond == NULL) return -1;
00169     ms = abs_ms - NutGetMillis();
00170     if (ms > 0x7FFFFFFF) return -1;
00171     
00172     NutMutexUnlock(&cond->mutex);
00173     NutEventWait(&cond->event, ms);
00174     NutMutexLock(&cond->mutex);
00175     return 0;
00176 }
00177   
00190 int NutConditionSignal(CONDITION cond)
00191 {
00192     if (cond == NULL) return -1;
00193     return NutEventPost(&cond->event);
00194 }
00195 
00208 int NutConditionBroadcast(CONDITION cond)
00209 {
00210     if (cond == NULL) return -1;
00211     return NutEventBroadcast(&cond->event);
00212 }
00213 
00219 void NutConditionFree(CONDITION *cond)
00220 {
00221     NutMutexDestroy(&(*cond)->mutex);
00222     NutHeapFree(cond);
00223     cond = NULL;
00224 }
00225 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/