4 * Implements the architecture independant portion of the LWKT
7 * Types which must already be defined when this header is included by
8 * userland: struct md_thread
10 * $DragonFly: src/sys/sys/thread.h,v 1.46 2004/02/17 19:38:50 dillon Exp $
13 #ifndef _SYS_THREAD_H_
14 #define _SYS_THREAD_H_
16 #ifndef _SYS_STDINT_H_
17 #include <sys/stdint.h> /* __int types */
20 #include <sys/param.h> /* MAXCOMLEN */
23 #include <sys/queue.h> /* TAILQ_* macros */
25 #ifndef _SYS_MSGPORT_H_
26 #include <sys/msgport.h> /* lwkt_port */
44 typedef struct lwkt_queue *lwkt_queue_t;
45 typedef struct lwkt_token *lwkt_token_t;
46 typedef struct lwkt_wait *lwkt_wait_t;
47 typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t;
48 typedef struct lwkt_cpu_port *lwkt_cpu_port_t;
49 typedef struct lwkt_rwlock *lwkt_rwlock_t;
50 typedef struct lwkt_ipiq *lwkt_ipiq_t;
51 typedef struct lwkt_cpusync *lwkt_cpusync_t;
52 typedef struct thread *thread_t;
54 typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
57 * Differentiation between kernel threads and user threads. Userland
58 * programs which want to access to kernel structures have to define
59 * _KERNEL_STRUCTURES. This is a kinda safety valve to prevent badly
60 * written user programs from getting an LWKT thread that is neither the
61 * kernel nor the user version.
63 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
64 #ifndef _MACHINE_THREAD_H_
65 #include <machine/thread.h> /* md_thread */
67 #ifndef _MACHINE_FRAME_H_
68 #include <machine/frame.h>
75 * Tokens arbitrate access to information. They are 'soft' arbitrators
76 * in that they are associated with cpus rather then threads, making the
77 * optimal aquisition case very fast if your cpu already happens to own the
78 * token you are requesting.
80 typedef struct lwkt_token {
81 struct globaldata *t_cpu; /* the current owner of the token */
82 struct globaldata *t_reqcpu;/* return ownership to this cpu on release */
83 int t_gen; /* generation number */
87 * Wait structures deal with blocked threads. Due to the way remote cpus
88 * interact with these structures stable storage must be used.
90 typedef struct lwkt_wait {
91 lwkt_queue wa_waitq; /* list of waiting threads */
92 lwkt_token wa_token; /* who currently owns the list */
97 #define MAXCPUFIFO 16 /* power of 2 */
98 #define MAXCPUFIFO_MASK (MAXCPUFIFO - 1)
101 * Always cast to ipifunc_t when registering an ipi. The actual ipi function
102 * is called with both the data and an interrupt frame, but the ipi function
103 * that is registered might only declare a data argument.
105 typedef void (*ipifunc_t)(void *arg);
106 typedef void (*ipifunc2_t)(void *arg, struct intrframe *frame);
108 typedef struct lwkt_ipiq {
109 int ip_rindex; /* only written by target cpu */
110 int ip_xindex; /* writte by target, indicates completion */
111 int ip_windex; /* only written by source cpu */
112 ipifunc2_t ip_func[MAXCPUFIFO];
113 void *ip_arg[MAXCPUFIFO];
118 * CPU Synchronization structure. See lwkt_cpusync_start() and
119 * lwkt_cpusync_finish() for more information.
121 typedef void (*cpusync_func_t)(lwkt_cpusync_t poll);
122 typedef void (*cpusync_func2_t)(void *data);
124 struct lwkt_cpusync {
125 cpusync_func_t cs_run_func; /* run (tandem w/ acquire) */
126 cpusync_func_t cs_fin1_func; /* fin1 (synchronized) */
127 cpusync_func2_t cs_fin2_func; /* fin2 (tandem w/ release) */
130 volatile int cs_count;
135 * The standard message and queue structure used for communications between
136 * cpus. Messages are typically queued via a machine-specific non-linked
137 * FIFO matrix allowing any cpu to send a message to any other cpu without
140 typedef struct lwkt_cpu_msg {
141 void (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */
142 int cm_code; /* request code if applicable */
143 int cm_cpu; /* reply to cpu */
144 thread_t cm_originator; /* originating thread for wakeup */
150 typedef struct lwkt_rwlock {
157 #define rw_token rw_wait.wa_token
160 * Thread structure. Note that ownership of a thread structure is special
161 * cased and there is no 'token'. A thread is always owned by the cpu
162 * represented by td_gd, any manipulation of the thread by some other cpu
163 * must be done through cpu_*msg() functions. e.g. you could request
164 * ownership of a thread that way, or hand a thread off to another cpu.
166 * NOTE: td_pri is bumped by TDPRI_CRIT when entering a critical section,
167 * but this does not effect how the thread is scheduled by LWKT.
173 TAILQ_ENTRY(thread) td_threadq;
174 TAILQ_ENTRY(thread) td_allq;
175 lwkt_port td_msgport; /* built-in message port for replies */
176 struct proc *td_proc; /* (optional) associated process */
177 struct pcb *td_pcb; /* points to pcb and top of kstack */
178 struct globaldata *td_gd; /* associated with this cpu */
179 const char *td_wmesg; /* string name for blockage */
180 void *td_wchan; /* waiting on channel */
181 int td_pri; /* 0-31, 31=highest priority (note 1) */
182 int td_flags; /* TDF flags */
183 int td_gen; /* wait queue chasing generation number */
185 void (*td_preemptable)(struct thread *td, int critpri);
186 void (*td_release)(struct thread *td);
188 struct md_intr_info *intdata;
190 char *td_kstack; /* kernel stack */
191 char *td_sp; /* kernel stack pointer for LWKT restore */
192 void (*td_switch)(struct thread *ntd);
193 lwkt_wait_t td_wait; /* thread sitting on wait structure */
194 __uint64_t td_uticks; /* Statclock hits in user mode (uS) */
195 __uint64_t td_sticks; /* Statclock hits in system mode (uS) */
196 __uint64_t td_iticks; /* Statclock hits processing intr (uS) */
197 int td_locks; /* lockmgr lock debugging YYY */
198 int td_refs; /* hold position in gd_tdallq / hold free */
199 int td_nest_count; /* prevent splz nesting */
201 int td_mpcount; /* MP lock held (count) */
202 int td_cscount; /* cpu synchronization master */
207 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
208 struct thread *td_preempted; /* we preempted this thread */
209 struct caps_kinfo *td_caps; /* list of client and server registrations */
210 struct md_thread td_mach;
214 * Thread flags. Note that TDF_RUNNING is cleared on the old thread after
215 * we switch to the new one, which is necessary because LWKTs don't need
216 * to hold the BGL. This flag is used by the exit code and the managed
217 * thread migration code.
219 * LWKT threads stay on their (per-cpu) run queue while running, not to
220 * be confused with user processes which are removed from the user scheduling
221 * run queue while actually running.
223 #define TDF_RUNNING 0x0001 /* thread still active */
224 #define TDF_RUNQ 0x0002 /* on an LWKT run queue */
225 #define TDF_PREEMPT_LOCK 0x0004 /* I have been preempted */
226 #define TDF_PREEMPT_DONE 0x0008 /* acknowledge preemption complete */
227 #define TDF_IDLE_NOHLT 0x0010 /* we need to spin */
229 #define TDF_SYSTHREAD 0x0100 /* system thread */
230 #define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */
231 #define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */
232 #define TDF_VERBOSE 0x0800 /* verbose on exit */
233 #define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */
234 #define TDF_STOPREQ 0x2000 /* suspend_kproc */
235 #define TDF_WAKEREQ 0x4000 /* resume_kproc */
236 #define TDF_TIMEOUT 0x8000 /* tsleep timeout */
237 #define TDF_INTTHREAD 0x00010000 /* interrupt thread */
240 * Thread priorities. Typically only one thread from any given
241 * user process scheduling queue is on the LWKT run queue at a time.
242 * Remember that there is one LWKT run queue per cpu.
244 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
245 * causes interrupts to be masked as they occur. When this occurs a
246 * rollup flag will be set in mycpu->gd_reqflags.
248 #define TDPRI_IDLE_THREAD 0 /* the idle thread */
249 #define TDPRI_USER_IDLE 4 /* user scheduler idle */
250 #define TDPRI_USER_NORM 6 /* user scheduler normal */
251 #define TDPRI_USER_REAL 8 /* user scheduler real time */
252 #define TDPRI_KERN_LPSCHED 9 /* scheduler helper for userland sch */
253 #define TDPRI_KERN_USER 10 /* kernel / block in syscall */
254 #define TDPRI_KERN_DAEMON 12 /* kernel daemon (pageout, etc) */
255 #define TDPRI_SOFT_NORM 14 /* kernel / normal */
256 #define TDPRI_SOFT_TIMER 16 /* kernel / timer */
257 #define TDPRI_EXITING 19 /* exiting thread */
258 #define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
259 #define TDPRI_INT_LOW 27 /* low priority interrupt */
260 #define TDPRI_INT_MED 28 /* medium priority interrupt */
261 #define TDPRI_INT_HIGH 29 /* high priority interrupt */
264 #define TDPRI_MASK 31
265 #define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
267 #define CACHE_NTHREADS 6
269 #define IN_CRITICAL_SECT(td) ((td)->td_pri >= TDPRI_CRIT)
273 extern struct vm_zone *thread_zone;
278 * Applies both to the kernel and to liblwkt.
280 extern struct thread *lwkt_alloc_thread(struct thread *template, int cpu);
281 extern void lwkt_init_thread(struct thread *td, void *stack, int flags,
282 struct globaldata *gd);
283 extern void lwkt_set_comm(thread_t td, const char *ctl, ...);
284 extern void lwkt_wait_free(struct thread *td);
285 extern void lwkt_free_thread(struct thread *td);
286 extern void lwkt_init_wait(struct lwkt_wait *w);
287 extern void lwkt_gdinit(struct globaldata *gd);
288 extern void lwkt_switch(void);
289 extern void lwkt_maybe_switch(void);
290 extern void lwkt_preempt(thread_t ntd, int critpri);
291 extern void lwkt_schedule(thread_t td);
292 extern void lwkt_schedule_self(void);
293 extern void lwkt_deschedule(thread_t td);
294 extern void lwkt_deschedule_self(void);
295 extern void lwkt_acquire(thread_t td);
296 extern void lwkt_yield(void);
297 extern void lwkt_yield_quick(void);
298 extern void lwkt_hold(thread_t td);
299 extern void lwkt_rele(thread_t td);
301 extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen);
302 extern void lwkt_signal(lwkt_wait_t w, int count);
303 extern int lwkt_trytoken(lwkt_token_t tok);
304 extern int lwkt_gettoken(lwkt_token_t tok);
305 extern int lwkt_gentoken(lwkt_token_t tok, int *gen);
306 extern int lwkt_reltoken(lwkt_token_t tok);
307 extern void lwkt_inittoken(lwkt_token_t tok);
308 extern int lwkt_regettoken(lwkt_token_t tok);
309 extern void lwkt_rwlock_init(lwkt_rwlock_t lock);
310 extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg);
311 extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg);
312 extern void lwkt_exunlock(lwkt_rwlock_t lock);
313 extern void lwkt_shunlock(lwkt_rwlock_t lock);
314 extern void lwkt_setpri(thread_t td, int pri);
315 extern void lwkt_setpri_self(int pri);
316 extern int lwkt_send_ipiq(struct globaldata *targ, ipifunc_t func, void *arg);
317 extern int lwkt_send_ipiq_bycpu(int dcpu, ipifunc_t func, void *arg);
318 extern int lwkt_send_ipiq_mask(cpumask_t mask, ipifunc_t func, void *arg);
319 extern void lwkt_wait_ipiq(struct globaldata *targ, int seq);
320 extern void lwkt_process_ipiq(void);
322 extern void lwkt_process_ipiq_frame(struct intrframe frame);
324 extern void lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *data);
325 extern void lwkt_cpusync_fastdata(cpumask_t mask, cpusync_func2_t func, void *data);
326 extern void lwkt_cpusync_start(cpumask_t mask, lwkt_cpusync_t poll);
327 extern void lwkt_cpusync_add(cpumask_t mask, lwkt_cpusync_t poll);
328 extern void lwkt_cpusync_finish(lwkt_cpusync_t poll);
329 extern void crit_panic(void);
330 extern struct proc *lwkt_preempted_proc(void);
332 extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd,
333 struct thread *template, int tdflags, int cpu,
334 const char *ctl, ...);
335 extern void lwkt_exit (void) __dead2;