7c4e2f2f9fe1342a3c69b120e8b16f7f03a7c202
[dragonfly.git] / sys / sys / thread2.h
1 /*
2  * SYS/THREAD2.H
3  *
4  * Implements inline procedure support for the LWKT subsystem. 
5  *
6  * Generally speaking these routines only operate on threads associated
7  * with the current cpu.  For example, a higher priority thread pending
8  * on a different cpu will not be immediately scheduled by a yield() on
9  * this cpu.
10  *
11  * $DragonFly: src/sys/sys/thread2.h,v 1.28 2006/12/23 00:27:03 swildner Exp $
12  */
13
14 #ifndef _SYS_THREAD2_H_
15 #define _SYS_THREAD2_H_
16
17 #ifndef _KERNEL
18
19 #error "This file should not be included by userland programs."
20
21 #else
22
23 /*
24  * Userland will have its own globaldata which it includes prior to this.
25  */
26 #ifndef _SYS_SYSTM_H_
27 #include <sys/systm.h>
28 #endif
29 #ifndef _SYS_GLOBALDATA_H_
30 #include <sys/globaldata.h>
31 #endif
32 #ifndef _MACHINE_CPUFUNC_H_
33 #include <machine/cpufunc.h>
34 #endif
35
36 /*
37  * Critical section debugging
38  */
39 #ifdef DEBUG_CRIT_SECTIONS
40 #define __DEBUG_CRIT_ARG__              const char *id
41 #define __DEBUG_CRIT_ADD_ARG__          , const char *id
42 #define __DEBUG_CRIT_PASS_ARG__         , id
43 #define __DEBUG_CRIT_ENTER(td)          _debug_crit_enter((td), id)
44 #define __DEBUG_CRIT_EXIT(td)           _debug_crit_exit((td), id)
45 #define crit_enter()                    _crit_enter(mycpu, __FUNCTION__)
46 #define crit_enter_id(id)               _crit_enter(mycpu, id)
47 #define crit_enter_gd(curgd)            _crit_enter((curgd), __FUNCTION__)
48 #define crit_enter_quick(curtd)         _crit_enter_quick((curtd), __FUNCTION__)
49 #define crit_enter_hard()               _crit_enter_hard(mycpu, __FUNCTION__)
50 #define crit_enter_hard_gd(curgd)       _crit_enter_hard((curgd), __FUNCTION__)
51 #define crit_exit()                     _crit_exit(mycpu, __FUNCTION__)
52 #define crit_exit_id(id)                _crit_exit(mycpu, id)
53 #define crit_exit_gd(curgd)             _crit_exit((curgd), __FUNCTION__)
54 #define crit_exit_quick(curtd)          _crit_exit_quick((curtd), __FUNCTION__)
55 #define crit_exit_hard()                _crit_exit_hard(mycpu, __FUNCTION__)
56 #define crit_exit_hard_gd(curgd)        _crit_exit_hard((curgd), __FUNCTION__)
57 #define crit_exit_noyield(curtd)        _crit_exit_noyield((curtd),__FUNCTION__)
58 #else
59 #define __DEBUG_CRIT_ARG__              void
60 #define __DEBUG_CRIT_ADD_ARG__
61 #define __DEBUG_CRIT_PASS_ARG__
62 #define __DEBUG_CRIT_ENTER(td)
63 #define __DEBUG_CRIT_EXIT(td)
64 #define crit_enter()                    _crit_enter(mycpu)
65 #define crit_enter_id(id)               _crit_enter(mycpu)
66 #define crit_enter_gd(curgd)            _crit_enter((curgd))
67 #define crit_enter_quick(curtd)         _crit_enter_quick((curtd))
68 #define crit_enter_hard()               _crit_enter_hard(mycpu)
69 #define crit_enter_hard_gd(curgd)       _crit_enter_hard((curgd))
70 #define crit_exit()                     crit_exit_wrapper()
71 #define crit_exit_id(id)                _crit_exit(mycpu)
72 #define crit_exit_gd(curgd)             _crit_exit((curgd))
73 #define crit_exit_quick(curtd)          _crit_exit_quick((curtd))
74 #define crit_exit_hard()                _crit_exit_hard(mycpu)
75 #define crit_exit_hard_gd(curgd)        _crit_exit_hard((curgd))
76 #define crit_exit_noyield(curtd)        _crit_exit_noyield((curtd))
77 #endif
78
79 /*
80  * Track crit_enter()/crit_exit() pairs and warn on mismatches.
81  */
82 #ifdef DEBUG_CRIT_SECTIONS
83
84 static __inline void
85 _debug_crit_enter(thread_t td, const char *id)
86 {
87     int wi = td->td_crit_debug_index;
88
89     td->td_crit_debug_array[wi & CRIT_DEBUG_ARRAY_MASK] = id;
90     ++td->td_crit_debug_index;
91 }
92
93 static __inline void
94 _debug_crit_exit(thread_t td, const char *id)
95 {
96     const char *gid;
97     int wi;
98
99     wi = td->td_crit_debug_index - 1;
100     if ((gid = td->td_crit_debug_array[wi & CRIT_DEBUG_ARRAY_MASK]) != id) {
101         if (td->td_in_crit_report == 0) {
102             td->td_in_crit_report = 1;
103             kprintf("crit_exit(%s) expected id %s\n", id, gid);
104             td->td_in_crit_report = 0;
105         }
106     }
107     --td->td_crit_debug_index;
108 }
109
110 #endif
111
112 /*
113  * Critical sections prevent preemption, but allowing explicit blocking
114  * and thread switching.  Any interrupt occuring while in a critical
115  * section is made pending and returns immediately.  Interrupts are not
116  * physically disabled.
117  *
118  * Hard critical sections prevent preemption and disallow any blocking
119  * or thread switching, and in addition will assert on any blockable
120  * operation (acquire token not already held, lockmgr, mutex ops, or
121  * splz).  Spinlocks can still be used in hard sections.
122  *
123  * All critical section routines only operate on the current thread.
124  * Passed gd or td arguments are simply optimizations when mycpu or
125  * curthread is already available to the caller.
126  */
127
128 /*
129  * crit_enter
130  */
131 static __inline void
132 _crit_enter_quick(thread_t td __DEBUG_CRIT_ADD_ARG__)
133 {
134     ++td->td_critcount;
135     __DEBUG_CRIT_ENTER(td);
136     cpu_ccfence();
137 }
138
139 static __inline void
140 _crit_enter(globaldata_t gd __DEBUG_CRIT_ADD_ARG__)
141 {
142     _crit_enter_quick(gd->gd_curthread __DEBUG_CRIT_PASS_ARG__);
143 }
144
145 static __inline void
146 _crit_enter_hard(globaldata_t gd __DEBUG_CRIT_ADD_ARG__)
147 {
148     _crit_enter_quick(gd->gd_curthread __DEBUG_CRIT_PASS_ARG__);
149     ++gd->gd_intr_nesting_level;
150 }
151
152
153 /*
154  * crit_exit*()
155  *
156  * NOTE: Conditionalizing just gd_reqflags, a case which is virtually
157  *       never true regardless of crit_count, should result in 100%
158  *       optimal code execution.  We don't check crit_count because
159  *       it just bloats the inline and does not improve performance.
160  *
161  * NOTE: This can produce a considerable amount of code despite the
162  *       relatively few lines of code so the non-debug case typically
163  *       just wraps it in a real function, crit_exit_wrapper().
164  */
165 static __inline void
166 _crit_exit_noyield(thread_t td __DEBUG_CRIT_ADD_ARG__)
167 {
168     __DEBUG_CRIT_EXIT(td);
169     --td->td_critcount;
170 #ifdef INVARIANTS
171     if (__predict_false(td->td_critcount < 0))
172         crit_panic();
173 #endif
174     cpu_ccfence();      /* prevent compiler reordering */
175 }
176
177 static __inline void
178 _crit_exit_quick(thread_t td __DEBUG_CRIT_ADD_ARG__)
179 {
180     _crit_exit_noyield(td __DEBUG_CRIT_PASS_ARG__);
181     if (__predict_false(td->td_gd->gd_reqflags & RQF_IDLECHECK_MASK))
182         lwkt_maybe_splz(td);
183 }
184
185 static __inline void
186 _crit_exit(globaldata_t gd __DEBUG_CRIT_ADD_ARG__)
187 {
188     _crit_exit_quick(gd->gd_curthread __DEBUG_CRIT_PASS_ARG__);
189 }
190
191 static __inline void
192 _crit_exit_hard(globaldata_t gd __DEBUG_CRIT_ADD_ARG__)
193 {
194     --gd->gd_intr_nesting_level;
195     _crit_exit_quick(gd->gd_curthread __DEBUG_CRIT_PASS_ARG__);
196 }
197
198 static __inline int
199 crit_test(thread_t td)
200 {
201     return(td->td_critcount);
202 }
203
204 /*
205  * Return whether any threads are runnable, whether they meet mp_lock
206  * requirements or not.
207  */
208 static __inline int
209 lwkt_runnable(void)
210 {
211     return (TAILQ_FIRST(&mycpu->gd_tdrunq) != NULL);
212 }
213
214 static __inline int
215 lwkt_getpri(thread_t td)
216 {
217     return(td->td_pri);
218 }
219
220 static __inline int
221 lwkt_getpri_self(void)
222 {
223     return(lwkt_getpri(curthread));
224 }
225
226 /*
227  * Reduce our priority in preparation for a return to userland.  If
228  * our passive release function was still in place, our priority was
229  * never raised and does not need to be reduced.
230  *
231  * See also lwkt_passive_release() and platform/blah/trap.c
232  */
233 static __inline void
234 lwkt_passive_recover(thread_t td)
235 {
236     if (td->td_release == NULL)
237         lwkt_setpri_self(TDPRI_USER_NORM);
238     td->td_release = NULL;
239 }
240
241 #ifdef SMP
242
243 /*
244  * IPIQ messaging wrappers.  IPIQ remote functions are passed three arguments:
245  * a void * pointer, an integer, and a pointer to the trap frame (or NULL if
246  * the trap frame is not known).  However, we wish to provide opaque 
247  * interfaces for simpler callbacks... the basic IPI messaging function as
248  * used by the kernel takes a single argument.
249  */
250 static __inline int
251 lwkt_send_ipiq(globaldata_t target, ipifunc1_t func, void *arg)
252 {
253     return(lwkt_send_ipiq3(target, (ipifunc3_t)func, arg, 0));
254 }
255
256 static __inline int
257 lwkt_send_ipiq2(globaldata_t target, ipifunc2_t func, void *arg1, int arg2)
258 {
259     return(lwkt_send_ipiq3(target, (ipifunc3_t)func, arg1, arg2));
260 }
261
262 static __inline int
263 lwkt_send_ipiq_mask(u_int32_t mask, ipifunc1_t func, void *arg)
264 {
265     return(lwkt_send_ipiq3_mask(mask, (ipifunc3_t)func, arg, 0));
266 }
267
268 static __inline int
269 lwkt_send_ipiq2_mask(u_int32_t mask, ipifunc2_t func, void *arg1, int arg2)
270 {
271     return(lwkt_send_ipiq3_mask(mask, (ipifunc3_t)func, arg1, arg2));
272 }
273
274 static __inline int
275 lwkt_send_ipiq_nowait(globaldata_t target, ipifunc1_t func, void *arg)
276 {
277     return(lwkt_send_ipiq3_nowait(target, (ipifunc3_t)func, arg, 0));
278 }
279
280 static __inline int
281 lwkt_send_ipiq2_nowait(globaldata_t target, ipifunc2_t func, 
282                        void *arg1, int arg2)
283 {
284     return(lwkt_send_ipiq3_nowait(target, (ipifunc3_t)func, arg1, arg2));
285 }
286
287 static __inline int
288 lwkt_send_ipiq_passive(globaldata_t target, ipifunc1_t func, void *arg)
289 {
290     return(lwkt_send_ipiq3_passive(target, (ipifunc3_t)func, arg, 0));
291 }
292
293 static __inline int
294 lwkt_send_ipiq2_passive(globaldata_t target, ipifunc2_t func, 
295                        void *arg1, int arg2)
296 {
297     return(lwkt_send_ipiq3_passive(target, (ipifunc3_t)func, arg1, arg2));
298 }
299
300 static __inline int
301 lwkt_send_ipiq_bycpu(int dcpu, ipifunc1_t func, void *arg)
302 {
303     return(lwkt_send_ipiq3_bycpu(dcpu, (ipifunc3_t)func, arg, 0));
304 }
305
306 static __inline int
307 lwkt_send_ipiq2_bycpu(int dcpu, ipifunc2_t func, void *arg1, int arg2)
308 {
309     return(lwkt_send_ipiq3_bycpu(dcpu, (ipifunc3_t)func, arg1, arg2));
310 }
311
312 #endif  /* SMP */
313 #endif  /* _KERNEL */
314 #endif  /* _SYS_THREAD2_H_ */
315