xref: /titanic_41/usr/src/uts/common/sys/thread.h (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_THREAD_H
28 #define	_SYS_THREAD_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/t_lock.h>
34 #include <sys/klwp.h>
35 #include <sys/time.h>
36 #include <sys/signal.h>
37 #include <sys/kcpc.h>
38 #if defined(__GNUC__) && defined(_ASM_INLINES) && defined(_KERNEL)
39 #include <asm/thread.h>
40 #endif
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * The thread object, its states, and the methods by which it
48  * is accessed.
49  */
50 
51 /*
52  * Values that t_state may assume. Note that t_state cannot have more
53  * than one of these flags set at a time.
54  */
55 #define	TS_FREE		0x00	/* Thread at loose ends */
56 #define	TS_SLEEP	0x01	/* Awaiting an event */
57 #define	TS_RUN		0x02	/* Runnable, but not yet on a processor */
58 #define	TS_ONPROC	0x04	/* Thread is being run on a processor */
59 #define	TS_ZOMB		0x08	/* Thread has died but hasn't been reaped */
60 #define	TS_STOPPED	0x10	/* Stopped, initial state */
61 
62 typedef struct ctxop {
63 	void	(*save_op)(void *);	/* function to invoke to save context */
64 	void	(*restore_op)(void *);	/* function to invoke to restore ctx */
65 	void	(*fork_op)(void *, void *);	/* invoke to fork context */
66 	void	(*lwp_create_op)(void *, void *);	/* lwp_create context */
67 	void	(*exit_op)(void *);	/* invoked during {thread,lwp}_exit() */
68 	void	(*free_op)(void *, int); /* function which frees the context */
69 	void	*arg;		/* argument to above functions, ctx pointer */
70 	struct ctxop *next;	/* next context ops */
71 } ctxop_t;
72 
73 /*
74  * The active file descriptor table.
75  * Each member of a_fd[] not equalling -1 represents an active fd.
76  * The structure is initialized on first use; all zeros means uninitialized.
77  */
78 typedef struct _afd {
79 	int	*a_fd;		/* pointer to list of fds */
80 	short	a_nfd;		/* number of entries in *a_fd */
81 	short	a_stale;	/* one of the active fds is being closed */
82 	int	a_buf[1];	/* buffer to which a_fd initially refers */
83 } afd_t;
84 
85 /*
86  * An lwpchan provides uniqueness when sleeping on user-level
87  * synchronization primitives.  The lc_wchan member is used
88  * for sleeping on kernel synchronization primitives.
89  */
90 typedef struct {
91 	caddr_t lc_wchan0;
92 	caddr_t lc_wchan;
93 } lwpchan_t;
94 
95 typedef struct _kthread	*kthread_id_t;
96 
97 struct turnstile;
98 struct trap_info;
99 struct upimutex;
100 struct kproject;
101 struct on_trap_data;
102 
103 /* Definition for kernel thread identifier type */
104 typedef uint64_t kt_did_t;
105 
106 typedef struct _kthread {
107 	struct _kthread	*t_link; /* dispq, sleepq, and free queue link */
108 
109 	caddr_t	t_stk;		/* base of stack (kernel sp value to use) */
110 	void	(*t_startpc)(void);	/* PC where thread started */
111 	struct cpu *t_bound_cpu; /* cpu bound to, or NULL if not bound */
112 	short	t_affinitycnt;	/* nesting level of kernel affinity-setting */
113 	short	t_bind_cpu;	/* user-specified CPU binding (-1 if none) */
114 	ushort_t t_flag;		/* modified only by current thread */
115 	ushort_t t_proc_flag;	/* modified holding ttproc(t)->p_lock */
116 	ushort_t t_schedflag;	/* modified holding thread_lock(t) */
117 	volatile char t_preempt;	/* don't preempt thread if set */
118 	volatile char t_preempt_lk;
119 	uint_t	t_state;	/* thread state	(protected by thread_lock) */
120 	pri_t	t_pri;		/* assigned thread priority */
121 	pri_t	t_epri;		/* inherited thread priority */
122 	char	t_writer;	/* sleeping in lwp_rwlock_lock(RW_WRITE_LOCK) */
123 	label_t	t_pcb;		/* pcb, save area when switching */
124 	lwpchan_t t_lwpchan;	/* reason for blocking */
125 #define	t_wchan0	t_lwpchan.lc_wchan0
126 #define	t_wchan		t_lwpchan.lc_wchan
127 	struct _sobj_ops *t_sobj_ops;
128 	id_t	t_cid;		/* scheduling class id */
129 	struct thread_ops *t_clfuncs;	/* scheduling class ops vector */
130 	void	*t_cldata;	/* per scheduling class specific data */
131 	ctxop_t	*t_ctx;		/* thread context */
132 	uintptr_t t_lofault;	/* ret pc for failed page faults */
133 	label_t	*t_onfault;	/* on_fault() setjmp buf */
134 	struct on_trap_data *t_ontrap;	/* on_trap() protection data */
135 	caddr_t t_swap;		/* swappable thread storage */
136 	lock_t	t_lock;		/* used to resume() a thread */
137 	uint8_t	t_lockstat;	/* set while thread is in lockstat code */
138 	uint8_t	t_pil;		/* interrupt thread PIL */
139 	disp_lock_t	t_pi_lock;	/* lock protecting t_prioinv list */
140 	char	t_nomigrate;	/* do not migrate if set */
141 	struct cpu	*t_cpu;	/* CPU that thread last ran on */
142 	struct cpu	*t_weakbound_cpu;	/* cpu weakly bound to */
143 	struct lgrp_ld	*t_lpl;	/* load average for home lgroup */
144 	void		*t_lgrp_reserv[2];	/* reserved for future */
145 	struct _kthread	*t_intr; /* interrupted (pinned) thread */
146 	uint64_t	t_intr_start;	/* timestamp when time slice began */
147 	kt_did_t	t_did;	/* thread id for kernel debuggers */
148 	caddr_t t_tnf_tpdp;	/* Trace facility data pointer */
149 	kcpc_ctx_t	*t_cpc_ctx;	/* performance counter context */
150 	kcpc_set_t	*t_cpc_set;	/* set this thread has bound */
151 
152 	/*
153 	 * non swappable part of the lwp state.
154 	 */
155 	id_t		t_tid;		/* lwp's id */
156 	id_t		t_waitfor;	/* target lwp id in lwp_wait() */
157 	struct sigqueue	*t_sigqueue;	/* queue of siginfo structs */
158 	k_sigset_t	t_sig;		/* signals pending to this process */
159 	k_sigset_t	t_extsig;	/* signals sent from another contract */
160 	k_sigset_t	t_hold;		/* hold signal bit mask */
161 	struct	_kthread *t_forw;	/* process's forward thread link */
162 	struct	_kthread *t_back;	/* process's backward thread link */
163 	struct	_kthread *t_thlink;	/* tid (lwpid) lookup hash link */
164 	klwp_t	*t_lwp;			/* thread's lwp pointer */
165 	struct	proc	*t_procp;	/* proc pointer */
166 	struct	t_audit_data *t_audit_data;	/* per thread audit data */
167 	struct	_kthread *t_next;	/* doubly linked list of all threads */
168 	struct	_kthread *t_prev;
169 	ushort_t t_whystop;		/* reason for stopping */
170 	ushort_t t_whatstop;		/* more detailed reason */
171 	int	t_dslot;		/* index in proc's thread directory */
172 	struct	pollstate *t_pollstate;	/* state used during poll(2) */
173 	struct	pollcache *t_pollcache;	/* to pass a pcache ptr by /dev/poll */
174 	struct	cred	*t_cred;	/* pointer to current cred */
175 	time_t	t_start;		/* start time, seconds since epoch */
176 	clock_t	t_lbolt;		/* lbolt at last clock_tick() */
177 	hrtime_t t_stoptime;		/* timestamp at stop() */
178 	uint_t	t_pctcpu;		/* %cpu at last clock_tick(), binary */
179 					/* point at right of high-order bit */
180 	short	t_sysnum;		/* system call number */
181 	kcondvar_t	t_delay_cv;
182 	kmutex_t	t_delay_lock;
183 
184 	/*
185 	 * Pointer to the dispatcher lock protecting t_state and state-related
186 	 * flags.  This pointer can change during waits on the lock, so
187 	 * it should be grabbed only by thread_lock().
188 	 */
189 	disp_lock_t	*t_lockp;	/* pointer to the dispatcher lock */
190 	ushort_t 	t_oldspl;	/* spl level before dispatcher locked */
191 	volatile char	t_pre_sys;	/* pre-syscall work needed */
192 	lock_t		t_lock_flush;	/* for lock_mutex_flush() impl */
193 	struct _disp	*t_disp_queue;	/* run queue for chosen CPU */
194 	clock_t		t_disp_time;	/* last time this thread was running */
195 	uint_t		t_kpri_req;	/* kernel priority required */
196 
197 	/*
198 	 * Post-syscall / post-trap flags.
199 	 * 	No lock is required to set these.
200 	 *	These must be cleared only by the thread itself.
201 	 *
202 	 *	t_astflag indicates that some post-trap processing is required,
203 	 *		possibly a signal or a preemption.  The thread will not
204 	 *		return to user with this set.
205 	 *	t_post_sys indicates that some unusualy post-system call
206 	 *		handling is required, such as an error or tracing.
207 	 *	t_sig_check indicates that some condition in ISSIG() must be
208 	 * 		checked, but doesn't prevent returning to user.
209 	 *	t_post_sys_ast is a way of checking whether any of these three
210 	 *		flags are set.
211 	 */
212 	union __tu {
213 		struct __ts {
214 			volatile char	_t_astflag;	/* AST requested */
215 			volatile char	_t_sig_check;	/* ISSIG required */
216 			volatile char	_t_post_sys;	/* post_syscall req */
217 			volatile char	_t_trapret;	/* call CL_TRAPRET */
218 		} _ts;
219 		volatile int	_t_post_sys_ast;	/* OR of these flags */
220 	} _tu;
221 #define	t_astflag	_tu._ts._t_astflag
222 #define	t_sig_check	_tu._ts._t_sig_check
223 #define	t_post_sys	_tu._ts._t_post_sys
224 #define	t_trapret	_tu._ts._t_trapret
225 #define	t_post_sys_ast	_tu._t_post_sys_ast
226 
227 	/*
228 	 * Real time microstate profiling.
229 	 */
230 					/* possible 4-byte filler */
231 	hrtime_t t_waitrq;		/* timestamp for run queue wait time */
232 	int	t_mstate;		/* current microstate */
233 	struct rprof {
234 		int	rp_anystate;		/* set if any state non-zero */
235 		uint_t	rp_state[NMSTATES];	/* mstate profiling counts */
236 	} *t_rprof;
237 
238 	/*
239 	 * There is a turnstile inserted into the list below for
240 	 * every priority inverted synchronization object that
241 	 * this thread holds.
242 	 */
243 
244 	struct turnstile *t_prioinv;
245 
246 	/*
247 	 * Pointer to the turnstile attached to the synchronization
248 	 * object where this thread is blocked.
249 	 */
250 
251 	struct turnstile *t_ts;
252 
253 	/*
254 	 * kernel thread specific data
255 	 *	Borrowed from userland implementation of POSIX tsd
256 	 */
257 	struct tsd_thread {
258 		struct tsd_thread *ts_next;	/* threads with TSD */
259 		struct tsd_thread *ts_prev;	/* threads with TSD */
260 		uint_t		  ts_nkeys;	/* entries in value array */
261 		void		  **ts_value;	/* array of value/key */
262 	} *t_tsd;
263 
264 	clock_t		t_stime;	/* time stamp used by the swapper */
265 	struct door_data *t_door;	/* door invocation data */
266 	kmutex_t	*t_plockp;	/* pointer to process's p_lock */
267 
268 	struct sc_shared *t_schedctl;	/* scheduler activations shared data */
269 	uintptr_t	t_sc_uaddr;	/* user-level address of shared data */
270 
271 	struct cpupart	*t_cpupart;	/* partition containing thread */
272 	int		t_bind_pset;	/* processor set binding */
273 
274 	struct copyops	*t_copyops;	/* copy in/out ops vector */
275 
276 	caddr_t		t_stkbase;	/* base of the the stack */
277 	struct page	*t_red_pp;	/* if non-NULL, redzone is mapped */
278 
279 	struct _afd	t_activefd;	/* active file descriptor table */
280 
281 	struct _kthread	*t_priforw;	/* sleepq per-priority sublist */
282 	struct _kthread	*t_priback;
283 
284 	struct sleepq	*t_sleepq;	/* sleep queue thread is waiting on */
285 	struct trap_info *t_panic_trap;	/* saved data from fatal trap */
286 	int		*t_lgrp_affinity;	/* lgroup affinity */
287 	struct upimutex	*t_upimutex;	/* list of upimutexes owned by thread */
288 	uint32_t	t_nupinest;	/* number of nested held upi mutexes */
289 	struct kproject *t_proj;	/* project containing this thread */
290 	uint8_t		t_unpark;	/* modified holding t_delay_lock */
291 	uint8_t		t_release;	/* lwp_release() waked up the thread */
292 	uint8_t		t_hatdepth;	/* depth of recursive hat_memloads */
293 	kcondvar_t	t_joincv;	/* cv used to wait for thread exit */
294 	void		*t_taskq;	/* for threads belonging to taskq */
295 	hrtime_t	t_anttime;	/* most recent time anticipatory load */
296 					/*	was added to an lgroup's load */
297 					/*	on this thread's behalf */
298 	char		*t_pdmsg;	/* privilege debugging message */
299 
300 	uint_t		t_predcache;	/* DTrace predicate cache */
301 	hrtime_t	t_dtrace_vtime;	/* DTrace virtual time */
302 	hrtime_t	t_dtrace_start;	/* DTrace slice start time */
303 
304 	uint8_t		t_dtrace_stop;	/* indicates a DTrace-desired stop */
305 	uint8_t		t_dtrace_sig;	/* signal sent via DTrace's raise() */
306 
307 	union __tdu {
308 		struct __tds {
309 			uint8_t	_t_dtrace_on;	/* hit a fasttrap tracepoint */
310 			uint8_t	_t_dtrace_step;	/* about to return to kernel */
311 			uint8_t	_t_dtrace_ret;	/* handling a return probe */
312 			uint8_t	_t_dtrace_ast;	/* saved ast flag */
313 #ifdef __amd64
314 			uint8_t	_t_dtrace_reg;	/* modified register */
315 #endif
316 		} _tds;
317 		ulong_t	_t_dtrace_ft;		/* bitwise or of these flags */
318 	} _tdu;
319 #define	t_dtrace_ft	_tdu._t_dtrace_ft
320 #define	t_dtrace_on	_tdu._tds._t_dtrace_on
321 #define	t_dtrace_step	_tdu._tds._t_dtrace_step
322 #define	t_dtrace_ret	_tdu._tds._t_dtrace_ret
323 #define	t_dtrace_ast	_tdu._tds._t_dtrace_ast
324 #ifdef __amd64
325 #define	t_dtrace_reg	_tdu._tds._t_dtrace_reg
326 #endif
327 
328 	uintptr_t	t_dtrace_pc;	/* DTrace saved pc from fasttrap */
329 	uintptr_t	t_dtrace_npc;	/* DTrace next pc from fasttrap */
330 	uintptr_t	t_dtrace_scrpc;	/* DTrace per-thread scratch location */
331 	uintptr_t	t_dtrace_astpc;	/* DTrace return sequence location */
332 #ifdef __amd64
333 	uint64_t	t_dtrace_regv;	/* DTrace saved reg from fasttrap */
334 #endif
335 	hrtime_t	t_hrtime;	/* high-res last time on cpu */
336 } kthread_t;
337 
338 /*
339  * Thread flag (t_flag) definitions.
340  *	These flags must be changed only for the current thread,
341  * 	and not during preemption code, since the code being
342  *	preempted could be modifying the flags.
343  *
344  *	For the most part these flags do not need locking.
345  *	The following flags will only be changed while the thread_lock is held,
346  *	to give assurrance that they are consistent with t_state:
347  *		T_WAKEABLE
348  */
349 #define	T_INTR_THREAD	0x0001	/* thread is an interrupt thread */
350 #define	T_WAKEABLE	0x0002	/* thread is blocked, signals enabled */
351 #define	T_TOMASK	0x0004	/* use lwp_sigoldmask on return from signal */
352 #define	T_TALLOCSTK	0x0008  /* thread structure allocated from stk */
353 #define	T_WOULDBLOCK	0x0020	/* for lockfs */
354 #define	T_DONTBLOCK	0x0040	/* for lockfs */
355 #define	T_DONTPEND	0x0080	/* for lockfs */
356 #define	T_SYS_PROF	0x0100	/* profiling on for duration of system call */
357 #define	T_WAITCVSEM	0x0200	/* waiting for a lwp_cv or lwp_sema on sleepq */
358 #define	T_WATCHPT	0x0400	/* thread undergoing a watchpoint emulation */
359 #define	T_PANIC		0x0800	/* thread initiated a system panic */
360 #define	T_DFLTSTK	0x1000	/* stack is default size */
361 
362 /*
363  * Flags in t_proc_flag.
364  *	These flags must be modified only when holding the p_lock
365  *	for the associated process.
366  */
367 #define	TP_DAEMON	0x0001	/* this is an LWP_DAEMON lwp */
368 #define	TP_HOLDLWP	0x0002	/* hold thread's lwp */
369 #define	TP_TWAIT	0x0004	/* wait to be freed by lwp_wait() */
370 #define	TP_LWPEXIT	0x0008	/* lwp has exited */
371 #define	TP_PRSTOP	0x0010	/* thread is being stopped via /proc */
372 #define	TP_CHKPT	0x0020	/* thread is being stopped via CPR checkpoint */
373 #define	TP_EXITLWP	0x0040	/* terminate this lwp */
374 #define	TP_PRVSTOP	0x0080	/* thread is virtually stopped via /proc */
375 #define	TP_MSACCT	0x0100	/* collect micro-state accounting information */
376 #define	TP_STOPPING	0x0200	/* thread is executing stop() */
377 #define	TP_WATCHPT	0x0400	/* process has watchpoints in effect */
378 #define	TP_PAUSE	0x0800	/* process is being stopped via pauselwps() */
379 #define	TP_CHANGEBIND	0x1000	/* thread has a new cpu/cpupart binding */
380 #define	TP_ZTHREAD	0x2000	/* this is a kernel thread for a zone */
381 #define	TP_WATCHSTOP	0x4000	/* thread is stopping via holdwatch() */
382 
383 /*
384  * Thread scheduler flag (t_schedflag) definitions.
385  *	The thread must be locked via thread_lock() or equiv. to change these.
386  */
387 #define	TS_LOAD		0x0001	/* thread is in memory */
388 #define	TS_DONT_SWAP	0x0002	/* thread/lwp should not be swapped */
389 #define	TS_SWAPENQ	0x0004	/* swap thread when it reaches a safe point */
390 #define	TS_ON_SWAPQ	0x0008	/* thread is on the swap queue */
391 #define	TS_SIGNALLED	0x0010	/* thread was awakened by cv_signal() */
392 #define	TS_CSTART	0x0100	/* setrun() by continuelwps() */
393 #define	TS_UNPAUSE	0x0200	/* setrun() by unpauselwps() */
394 #define	TS_XSTART	0x0400	/* setrun() by SIGCONT */
395 #define	TS_PSTART	0x0800	/* setrun() by /proc */
396 #define	TS_RESUME	0x1000	/* setrun() by CPR resume process */
397 #define	TS_CREATE	0x2000	/* setrun() by syslwp_create() */
398 #define	TS_RUNQMATCH	0x4000	/* exact run queue balancing by setbackdq() */
399 #define	TS_ALLSTART	\
400 	(TS_CSTART|TS_UNPAUSE|TS_XSTART|TS_PSTART|TS_RESUME|TS_CREATE)
401 
402 /*
403  * No locking needed for AST field.
404  */
405 #define	aston(t)		((t)->t_astflag = 1)
406 #define	astoff(t)		((t)->t_astflag = 0)
407 
408 /* True if thread is stopped on an event of interest */
409 #define	ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
410 			!((t)->t_schedflag & TS_PSTART))
411 
412 /* similar to ISTOPPED except the event of interest is CPR */
413 #define	CPR_ISTOPPED(t) ((t)->t_state == TS_STOPPED && \
414 			!((t)->t_schedflag & TS_RESUME))
415 
416 /*
417  * True if thread is virtually stopped (is or was asleep in
418  * one of the lwp_*() system calls and marked to stop by /proc.)
419  */
420 #define	VSTOPPED(t)	((t)->t_proc_flag & TP_PRVSTOP)
421 
422 /* similar to VSTOPPED except the point of interest is CPR */
423 #define	CPR_VSTOPPED(t)				\
424 	((t)->t_state == TS_SLEEP &&		\
425 	(t)->t_wchan0 != NULL &&		\
426 	((t)->t_flag & T_WAKEABLE) &&		\
427 	((t)->t_proc_flag & TP_CHKPT))
428 
429 /* True if thread has been stopped by hold*() or was created stopped */
430 #define	SUSPENDED(t) ((t)->t_state == TS_STOPPED && \
431 	((t)->t_schedflag & (TS_CSTART|TS_UNPAUSE)) != (TS_CSTART|TS_UNPAUSE))
432 
433 /* True if thread possesses an inherited priority */
434 #define	INHERITED(t)	((t)->t_epri != 0)
435 
436 /* The dispatch priority of a thread */
437 #define	DISP_PRIO(t) ((t)->t_epri > (t)->t_pri ? (t)->t_epri : (t)->t_pri)
438 
439 /* The assigned priority of a thread */
440 #define	ASSIGNED_PRIO(t)	((t)->t_pri)
441 
442 /*
443  * Macros to determine whether a thread can be swapped.
444  * If t_lock is held, the thread is either on a processor or being swapped.
445  */
446 #define	SWAP_OK(t)	(!LOCK_HELD(&(t)->t_lock))
447 
448 /*
449  * proctot(x)
450  *	convert a proc pointer to a thread pointer. this only works with
451  *	procs that have only one lwp.
452  *
453  * proctolwp(x)
454  *	convert a proc pointer to a lwp pointer. this only works with
455  *	procs that have only one lwp.
456  *
457  * ttolwp(x)
458  *	convert a thread pointer to its lwp pointer.
459  *
460  * ttoproc(x)
461  *	convert a thread pointer to its proc pointer.
462  *
463  * ttoproj(x)
464  * 	convert a thread pointer to its project pointer.
465  *
466  * lwptot(x)
467  *	convert a lwp pointer to its thread pointer.
468  *
469  * lwptoproc(x)
470  *	convert a lwp to its proc pointer.
471  */
472 #define	proctot(x)	((x)->p_tlist)
473 #define	proctolwp(x)	((x)->p_tlist->t_lwp)
474 #define	ttolwp(x)	((x)->t_lwp)
475 #define	ttoproc(x)	((x)->t_procp)
476 #define	ttoproj(x)	((x)->t_proj)
477 #define	lwptot(x)	((x)->lwp_thread)
478 #define	lwptoproc(x)	((x)->lwp_procp)
479 
480 #define	t_pc		t_pcb.val[0]
481 #define	t_sp		t_pcb.val[1]
482 
483 #ifdef	_KERNEL
484 
485 extern	kthread_t	*threadp(void);	/* inline, returns thread pointer */
486 #define	curthread	(threadp())		/* current thread pointer */
487 #define	curproc		(ttoproc(curthread))	/* current process pointer */
488 #define	curproj		(ttoproj(curthread))	/* current project pointer */
489 
490 extern	struct _kthread	t0;		/* the scheduler thread */
491 extern	kmutex_t	pidlock;	/* global process lock */
492 
493 /*
494  * thread_free_lock is used by the clock thread to keep a thread
495  * from being freed while it is being examined.
496  */
497 extern	kmutex_t	thread_free_lock;
498 
499 /*
500  * Routines to change the priority and effective priority
501  * of a thread-locked thread, whatever its state.
502  */
503 extern int	thread_change_pri(kthread_t *t, pri_t disp_pri, int front);
504 extern void	thread_change_epri(kthread_t *t, pri_t disp_pri);
505 
506 /*
507  * Routines that manipulate the dispatcher lock for the thread.
508  * The locking heirarchy is as follows:
509  *	cpu_lock > sleepq locks > run queue locks
510  */
511 void	thread_transition(kthread_t *); /* move to transition lock */
512 void	thread_stop(kthread_t *);	/* move to stop lock */
513 void	thread_lock(kthread_t *);	/* lock thread and its queue */
514 void	thread_lock_high(kthread_t *);	/* lock thread and its queue */
515 void	thread_onproc(kthread_t *, struct cpu *); /* set onproc state lock */
516 
517 #define	thread_unlock(t)		disp_lock_exit((t)->t_lockp)
518 #define	thread_unlock_high(t)		disp_lock_exit_high((t)->t_lockp)
519 #define	thread_unlock_nopreempt(t)	disp_lock_exit_nopreempt((t)->t_lockp)
520 
521 #define	THREAD_LOCK_HELD(t)	(DISP_LOCK_HELD((t)->t_lockp))
522 
523 extern disp_lock_t transition_lock;	/* lock protecting transiting threads */
524 extern disp_lock_t stop_lock;		/* lock protecting stopped threads */
525 
526 caddr_t	thread_stk_init(caddr_t);	/* init thread stack */
527 
528 #endif	/* _KERNEL */
529 
530 /*
531  * Macros to indicate that the thread holds resources that could be critical
532  * to other kernel threads, so this thread needs to have kernel priority
533  * if it blocks or is preempted.  Note that this is not necessary if the
534  * resource is a mutex or a writer lock because of priority inheritance.
535  *
536  * The only way one thread may legally manipulate another thread's t_kpri_req
537  * is to hold the target thread's thread lock while that thread is asleep.
538  * (The rwlock code does this to implement direct handoff to waiting readers.)
539  */
540 #define	THREAD_KPRI_REQUEST()	(curthread->t_kpri_req++)
541 #define	THREAD_KPRI_RELEASE()	(curthread->t_kpri_req--)
542 #define	THREAD_KPRI_RELEASE_N(n) (curthread->t_kpri_req -= (n))
543 
544 /*
545  * Macro to change a thread's priority.
546  */
547 #define	THREAD_CHANGE_PRI(t, pri) {					\
548 	pri_t __new_pri = (pri);					\
549 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, __new_pri);	\
550 	(t)->t_pri = __new_pri;						\
551 }
552 
553 /*
554  * Macro to indicate that a thread's priority is about to be changed.
555  */
556 #define	THREAD_WILLCHANGE_PRI(t, pri) {					\
557 	DTRACE_SCHED2(change__pri, kthread_t *, (t), pri_t, (pri));	\
558 }
559 
560 /*
561  * Macros to change thread state and the associated lock.
562  */
563 #define	THREAD_SET_STATE(tp, state, lp) \
564 		((tp)->t_state = state, (tp)->t_lockp = lp)
565 
566 /*
567  * Point it at the transition lock, which is always held.
568  * The previosly held lock is dropped.
569  */
570 #define	THREAD_TRANSITION(tp) 	thread_transition(tp);
571 /*
572  * Set the thread's lock to be the transition lock, without dropping
573  * previosly held lock.
574  */
575 #define	THREAD_TRANSITION_NOLOCK(tp) 	((tp)->t_lockp = &transition_lock)
576 
577 /*
578  * Put thread in run state, and set the lock pointer to the dispatcher queue
579  * lock pointer provided.  This lock should be held.
580  */
581 #define	THREAD_RUN(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
582 
583 /*
584  * Put thread in run state, and set the lock pointer to the dispatcher queue
585  * lock pointer provided (i.e., the "swapped_lock").  This lock should be held.
586  */
587 #define	THREAD_SWAP(tp, lp)	THREAD_SET_STATE(tp, TS_RUN, lp)
588 
589 /*
590  * Put the thread in zombie state and set the lock pointer to NULL.
591  * The NULL will catch anything that tries to lock a zombie.
592  */
593 #define	THREAD_ZOMB(tp)		THREAD_SET_STATE(tp, TS_ZOMB, NULL)
594 
595 /*
596  * Set the thread into ONPROC state, and point the lock at the CPUs
597  * lock for the onproc thread(s).  This lock should be held, so the
598  * thread deoes not become unlocked, since these stores can be reordered.
599  */
600 #define	THREAD_ONPROC(tp, cpu)	\
601 		THREAD_SET_STATE(tp, TS_ONPROC, &(cpu)->cpu_thread_lock)
602 
603 /*
604  * Set the thread into the TS_SLEEP state, and set the lock pointer to
605  * to some sleep queue's lock.  The new lock should already be held.
606  */
607 #define	THREAD_SLEEP(tp, lp)	{				\
608 			disp_lock_t	*tlp;			\
609 			tlp = (tp)->t_lockp;			\
610 			THREAD_SET_STATE(tp, TS_SLEEP, lp);	\
611 			disp_lock_exit_high(tlp);		\
612 			}
613 
614 /*
615  * Interrupt threads are created in TS_FREE state, and their lock
616  * points at the associated CPU's lock.
617  */
618 #define	THREAD_FREEINTR(tp, cpu)	\
619 		THREAD_SET_STATE(tp, TS_FREE, &(cpu)->cpu_thread_lock)
620 
621 
622 #ifdef	__cplusplus
623 }
624 #endif
625 
626 #endif /* _SYS_THREAD_H */
627