Fix a minor bug in lwkt_init_thread() (the thread was being added to the
[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 *
a72187e9 7 * $DragonFly: src/sys/sys/thread.h,v 1.28 2003/07/25 05:26:52 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;
96728c05 23struct lwkt_ipiq;
f1d1c3fa
MD
24struct lwkt_cpu_msg;
25struct lwkt_cpu_port;
26struct lwkt_rwlock;
ece04fd0
MD
27struct lwkt_msg;
28struct lwkt_port;
4fd10eb6 29union sysunion;
f1d1c3fa
MD
30
31typedef struct lwkt_queue *lwkt_queue_t;
32typedef struct lwkt_token *lwkt_token_t;
33typedef struct lwkt_wait *lwkt_wait_t;
f1d1c3fa
MD
34typedef struct lwkt_cpu_msg *lwkt_cpu_msg_t;
35typedef struct lwkt_cpu_port *lwkt_cpu_port_t;
36typedef struct lwkt_rwlock *lwkt_rwlock_t;
96728c05 37typedef struct lwkt_ipiq *lwkt_ipiq_t;
f1d1c3fa
MD
38typedef struct thread *thread_t;
39
40typedef TAILQ_HEAD(lwkt_queue, thread) lwkt_queue;
f1d1c3fa 41
85100692
MD
42#ifndef _MACHINE_THREAD_H_
43#include <machine/thread.h> /* md_thread */
44#endif
ece04fd0
MD
45#ifndef _SYS_MSGPORT_H_
46#include <sys/msgport.h>
47#endif
f1d1c3fa
MD
48
49/*
50 * Tokens arbitrate access to information. They are 'soft' arbitrators
51 * in that they are associated with cpus rather then threads, making the
52 * optimal aquisition case very fast if your cpu already happens to own the
53 * token you are requesting.
54 */
55typedef struct lwkt_token {
56 int t_cpu; /* the current owner of the token */
57 int t_reqcpu; /* return ownership to this cpu on release */
8a8d5d85 58 int t_gen; /* generation number */
f1d1c3fa
MD
59#if 0
60 int t_pri; /* raise thread priority to hold token */
61#endif
62} lwkt_token;
63
64/*
65 * Wait structures deal with blocked threads. Due to the way remote cpus
66 * interact with these structures stable storage must be used.
67 */
68typedef struct lwkt_wait {
69 lwkt_queue wa_waitq; /* list of waiting threads */
70 lwkt_token wa_token; /* who currently owns the list */
71 int wa_gen;
72 int wa_count;
73} lwkt_wait;
74
96728c05
MD
75#define MAXCPUFIFO 16 /* power of 2 */
76#define MAXCPUFIFO_MASK (MAXCPUFIFO - 1)
77
78typedef void (*ipifunc_t)(void *arg);
79
80typedef struct lwkt_ipiq {
81 int ip_rindex; /* only written by target cpu */
cb973d15 82 int ip_xindex; /* writte by target, indicates completion */
96728c05
MD
83 int ip_windex; /* only written by source cpu */
84 ipifunc_t ip_func[MAXCPUFIFO];
85 void *ip_arg[MAXCPUFIFO];
86} lwkt_ipiq;
87
f1d1c3fa
MD
88/*
89 * The standard message and queue structure used for communications between
90 * cpus. Messages are typically queued via a machine-specific non-linked
91 * FIFO matrix allowing any cpu to send a message to any other cpu without
92 * blocking.
93 */
94typedef struct lwkt_cpu_msg {
95 void (*cm_func)(lwkt_cpu_msg_t msg); /* primary dispatch function */
96 int cm_code; /* request code if applicable */
97 int cm_cpu; /* reply to cpu */
98 thread_t cm_originator; /* originating thread for wakeup */
99} lwkt_cpu_msg;
100
101/*
102 * reader/writer lock
103 */
104typedef struct lwkt_rwlock {
ae8050a4 105 lwkt_wait rw_wait;
f1d1c3fa
MD
106 thread_t rw_owner;
107 int rw_count;
17d46a8c 108 int rw_requests;
f1d1c3fa
MD
109} lwkt_rwlock;
110
111#define rw_token rw_wait.wa_token
112
113/*
114 * Thread structure. Note that ownership of a thread structure is special
a72187e9
MD
115 * cased and there is no 'token'. A thread is always owned by the cpu
116 * represented by td_gd, any manipulation of the thread by some other cpu
117 * must be done through cpu_*msg() functions. e.g. you could request
118 * ownership of a thread that way, or hand a thread off to another cpu.
4b5f931b
MD
119 *
120 * NOTE: td_pri is bumped by TDPRI_CRIT when entering a critical section,
121 * but this does not effect how the thread is scheduled by LWKT.
f1d1c3fa 122 */
96728c05
MD
123struct md_intr_info;
124
2df9fa87
MD
125struct thread {
126 TAILQ_ENTRY(thread) td_threadq;
73e4f7b9 127 TAILQ_ENTRY(thread) td_allq;
ece04fd0 128 lwkt_port td_msgport; /* built-in message port for replies */
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 */
4b5f931b 134 int td_pri; /* 0-31, 31=highest priority (note 1) */
8ad65e08 135 int td_flags; /* THF flags */
f1d1c3fa 136 int td_gen; /* wait queue chasing generation number */
96728c05
MD
137 /* maybe preempt */
138 void (*td_preemptable)(struct thread *td, int critpri);
a2a5ad0d 139 void (*td_release)(struct thread *td);
96728c05
MD
140 union {
141 struct md_intr_info *intdata;
142 } td_info;
7e1d4bf4 143 char *td_kstack; /* kernel stack */
8ad65e08
MD
144 char *td_sp; /* kernel stack pointer for LWKT restore */
145 void (*td_switch)(struct thread *ntd);
f1d1c3fa 146 lwkt_wait_t td_wait; /* thread sitting on wait structure */
d16a8831
MD
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) */
dadab5e9 150 int td_locks; /* lockmgr lock debugging YYY */
73e4f7b9 151 int td_refs; /* hold position in gd_tdallq / hold free */
8a8d5d85
MD
152#ifdef SMP
153 int td_mpcount; /* MP lock held (count) */
154#else
155 int td_unused001;
156#endif
0cfcada1 157 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
99df837e 158 struct thread *td_preempted; /* we preempted this thread */
85100692 159 struct md_thread td_mach;
2df9fa87
MD
160};
161
8ad65e08 162/*
d9eea1a5
MD
163 * Thread flags. Note that TDF_RUNNING is cleared on the old thread after
164 * we switch to the new one, which is necessary because LWKTs don't need
165 * to hold the BGL. This flag is used by the exit code and the managed
166 * thread migration code.
a2a5ad0d
MD
167 *
168 * LWKT threads stay on their (per-cpu) run queue while running, not to
169 * be confused with user processes which are removed from the user scheduling
170 * run queue while actually running.
8ad65e08 171 */
d9eea1a5 172#define TDF_RUNNING 0x0001 /* thread still active */
a2a5ad0d 173#define TDF_RUNQ 0x0002 /* on an LWKT run queue */
26a0694b
MD
174#define TDF_PREEMPT_LOCK 0x0004 /* I have been preempted */
175#define TDF_PREEMPT_DONE 0x0008 /* acknowledge preemption complete */
a2a5ad0d 176#define TDF_IDLE_NOHLT 0x0010 /* we need to spin */
26a0694b 177
73e4f7b9 178#define TDF_ONALLQ 0x0100 /* on gd_tdallq */
99df837e
MD
179#define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */
180#define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */
181#define TDF_VERBOSE 0x0800 /* verbose on exit */
dadab5e9 182#define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */
0cfcada1
MD
183#define TDF_STOPREQ 0x2000 /* suspend_kproc */
184#define TDF_WAKEREQ 0x4000 /* resume_kproc */
185#define TDF_TIMEOUT 0x8000 /* tsleep timeout */
8ad65e08 186
2df9fa87
MD
187/*
188 * Thread priorities. Typically only one thread from any given
189 * user process scheduling queue is on the LWKT run queue at a time.
190 * Remember that there is one LWKT run queue per cpu.
f1d1c3fa
MD
191 *
192 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
235957ed
MD
193 * causes interrupts to be masked as they occur. When this occurs a
194 * rollup flag will be set in mycpu->gd_reqflags.
2df9fa87 195 */
f1d1c3fa
MD
196#define TDPRI_IDLE_THREAD 0 /* the idle thread */
197#define TDPRI_USER_IDLE 4 /* user scheduler idle */
198#define TDPRI_USER_NORM 6 /* user scheduler normal */
199#define TDPRI_USER_REAL 8 /* user scheduler real time */
200#define TDPRI_KERN_USER 10 /* kernel / block in syscall */
26a0694b 201#define TDPRI_KERN_DAEMON 12 /* kernel daemon (pageout, etc) */
f1d1c3fa
MD
202#define TDPRI_SOFT_NORM 14 /* kernel / normal */
203#define TDPRI_SOFT_TIMER 16 /* kernel / timer */
ae8050a4 204#define TDPRI_EXITING 19 /* exiting thread */
f1d1c3fa
MD
205#define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
206#define TDPRI_INT_LOW 27 /* low priority interrupt */
207#define TDPRI_INT_MED 28 /* medium priority interrupt */
208#define TDPRI_INT_HIGH 29 /* high priority interrupt */
209#define TDPRI_MAX 31
210
211#define TDPRI_MASK 31
212#define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
2df9fa87 213
ef0fdad1 214#define CACHE_NTHREADS 6
7e1d4bf4 215
2df9fa87
MD
216#ifdef _KERNEL
217
218extern struct vm_zone *thread_zone;
219
ef0fdad1 220extern struct thread *lwkt_alloc_thread(struct thread *template);
26a0694b
MD
221extern void lwkt_init_thread(struct thread *td, void *stack, int flags,
222 struct globaldata *gd);
73e4f7b9
MD
223extern void lwkt_set_comm(thread_t td, const char *ctl, ...);
224extern void lwkt_wait_free(struct thread *td);
99df837e 225extern void lwkt_free_thread(struct thread *td);
7d0bac62 226extern void lwkt_init_wait(struct lwkt_wait *w);
8ad65e08
MD
227extern void lwkt_gdinit(struct globaldata *gd);
228extern void lwkt_switch(void);
cb973d15 229extern void lwkt_maybe_switch(void);
96728c05 230extern void lwkt_preempt(thread_t ntd, int critpri);
8ad65e08 231extern void lwkt_schedule(thread_t td);
f1d1c3fa 232extern void lwkt_schedule_self(void);
8ad65e08 233extern void lwkt_deschedule(thread_t td);
f1d1c3fa 234extern void lwkt_deschedule_self(void);
a2a5ad0d 235extern void lwkt_acquire(thread_t td);
f1d1c3fa
MD
236extern void lwkt_yield(void);
237extern void lwkt_yield_quick(void);
73e4f7b9
MD
238extern void lwkt_hold(thread_t td);
239extern void lwkt_rele(thread_t td);
f1d1c3fa 240
ae8050a4 241extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen);
ece04fd0 242extern void lwkt_signal(lwkt_wait_t w, int count);
96728c05 243extern int lwkt_trytoken(lwkt_token_t tok);
8a8d5d85
MD
244extern int lwkt_gettoken(lwkt_token_t tok);
245extern int lwkt_gentoken(lwkt_token_t tok, int *gen);
f1d1c3fa 246extern void lwkt_reltoken(lwkt_token_t tok);
72740893 247extern void lwkt_inittoken(lwkt_token_t tok);
f1d1c3fa 248extern int lwkt_regettoken(lwkt_token_t tok);
ae8050a4
MD
249extern void lwkt_rwlock_init(lwkt_rwlock_t lock);
250extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg);
251extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg);
252extern void lwkt_exunlock(lwkt_rwlock_t lock);
253extern void lwkt_shunlock(lwkt_rwlock_t lock);
4b5f931b 254extern void lwkt_setpri(thread_t td, int pri);
26a0694b 255extern void lwkt_setpri_self(int pri);
96728c05 256extern int lwkt_send_ipiq(int dcpu, ipifunc_t func, void *arg);
cb973d15 257extern void lwkt_send_ipiq_mask(u_int32_t mask, ipifunc_t func, void *arg);
96728c05
MD
258extern void lwkt_wait_ipiq(int dcpu, int seq);
259extern void lwkt_process_ipiq(void);
26a0694b 260extern void crit_panic(void);
4b5f931b
MD
261extern struct proc *lwkt_preempted_proc(void);
262
8ad65e08 263
99df837e 264extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd,
ef0fdad1 265 struct thread *template, int tdflags,
99df837e
MD
266 const char *ctl, ...);
267extern void lwkt_exit __P((void)) __dead2;
268
2df9fa87
MD
269#endif
270
271#endif
272