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