OpenAFS
OpenAFS distributed network file system
/cygdrive/c/src/openafs/openafs.git/repo/src/rx/FBSD/rx_kmutex.h
00001 /*
00002  * Copyright 2000, International Business Machines Corporation and others.
00003  * All Rights Reserved.
00004  *
00005  * This software has been released under the terms of the IBM Public
00006  * License.  For details, see the LICENSE file in the top-level source
00007  * directory or online at http://www.openafs.org/dl/license10.html
00008  */
00009 
00010 /*
00011  * rx_kmutex.h - mutex and condition variable macros for kernel environment.
00012  *
00013  * FBSD implementation.
00014  */
00015 
00016 #ifndef _RX_KMUTEX_H_
00017 #define _RX_KMUTEX_H_
00018 
00019 #include <sys/systm.h>
00020 #include <sys/proc.h>
00021 #ifdef AFS_FBSD70_ENV
00022 #include <sys/lock.h>
00023 #include <sys/lockmgr.h>
00024 #else
00025 #include <sys/lock.h>
00026 #endif
00027 
00028 #define RX_ENABLE_LOCKS         1
00029 #define AFS_GLOBAL_RXLOCK_KERNEL
00030 
00031 typedef int afs_kcondvar_t;
00032 
00033 #define HEAVY_LOCKS
00034 #if defined(NULL_LOCKS)
00035 typedef struct {
00036     struct proc *owner;
00037 } afs_kmutex_t;
00038 
00039 #define MUTEX_INIT(a,b,c,d) \
00040     do { \
00041         (a)->owner = 0; \
00042     } while(0);
00043 #define MUTEX_DESTROY(a) \
00044     do { \
00045         (a)->owner = (struct proc *)-1; \
00046     } while(0);
00047 #define MUTEX_ENTER(a) \
00048     do { \
00049         osi_Assert((a)->owner == 0); \
00050         (a)->owner = curproc; \
00051     } while(0);
00052 #define MUTEX_TRYENTER(a) \
00053     ( osi_Assert((a)->owner == 0), (a)->owner = curproc, 1)
00054 #define MUTEX_EXIT(a) \
00055     do { \
00056         osi_Assert((a)->owner == curproc); \
00057         (a)->owner = 0; \
00058     } while(0);
00059 
00060 #undef MUTEX_ISMINE
00061 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curproc)
00062 
00063 #elif defined(AFS_FBSD70_ENV) /* dunno about 6.x */
00064 
00065 typedef struct mtx afs_kmutex_t;
00066 
00067 #if defined(AFS_FBSD80_ENV) && defined(WITNESS)
00068 #define WITCLEAR_MTX(a)                                 \
00069     do { memset((a), 0, sizeof(struct mtx)); } while(0);
00070 #else
00071 #define WITCLEAR_MTX(a) {}
00072 #endif
00073 
00074 #define MUTEX_INIT(a,b,c,d)                                          \
00075   do {                                                               \
00076       WITCLEAR_MTX(a);                                               \
00077       mtx_init((a), (b), 0 /* type defaults to name */, MTX_DEF | MTX_DUPOK);    \
00078   } while(0);
00079 
00080 #define MUTEX_DESTROY(a)                        \
00081     do {                                        \
00082         mtx_destroy((a));                       \
00083     } while(0);
00084 
00085 #define MUTEX_ENTER(a) \
00086     do {               \
00087         mtx_lock((a)); \
00088     } while(0);
00089 
00090 #define MUTEX_TRYENTER(a)                       \
00091     ( mtx_trylock((a)) )
00092 
00093 #define MUTEX_EXIT(a)    \
00094     do {                 \
00095         mtx_unlock((a)); \
00096     } while(0);
00097 
00098 #undef MUTEX_ISMINE
00099 #define MUTEX_ISMINE(a)                         \
00100     ( mtx_owned((a)) )
00101 
00102 #else
00103 
00104 typedef struct {
00105     struct lock lock;
00106     struct thread *owner;
00107 } afs_kmutex_t;
00108 
00109 
00110 #define MUTEX_INIT(a,b,c,d) \
00111     do { \
00112         lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
00113         (a)->owner = 0; \
00114     } while(0);
00115 #define MUTEX_DESTROY(a) \
00116     do { \
00117         (a)->owner = (struct proc *)-1; \
00118     } while(0);
00119 #define MUTEX_ENTER(a) \
00120     do { \
00121         lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, curthread); \
00122         osi_Assert((a)->owner == 0); \
00123         (a)->owner = curthread; \
00124     } while(0);
00125 #define MUTEX_TRYENTER(a) \
00126     ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, curthread) ? 0 : ((a)->owner = curthread, 1) )
00127 #define xMUTEX_TRYENTER(a) \
00128     ( osi_Assert((a)->owner == 0), (a)->owner = curthread, 1)
00129 #define MUTEX_EXIT(a) \
00130     do { \
00131         osi_Assert((a)->owner == curthread); \
00132         (a)->owner = 0; \
00133         lockmgr(&(a)->lock, LK_RELEASE, 0, curthread); \
00134     } while(0);
00135 
00136 #undef MUTEX_ISMINE
00137 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == curthread)
00138 #endif
00139 
00140 
00141 #undef osirx_AssertMine
00142 extern void osirx_AssertMine(afs_kmutex_t * lockaddr, char *msg);
00143 
00144 
00145 /*
00146  * Condition variables
00147  *
00148  * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
00149  * mechanism.  The condition variable itself plays no role; we just use its
00150  * address as a convenient unique number.
00151  */
00152 #define CV_INIT(cv,a,b,c)
00153 #define CV_DESTROY(cv)
00154 
00155 #if defined(AFS_FBSD70_ENV)
00156 
00157 #define CV_WAIT(cv, lck)    { \
00158     int isGlockOwner = ISAFS_GLOCK();                                   \
00159     if (isGlockOwner) AFS_GUNLOCK();                                    \
00160     msleep(cv, lck, PSOCK, "afs_rx_cv_wait", 0);                        \
00161     if (isGlockOwner) AFS_GLOCK();                                      \
00162   }
00163 
00164 #define CV_TIMEDWAIT(cv,lck,t)  { \
00165                                 int isGlockOwner = ISAFS_GLOCK(); \
00166                                 if (isGlockOwner) AFS_GUNLOCK();  \
00167                                 msleep(cv, lck, PSOCK, "afs_rx_cv_timedwait", t); \
00168                                 if (isGlockOwner) AFS_GLOCK();  \
00169                                 }
00170 #else /* !AFS_FBSD70_ENV */
00171 #define CV_WAIT(cv, lck)    { \
00172                                 int isGlockOwner = ISAFS_GLOCK(); \
00173                                 if (isGlockOwner) AFS_GUNLOCK();  \
00174                                 MUTEX_EXIT(lck);        \
00175                                 tsleep(cv, PSOCK, "afs_rx_cv_wait", 0);  \
00176                                 if (isGlockOwner) AFS_GLOCK();  \
00177                                 MUTEX_ENTER(lck); \
00178                             }
00179 
00180 #define CV_TIMEDWAIT(cv,lck,t)  { \
00181                                 int isGlockOwner = ISAFS_GLOCK(); \
00182                                 if (isGlockOwner) AFS_GUNLOCK();  \
00183                                 MUTEX_EXIT(lck);        \
00184                                 tsleep(cv, PSOCK, "afs_rx_cv_timedwait", t); \
00185                                 if (isGlockOwner) AFS_GLOCK();  \
00186                                 MUTEX_ENTER(lck);       \
00187                                 }
00188 #endif /* AFS_FBSD80_ENV */
00189 
00190 #define CV_SIGNAL(cv)           wakeup_one(cv)
00191 #define CV_BROADCAST(cv)        wakeup(cv)
00192 
00193 /* #define osi_rxWakeup(cv)        wakeup(cv) */
00194 
00195 
00196 #endif /* _RX_KMUTEX_H_ */
 All Data Structures Files Functions Variables