Nut/OS  4.10.3
API Reference
mutex.c
Go to the documentation of this file.
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 /* mutex.c - a nut/os implementation of recursive mutex functions
00035  *
00036  * 2004.05.06 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
00037  *
00038  */
00039 /*
00040  * $Log$
00041  * Revision 1.6  2009/01/17 15:37:52  haraldkipp
00042  * Added some NUTASSERT macros to check function parameters.
00043  *
00044  * Revision 1.5  2006/10/08 16:48:22  haraldkipp
00045  * Documentation fixed
00046  *
00047  * Revision 1.4  2005/08/02 17:47:04  haraldkipp
00048  * Major API documentation update.
00049  *
00050  * Revision 1.3  2004/07/19 16:24:23  freckle
00051  * Code contained same bug as os/semaphore.c.
00052  * Unfortunately I didn't understand the other fix, but anyway made
00053  * NutMutexLock more robust.
00054  * Fixed wrong indention of all functions
00055  *
00056  * Revision 1.2  2004/05/18 18:38:42  drsung
00057  * Added $Log keyword for CVS.
00058  *
00059  */
00060 
00065 
00066 #ifdef __cplusplus
00067 extern "C" {
00068 #endif
00069 
00070 #include <sys/nutdebug.h>
00071 #include <sys/mutex.h>
00072 #include <sys/event.h>
00073 
00080 void NutMutexInit(MUTEX * mutex) {
00081     NUTASSERT(mutex != NULL);
00082     mutex->thread = 0;
00083     mutex->count = 0;
00084     mutex->qhp = 0;
00085 }
00086 
00096 void NutMutexLock(MUTEX * mutex) {
00097     NUTASSERT(mutex != NULL);
00098     if (mutex->thread != runningThread) {
00099         while( mutex->count != 0)
00100             NutEventWaitNext(&mutex->qhp, NUT_WAIT_INFINITE);
00101     }
00102     mutex->thread = runningThread;
00103     mutex->count++;
00104 }
00105 
00114 int NutMutexTrylock(MUTEX * mutex) {
00115     NUTASSERT(mutex != NULL);
00116     if ((mutex->count != 0) && (mutex->thread != runningThread))
00117         return -1;
00118     NutMutexLock(mutex);
00119     return 0;
00120 }
00121 
00130 int NutMutexUnlock(MUTEX * mutex) {
00131     NUTASSERT(mutex != NULL);
00132     if (mutex->thread != runningThread)
00133         return -1;
00134     if (--mutex->count == 0) {
00135         NutEventPost(&mutex->qhp);
00136     }
00137     return 0;
00138 }
00139 
00147 int NutMutexDestroy(MUTEX * mutex) {
00148     NUTASSERT(mutex != NULL);
00149     if (mutex->count == 0)
00150         return 0;
00151     if (mutex->thread == runningThread)
00152         return 0;
00153     return -1;
00154 }
00155 
00156 #ifdef __cplusplus
00157 }
00158 #endif
00159