4 * Implements the architecture independant portion of the LWKT
7 * $DragonFly: src/sys/sys/thread.h,v 1.13 2003/06/29 03:28:46 dillon Exp $
10 #ifndef _SYS_THREAD_H_
11 #define _SYS_THREAD_H_
25 typedef struct lwkt_queue *lwkt_queue_t;
26 typedef struct lwkt_token *lwkt_token_t;
27 typedef struct lwkt_wait *lwkt_wait_t;
28 typedef struct lwkt_msg *lwkt_msg_t;
29 typedef struct lwkt_port *lwkt_port_t;
30 typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t;
31 typedef struct lwkt_cpu_port *lwkt_cpu_port_t;
32 typedef struct lwkt_rwlock *lwkt_rwlock_t;
33 typedef struct thread *thread_t;
35 typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
36 typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue;
38 #ifndef _MACHINE_THREAD_H_
39 #include <machine/thread.h> /* md_thread */
43 * Tokens arbitrate access to information. They are 'soft' arbitrators
44 * in that they are associated with cpus rather then threads, making the
45 * optimal aquisition case very fast if your cpu already happens to own the
46 * token you are requesting.
48 typedef struct lwkt_token {
49 int t_cpu; /* the current owner of the token */
50 int t_reqcpu; /* return ownership to this cpu on release */
52 int t_pri; /* raise thread priority to hold token */
57 * Wait structures deal with blocked threads. Due to the way remote cpus
58 * interact with these structures stable storage must be used.
60 typedef struct lwkt_wait {
61 lwkt_queue wa_waitq; /* list of waiting threads */
62 lwkt_token wa_token; /* who currently owns the list */
68 * The standarding message and port structure for communications between
71 typedef struct lwkt_msg {
72 TAILQ_ENTRY(lwkt_msg) ms_node;
73 lwkt_port_t ms_replyport;
79 #define MSGF_DONE 0x0001
80 #define MSGF_REPLY 0x0002
81 #define MSGF_QUEUED 0x0004
83 typedef struct lwkt_port {
84 lwkt_msg_queue mp_msgq;
88 #define mp_token mp_wait.wa_token
91 * The standard message and queue structure used for communications between
92 * cpus. Messages are typically queued via a machine-specific non-linked
93 * FIFO matrix allowing any cpu to send a message to any other cpu without
96 typedef struct lwkt_cpu_msg {
97 void (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */
98 int cm_code; /* request code if applicable */
99 int cm_cpu; /* reply to cpu */
100 thread_t cm_originator; /* originating thread for wakeup */
106 typedef struct lwkt_rwlock {
113 #define rw_token rw_wait.wa_token
116 * Thread structure. Note that ownership of a thread structure is special
117 * cased and there is no 'token'. A thread is always owned by td_cpu and
118 * any manipulation of the thread by some other cpu must be done through
119 * cpu_*msg() functions. e.g. you could request ownership of a thread that
120 * way, or hand a thread off to another cpu by changing td_cpu and sending
121 * a schedule request to the other cpu.
124 TAILQ_ENTRY(thread) td_threadq;
125 struct proc *td_proc; /* (optional) associated process */
126 struct pcb *td_pcb; /* points to pcb and top of kstack */
127 const char *td_wmesg; /* string name for blockage */
128 void *td_wchan; /* waiting on channel */
129 int td_cpu; /* cpu owning the thread */
130 int td_pri; /* 0-31, 0=highest priority */
131 int td_flags; /* THF flags */
132 int td_gen; /* wait queue chasing generation number */
133 char *td_kstack; /* kernel stack */
134 char *td_sp; /* kernel stack pointer for LWKT restore */
135 void (*td_switch)(struct thread *ntd);
136 lwkt_wait_t td_wait; /* thread sitting on wait structure */
137 u_int64_t td_uticks; /* Statclock hits in user mode (uS) */
138 u_int64_t td_sticks; /* Statclock hits in system mode (uS) */
139 u_int64_t td_iticks; /* Statclock hits processing intr (uS) */
140 int td_locks; /* lockmgr lock debugging YYY */
141 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
142 struct thread *td_preempted; /* we preempted this thread */
143 struct md_thread td_mach;
147 * Thread flags. Note that TDF_EXITED is set by the appropriate switchout
148 * code when a thread exits, after it has switched to another stack and
149 * cleaned up the MMU state.
151 #define TDF_EXITED 0x0001 /* thread finished exiting */
152 #define TDF_RUNQ 0x0002 /* on run queue */
153 #define TDF_PREEMPTED 0x0004 /* thread is currently preempted */
154 #define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */
155 #define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */
156 #define TDF_VERBOSE 0x0800 /* verbose on exit */
157 #define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */
158 #define TDF_STOPREQ 0x2000 /* suspend_kproc */
159 #define TDF_WAKEREQ 0x4000 /* resume_kproc */
160 #define TDF_TIMEOUT 0x8000 /* tsleep timeout */
163 * Thread priorities. Typically only one thread from any given
164 * user process scheduling queue is on the LWKT run queue at a time.
165 * Remember that there is one LWKT run queue per cpu.
167 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
168 * causes interrupts to be masked as they occur. When this occurs
169 * mycpu->gd_reqpri will be raised (possibly just set to TDPRI_CRIT for
170 * interrupt masking).
172 #define TDPRI_IDLE_THREAD 0 /* the idle thread */
173 #define TDPRI_USER_IDLE 4 /* user scheduler idle */
174 #define TDPRI_USER_NORM 6 /* user scheduler normal */
175 #define TDPRI_USER_REAL 8 /* user scheduler real time */
176 #define TDPRI_KERN_USER 10 /* kernel / block in syscall */
177 #define TDPRI_SOFT_NORM 14 /* kernel / normal */
178 #define TDPRI_SOFT_TIMER 16 /* kernel / timer */
179 #define TDPRI_EXITING 19 /* exiting thread */
180 #define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
181 #define TDPRI_INT_LOW 27 /* low priority interrupt */
182 #define TDPRI_INT_MED 28 /* medium priority interrupt */
183 #define TDPRI_INT_HIGH 29 /* high priority interrupt */
186 #define TDPRI_MASK 31
187 #define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
189 #define CACHE_NTHREADS 6
193 extern struct vm_zone *thread_zone;
195 extern struct thread *lwkt_alloc_thread(struct thread *template);
196 extern void lwkt_init_thread(struct thread *td, void *stack, int flags);
197 extern void lwkt_free_thread(struct thread *td);
198 extern void lwkt_init_wait(struct lwkt_wait *w);
199 extern void lwkt_gdinit(struct globaldata *gd);
200 extern void lwkt_switch(void);
201 extern void lwkt_preempt(void);
202 extern void lwkt_schedule(thread_t td);
203 extern void lwkt_schedule_self(void);
204 extern void lwkt_deschedule(thread_t td);
205 extern void lwkt_deschedule_self(void);
206 extern void lwkt_yield(void);
207 extern void lwkt_yield_quick(void);
209 extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen);
210 extern void lwkt_signal(lwkt_wait_t w);
211 extern void lwkt_gettoken(lwkt_token_t tok);
212 extern void lwkt_reltoken(lwkt_token_t tok);
213 extern int lwkt_regettoken(lwkt_token_t tok);
214 extern void lwkt_rwlock_init(lwkt_rwlock_t lock);
215 extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg);
216 extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg);
217 extern void lwkt_exunlock(lwkt_rwlock_t lock);
218 extern void lwkt_shunlock(lwkt_rwlock_t lock);
220 extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd,
221 struct thread *template, int tdflags,
222 const char *ctl, ...);
223 extern void lwkt_exit __P((void)) __dead2;