4 * Implements inline procedure support for the LWKT subsystem.
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
11 * $DragonFly: src/sys/sys/thread2.h,v 1.21 2005/07/07 20:28:26 hmp Exp $
14 #ifndef _SYS_THREAD2_H_
15 #define _SYS_THREAD2_H_
18 * Userland will have its own globaldata which it includes prior to this.
20 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
21 #ifndef _SYS_GLOBALDATA_H_
22 #include <sys/globaldata.h>
24 #ifndef _MACHINE_CPUFUNC_H_
25 #include <machine/cpufunc.h>
30 * Critical sections prevent preemption by raising a thread's priority
31 * above the highest possible interrupting priority. Additionally, the
32 * current cpu will not be able to schedule a new thread but will instead
33 * place it on a pending list (with interrupts physically disabled) and
34 * set mycpu->gd_reqflags to indicate that work needs to be done, which
35 * lwkt_yield_quick() takes care of.
37 * Some of these routines take a struct thread pointer as an argument. This
38 * pointer MUST be curthread and is only passed as an optimization.
40 * Synchronous switching and blocking is allowed while in a critical section.
46 struct thread *td = curthread;
48 td->td_pri += TDPRI_CRIT;
57 crit_enter_quick(struct thread *curtd)
59 curtd->td_pri += TDPRI_CRIT;
64 crit_enter_gd(globaldata_t mygd)
66 crit_enter_quick(mygd->gd_curthread);
70 crit_exit_noyield(struct thread *curtd)
72 curtd->td_pri -= TDPRI_CRIT;
74 if (curtd->td_pri < 0)
77 cpu_ccfence(); /* prevent compiler reordering */
83 thread_t td = curthread;
85 td->td_pri -= TDPRI_CRIT;
90 cpu_ccfence(); /* prevent compiler reordering */
91 if (td->td_gd->gd_reqflags && td->td_pri < TDPRI_CRIT)
96 crit_exit_quick(struct thread *curtd)
98 globaldata_t gd = curtd->td_gd;
100 curtd->td_pri -= TDPRI_CRIT;
101 cpu_ccfence(); /* prevent compiler reordering */
102 if (gd->gd_reqflags && curtd->td_pri < TDPRI_CRIT)
107 crit_exit_gd(globaldata_t mygd)
109 crit_exit_quick(mygd->gd_curthread);
113 crit_test(thread_t td)
115 return(td->td_pri >= TDPRI_CRIT);
119 * Initialize a tokref_t. We only need to initialize the token pointer
120 * and the magic number. We do not have to initialize tr_next, tr_gdreqnext,
124 lwkt_tokref_init(lwkt_tokref_t ref, lwkt_token_t tok)
126 ref->tr_magic = LWKT_TOKREF_MAGIC1;
132 * Return whether any threads are runnable, whether they meet mp_lock
133 * requirements or not.
138 return (mycpu->gd_runqmask != 0);
142 lwkt_getpri(thread_t td)
144 return(td->td_pri & TDPRI_MASK);
148 lwkt_getpri_self(void)
150 return(lwkt_getpri(curthread));