oops, forgot one. Remove another curproc/cred dependancy
[dragonfly.git] / sys / sys / thread.h
CommitLineData
2df9fa87
MD
1/*
2 * SYS/THREAD.H
3 *
4 * Implements the architecture independant portion of the LWKT
5 * subsystem.
6 *
72740893 7 * $DragonFly: src/sys/sys/thread.h,v 1.18 2003/07/04 00:32:32 dillon Exp $
2df9fa87
MD
8 */
9
10#ifndef _SYS_THREAD_H_
11#define _SYS_THREAD_H_
12
f1d1c3fa 13struct globaldata;
2df9fa87
MD
14struct proc;
15struct thread;
f1d1c3fa
MD
16struct lwkt_queue;
17struct lwkt_token;
18struct lwkt_wait;
19struct lwkt_msg;
20struct lwkt_port;
21struct lwkt_cpu_msg;
22struct lwkt_cpu_port;
23struct lwkt_rwlock;
24
25typedef struct lwkt_queue *lwkt_queue_t;
26typedef struct lwkt_token *lwkt_token_t;
27typedef struct lwkt_wait *lwkt_wait_t;
28typedef struct lwkt_msg *lwkt_msg_t;
29typedef struct lwkt_port *lwkt_port_t;
30typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t;
31typedef struct lwkt_cpu_port *lwkt_cpu_port_t;
32typedef struct lwkt_rwlock *lwkt_rwlock_t;
33typedef struct thread *thread_t;
34
35typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
36typedef TAILQ_HEAD(lwkt_msg_queue, lwkt_msg) lwkt_msg_queue;
37
85100692
MD
38#ifndef _MACHINE_THREAD_H_
39#include <machine/thread.h> /* md_thread */
40#endif
f1d1c3fa
MD
41
42/*
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.
47 */
48typedef struct lwkt_token {
49 int t_cpu; /* the current owner of the token */
50 int t_reqcpu; /* return ownership to this cpu on release */
51#if 0
52 int t_pri; /* raise thread priority to hold token */
53#endif
54} lwkt_token;
55
56/*
57 * Wait structures deal with blocked threads. Due to the way remote cpus
58 * interact with these structures stable storage must be used.
59 */
60typedef struct lwkt_wait {
61 lwkt_queue wa_waitq; /* list of waiting threads */
62 lwkt_token wa_token; /* who currently owns the list */
63 int wa_gen;
64 int wa_count;
65} lwkt_wait;
66
67/*
68 * The standarding message and port structure for communications between
69 * threads.
70 */
71typedef struct lwkt_msg {
72 TAILQ_ENTRY(lwkt_msg) ms_node;
73 lwkt_port_t ms_replyport;
74 int ms_cmd;
75 int ms_flags;
76 int ms_error;
77} lwkt_msg;
78
79#define MSGF_DONE 0x0001
80#define MSGF_REPLY 0x0002
81#define MSGF_QUEUED 0x0004
82
83typedef struct lwkt_port {
84 lwkt_msg_queue mp_msgq;
85 lwkt_wait mp_wait;
86} lwkt_port;
2df9fa87 87
f1d1c3fa 88#define mp_token mp_wait.wa_token
2df9fa87 89
f1d1c3fa
MD
90/*
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
94 * blocking.
95 */
96typedef 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 */
101} lwkt_cpu_msg;
102
103/*
104 * reader/writer lock
105 */
106typedef struct lwkt_rwlock {
ae8050a4 107 lwkt_wait rw_wait;
f1d1c3fa
MD
108 thread_t rw_owner;
109 int rw_count;
17d46a8c 110 int rw_requests;
f1d1c3fa
MD
111} lwkt_rwlock;
112
113#define rw_token rw_wait.wa_token
114
115/*
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.
4b5f931b
MD
122 *
123 * NOTE: td_pri is bumped by TDPRI_CRIT when entering a critical section,
124 * but this does not effect how the thread is scheduled by LWKT.
f1d1c3fa 125 */
2df9fa87
MD
126struct thread {
127 TAILQ_ENTRY(thread) td_threadq;
73e4f7b9 128 TAILQ_ENTRY(thread) td_allq;
2df9fa87
MD
129 struct proc *td_proc; /* (optional) associated process */
130 struct pcb *td_pcb; /* points to pcb and top of kstack */
26a0694b 131 struct globaldata *td_gd; /* associated with this cpu */
ae8050a4 132 const char *td_wmesg; /* string name for blockage */
0cfcada1 133 void *td_wchan; /* waiting on channel */
f1d1c3fa 134 int td_cpu; /* cpu owning the thread */
4b5f931b 135 int td_pri; /* 0-31, 31=highest priority (note 1) */
8ad65e08 136 int td_flags; /* THF flags */
f1d1c3fa 137 int td_gen; /* wait queue chasing generation number */
7e1d4bf4 138 char *td_kstack; /* kernel stack */
8ad65e08
MD
139 char *td_sp; /* kernel stack pointer for LWKT restore */
140 void (*td_switch)(struct thread *ntd);
f1d1c3fa 141 lwkt_wait_t td_wait; /* thread sitting on wait structure */
d16a8831
MD
142 u_int64_t td_uticks; /* Statclock hits in user mode (uS) */
143 u_int64_t td_sticks; /* Statclock hits in system mode (uS) */
144 u_int64_t td_iticks; /* Statclock hits processing intr (uS) */
dadab5e9 145 int td_locks; /* lockmgr lock debugging YYY */
73e4f7b9 146 int td_refs; /* hold position in gd_tdallq / hold free */
0cfcada1 147 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
99df837e 148 struct thread *td_preempted; /* we preempted this thread */
85100692 149 struct md_thread td_mach;
2df9fa87
MD
150};
151
8ad65e08 152/*
99df837e
MD
153 * Thread flags. Note that TDF_EXITED is set by the appropriate switchout
154 * code when a thread exits, after it has switched to another stack and
155 * cleaned up the MMU state.
8ad65e08 156 */
99df837e 157#define TDF_EXITED 0x0001 /* thread finished exiting */
8ad65e08 158#define TDF_RUNQ 0x0002 /* on run queue */
26a0694b
MD
159#define TDF_PREEMPT_LOCK 0x0004 /* I have been preempted */
160#define TDF_PREEMPT_DONE 0x0008 /* acknowledge preemption complete */
161
73e4f7b9 162#define TDF_ONALLQ 0x0100 /* on gd_tdallq */
99df837e
MD
163#define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */
164#define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */
165#define TDF_VERBOSE 0x0800 /* verbose on exit */
dadab5e9 166#define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */
0cfcada1
MD
167#define TDF_STOPREQ 0x2000 /* suspend_kproc */
168#define TDF_WAKEREQ 0x4000 /* resume_kproc */
169#define TDF_TIMEOUT 0x8000 /* tsleep timeout */
8ad65e08 170
2df9fa87
MD
171/*
172 * Thread priorities. Typically only one thread from any given
173 * user process scheduling queue is on the LWKT run queue at a time.
174 * Remember that there is one LWKT run queue per cpu.
f1d1c3fa
MD
175 *
176 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
177 * causes interrupts to be masked as they occur. When this occurs
178 * mycpu->gd_reqpri will be raised (possibly just set to TDPRI_CRIT for
179 * interrupt masking).
2df9fa87 180 */
f1d1c3fa
MD
181#define TDPRI_IDLE_THREAD 0 /* the idle thread */
182#define TDPRI_USER_IDLE 4 /* user scheduler idle */
183#define TDPRI_USER_NORM 6 /* user scheduler normal */
184#define TDPRI_USER_REAL 8 /* user scheduler real time */
185#define TDPRI_KERN_USER 10 /* kernel / block in syscall */
26a0694b 186#define TDPRI_KERN_DAEMON 12 /* kernel daemon (pageout, etc) */
f1d1c3fa
MD
187#define TDPRI_SOFT_NORM 14 /* kernel / normal */
188#define TDPRI_SOFT_TIMER 16 /* kernel / timer */
ae8050a4 189#define TDPRI_EXITING 19 /* exiting thread */
f1d1c3fa
MD
190#define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
191#define TDPRI_INT_LOW 27 /* low priority interrupt */
192#define TDPRI_INT_MED 28 /* medium priority interrupt */
193#define TDPRI_INT_HIGH 29 /* high priority interrupt */
194#define TDPRI_MAX 31
195
196#define TDPRI_MASK 31
197#define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
2df9fa87 198
ef0fdad1 199#define CACHE_NTHREADS 6
7e1d4bf4 200
2df9fa87
MD
201#ifdef _KERNEL
202
203extern struct vm_zone *thread_zone;
204
ef0fdad1 205extern struct thread *lwkt_alloc_thread(struct thread *template);
26a0694b
MD
206extern void lwkt_init_thread(struct thread *td, void *stack, int flags,
207 struct globaldata *gd);
73e4f7b9
MD
208extern void lwkt_set_comm(thread_t td, const char *ctl, ...);
209extern void lwkt_wait_free(struct thread *td);
99df837e 210extern void lwkt_free_thread(struct thread *td);
7d0bac62 211extern void lwkt_init_wait(struct lwkt_wait *w);
8ad65e08
MD
212extern void lwkt_gdinit(struct globaldata *gd);
213extern void lwkt_switch(void);
b68b7282 214extern void lwkt_preempt(thread_t ntd, int id);
8ad65e08 215extern void lwkt_schedule(thread_t td);
f1d1c3fa 216extern void lwkt_schedule_self(void);
8ad65e08 217extern void lwkt_deschedule(thread_t td);
f1d1c3fa
MD
218extern void lwkt_deschedule_self(void);
219extern void lwkt_yield(void);
220extern void lwkt_yield_quick(void);
73e4f7b9
MD
221extern void lwkt_hold(thread_t td);
222extern void lwkt_rele(thread_t td);
f1d1c3fa 223
ae8050a4 224extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen);
f1d1c3fa
MD
225extern void lwkt_signal(lwkt_wait_t w);
226extern void lwkt_gettoken(lwkt_token_t tok);
227extern void lwkt_reltoken(lwkt_token_t tok);
72740893 228extern void lwkt_inittoken(lwkt_token_t tok);
f1d1c3fa 229extern int lwkt_regettoken(lwkt_token_t tok);
ae8050a4
MD
230extern void lwkt_rwlock_init(lwkt_rwlock_t lock);
231extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg);
232extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg);
233extern void lwkt_exunlock(lwkt_rwlock_t lock);
234extern void lwkt_shunlock(lwkt_rwlock_t lock);
4b5f931b 235extern void lwkt_setpri(thread_t td, int pri);
26a0694b
MD
236extern void lwkt_setpri_self(int pri);
237extern void crit_panic(void);
4b5f931b
MD
238extern struct proc *lwkt_preempted_proc(void);
239
8ad65e08 240
99df837e 241extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd,
ef0fdad1 242 struct thread *template, int tdflags,
99df837e
MD
243 const char *ctl, ...);
244extern void lwkt_exit __P((void)) __dead2;
245
2df9fa87
MD
246#endif
247
248#endif
249