70ca3e7ef5d6f868399efb3a5b2d64671c8094cd
[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.6 2003/07/08 06:27:28 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_reqpri 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 static __inline void
28 crit_enter(void)
29 {
30     struct thread *td = curthread;
31
32     if (td->td_pri < 0)
33         crit_panic();
34     td->td_pri += TDPRI_CRIT;
35 }
36
37 static __inline void
38 crit_exit_noyield(void)
39 {
40     thread_t td = curthread;
41
42     td->td_pri -= TDPRI_CRIT;
43     if (td->td_pri < 0)
44         crit_panic();
45 }
46
47 static __inline void
48 crit_exit(void)
49 {
50     thread_t td = curthread;
51
52     td->td_pri -= TDPRI_CRIT;
53     if (td->td_pri < 0)
54         crit_panic();
55     if (td->td_pri < mycpu->gd_reqpri)
56         lwkt_yield_quick();
57 }
58
59 static __inline int
60 crit_panic_save(void)
61 {
62     thread_t td = curthread;
63     int pri = td->td_pri;
64     td->td_pri = td->td_pri & TDPRI_MASK;
65     return(pri);
66 }
67
68 static __inline void
69 crit_panic_restore(int cpri)
70 {
71     curthread->td_pri = cpri;
72 }
73
74 static __inline int
75 lwkt_havetoken(lwkt_token_t tok)
76 {
77     return (tok->t_cpu == mycpu->gd_cpuid);
78 }
79
80 /*
81  * Return whether any threads are runnable, whether they meet mp_lock
82  * requirements or not.
83  */
84 static __inline int
85 lwkt_runnable(void)
86 {
87     return (mycpu->gd_runqmask != 0);
88 }
89
90 #endif
91