4 * Implements the architecture independant portion of the LWKT
7 * $DragonFly: src/sys/sys/thread.h,v 1.21 2003/07/10 04:47:55 dillon Exp $
10 #ifndef _SYS_THREAD_H_
11 #define _SYS_THREAD_H_
14 #include <sys/queue.h> /* TAILQ_* macros */
30 typedef struct lwkt_queue *lwkt_queue_t;
31 typedef struct lwkt_token *lwkt_token_t;
32 typedef struct lwkt_wait *lwkt_wait_t;
33 typedef struct lwkt_msg *lwkt_msg_t;
34 typedef struct lwkt_port *lwkt_port_t;
35 typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t;
36 typedef struct lwkt_cpu_port *lwkt_cpu_port_t;
37 typedef struct lwkt_rwlock *lwkt_rwlock_t;
38 typedef struct lwkt_ipiq *lwkt_ipiq_t;
39 typedef struct thread *thread_t;
41 typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
42 typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue;
44 #ifndef _MACHINE_THREAD_H_
45 #include <machine/thread.h> /* md_thread */
49 * Tokens arbitrate access to information. They are 'soft' arbitrators
50 * in that they are associated with cpus rather then threads, making the
51 * optimal aquisition case very fast if your cpu already happens to own the
52 * token you are requesting.
54 typedef struct lwkt_token {
55 int t_cpu; /* the current owner of the token */
56 int t_reqcpu; /* return ownership to this cpu on release */
57 int t_gen; /* generation number */
59 int t_pri; /* raise thread priority to hold token */
64 * Wait structures deal with blocked threads. Due to the way remote cpus
65 * interact with these structures stable storage must be used.
67 typedef struct lwkt_wait {
68 lwkt_queue wa_waitq; /* list of waiting threads */
69 lwkt_token wa_token; /* who currently owns the list */
75 * The standard message and port structure for communications between
78 typedef struct lwkt_msg {
79 TAILQ_ENTRY(lwkt_msg) ms_node;
80 lwkt_port_t ms_replyport;
86 #define MSGF_DONE 0x0001
87 #define MSGF_REPLY 0x0002
88 #define MSGF_QUEUED 0x0004
90 typedef struct lwkt_port {
91 lwkt_msg_queue mp_msgq;
95 #define mp_token mp_wait.wa_token
97 #define MAXCPUFIFO 16 /* power of 2 */
98 #define MAXCPUFIFO_MASK (MAXCPUFIFO - 1)
100 typedef void (*ipifunc_t)(void *arg);
102 typedef struct lwkt_ipiq {
103 int ip_rindex; /* only written by target cpu */
104 int ip_windex; /* only written by source cpu */
105 ipifunc_t ip_func[MAXCPUFIFO];
106 void *ip_arg[MAXCPUFIFO];
110 * The standard message and queue structure used for communications between
111 * cpus. Messages are typically queued via a machine-specific non-linked
112 * FIFO matrix allowing any cpu to send a message to any other cpu without
115 typedef struct lwkt_cpu_msg {
116 void (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */
117 int cm_code; /* request code if applicable */
118 int cm_cpu; /* reply to cpu */
119 thread_t cm_originator; /* originating thread for wakeup */
125 typedef struct lwkt_rwlock {
132 #define rw_token rw_wait.wa_token
135 * Thread structure. Note that ownership of a thread structure is special
136 * cased and there is no 'token'. A thread is always owned by td_cpu and
137 * any manipulation of the thread by some other cpu must be done through
138 * cpu_*msg() functions. e.g. you could request ownership of a thread that
139 * way, or hand a thread off to another cpu by changing td_cpu and sending
140 * a schedule request to the other cpu.
142 * NOTE: td_pri is bumped by TDPRI_CRIT when entering a critical section,
143 * but this does not effect how the thread is scheduled by LWKT.
148 TAILQ_ENTRY(thread) td_threadq;
149 TAILQ_ENTRY(thread) td_allq;
150 struct proc *td_proc; /* (optional) associated process */
151 struct pcb *td_pcb; /* points to pcb and top of kstack */
152 struct globaldata *td_gd; /* associated with this cpu */
153 const char *td_wmesg; /* string name for blockage */
154 void *td_wchan; /* waiting on channel */
155 int td_cpu; /* cpu owning the thread */
156 int td_pri; /* 0-31, 31=highest priority (note 1) */
157 int td_flags; /* THF flags */
158 int td_gen; /* wait queue chasing generation number */
160 void (*td_preemptable)(struct thread *td, int critpri);
161 void (*td_release)(struct thread *td);
163 struct md_intr_info *intdata;
165 char *td_kstack; /* kernel stack */
166 char *td_sp; /* kernel stack pointer for LWKT restore */
167 void (*td_switch)(struct thread *ntd);
168 lwkt_wait_t td_wait; /* thread sitting on wait structure */
169 u_int64_t td_uticks; /* Statclock hits in user mode (uS) */
170 u_int64_t td_sticks; /* Statclock hits in system mode (uS) */
171 u_int64_t td_iticks; /* Statclock hits processing intr (uS) */
172 int td_locks; /* lockmgr lock debugging YYY */
173 int td_refs; /* hold position in gd_tdallq / hold free */
175 int td_mpcount; /* MP lock held (count) */
179 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
180 struct thread *td_preempted; /* we preempted this thread */
181 struct md_thread td_mach;
185 * Thread flags. Note that TDF_EXITED is set by the appropriate switchout
186 * code when a thread exits, after it has switched to another stack and
187 * cleaned up the MMU state.
189 * LWKT threads stay on their (per-cpu) run queue while running, not to
190 * be confused with user processes which are removed from the user scheduling
191 * run queue while actually running.
193 #define TDF_EXITED 0x0001 /* thread finished exiting */
194 #define TDF_RUNQ 0x0002 /* on an LWKT run queue */
195 #define TDF_PREEMPT_LOCK 0x0004 /* I have been preempted */
196 #define TDF_PREEMPT_DONE 0x0008 /* acknowledge preemption complete */
197 #define TDF_IDLE_NOHLT 0x0010 /* we need to spin */
199 #define TDF_ONALLQ 0x0100 /* on gd_tdallq */
200 #define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */
201 #define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */
202 #define TDF_VERBOSE 0x0800 /* verbose on exit */
203 #define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */
204 #define TDF_STOPREQ 0x2000 /* suspend_kproc */
205 #define TDF_WAKEREQ 0x4000 /* resume_kproc */
206 #define TDF_TIMEOUT 0x8000 /* tsleep timeout */
209 * Thread priorities. Typically only one thread from any given
210 * user process scheduling queue is on the LWKT run queue at a time.
211 * Remember that there is one LWKT run queue per cpu.
213 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
214 * causes interrupts to be masked as they occur. When this occurs
215 * mycpu->gd_reqpri will be raised (possibly just set to TDPRI_CRIT for
216 * interrupt masking).
218 #define TDPRI_IDLE_THREAD 0 /* the idle thread */
219 #define TDPRI_USER_IDLE 4 /* user scheduler idle */
220 #define TDPRI_USER_NORM 6 /* user scheduler normal */
221 #define TDPRI_USER_REAL 8 /* user scheduler real time */
222 #define TDPRI_KERN_USER 10 /* kernel / block in syscall */
223 #define TDPRI_KERN_DAEMON 12 /* kernel daemon (pageout, etc) */
224 #define TDPRI_SOFT_NORM 14 /* kernel / normal */
225 #define TDPRI_SOFT_TIMER 16 /* kernel / timer */
226 #define TDPRI_EXITING 19 /* exiting thread */
227 #define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
228 #define TDPRI_INT_LOW 27 /* low priority interrupt */
229 #define TDPRI_INT_MED 28 /* medium priority interrupt */
230 #define TDPRI_INT_HIGH 29 /* high priority interrupt */
233 #define TDPRI_MASK 31
234 #define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
236 #define CACHE_NTHREADS 6
240 extern struct vm_zone *thread_zone;
242 extern struct thread *lwkt_alloc_thread(struct thread *template);
243 extern void lwkt_init_thread(struct thread *td, void *stack, int flags,
244 struct globaldata *gd);
245 extern void lwkt_set_comm(thread_t td, const char *ctl, ...);
246 extern void lwkt_wait_free(struct thread *td);
247 extern void lwkt_free_thread(struct thread *td);
248 extern void lwkt_init_wait(struct lwkt_wait *w);
249 extern void lwkt_gdinit(struct globaldata *gd);
250 extern void lwkt_switch(void);
251 extern void lwkt_preempt(thread_t ntd, int critpri);
252 extern void lwkt_schedule(thread_t td);
253 extern void lwkt_schedule_self(void);
254 extern void lwkt_deschedule(thread_t td);
255 extern void lwkt_deschedule_self(void);
256 extern void lwkt_acquire(thread_t td);
257 extern void lwkt_yield(void);
258 extern void lwkt_yield_quick(void);
259 extern void lwkt_hold(thread_t td);
260 extern void lwkt_rele(thread_t td);
262 extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen);
263 extern void lwkt_signal(lwkt_wait_t w);
264 extern int lwkt_trytoken(lwkt_token_t tok);
265 extern int lwkt_gettoken(lwkt_token_t tok);
266 extern int lwkt_gentoken(lwkt_token_t tok, int *gen);
267 extern void lwkt_reltoken(lwkt_token_t tok);
268 extern void lwkt_inittoken(lwkt_token_t tok);
269 extern int lwkt_regettoken(lwkt_token_t tok);
270 extern void lwkt_rwlock_init(lwkt_rwlock_t lock);
271 extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg);
272 extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg);
273 extern void lwkt_exunlock(lwkt_rwlock_t lock);
274 extern void lwkt_shunlock(lwkt_rwlock_t lock);
275 extern void lwkt_setpri(thread_t td, int pri);
276 extern void lwkt_setpri_self(int pri);
277 extern int lwkt_send_ipiq(int dcpu, ipifunc_t func, void *arg);
278 extern void lwkt_wait_ipiq(int dcpu, int seq);
279 extern void lwkt_process_ipiq(void);
280 extern void crit_panic(void);
281 extern struct proc *lwkt_preempted_proc(void);
284 extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd,
285 struct thread *template, int tdflags,
286 const char *ctl, ...);
287 extern void lwkt_exit __P((void)) __dead2;