Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / rx / DUX / rx_kmutex.h
1 /* Copyright Transarc Corporation 1998 - All Rights Reserved
2  *
3  * rx_kmutex.h - mutex and condition variable macros for kernel environment.
4  *
5  * DUX implementation.
6  */
7
8 #ifndef _RX_KMUTEX_H_
9 #define _RX_KMUTEX_H_
10
11 #ifdef AFS_DUX40_ENV
12
13 #include <kern/lock.h>
14 #include <kern/sched_prim.h>
15 #include <sys/unix_defs.h>
16
17 #define RX_ENABLE_LOCKS         1
18 #define AFS_GLOBAL_RXLOCK_KERNEL
19
20 /*
21  * Condition variables
22  *
23  * In Digital Unix (OSF/1), we use something akin to the ancient sleep/wakeup
24  * mechanism.  The condition variable itself plays no role; we just use its
25  * address as a convenient unique number.
26  */
27 #define CV_INIT(cv,a,b,c)
28 #define CV_DESTROY(cv)
29 #define CV_WAIT(cv, lck)    { \
30                                 int isGlockOwner = ISAFS_GLOCK(); \
31                                 if (isGlockOwner) AFS_GUNLOCK();  \
32                                 assert_wait((vm_offset_t)(cv), 0);      \
33                                 MUTEX_EXIT(lck);        \
34                                 thread_block();         \
35                                 if (isGlockOwner) AFS_GLOCK();  \
36                                 MUTEX_ENTER(lck); \
37                             }
38
39 #define CV_TIMEDWAIT(cv,lck,t)  { \
40                                 int isGlockOwner = ISAFS_GLOCK(); \
41                                 if (isGlockOwner) AFS_GUNLOCK();  \
42                                 assert_wait((vm_offset_t)(cv), 0);      \
43                                 thread_set_timeout(t);  \
44                                 MUTEX_EXIT(lck);        \
45                                 thread_block();         \
46                                 if (isGlockOwner) AFS_GLOCK();  \
47                                 MUTEX_ENTER(lck);       \
48
49 #define CV_SIGNAL(cv)           thread_wakeup_one((vm_offset_t)(cv))
50 #define CV_BROADCAST(cv)        thread_wakeup((vm_offset_t)(cv))
51
52 typedef struct {
53     simple_lock_data_t lock;
54     thread_t owner;
55 } afs_kmutex_t;
56 typedef int afs_kcondvar_t;
57
58 #define osi_rxWakeup(cv)        thread_wakeup((vm_offset_t)(cv))
59
60 #define LOCK_INIT(a,b) \
61     do { \
62         usimple_lock_init(&(a)->lock); \
63         (a)->owner = (thread_t)0; \
64     } while(0);
65 #define MUTEX_INIT(a,b,c,d) \
66     do { \
67         usimple_lock_init(&(a)->lock); \
68         (a)->owner = (thread_t)0; \
69     } while(0);
70 #define MUTEX_DESTROY(a) \
71     do { \
72         usimple_lock_terminate(&(a)->lock); \
73         (a)->owner = (thread_t)-1; \
74     } while(0);
75 #define MUTEX_ENTER(a) \
76     do { \
77         usimple_lock(&(a)->lock); \
78         osi_Assert((a)->owner == (thread_t)0); \
79         (a)->owner = current_thread(); \
80     } while(0);
81 #define MUTEX_TRYENTER(a) \
82     ( usimple_lock_try(&(a)->lock) ? ((a)->owner = current_thread(), 1) : 0 )
83 #define MUTEX_EXIT(a) \
84     do { \
85         osi_Assert((a)->owner == current_thread()); \
86         (a)->owner = (thread_t)0; \
87         usimple_unlock(&(a)->lock); \
88     } while(0);
89
90 #undef MUTEX_ISMINE
91 #define MUTEX_ISMINE(a) (((afs_kmutex_t *)(a))->owner == current_thread())
92
93 #undef osirx_AssertMine
94 extern void osirx_AssertMine(afs_kmutex_t *lockaddr, char *msg);
95
96 #endif  /* DUX40 */
97
98
99 #endif /* _RX_KMUTEX_H_ */
100