netbsd-server-support-20020101
[openafs.git] / src / rx / NBSD / rx_kmutex.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * rx_kmutex.h - mutex and condition variable macros for kernel environment.
12  *
13  * MACOS implementation.
14  */
15
16 #ifndef _RX_KMUTEX_H_
17 #define _RX_KMUTEX_H_
18
19 #include <sys/lock.h>
20 #include <kern/thread.h>
21 #include <sys/vm.h>
22
23 #define RX_ENABLE_LOCKS         1
24 #define AFS_GLOBAL_RXLOCK_KERNEL
25
26 /*
27  * Condition variables
28  *
29  * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
30  * mechanism.  The condition variable itself plays no role; we just use its
31  * address as a convenient unique number.
32  * 
33  * XXX in darwin, both mach and bsd facilities are available. Should really
34  * stick to one or the other (but mach locks don't have a _try.....)
35  */
36 #define CV_INIT(cv,a,b,c)
37 #define CV_DESTROY(cv)
38 #define CV_WAIT(cv, lck)    { \
39                                 int isGlockOwner = ISAFS_GLOCK(); \
40                                 if (isGlockOwner) AFS_GUNLOCK();  \
41                                 assert_wait((event_t)(cv), 0);  \
42                                 MUTEX_EXIT(lck);        \
43                                 thread_block(0);                \
44                                 if (isGlockOwner) AFS_GLOCK();  \
45                                 MUTEX_ENTER(lck); \
46                             }
47
48 #define CV_TIMEDWAIT(cv,lck,t)  { \
49                                 int isGlockOwner = ISAFS_GLOCK(); \
50                                 if (isGlockOwner) AFS_GUNLOCK();  \
51                                 assert_wait((event_t)(cv), 0);  \
52                                 thread_set_timer(t, NSEC_PER_SEC/hz);   \
53                                 MUTEX_EXIT(lck);        \
54                                 thread_block(0);                \
55                                 if (isGlockOwner) AFS_GLOCK();  \
56                                 MUTEX_ENTER(lck);       \
57
58 #define CV_SIGNAL(cv)           thread_wakeup_one((event_t)(cv))
59 #define CV_BROADCAST(cv)        thread_wakeup((event_t)(cv))
60
61 typedef struct {
62     struct lock__bsd__ lock;
63     thread_t owner;
64 } afs_kmutex_t;
65 typedef int afs_kcondvar_t;
66
67 #define osi_rxWakeup(cv)        thread_wakeup((event_t)(cv))
68
69 #define LOCK_INIT(a,b) \
70     do { \
71         lockinit(&(a)->lock,PSOCK, "afs rx lock", 0, 0); \
72         (a)->owner = (thread_t)0; \
73     } while(0);
74 #define MUTEX_INIT(a,b,c,d) \
75     do { \
76         lockinit(&(a)->lock,PSOCK, "afs rx mutex", 0, 0); \
77         (a)->owner = (thread_t)0; \
78     } while(0);
79 #define MUTEX_DESTROY(a) \
80     do { \
81         (a)->owner = (thread_t)-1; \
82     } while(0);
83 #define MUTEX_ENTER(a) \
84     do { \
85         lockmgr(&(a)->lock, LK_EXCLUSIVE, 0, current_proc()); \
86         osi_Assert((a)->owner == (thread_t)0); \
87         (a)->owner = current_thread(); \
88     } while(0);
89 #define MUTEX_TRYENTER(a) \
90     ( lockmgr(&(a)->lock, LK_EXCLUSIVE|LK_NOWAIT, 0, current_proc()) ? 0 : ((a)->owner = current_thread(), 1) )
91 #define xMUTEX_TRYENTER(a) \
92     ( osi_Assert((a)->owner == (thread_t)0), (a)->owner = current_thread(), 1)
93 #define MUTEX_EXIT(a) \
94     do { \
95         osi_Assert((a)->owner == current_thread()); \
96         (a)->owner = (thread_t)0; \
97         lockmgr(&(a)->lock, LK_RELEASE, 0, current_proc()); \
98     } while(0);
99
100 #undef MUTEX_ISMINE
101 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == current_thread())
102
103 #undef osirx_AssertMine
104 extern void osirx_AssertMine(afs_kmutex_t *lockaddr, char *msg);
105
106 #endif /* _RX_KMUTEX_H_ */
107