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