rf_threadstuff.h revision 1.13 1 /* $NetBSD: rf_threadstuff.h,v 1.13 2002/10/02 21:48:00 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Mark Holland, Daniel Stodolsky, Jim Zelenka
7 *
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29 /*
30 * threadstuff.h -- definitions for threads, locks, and synchronization
31 *
32 * The purpose of this file is provide some illusion of portability.
33 * If the functions below can be implemented with the same semantics on
34 * some new system, then at least the synchronization and thread control
35 * part of the code should not require modification to port to a new machine.
36 * the only other place where the pthread package is explicitly used is
37 * threadid.h
38 *
39 * this file should be included above stdio.h to get some necessary defines.
40 *
41 */
42
43 #ifndef _RF__RF_THREADSTUFF_H_
44 #define _RF__RF_THREADSTUFF_H_
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/kthread.h>
51
52 #include <dev/raidframe/raidframevar.h>
53
54 #define rf_create_managed_mutex(a,b) _rf_create_managed_mutex(a,b,__FILE__,__LINE__)
55 #define rf_create_managed_cond(a,b) _rf_create_managed_cond(a,b,__FILE__,__LINE__)
56 #define rf_init_managed_threadgroup(a,b) _rf_init_managed_threadgroup(a,b,__FILE__,__LINE__)
57 #define rf_init_threadgroup(a) _rf_init_threadgroup(a,__FILE__,__LINE__)
58 #define rf_destroy_threadgroup(a) _rf_destroy_threadgroup(a,__FILE__,__LINE__)
59
60 int _rf_init_threadgroup(RF_ThreadGroup_t * g, char *file, int line);
61 int _rf_destroy_threadgroup(RF_ThreadGroup_t * g, char *file, int line);
62 int
63 _rf_init_managed_threadgroup(RF_ShutdownList_t ** listp,
64 RF_ThreadGroup_t * g, char *file, int line);
65
66 #include <sys/lock.h>
67
68 #define decl_simple_lock_data(a,b) a struct simplelock b;
69 #define simple_lock_addr(a) ((struct simplelock *)&(a))
70
71 typedef struct proc *RF_Thread_t;
72 typedef void *RF_ThreadArg_t;
73
74 #define RF_DECLARE_MUTEX(_m_) decl_simple_lock_data(,(_m_))
75 #define RF_DECLARE_STATIC_MUTEX(_m_) decl_simple_lock_data(static,(_m_))
76 #define RF_DECLARE_EXTERN_MUTEX(_m_) decl_simple_lock_data(extern,(_m_))
77
78 #define RF_DECLARE_COND(_c_) int _c_;
79 #define RF_DECLARE_STATIC_COND(_c_) static int _c_;
80 #define RF_DECLARE_EXTERN_COND(_c_) extern int _c_;
81
82 #define RF_LOCK_MUTEX(_m_) simple_lock(&(_m_))
83 #define RF_UNLOCK_MUTEX(_m_) simple_unlock(&(_m_))
84
85
86 /* non-spinlock */
87 #define decl_lock_data(a,b) a struct lock b;
88
89 #define RF_DECLARE_LKMGR_MUTEX(_m_) decl_lock_data(,(_m_))
90 #define RF_DECLARE_LKMGR_STATIC_MUTEX(_m_) decl_lock_data(static,(_m_))
91 #define RF_DECLARE_LKMGR_EXTERN_MUTEX(_m_) decl_lock_data(extern,(_m_))
92
93 #define RF_LOCK_LKMGR_MUTEX(_m_) lockmgr(&(_m_),LK_EXCLUSIVE,NULL)
94 #define RF_UNLOCK_LKMGR_MUTEX(_m_) lockmgr(&(_m_),LK_RELEASE,NULL)
95
96
97 /*
98 * In NetBSD, kernel threads are simply processes which share several
99 * substructures and never run in userspace.
100 */
101 #define RF_WAIT_COND(_c_,_m_) \
102 ltsleep(&(_c_), PRIBIO, "rfwcond", 0, &(_m_))
103 #define RF_SIGNAL_COND(_c_) wakeup_one(&(_c_))
104 #define RF_BROADCAST_COND(_c_) wakeup(&(_c_))
105 #define RF_CREATE_THREAD(_handle_, _func_, _arg_, _name_) \
106 kthread_create1((void (*)(void *))(_func_), (void *)(_arg_), \
107 (struct proc **)&(_handle_), _name_)
108
109 #define RF_CREATE_ENGINE_THREAD(_handle_, _func_, _arg_, _fmt_, _fmt_arg_) \
110 kthread_create1((void (*)(void *))(_func_), (void *)(_arg_), \
111 (struct proc **)&(_handle_), _fmt_, _fmt_arg_)
112
113 struct RF_ThreadGroup_s {
114 int created;
115 int running;
116 int shutdown;
117 RF_DECLARE_MUTEX(mutex)
118 RF_DECLARE_COND(cond)
119 };
120 /*
121 * Someone has started a thread in the group
122 */
123 #define RF_THREADGROUP_STARTED(_g_) { \
124 RF_LOCK_MUTEX((_g_)->mutex); \
125 (_g_)->created++; \
126 RF_UNLOCK_MUTEX((_g_)->mutex); \
127 }
128
129 /*
130 * Thread announcing that it is now running
131 */
132 #define RF_THREADGROUP_RUNNING(_g_) { \
133 RF_LOCK_MUTEX((_g_)->mutex); \
134 (_g_)->running++; \
135 RF_UNLOCK_MUTEX((_g_)->mutex); \
136 RF_SIGNAL_COND((_g_)->cond); \
137 }
138
139 /*
140 * Thread announcing that it is now done
141 */
142 #define RF_THREADGROUP_DONE(_g_) { \
143 RF_LOCK_MUTEX((_g_)->mutex); \
144 (_g_)->shutdown++; \
145 RF_UNLOCK_MUTEX((_g_)->mutex); \
146 RF_SIGNAL_COND((_g_)->cond); \
147 }
148
149 /*
150 * Wait for all threads to start running
151 */
152 #define RF_THREADGROUP_WAIT_START(_g_) { \
153 RF_LOCK_MUTEX((_g_)->mutex); \
154 while((_g_)->running < (_g_)->created) { \
155 RF_WAIT_COND((_g_)->cond, (_g_)->mutex); \
156 } \
157 RF_UNLOCK_MUTEX((_g_)->mutex); \
158 }
159
160 /*
161 * Wait for all threads to stop running
162 */
163 #ifndef __NetBSD__
164 #define RF_THREADGROUP_WAIT_STOP(_g_) { \
165 RF_LOCK_MUTEX((_g_)->mutex); \
166 RF_ASSERT((_g_)->running == (_g_)->created); \
167 while((_g_)->shutdown < (_g_)->running) { \
168 RF_WAIT_COND((_g_)->cond, (_g_)->mutex); \
169 } \
170 RF_UNLOCK_MUTEX((_g_)->mutex); \
171 }
172 #else
173 /* XXX Note that we've removed the assert. That should get put back in once
174 * we actually get something like a kernel thread running */
175 #define RF_THREADGROUP_WAIT_STOP(_g_) { \
176 RF_LOCK_MUTEX((_g_)->mutex); \
177 while((_g_)->shutdown < (_g_)->running) { \
178 RF_WAIT_COND((_g_)->cond, (_g_)->mutex); \
179 } \
180 RF_UNLOCK_MUTEX((_g_)->mutex); \
181 }
182 #endif
183
184 int rf_mutex_init(struct simplelock *);
185 int rf_mutex_destroy(struct simplelock *);
186 int
187 _rf_create_managed_mutex(RF_ShutdownList_t **, struct simplelock *,
188 char *, int);
189
190 int rf_lkmgr_mutex_init(struct lock *);
191 int rf_lkmgr_mutex_destroy(struct lock *);
192 int
193 _rf_create_managed_lkmgr_mutex(RF_ShutdownList_t **, struct lock *,
194 char *, int);
195
196
197 int
198 _rf_create_managed_cond(RF_ShutdownList_t ** listp, int *,
199 char *file, int line);
200
201 int rf_cond_init(int *c);
202 int rf_cond_destroy(int *c);
203 #endif /* !_RF__RF_THREADSTUFF_H_ */
204