]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/raidframe/rf_threadstuff.h
This commit was generated by cvs2svn to compensate for changes in r106907,
[FreeBSD/FreeBSD.git] / sys / dev / raidframe / rf_threadstuff.h
1 /*      $FreeBSD$ */
2 /*      $NetBSD: rf_threadstuff.h,v 1.10 2001/01/27 20:42:21 oster Exp $        */
3 /*
4  * Copyright (c) 1995 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Mark Holland, Daniel Stodolsky, Jim Zelenka
8  *
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.
14  *
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.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29
30 /*
31  * threadstuff.h -- definitions for threads, locks, and synchronization
32  *
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
38  * threadid.h
39  *
40  * this file should be included above stdio.h to get some necessary defines.
41  *
42  */
43
44 #ifndef _RF__RF_THREADSTUFF_H_
45 #define _RF__RF_THREADSTUFF_H_
46
47 #include <dev/raidframe/rf_types.h>
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #ifdef _KERNEL
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/kthread.h>
54 #endif
55
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__)
61
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);
64 int 
65 _rf_init_managed_threadgroup(RF_ShutdownList_t ** listp,
66     RF_ThreadGroup_t * g, char *file, int line);
67
68 #include <sys/lock.h>
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))
73
74 typedef struct thread *RF_Thread_t;
75 typedef void *RF_ThreadArg_t;
76
77 #ifdef _KERNEL
78 static __inline struct ucred *
79 rf_getucred(RF_Thread_t td)
80 {
81         return (((struct thread *)td)->td_proc->p_ucred);
82 }
83 #endif
84
85 #define RF_LOCK_MUTEX(_m_)              mtx_lock(&(_m_))
86 #define RF_UNLOCK_MUTEX(_m_)            mtx_unlock(&(_m_))
87 #else
88 #define decl_simple_lock_data(a,b) a struct simplelock b;
89 #define simple_lock_addr(a) ((struct simplelock *)&(a))
90
91 typedef struct proc *RF_Thread_t;
92 typedef void *RF_ThreadArg_t;
93
94 static __inline struct ucred *
95 rf_getucred(RF_Thread_t td)
96 {
97         return (((struct proc *)td)->p_ucred);
98 }
99
100 #define RF_LOCK_MUTEX(_m_)              simple_lock(&(_m_))
101 #define RF_UNLOCK_MUTEX(_m_)            simple_unlock(&(_m_))
102 #endif
103
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_))
107
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_;
111
112 /*
113  * In NetBSD, kernel threads are simply processes which share several
114  * substructures and never run in userspace.
115  */
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)     \
125         kthread_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)     \
132         kthread_exit(ret)
133 #else
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)     \
138         kthread_exit(ret);
139 #endif
140 #endif
141
142 struct RF_ThreadGroup_s {
143         int     created;
144         int     running;
145         int     shutdown;
146                 RF_DECLARE_MUTEX(mutex)
147                 RF_DECLARE_COND(cond)
148 };
149 /*
150  * Someone has started a thread in the group
151  */
152 #define RF_THREADGROUP_STARTED(_g_) { \
153         RF_LOCK_MUTEX((_g_)->mutex); \
154         (_g_)->created++; \
155         RF_UNLOCK_MUTEX((_g_)->mutex); \
156 }
157
158 /*
159  * Thread announcing that it is now running
160  */
161 #define RF_THREADGROUP_RUNNING(_g_) { \
162         RF_LOCK_MUTEX((_g_)->mutex); \
163         (_g_)->running++; \
164         RF_UNLOCK_MUTEX((_g_)->mutex); \
165         RF_SIGNAL_COND((_g_)->cond); \
166 }
167
168 /*
169  * Thread announcing that it is now done
170  */
171 #define RF_THREADGROUP_DONE(_g_) { \
172         RF_LOCK_MUTEX((_g_)->mutex); \
173         (_g_)->shutdown++; \
174         RF_UNLOCK_MUTEX((_g_)->mutex); \
175         RF_SIGNAL_COND((_g_)->cond); \
176 }
177
178 /*
179  * Wait for all threads to start running
180  */
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); \
185         } \
186         RF_UNLOCK_MUTEX((_g_)->mutex); \
187 }
188
189 /*
190  * Wait for all threads to stop running
191  */
192 #ifndef __NetBSD__
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); \
198         } \
199         RF_UNLOCK_MUTEX((_g_)->mutex); \
200 }
201 #else
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); \
208         } \
209         RF_UNLOCK_MUTEX((_g_)->mutex); \
210 }
211 #endif
212
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 *,
217                                  char *, int);
218 #else
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 *,
222                                  char *, int);
223 #endif
224 int     _rf_create_managed_cond(RF_ShutdownList_t ** listp, int *,
225                                 char *file, int line);
226
227 int     rf_cond_init(int *c);
228 int     rf_cond_destroy(int *c);
229 #endif                          /* !_RF__RF_THREADSTUFF_H_ */