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