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$
00036  * Revision 1.3  2008/08/11 07:00:33  haraldkipp
00037  * BSD types replaced by stdint types (feature request #1282721).
00038  *
00039  * Revision 1.2  2008/04/29 01:51:35  thiagocorrea
00040  * Compile fix
00041  *
00042  * Revision 1.1  2008/04/21 22:24:53  olereinhardt
00043  * Implemented condition variables to use with NutOS as an application candy
00044  *
00045  */
00046 
00047 #include <sys/heap.h>
00048 #include <sys/event.h>
00049 #include <sys/atom.h>
00050 #include <sys/timer.h>
00051 #include <sys/thread.h>
00052 #include <sys/condition.h>
00053 #include <stddef.h> /* NULL definition */
00054 
00059 
00066 CONDITION NutConditionInit(void)
00067 {
00068     CONDITION cond;
00069     cond = NutHeapAlloc(sizeof(cond));
00070     if (cond == NULL) return NULL;
00071     NutMutexInit(&cond->mutex);
00072     return cond;
00073 }
00074 
00085 void NutConditionLock(CONDITION cond)
00086 {
00087     if (cond == NULL) return;
00088     NutMutexLock(&cond->mutex);
00089 }
00090 
00101 void NutConditionUnlock(CONDITION cond)
00102 {
00103     if (cond == NULL) return;
00104     NutMutexUnlock(&cond->mutex);
00105 }
00106 
00130 int NutConditionWait(CONDITION cond)
00131 {
00132     if (cond == NULL) return -1;
00133     NutMutexUnlock(&cond->mutex);
00134     NutEventWait(cond->event, NUT_WAIT_INFINITE);
00135     NutMutexLock(&cond->mutex);
00136     return 0;
00137 }
00138 
00167 int NutConditionTimedWait(CONDITION cond, uint32_t abs_ms)
00168 {
00169     uint32_t ms;
00170     
00171     if (cond == NULL) return -1;
00172     ms = abs_ms - NutGetMillis();
00173     if (ms > 0x7FFFFFFF) return -1;
00174     
00175     NutMutexUnlock(&cond->mutex);
00176     NutEventWait(&cond->event, ms);
00177     NutMutexLock(&cond->mutex);
00178     return 0;
00179 }
00180   
00193 int NutConditionSignal(CONDITION cond)
00194 {
00195     if (cond == NULL) return -1;
00196     return NutEventPost(&cond->event);
00197 }
00198 
00211 int NutConditionBroadcast(CONDITION cond)
00212 {
00213     if (cond == NULL) return -1;
00214     return NutEventBroadcast(&cond->event);
00215 }
00216 
00222 void NutConditionFree(CONDITION *cond)
00223 {
00224     NutMutexDestroy(&(*cond)->mutex);
00225     NutHeapFree(cond);
00226     cond = NULL;
00227 }
00228 

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