rtai-core/include/xenomai/mutex.h

00001 /*
00002  * Copyright (C) 2001,2002,2003 Philippe Gerum <rpm@xenomai.org>.
00003  *
00004  * Xenomai is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * Xenomai is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with Xenomai; if not, write to the Free Software Foundation,
00016  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * As a special exception, the RTAI project gives permission
00019  * for additional uses of the text contained in its release of
00020  * Xenomai.
00021  *
00022  * The exception is that, if you link the Xenomai libraries with other
00023  * files to produce an executable, this does not by itself cause the
00024  * resulting executable to be covered by the GNU General Public License.
00025  * Your use of that executable is in no way restricted on account of
00026  * linking the Xenomai libraries code into it.
00027  *
00028  * This exception does not however invalidate any other reasons why
00029  * the executable file might be covered by the GNU General Public
00030  * License.
00031  *
00032  * This exception applies only to the code released by the
00033  * RTAI project under the name Xenomai.  If you copy code from other
00034  * RTAI project releases into a copy of Xenomai, as the General Public
00035  * License permits, the exception does not apply to the code that you
00036  * add in this way.  To avoid misleading anyone as to the status of
00037  * such modified files, you must delete this exception notice from
00038  * them.
00039  *
00040  * If you write modifications of your own for Xenomai, it is your
00041  * choice whether to permit this exception to apply to your
00042  * modifications. If you do not wish that, delete this exception
00043  * notice.
00044  */
00045 
00046 #ifndef _xenomai_mutex_h
00047 #define _xenomai_mutex_h
00048 
00049 #include <xenomai/pod.h>
00050 #include <xenomai/synch.h>
00051 
00052 struct xnthread;
00053 
00054 typedef struct xnmutex {
00055 
00056     xnsynch_t synchbase;        /* Must be first. */
00057 
00058     atomic_counter_t lockcnt;
00059 
00060 } xnmutex_t; /* Kernel mutex */
00061 
00062 #ifdef __cplusplus
00063 extern "C" {
00064 #endif
00065 
00066 void xnmutex_forget_sleeper(struct xnthread *sleeper);
00067 
00068 void xnmutex_sleepon_inner(xnmutex_t *mutex,
00069                            struct xnthread *thread);
00070 
00071 void xnmutex_wakeup_inner(xnmutex_t *mutex,
00072                           int flags);
00073 
00074 static inline int xnmutex_owner_p (xnmutex_t *mutex) {
00075 
00076     /* This code must be entered interrupts off. */
00077 
00078     return (xnarch_atomic_get(&mutex->lockcnt) < 1 &&
00079             mutex->synchbase.owner == nkpod->sched.runthread);
00080 }
00081 
00082 static inline int xnmutex_clear_lock (xnmutex_t *mutex,
00083                                       atomic_counter_t *pcounter) {
00084 
00085     /* This code must be entered interrupts off. */
00086 
00087     int s = 0;
00088 
00089     if (xnmutex_owner_p(mutex)) {
00090         *pcounter = mutex->lockcnt;
00091         if (xnsynch_nsleepers(&mutex->synchbase) > 0) {
00092             /* Remove the lock count which accounted for the
00093                sleepers. */
00094             xnarch_atomic_inc(pcounter);
00095             xnarch_atomic_set(&mutex->lockcnt,0);
00096             /* Wake up one sleeper. */
00097             xnmutex_wakeup_inner(mutex,XNPOD_NOSWITCH);
00098             s = -1;
00099         } else {
00100             xnarch_atomic_set(&mutex->lockcnt,1);
00101             s = 1;
00102         }
00103     }
00104 
00105     return s;
00106 }
00107 
00108 static inline void xnmutex_set_lock (xnmutex_t *mutex,
00109                                      atomic_counter_t *pcounter) {
00110 
00111     xnthread_t *runthread = nkpod->sched.runthread;
00112 
00113     /* This code must be entered interrupts off. */
00114 
00115     if (xnarch_atomic_dec_and_test(&mutex->lockcnt)) {
00116         /* Mutex was free on entry */
00117         xnsynch_set_owner(&mutex->synchbase,runthread);
00118     } else {
00119            if (xnsynch_owner(&mutex->synchbase) != runthread) {
00120                /* Mutex was busy on entry */
00121                xnmutex_sleepon_inner(mutex,runthread);
00122         }
00123     }
00124 
00125     /* Account for the sleepers if any. */
00126     if (xnsynch_nsleepers(&mutex->synchbase) > 0)
00127         xnarch_atomic_dec(pcounter);
00128 
00129     xnarch_atomic_set(&mutex->lockcnt,xnarch_atomic_get(pcounter));
00130 }
00131 
00132     /* -- Beginning of the exported interface */
00133 
00134 void xnmutex_init(xnmutex_t *mutex);
00135 
00136 static inline void xnmutex_lock (xnmutex_t *mutex) {
00137 
00138     xnthread_t *runthread = nkpod->sched.runthread;
00139     spl_t s;
00140 
00141     splhigh(s);
00142 
00143     if (xnarch_atomic_dec_and_test(&mutex->lockcnt)) {
00144         /* Mutex was free on entry */
00145         xnsynch_set_owner(&mutex->synchbase,runthread);
00146     } else {
00147         if (xnsynch_owner(&mutex->synchbase) != runthread) {
00148             /* Mutex was busy on entry. */
00149             xnmutex_sleepon_inner(mutex,runthread);
00150         }
00151     }
00152 
00153     splexit(s);
00154 }
00155 
00156 static inline void xnmutex_unlock (xnmutex_t *mutex) {
00157 
00158     if (xnarch_atomic_inc_and_test(&mutex->lockcnt)) {
00159         if (xnsynch_nsleepers(&mutex->synchbase) > 0) {
00160             /* Wake up one sleeper. */
00161             xnmutex_wakeup_inner(mutex,0);
00162         }
00163     }
00164 }
00165 
00166 #ifdef __cplusplus
00167 }
00168 #endif
00169 
00170 #endif /* !_xenomai_mutex_h */

Generated on Sat Jul 24 19:36:04 2004 for RTAI API by doxygen 1.3.4