Give ps access to a process's thread structure.
[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 *
99df837e 7 * $DragonFly: src/sys/sys/thread.h,v 1.11 2003/06/27 03:30:43 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
38#include <machine/thread.h>
39
40/*
41 * Tokens arbitrate access to information. They are 'soft' arbitrators
42 * in that they are associated with cpus rather then threads, making the
43 * optimal aquisition case very fast if your cpu already happens to own the
44 * token you are requesting.
45 */
46typedef struct lwkt_token {
47 int t_cpu; /* the current owner of the token */
48 int t_reqcpu; /* return ownership to this cpu on release */
49#if 0
50 int t_pri; /* raise thread priority to hold token */
51#endif
52} lwkt_token;
53
54/*
55 * Wait structures deal with blocked threads. Due to the way remote cpus
56 * interact with these structures stable storage must be used.
57 */
58typedef struct lwkt_wait {
59 lwkt_queue wa_waitq; /* list of waiting threads */
60 lwkt_token wa_token; /* who currently owns the list */
61 int wa_gen;
62 int wa_count;
63} lwkt_wait;
64
65/*
66 * The standarding message and port structure for communications between
67 * threads.
68 */
69typedef struct lwkt_msg {
70 TAILQ_ENTRY(lwkt_msg) ms_node;
71 lwkt_port_t ms_replyport;
72 int ms_cmd;
73 int ms_flags;
74 int ms_error;
75} lwkt_msg;
76
77#define MSGF_DONE 0x0001
78#define MSGF_REPLY 0x0002
79#define MSGF_QUEUED 0x0004
80
81typedef struct lwkt_port {
82 lwkt_msg_queue mp_msgq;
83 lwkt_wait mp_wait;
84} lwkt_port;
2df9fa87 85
f1d1c3fa 86#define mp_token mp_wait.wa_token
2df9fa87 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
115 * cased and there is no 'token'. A thread is always owned by td_cpu and
116 * any manipulation of the thread by some other cpu must be done through
117 * cpu_*msg() functions. e.g. you could request ownership of a thread that
118 * way, or hand a thread off to another cpu by changing td_cpu and sending
119 * a schedule request to the other cpu.
120 */
2df9fa87
MD
121struct thread {
122 TAILQ_ENTRY(thread) td_threadq;
123 struct proc *td_proc; /* (optional) associated process */
124 struct pcb *td_pcb; /* points to pcb and top of kstack */
ae8050a4 125 const char *td_wmesg; /* string name for blockage */
0cfcada1 126 void *td_wchan; /* waiting on channel */
f1d1c3fa 127 int td_cpu; /* cpu owning the thread */
2df9fa87 128 int td_pri; /* 0-31, 0=highest priority */
8ad65e08 129 int td_flags; /* THF flags */
f1d1c3fa 130 int td_gen; /* wait queue chasing generation number */
7e1d4bf4 131 char *td_kstack; /* kernel stack */
8ad65e08
MD
132 char *td_sp; /* kernel stack pointer for LWKT restore */
133 void (*td_switch)(struct thread *ntd);
f1d1c3fa 134 lwkt_wait_t td_wait; /* thread sitting on wait structure */
d16a8831
MD
135 u_int64_t td_uticks; /* Statclock hits in user mode (uS) */
136 u_int64_t td_sticks; /* Statclock hits in system mode (uS) */
137 u_int64_t td_iticks; /* Statclock hits processing intr (uS) */
dadab5e9 138 int td_locks; /* lockmgr lock debugging YYY */
0cfcada1 139 char td_comm[MAXCOMLEN+1]; /* typ 16+1 bytes */
99df837e 140 struct thread *td_preempted; /* we preempted this thread */
f1d1c3fa 141 struct mi_thread td_mach;
2df9fa87
MD
142};
143
8ad65e08 144/*
99df837e
MD
145 * Thread flags. Note that TDF_EXITED is set by the appropriate switchout
146 * code when a thread exits, after it has switched to another stack and
147 * cleaned up the MMU state.
8ad65e08 148 */
99df837e 149#define TDF_EXITED 0x0001 /* thread finished exiting */
8ad65e08 150#define TDF_RUNQ 0x0002 /* on run queue */
99df837e
MD
151#define TDF_PREEMPTED 0x0004 /* thread is currently preempted */
152#define TDF_ALLOCATED_THREAD 0x0200 /* zalloc allocated thread */
153#define TDF_ALLOCATED_STACK 0x0400 /* zalloc allocated stack */
154#define TDF_VERBOSE 0x0800 /* verbose on exit */
dadab5e9 155#define TDF_DEADLKTREAT 0x1000 /* special lockmgr deadlock treatment */
0cfcada1
MD
156#define TDF_STOPREQ 0x2000 /* suspend_kproc */
157#define TDF_WAKEREQ 0x4000 /* resume_kproc */
158#define TDF_TIMEOUT 0x8000 /* tsleep timeout */
8ad65e08 159
2df9fa87
MD
160/*
161 * Thread priorities. Typically only one thread from any given
162 * user process scheduling queue is on the LWKT run queue at a time.
163 * Remember that there is one LWKT run queue per cpu.
f1d1c3fa
MD
164 *
165 * Critical sections are handled by bumping td_pri above TDPRI_MAX, which
166 * causes interrupts to be masked as they occur. When this occurs
167 * mycpu->gd_reqpri will be raised (possibly just set to TDPRI_CRIT for
168 * interrupt masking).
2df9fa87 169 */
f1d1c3fa
MD
170#define TDPRI_IDLE_THREAD 0 /* the idle thread */
171#define TDPRI_USER_IDLE 4 /* user scheduler idle */
172#define TDPRI_USER_NORM 6 /* user scheduler normal */
173#define TDPRI_USER_REAL 8 /* user scheduler real time */
174#define TDPRI_KERN_USER 10 /* kernel / block in syscall */
175#define TDPRI_SOFT_NORM 14 /* kernel / normal */
176#define TDPRI_SOFT_TIMER 16 /* kernel / timer */
ae8050a4 177#define TDPRI_EXITING 19 /* exiting thread */
f1d1c3fa
MD
178#define TDPRI_INT_SUPPORT 20 /* kernel / high priority support */
179#define TDPRI_INT_LOW 27 /* low priority interrupt */
180#define TDPRI_INT_MED 28 /* medium priority interrupt */
181#define TDPRI_INT_HIGH 29 /* high priority interrupt */
182#define TDPRI_MAX 31
183
184#define TDPRI_MASK 31
185#define TDPRI_CRIT 32 /* high bits of td_pri used for crit */
2df9fa87 186
7e1d4bf4
MD
187#define CACHE_NTHREADS 4
188
2df9fa87
MD
189#ifdef _KERNEL
190
191extern struct vm_zone *thread_zone;
192
7d0bac62 193extern struct thread *lwkt_alloc_thread(void);
99df837e
MD
194extern void lwkt_init_thread(struct thread *td, void *stack, int flags);
195extern void lwkt_free_thread(struct thread *td);
7d0bac62 196extern void lwkt_init_wait(struct lwkt_wait *w);
8ad65e08
MD
197extern void lwkt_gdinit(struct globaldata *gd);
198extern void lwkt_switch(void);
199extern void lwkt_preempt(void);
200extern void lwkt_schedule(thread_t td);
f1d1c3fa 201extern void lwkt_schedule_self(void);
8ad65e08 202extern void lwkt_deschedule(thread_t td);
f1d1c3fa
MD
203extern void lwkt_deschedule_self(void);
204extern void lwkt_yield(void);
205extern void lwkt_yield_quick(void);
206
ae8050a4 207extern void lwkt_block(lwkt_wait_t w, const char *wmesg, int *gen);
f1d1c3fa
MD
208extern void lwkt_signal(lwkt_wait_t w);
209extern void lwkt_gettoken(lwkt_token_t tok);
210extern void lwkt_reltoken(lwkt_token_t tok);
211extern int lwkt_regettoken(lwkt_token_t tok);
ae8050a4
MD
212extern void lwkt_rwlock_init(lwkt_rwlock_t lock);
213extern void lwkt_exlock(lwkt_rwlock_t lock, const char *wmesg);
214extern void lwkt_shlock(lwkt_rwlock_t lock, const char *wmesg);
215extern void lwkt_exunlock(lwkt_rwlock_t lock);
216extern void lwkt_shunlock(lwkt_rwlock_t lock);
8ad65e08 217
99df837e
MD
218extern int lwkt_create (void (*func)(void *), void *arg, struct thread **ptd,
219 const char *ctl, ...);
220extern void lwkt_exit __P((void)) __dead2;
221
2df9fa87
MD
222#endif
223
224#endif
225