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 int NutConditionInit( CONDITION * cond) 00067 { 00068 cond = NutHeapAlloc(sizeof(struct _CONDITION)); 00069 if (cond == NULL) return 1; 00070 NutMutexInit(&cond->mutex); 00071 return 0; 00072 } 00073 00084 void NutConditionLock(CONDITION * cond) 00085 { 00086 if (cond == NULL) return; 00087 NutMutexLock(&cond->mutex); 00088 } 00089 00100 void NutConditionUnlock(CONDITION * cond) 00101 { 00102 if (cond == NULL) return; 00103 NutMutexUnlock(&cond->mutex); 00104 } 00105 00129 int NutConditionWait(CONDITION * cond) 00130 { 00131 if (cond == NULL) return -1; 00132 NutMutexUnlock(&cond->mutex); 00133 NutEventWait(cond->event, NUT_WAIT_INFINITE); 00134 NutMutexLock(&cond->mutex); 00135 return 0; 00136 } 00137 00166 int NutConditionTimedWait(CONDITION * cond, uint32_t abs_ms) 00167 { 00168 uint32_t ms; 00169 00170 if (cond == NULL) return -1; 00171 ms = abs_ms - NutGetMillis(); 00172 if (ms > 0x7FFFFFFF) return -1; 00173 00174 NutMutexUnlock(&cond->mutex); 00175 NutEventWait(&cond->event, ms); 00176 NutMutexLock(&cond->mutex); 00177 return 0; 00178 } 00179 00192 int NutConditionSignal(CONDITION * cond) 00193 { 00194 if (cond == NULL) return -1; 00195 return NutEventPost(&cond->event); 00196 } 00197 00210 int NutConditionBroadcast(CONDITION * cond) 00211 { 00212 if (cond == NULL) return -1; 00213 return NutEventBroadcast(&cond->event); 00214 } 00215 00221 void NutConditionFree(CONDITION * cond) 00222 { 00223 NutMutexDestroy(&cond->mutex); 00224 NutHeapFree(cond); 00225 cond = NULL; 00226 } 00227