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