2 /* $NetBSD: rf_threadstuff.h,v 1.10 2001/01/27 20:42:21 oster Exp $ */
4 * Copyright (c) 1995 Carnegie-Mellon University.
7 * Author: Mark Holland, Daniel Stodolsky, Jim Zelenka
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
31 * threadstuff.h -- definitions for threads, locks, and synchronization
33 * The purpose of this file is provide some illusion of portability.
34 * If the functions below can be implemented with the same semantics on
35 * some new system, then at least the synchronization and thread control
36 * part of the code should not require modification to port to a new machine.
37 * the only other place where the pthread package is explicitly used is
40 * this file should be included above stdio.h to get some necessary defines.
44 #ifndef _RF__RF_THREADSTUFF_H_
45 #define _RF__RF_THREADSTUFF_H_
47 #include <dev/raidframe/rf_types.h>
48 #include <sys/types.h>
49 #include <sys/param.h>
51 #include <sys/systm.h>
53 #include <sys/kthread.h>
56 #define rf_create_managed_mutex(a,b) _rf_create_managed_mutex(a,b,__FILE__,__LINE__)
57 #define rf_create_managed_cond(a,b) _rf_create_managed_cond(a,b,__FILE__,__LINE__)
58 #define rf_init_managed_threadgroup(a,b) _rf_init_managed_threadgroup(a,b,__FILE__,__LINE__)
59 #define rf_init_threadgroup(a) _rf_init_threadgroup(a,__FILE__,__LINE__)
60 #define rf_destroy_threadgroup(a) _rf_destroy_threadgroup(a,__FILE__,__LINE__)
62 int _rf_init_threadgroup(RF_ThreadGroup_t * g, char *file, int line);
63 int _rf_destroy_threadgroup(RF_ThreadGroup_t * g, char *file, int line);
65 _rf_init_managed_threadgroup(RF_ShutdownList_t ** listp,
66 RF_ThreadGroup_t * g, char *file, int line);
69 #if defined(__FreeBSD__ ) && __FreeBSD_version > 500005
70 #include <sys/mutex.h>
71 #define decl_simple_lock_data(a,b) a struct mtx b;
72 #define simple_lock_addr(a) ((struct mtx *)&(a))
74 typedef struct thread *RF_Thread_t;
75 typedef void *RF_ThreadArg_t;
78 static __inline struct ucred *
79 rf_getucred(RF_Thread_t td)
81 return (((struct thread *)td)->td_proc->p_ucred);
85 #define RF_LOCK_MUTEX(_m_) mtx_lock(&(_m_))
86 #define RF_UNLOCK_MUTEX(_m_) mtx_unlock(&(_m_))
88 #define decl_simple_lock_data(a,b) a struct simplelock b;
89 #define simple_lock_addr(a) ((struct simplelock *)&(a))
91 typedef struct proc *RF_Thread_t;
92 typedef void *RF_ThreadArg_t;
94 static __inline struct ucred *
95 rf_getucred(RF_Thread_t td)
97 return (((struct proc *)td)->p_ucred);
100 #define RF_LOCK_MUTEX(_m_) simple_lock(&(_m_))
101 #define RF_UNLOCK_MUTEX(_m_) simple_unlock(&(_m_))
104 #define RF_DECLARE_MUTEX(_m_) decl_simple_lock_data(,(_m_))
105 #define RF_DECLARE_STATIC_MUTEX(_m_) decl_simple_lock_data(static,(_m_))
106 #define RF_DECLARE_EXTERN_MUTEX(_m_) decl_simple_lock_data(extern,(_m_))
108 #define RF_DECLARE_COND(_c_) int _c_;
109 #define RF_DECLARE_STATIC_COND(_c_) static int _c_;
110 #define RF_DECLARE_EXTERN_COND(_c_) extern int _c_;
113 * In NetBSD, kernel threads are simply processes which share several
114 * substructures and never run in userspace.
116 #define RF_WAIT_COND(_c_,_m_) \
117 RF_LTSLEEP(&(_c_), PRIBIO, "rfwcond", 0, &(_m_))
118 #define RF_SIGNAL_COND(_c_) wakeup_one(&(_c_))
119 #define RF_BROADCAST_COND(_c_) wakeup(&(_c_))
120 #if defined(__NetBSD__)
121 #define RF_CREATE_THREAD(_handle_, _func_, _arg_, _name_) \
122 kthread_create1((void (*)(void *))(_func_), (void *)(_arg_), \
123 (struct proc **)&(_handle_), _name_)
124 #define RF_THREAD_EXIT(ret) \
126 #elif defined(__FreeBSD__)
127 #if __FreeBSD_version > 500005
128 #define RF_CREATE_THREAD(_handle_, _func_, _arg_, _name_) \
129 kthread_create((void (*)(void *))(_func_), (void *)(_arg_), \
130 (struct proc **)&(_handle_), 0, 4, _name_)
131 #define RF_THREAD_EXIT(ret) \
134 #define RF_CREATE_THREAD(_handle_, _func_, _arg_, _name_) \
135 kthread_create((void (*)(void *))(_func_), (void *)(_arg_), \
136 (struct proc **)&(_handle_), _name_)
137 #define RF_THREAD_EXIT(ret) \
142 struct RF_ThreadGroup_s {
146 RF_DECLARE_MUTEX(mutex)
147 RF_DECLARE_COND(cond)
150 * Someone has started a thread in the group
152 #define RF_THREADGROUP_STARTED(_g_) { \
153 RF_LOCK_MUTEX((_g_)->mutex); \
155 RF_UNLOCK_MUTEX((_g_)->mutex); \
159 * Thread announcing that it is now running
161 #define RF_THREADGROUP_RUNNING(_g_) { \
162 RF_LOCK_MUTEX((_g_)->mutex); \
164 RF_UNLOCK_MUTEX((_g_)->mutex); \
165 RF_SIGNAL_COND((_g_)->cond); \
169 * Thread announcing that it is now done
171 #define RF_THREADGROUP_DONE(_g_) { \
172 RF_LOCK_MUTEX((_g_)->mutex); \
174 RF_UNLOCK_MUTEX((_g_)->mutex); \
175 RF_SIGNAL_COND((_g_)->cond); \
179 * Wait for all threads to start running
181 #define RF_THREADGROUP_WAIT_START(_g_) { \
182 RF_LOCK_MUTEX((_g_)->mutex); \
183 while((_g_)->running < (_g_)->created) { \
184 RF_WAIT_COND((_g_)->cond, (_g_)->mutex); \
186 RF_UNLOCK_MUTEX((_g_)->mutex); \
190 * Wait for all threads to stop running
193 #define RF_THREADGROUP_WAIT_STOP(_g_) { \
194 RF_LOCK_MUTEX((_g_)->mutex); \
195 RF_ASSERT((_g_)->running == (_g_)->created); \
196 while((_g_)->shutdown < (_g_)->running) { \
197 RF_WAIT_COND((_g_)->cond, (_g_)->mutex); \
199 RF_UNLOCK_MUTEX((_g_)->mutex); \
202 /* XXX Note that we've removed the assert. That should get put back in once
203 * we actually get something like a kernel thread running */
204 #define RF_THREADGROUP_WAIT_STOP(_g_) { \
205 RF_LOCK_MUTEX((_g_)->mutex); \
206 while((_g_)->shutdown < (_g_)->running) { \
207 RF_WAIT_COND((_g_)->cond, (_g_)->mutex); \
209 RF_UNLOCK_MUTEX((_g_)->mutex); \
213 #if defined(__FreeBSD__) && __FreeBSD_version > 500005
214 int rf_mutex_init(struct mtx *, const char *);
215 int rf_mutex_destroy(struct mtx *);
216 int _rf_create_managed_mutex(RF_ShutdownList_t **, struct mtx *,
219 int rf_mutex_init(struct simplelock *, const char *);
220 int rf_mutex_destroy(struct simplelock *);
221 int _rf_create_managed_mutex(RF_ShutdownList_t **, struct simplelock *,
224 int _rf_create_managed_cond(RF_ShutdownList_t ** listp, int *,
225 char *file, int line);
227 int rf_cond_init(int *c);
228 int rf_cond_destroy(int *c);
229 #endif /* !_RF__RF_THREADSTUFF_H_ */