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