xref: /dragonfly/sys/sys/thread2.h (revision 9c600e7d)
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.8 2003/07/12 17:54:36 dillon Exp $
12  */
13 
14 #ifndef _SYS_THREAD2_H_
15 #define _SYS_THREAD2_H_
16 
17 /*
18  * Critical sections prevent preemption by raising a thread's priority
19  * above the highest possible interrupting priority.  Additionally, the
20  * current cpu will not be able to schedule a new thread but will instead
21  * place it on a pending list (with interrupts physically disabled) and
22  * set mycpu->gd_reqflags to indicate that work needs to be done, which
23  * lwkt_yield_quick() takes care of.
24  *
25  * Synchronous switching and blocking is allowed while in a critical section.
26  */
27 
28 static __inline void
29 crit_enter(void)
30 {
31     struct thread *td = curthread;
32 
33     if (td->td_pri < 0)
34 	crit_panic();
35     td->td_pri += TDPRI_CRIT;
36 }
37 
38 static __inline void
39 crit_exit_noyield(void)
40 {
41     thread_t td = curthread;
42 
43     td->td_pri -= TDPRI_CRIT;
44     if (td->td_pri < 0)
45 	crit_panic();
46 }
47 
48 static __inline void
49 crit_exit(void)
50 {
51     thread_t td = curthread;
52 
53     td->td_pri -= TDPRI_CRIT;
54     if (td->td_pri < 0)
55 	crit_panic();
56     if (td->td_pri < TDPRI_CRIT && mycpu->gd_reqflags)
57 	lwkt_yield_quick();
58 }
59 
60 static __inline int
61 crit_panic_save(void)
62 {
63     thread_t td = curthread;
64     int pri = td->td_pri;
65     td->td_pri = td->td_pri & TDPRI_MASK;
66     return(pri);
67 }
68 
69 static __inline void
70 crit_panic_restore(int cpri)
71 {
72     curthread->td_pri = cpri;
73 }
74 
75 static __inline int
76 lwkt_havetoken(lwkt_token_t tok)
77 {
78     return (tok->t_cpu == mycpu->gd_cpuid);
79 }
80 
81 /*
82  * Return whether any threads are runnable, whether they meet mp_lock
83  * requirements or not.
84  */
85 static __inline int
86 lwkt_runnable(void)
87 {
88     return (mycpu->gd_runqmask != 0);
89 }
90 
91 #endif
92 
93