00001 /* 00002 * Copyright (C) 2000-2004 by ETH Zurich 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the copyright holders nor the names of 00014 * contributors may be used to endorse or promote products derived 00015 * from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS 00018 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00019 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00020 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH 00021 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00022 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00023 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00024 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00025 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00026 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00027 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00028 * SUCH DAMAGE. 00029 * 00030 * For additional information see http://www.ethernut.de/ 00031 * 00032 */ 00033 00034 /* semaphore.c - a nut/os implementation of semaphore functions 00035 * 00036 * 2004.05.06 Matthias Ringwald <matthias.ringwald@inf.ethz.ch> 00037 * 00038 */ 00039 /* 00040 * $Log: semaphore.c,v $ 00041 * Revision 1.8 2008/06/16 15:12:07 freckle 00042 * fix bug in NutSemTryWait of os/semaphore.c 00043 * 00044 * Revision 1.7 2006/10/08 16:48:22 haraldkipp 00045 * Documentation fixed 00046 * 00047 * Revision 1.6 2005/08/02 17:47:04 haraldkipp 00048 * Major API documentation update. 00049 * 00050 * Revision 1.5 2004/06/03 08:44:50 olereinhardt 00051 * According to a hint from oliver I changed NutEventWait to NutEventWaitNext 00052 * 00053 * Revision 1.4 2004/06/03 08:24:21 olereinhardt 00054 * Changed semaphore behavior in NutSemTryWait too. 00055 * 00056 * Revision 1.3 2004/06/02 16:42:53 olereinhardt 00057 * fixed bug (integer overflow) in semaphore implementation. 00058 * 00059 * Revision 1.2 2004/05/18 18:38:42 drsung 00060 * Added $Log keyword for CVS. 00061 * 00062 */ 00063 00068 00069 #ifdef __cplusplus 00070 extern "C" { 00071 #endif 00072 00073 #include <sys/semaphore.h> 00074 #include <sys/event.h> 00075 00079 void NutSemInit(SEM * sem, short value) { 00080 sem->qhp = 0; 00081 sem->value = value; 00082 } void NutSemWait(SEM * sem) { 00091 sem->value--; 00092 if (sem->value < 0) 00093 { 00094 NutEventWaitNext(&sem->qhp, NUT_WAIT_INFINITE); 00095 } 00096 } 00097 00103 void NutSemPost(SEM * sem) { 00104 sem->value++; 00105 if (sem->value <= 0) 00106 { 00107 NutEventPost(&sem->qhp); 00108 } 00109 } 00110 00118 int NutSemTryWait(SEM * sem) { 00119 if (sem->value <= 0) 00120 return -1; 00121 else 00122 NutSemWait(sem); 00123 return 0; 00124 } 00125 00133 int NutSemDestroy(SEM * sem) { 00134 if (sem->qhp == SIGNALED) 00135 return 0; 00136 if (sem->qhp == 0) 00137 return 0; 00138 return -1; 00139 } 00140 00141 #ifdef __cplusplus 00142 } 00143 #endif 00144