Print this page
8276 rpcbind leaks memory due to libumem per thread caching.
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>
Reviewed by: Gordon Ross <gordon.w.ross@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/threads/thr.c
+++ new/usr/src/lib/libc/port/threads/thr.c
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.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2016 by Delphix. All rights reserved.
25 + * Copyright (c) 2017 by The MathWorks, Inc. All rights reserved.
25 26 */
26 27 /*
27 28 * Copyright 2016 Joyent, Inc.
28 29 */
29 30
30 31 #include "lint.h"
31 32 #include "thr_uberdata.h"
32 33 #include <pthread.h>
33 34 #include <procfs.h>
34 35 #include <sys/uio.h>
35 36 #include <ctype.h>
36 37 #include "libc.h"
37 38
38 39 /*
39 40 * These symbols should not be exported from libc, but
40 41 * /lib/libm.so.2 references _thr_main. libm needs to be fixed.
41 42 * Also, some older versions of the Studio compiler/debugger
42 43 * components reference them. These need to be fixed, too.
43 44 */
44 45 #pragma weak _thr_main = thr_main
45 46 #pragma weak _thr_create = thr_create
46 47 #pragma weak _thr_join = thr_join
47 48 #pragma weak _thr_self = thr_self
48 49
49 50 #undef errno
50 51 extern int errno;
51 52
52 53 /*
53 54 * Between Solaris 2.5 and Solaris 9, __threaded was used to indicate
54 55 * "we are linked with libthread". The Sun Workshop 6 update 1 compilation
55 56 * system used it illegally (it is a consolidation private symbol).
56 57 * To accommodate this and possibly other abusers of the symbol,
57 58 * we make it always equal to 1 now that libthread has been folded
58 59 * into libc. The new __libc_threaded symbol is used to indicate
59 60 * the new meaning, "more than one thread exists".
60 61 */
61 62 int __threaded = 1; /* always equal to 1 */
62 63 int __libc_threaded = 0; /* zero until first thr_create() */
63 64
64 65 /*
65 66 * thr_concurrency and pthread_concurrency are not used by the library.
66 67 * They exist solely to hold and return the values set by calls to
67 68 * thr_setconcurrency() and pthread_setconcurrency().
68 69 * Because thr_concurrency is affected by the THR_NEW_LWP flag
69 70 * to thr_create(), thr_concurrency is protected by link_lock.
70 71 */
71 72 static int thr_concurrency = 1;
72 73 static int pthread_concurrency;
73 74
74 75 #define HASHTBLSZ 1024 /* must be a power of two */
75 76 #define TIDHASH(tid, udp) (tid & (udp)->hash_mask)
76 77
77 78 /* initial allocation, just enough for one lwp */
78 79 #pragma align 64(init_hash_table)
79 80 thr_hash_table_t init_hash_table[1] = {
80 81 { DEFAULTMUTEX, DEFAULTCV, NULL },
81 82 };
82 83
83 84 extern const Lc_interface rtld_funcs[];
84 85
85 86 /*
86 87 * The weak version is known to libc_db and mdb.
87 88 */
88 89 #pragma weak _uberdata = __uberdata
89 90 uberdata_t __uberdata = {
90 91 { DEFAULTMUTEX, NULL, 0 }, /* link_lock */
91 92 { RECURSIVEMUTEX, NULL, 0 }, /* ld_lock */
92 93 { RECURSIVEMUTEX, NULL, 0 }, /* fork_lock */
93 94 { RECURSIVEMUTEX, NULL, 0 }, /* atfork_lock */
94 95 { RECURSIVEMUTEX, NULL, 0 }, /* callout_lock */
95 96 { DEFAULTMUTEX, NULL, 0 }, /* tdb_hash_lock */
96 97 { 0, }, /* tdb_hash_lock_stats */
97 98 { { 0 }, }, /* siguaction[NSIG] */
98 99 {{ DEFAULTMUTEX, NULL, 0 }, /* bucket[NBUCKETS] */
99 100 { DEFAULTMUTEX, NULL, 0 },
100 101 { DEFAULTMUTEX, NULL, 0 },
101 102 { DEFAULTMUTEX, NULL, 0 },
102 103 { DEFAULTMUTEX, NULL, 0 },
103 104 { DEFAULTMUTEX, NULL, 0 },
104 105 { DEFAULTMUTEX, NULL, 0 },
105 106 { DEFAULTMUTEX, NULL, 0 },
106 107 { DEFAULTMUTEX, NULL, 0 },
107 108 { DEFAULTMUTEX, NULL, 0 }},
108 109 { RECURSIVEMUTEX, NULL, NULL }, /* atexit_root */
109 110 { RECURSIVEMUTEX, NULL }, /* quickexit_root */
110 111 { DEFAULTMUTEX, 0, 0, NULL }, /* tsd_metadata */
111 112 { DEFAULTMUTEX, {0, 0}, {0, 0} }, /* tls_metadata */
112 113 0, /* primary_map */
113 114 0, /* bucket_init */
114 115 0, /* pad[0] */
115 116 0, /* pad[1] */
116 117 { 0 }, /* uberflags */
117 118 NULL, /* queue_head */
118 119 init_hash_table, /* thr_hash_table */
119 120 1, /* hash_size: size of the hash table */
120 121 0, /* hash_mask: hash_size - 1 */
121 122 NULL, /* ulwp_one */
122 123 NULL, /* all_lwps */
123 124 NULL, /* all_zombies */
124 125 0, /* nthreads */
125 126 0, /* nzombies */
126 127 0, /* ndaemons */
127 128 0, /* pid */
128 129 sigacthandler, /* sigacthandler */
129 130 NULL, /* lwp_stacks */
130 131 NULL, /* lwp_laststack */
131 132 0, /* nfreestack */
132 133 10, /* thread_stack_cache */
133 134 NULL, /* ulwp_freelist */
134 135 NULL, /* ulwp_lastfree */
135 136 NULL, /* ulwp_replace_free */
136 137 NULL, /* ulwp_replace_last */
137 138 NULL, /* atforklist */
138 139 NULL, /* robustlocks */
139 140 NULL, /* robustlist */
140 141 NULL, /* progname */
141 142 NULL, /* ub_comm_page */
142 143 NULL, /* __tdb_bootstrap */
143 144 { /* tdb */
144 145 NULL, /* tdb_sync_addr_hash */
145 146 0, /* tdb_register_count */
146 147 0, /* tdb_hash_alloc_failed */
147 148 NULL, /* tdb_sync_addr_free */
148 149 NULL, /* tdb_sync_addr_last */
149 150 0, /* tdb_sync_alloc */
150 151 { 0, 0 }, /* tdb_ev_global_mask */
151 152 tdb_events, /* tdb_events array */
152 153 },
153 154 };
154 155
155 156 /*
156 157 * The weak version is known to libc_db and mdb.
157 158 */
158 159 #pragma weak _tdb_bootstrap = __tdb_bootstrap
159 160 uberdata_t **__tdb_bootstrap = NULL;
160 161
161 162 int thread_queue_fifo = 4;
162 163 int thread_queue_dump = 0;
163 164 int thread_cond_wait_defer = 0;
164 165 int thread_error_detection = 0;
165 166 int thread_async_safe = 0;
166 167 int thread_stack_cache = 10;
167 168 int thread_door_noreserve = 0;
168 169 int thread_locks_misaligned = 0;
169 170
170 171 static ulwp_t *ulwp_alloc(void);
171 172 static void ulwp_free(ulwp_t *);
172 173
173 174 /*
174 175 * Insert the lwp into the hash table.
175 176 */
176 177 void
177 178 hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
178 179 {
179 180 ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket;
180 181 udp->thr_hash_table[ix].hash_bucket = ulwp;
181 182 ulwp->ul_ix = ix;
182 183 }
183 184
184 185 void
185 186 hash_in(ulwp_t *ulwp, uberdata_t *udp)
186 187 {
187 188 int ix = TIDHASH(ulwp->ul_lwpid, udp);
188 189 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
189 190
190 191 lmutex_lock(mp);
191 192 hash_in_unlocked(ulwp, ix, udp);
192 193 lmutex_unlock(mp);
193 194 }
194 195
195 196 /*
196 197 * Delete the lwp from the hash table.
197 198 */
198 199 void
199 200 hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
200 201 {
201 202 ulwp_t **ulwpp;
202 203
203 204 for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
204 205 ulwp != *ulwpp;
205 206 ulwpp = &(*ulwpp)->ul_hash)
206 207 ;
207 208 *ulwpp = ulwp->ul_hash;
208 209 ulwp->ul_hash = NULL;
209 210 ulwp->ul_ix = -1;
210 211 }
211 212
212 213 void
213 214 hash_out(ulwp_t *ulwp, uberdata_t *udp)
214 215 {
215 216 int ix;
216 217
217 218 if ((ix = ulwp->ul_ix) >= 0) {
218 219 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
219 220
220 221 lmutex_lock(mp);
221 222 hash_out_unlocked(ulwp, ix, udp);
222 223 lmutex_unlock(mp);
223 224 }
224 225 }
225 226
226 227 /*
227 228 * Retain stack information for thread structures that are being recycled for
228 229 * new threads. All other members of the thread structure should be zeroed.
229 230 */
230 231 static void
231 232 ulwp_clean(ulwp_t *ulwp)
232 233 {
233 234 caddr_t stk = ulwp->ul_stk;
234 235 size_t mapsiz = ulwp->ul_mapsiz;
235 236 size_t guardsize = ulwp->ul_guardsize;
236 237 uintptr_t stktop = ulwp->ul_stktop;
237 238 size_t stksiz = ulwp->ul_stksiz;
238 239
239 240 (void) memset(ulwp, 0, sizeof (*ulwp));
240 241
241 242 ulwp->ul_stk = stk;
242 243 ulwp->ul_mapsiz = mapsiz;
243 244 ulwp->ul_guardsize = guardsize;
244 245 ulwp->ul_stktop = stktop;
245 246 ulwp->ul_stksiz = stksiz;
246 247 }
247 248
248 249 static int stackprot;
249 250
250 251 /*
251 252 * Answer the question, "Is the lwp in question really dead?"
252 253 * We must inquire of the operating system to be really sure
253 254 * because the lwp may have called lwp_exit() but it has not
254 255 * yet completed the exit.
255 256 */
256 257 static int
257 258 dead_and_buried(ulwp_t *ulwp)
258 259 {
259 260 if (ulwp->ul_lwpid == (lwpid_t)(-1))
260 261 return (1);
261 262 if (ulwp->ul_dead && ulwp->ul_detached &&
262 263 _lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) {
263 264 ulwp->ul_lwpid = (lwpid_t)(-1);
264 265 return (1);
265 266 }
266 267 return (0);
267 268 }
268 269
269 270 /*
270 271 * Attempt to keep the stack cache within the specified cache limit.
271 272 */
272 273 static void
273 274 trim_stack_cache(int cache_limit)
274 275 {
275 276 ulwp_t *self = curthread;
276 277 uberdata_t *udp = self->ul_uberdata;
277 278 ulwp_t *prev = NULL;
278 279 ulwp_t **ulwpp = &udp->lwp_stacks;
279 280 ulwp_t *ulwp;
280 281
281 282 ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self));
282 283
283 284 while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) {
284 285 if (dead_and_buried(ulwp)) {
285 286 *ulwpp = ulwp->ul_next;
286 287 if (ulwp == udp->lwp_laststack)
287 288 udp->lwp_laststack = prev;
288 289 hash_out(ulwp, udp);
289 290 udp->nfreestack--;
290 291 (void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz);
291 292 /*
292 293 * Now put the free ulwp on the ulwp freelist.
293 294 */
294 295 ulwp->ul_mapsiz = 0;
295 296 ulwp->ul_next = NULL;
296 297 if (udp->ulwp_freelist == NULL)
297 298 udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
298 299 else {
299 300 udp->ulwp_lastfree->ul_next = ulwp;
300 301 udp->ulwp_lastfree = ulwp;
301 302 }
302 303 } else {
303 304 prev = ulwp;
304 305 ulwpp = &ulwp->ul_next;
305 306 }
306 307 }
307 308 }
308 309
309 310 /*
310 311 * Find an unused stack of the requested size
311 312 * or create a new stack of the requested size.
312 313 * Return a pointer to the ulwp_t structure referring to the stack, or NULL.
313 314 * thr_exit() stores 1 in the ul_dead member.
314 315 * thr_join() stores -1 in the ul_lwpid member.
315 316 */
316 317 static ulwp_t *
317 318 find_stack(size_t stksize, size_t guardsize)
318 319 {
319 320 static size_t pagesize = 0;
320 321
321 322 uberdata_t *udp = curthread->ul_uberdata;
322 323 size_t mapsize;
323 324 ulwp_t *prev;
324 325 ulwp_t *ulwp;
325 326 ulwp_t **ulwpp;
326 327 void *stk;
327 328
328 329 /*
329 330 * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC
330 331 * unless overridden by the system's configuration.
331 332 */
332 333 if (stackprot == 0) { /* do this once */
333 334 long lprot = _sysconf(_SC_STACK_PROT);
334 335 if (lprot <= 0)
335 336 lprot = (PROT_READ|PROT_WRITE|PROT_EXEC);
336 337 stackprot = (int)lprot;
337 338 }
338 339 if (pagesize == 0) /* do this once */
339 340 pagesize = _sysconf(_SC_PAGESIZE);
340 341
341 342 /*
342 343 * One megabyte stacks by default, but subtract off
343 344 * two pages for the system-created red zones.
344 345 * Round up a non-zero stack size to a pagesize multiple.
345 346 */
346 347 if (stksize == 0)
347 348 stksize = DEFAULTSTACK - 2 * pagesize;
348 349 else
349 350 stksize = ((stksize + pagesize - 1) & -pagesize);
350 351
351 352 /*
352 353 * Round up the mapping size to a multiple of pagesize.
353 354 * Note: mmap() provides at least one page of red zone
354 355 * so we deduct that from the value of guardsize.
355 356 */
356 357 if (guardsize != 0)
357 358 guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize;
358 359 mapsize = stksize + guardsize;
359 360
360 361 lmutex_lock(&udp->link_lock);
361 362 for (prev = NULL, ulwpp = &udp->lwp_stacks;
362 363 (ulwp = *ulwpp) != NULL;
363 364 prev = ulwp, ulwpp = &ulwp->ul_next) {
364 365 if (ulwp->ul_mapsiz == mapsize &&
365 366 ulwp->ul_guardsize == guardsize &&
366 367 dead_and_buried(ulwp)) {
367 368 /*
368 369 * The previous lwp is gone; reuse the stack.
369 370 * Remove the ulwp from the stack list.
370 371 */
371 372 *ulwpp = ulwp->ul_next;
372 373 ulwp->ul_next = NULL;
373 374 if (ulwp == udp->lwp_laststack)
374 375 udp->lwp_laststack = prev;
375 376 hash_out(ulwp, udp);
376 377 udp->nfreestack--;
377 378 lmutex_unlock(&udp->link_lock);
378 379 ulwp_clean(ulwp);
379 380 return (ulwp);
380 381 }
381 382 }
382 383
383 384 /*
384 385 * None of the cached stacks matched our mapping size.
385 386 * Reduce the stack cache to get rid of possibly
386 387 * very old stacks that will never be reused.
387 388 */
388 389 if (udp->nfreestack > udp->thread_stack_cache)
389 390 trim_stack_cache(udp->thread_stack_cache);
390 391 else if (udp->nfreestack > 0)
391 392 trim_stack_cache(udp->nfreestack - 1);
392 393 lmutex_unlock(&udp->link_lock);
393 394
394 395 /*
395 396 * Create a new stack.
396 397 */
397 398 if ((stk = mmap(NULL, mapsize, stackprot,
398 399 MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) {
399 400 /*
400 401 * We have allocated our stack. Now allocate the ulwp.
401 402 */
402 403 ulwp = ulwp_alloc();
403 404 if (ulwp == NULL)
404 405 (void) munmap(stk, mapsize);
405 406 else {
406 407 ulwp->ul_stk = stk;
407 408 ulwp->ul_mapsiz = mapsize;
408 409 ulwp->ul_guardsize = guardsize;
409 410 ulwp->ul_stktop = (uintptr_t)stk + mapsize;
410 411 ulwp->ul_stksiz = stksize;
411 412 if (guardsize) /* protect the extra red zone */
412 413 (void) mprotect(stk, guardsize, PROT_NONE);
413 414 }
414 415 }
415 416 return (ulwp);
416 417 }
417 418
418 419 /*
419 420 * Get a ulwp_t structure from the free list or allocate a new one.
420 421 * Such ulwp_t's do not have a stack allocated by the library.
421 422 */
422 423 static ulwp_t *
423 424 ulwp_alloc(void)
424 425 {
425 426 ulwp_t *self = curthread;
426 427 uberdata_t *udp = self->ul_uberdata;
427 428 size_t tls_size;
428 429 ulwp_t *prev;
429 430 ulwp_t *ulwp;
430 431 ulwp_t **ulwpp;
431 432 caddr_t data;
432 433
433 434 lmutex_lock(&udp->link_lock);
434 435 for (prev = NULL, ulwpp = &udp->ulwp_freelist;
435 436 (ulwp = *ulwpp) != NULL;
436 437 prev = ulwp, ulwpp = &ulwp->ul_next) {
437 438 if (dead_and_buried(ulwp)) {
438 439 *ulwpp = ulwp->ul_next;
439 440 ulwp->ul_next = NULL;
440 441 if (ulwp == udp->ulwp_lastfree)
441 442 udp->ulwp_lastfree = prev;
442 443 hash_out(ulwp, udp);
443 444 lmutex_unlock(&udp->link_lock);
444 445 ulwp_clean(ulwp);
445 446 return (ulwp);
446 447 }
447 448 }
448 449 lmutex_unlock(&udp->link_lock);
449 450
450 451 tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
451 452 data = lmalloc(sizeof (*ulwp) + tls_size);
452 453 if (data != NULL) {
453 454 /* LINTED pointer cast may result in improper alignment */
454 455 ulwp = (ulwp_t *)(data + tls_size);
455 456 }
456 457 return (ulwp);
457 458 }
458 459
459 460 /*
460 461 * Free a ulwp structure.
461 462 * If there is an associated stack, put it on the stack list and
462 463 * munmap() previously freed stacks up to the residual cache limit.
463 464 * Else put it on the ulwp free list and never call lfree() on it.
464 465 */
465 466 static void
466 467 ulwp_free(ulwp_t *ulwp)
467 468 {
468 469 uberdata_t *udp = curthread->ul_uberdata;
469 470
470 471 ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread));
471 472 ulwp->ul_next = NULL;
472 473 if (ulwp == udp->ulwp_one) /* don't reuse the primoridal stack */
473 474 /*EMPTY*/;
474 475 else if (ulwp->ul_mapsiz != 0) {
475 476 if (udp->lwp_stacks == NULL)
476 477 udp->lwp_stacks = udp->lwp_laststack = ulwp;
477 478 else {
478 479 udp->lwp_laststack->ul_next = ulwp;
479 480 udp->lwp_laststack = ulwp;
480 481 }
481 482 if (++udp->nfreestack > udp->thread_stack_cache)
482 483 trim_stack_cache(udp->thread_stack_cache);
483 484 } else {
484 485 if (udp->ulwp_freelist == NULL)
485 486 udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
486 487 else {
487 488 udp->ulwp_lastfree->ul_next = ulwp;
488 489 udp->ulwp_lastfree = ulwp;
489 490 }
490 491 }
491 492 }
492 493
493 494 /*
494 495 * Find a named lwp and return a pointer to its hash list location.
495 496 * On success, returns with the hash lock held.
496 497 */
497 498 ulwp_t **
498 499 find_lwpp(thread_t tid)
499 500 {
500 501 uberdata_t *udp = curthread->ul_uberdata;
501 502 int ix = TIDHASH(tid, udp);
502 503 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
503 504 ulwp_t *ulwp;
504 505 ulwp_t **ulwpp;
505 506
506 507 if (tid == 0)
507 508 return (NULL);
508 509
509 510 lmutex_lock(mp);
510 511 for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
511 512 (ulwp = *ulwpp) != NULL;
512 513 ulwpp = &ulwp->ul_hash) {
513 514 if (ulwp->ul_lwpid == tid)
514 515 return (ulwpp);
515 516 }
516 517 lmutex_unlock(mp);
517 518 return (NULL);
518 519 }
519 520
520 521 /*
521 522 * Wake up all lwps waiting on this lwp for some reason.
522 523 */
523 524 void
524 525 ulwp_broadcast(ulwp_t *ulwp)
525 526 {
526 527 ulwp_t *self = curthread;
527 528 uberdata_t *udp = self->ul_uberdata;
528 529
529 530 ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
530 531 (void) cond_broadcast(ulwp_condvar(ulwp, udp));
531 532 }
532 533
533 534 /*
534 535 * Find a named lwp and return a pointer to it.
535 536 * Returns with the hash lock held.
536 537 */
537 538 ulwp_t *
538 539 find_lwp(thread_t tid)
539 540 {
540 541 ulwp_t *self = curthread;
541 542 uberdata_t *udp = self->ul_uberdata;
542 543 ulwp_t *ulwp = NULL;
543 544 ulwp_t **ulwpp;
544 545
545 546 if (self->ul_lwpid == tid) {
546 547 ulwp = self;
547 548 ulwp_lock(ulwp, udp);
548 549 } else if ((ulwpp = find_lwpp(tid)) != NULL) {
549 550 ulwp = *ulwpp;
550 551 }
551 552
552 553 if (ulwp && ulwp->ul_dead) {
553 554 ulwp_unlock(ulwp, udp);
554 555 ulwp = NULL;
555 556 }
556 557
557 558 return (ulwp);
558 559 }
559 560
560 561 int
561 562 _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
562 563 long flags, thread_t *new_thread, size_t guardsize)
563 564 {
564 565 ulwp_t *self = curthread;
565 566 uberdata_t *udp = self->ul_uberdata;
566 567 ucontext_t uc;
567 568 uint_t lwp_flags;
568 569 thread_t tid;
569 570 int error;
570 571 ulwp_t *ulwp;
571 572
572 573 /*
573 574 * Enforce the restriction of not creating any threads
574 575 * until the primary link map has been initialized.
575 576 * Also, disallow thread creation to a child of vfork().
576 577 */
577 578 if (!self->ul_primarymap || self->ul_vfork)
578 579 return (ENOTSUP);
579 580
580 581 if (udp->hash_size == 1)
581 582 finish_init();
582 583
583 584 if ((stk || stksize) && stksize < MINSTACK)
584 585 return (EINVAL);
585 586
586 587 if (stk == NULL) {
587 588 if ((ulwp = find_stack(stksize, guardsize)) == NULL)
588 589 return (ENOMEM);
589 590 stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize;
590 591 } else {
591 592 /* initialize the private stack */
592 593 if ((ulwp = ulwp_alloc()) == NULL)
593 594 return (ENOMEM);
594 595 ulwp->ul_stk = stk;
595 596 ulwp->ul_stktop = (uintptr_t)stk + stksize;
596 597 ulwp->ul_stksiz = stksize;
597 598 }
598 599 /* ulwp is not in the hash table; make sure hash_out() doesn't fail */
599 600 ulwp->ul_ix = -1;
600 601 ulwp->ul_errnop = &ulwp->ul_errno;
601 602
602 603 lwp_flags = LWP_SUSPENDED;
603 604 if (flags & (THR_DETACHED|THR_DAEMON)) {
604 605 flags |= THR_DETACHED;
605 606 lwp_flags |= LWP_DETACHED;
606 607 }
607 608 if (flags & THR_DAEMON)
608 609 lwp_flags |= LWP_DAEMON;
609 610
610 611 /* creating a thread: enforce mt-correctness in mutex_lock() */
611 612 self->ul_async_safe = 1;
612 613
613 614 /* per-thread copies of global variables, for speed */
614 615 ulwp->ul_queue_fifo = self->ul_queue_fifo;
615 616 ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer;
616 617 ulwp->ul_error_detection = self->ul_error_detection;
617 618 ulwp->ul_async_safe = self->ul_async_safe;
618 619 ulwp->ul_max_spinners = self->ul_max_spinners;
619 620 ulwp->ul_adaptive_spin = self->ul_adaptive_spin;
620 621 ulwp->ul_queue_spin = self->ul_queue_spin;
621 622 ulwp->ul_door_noreserve = self->ul_door_noreserve;
622 623 ulwp->ul_misaligned = self->ul_misaligned;
623 624
624 625 /* new thread inherits creating thread's scheduling parameters */
625 626 ulwp->ul_policy = self->ul_policy;
626 627 ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri);
627 628 ulwp->ul_cid = self->ul_cid;
628 629 ulwp->ul_rtclassid = self->ul_rtclassid;
629 630
630 631 ulwp->ul_primarymap = self->ul_primarymap;
631 632 ulwp->ul_self = ulwp;
632 633 ulwp->ul_uberdata = udp;
633 634
634 635 /* debugger support */
635 636 ulwp->ul_usropts = flags;
636 637
637 638 #ifdef __sparc
638 639 /*
639 640 * We cache several instructions in the thread structure for use
640 641 * by the fasttrap DTrace provider. When changing this, read the
641 642 * comment in fasttrap.h for the all the other places that must
642 643 * be changed.
643 644 */
644 645 ulwp->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */
645 646 ulwp->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */
646 647 ulwp->ul_dftret = 0x91d0203a; /* ta 0x3a */
647 648 ulwp->ul_dreturn = 0x81ca0000; /* return %o0 */
648 649 #endif
649 650
650 651 ulwp->ul_startpc = func;
651 652 ulwp->ul_startarg = arg;
652 653 _fpinherit(ulwp);
653 654 /*
654 655 * Defer signals on the new thread until its TLS constructors
655 656 * have been called. _thrp_setup() will call sigon() after
656 657 * it has called tls_setup().
657 658 */
658 659 ulwp->ul_sigdefer = 1;
659 660
660 661 error = setup_context(&uc, _thrp_setup, ulwp,
661 662 (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize);
662 663 if (error != 0 && stk != NULL) /* inaccessible stack */
663 664 error = EFAULT;
664 665
665 666 /*
666 667 * Call enter_critical() to avoid being suspended until we
667 668 * have linked the new thread into the proper lists.
668 669 * This is necessary because forkall() and fork1() must
669 670 * suspend all threads and they must see a complete list.
670 671 */
671 672 enter_critical(self);
672 673 uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask;
673 674 if (error != 0 ||
674 675 (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) {
675 676 exit_critical(self);
676 677 ulwp->ul_lwpid = (lwpid_t)(-1);
677 678 ulwp->ul_dead = 1;
678 679 ulwp->ul_detached = 1;
679 680 lmutex_lock(&udp->link_lock);
680 681 ulwp_free(ulwp);
681 682 lmutex_unlock(&udp->link_lock);
682 683 return (error);
683 684 }
684 685 self->ul_nocancel = 0; /* cancellation is now possible */
685 686 udp->uberflags.uf_mt = 1;
686 687 if (new_thread)
687 688 *new_thread = tid;
688 689 if (flags & THR_DETACHED)
689 690 ulwp->ul_detached = 1;
690 691 ulwp->ul_lwpid = tid;
691 692 ulwp->ul_stop = TSTP_REGULAR;
692 693 if (flags & THR_SUSPENDED)
693 694 ulwp->ul_created = 1;
694 695
695 696 lmutex_lock(&udp->link_lock);
696 697 ulwp->ul_forw = udp->all_lwps;
697 698 ulwp->ul_back = udp->all_lwps->ul_back;
698 699 ulwp->ul_back->ul_forw = ulwp;
699 700 ulwp->ul_forw->ul_back = ulwp;
700 701 hash_in(ulwp, udp);
701 702 udp->nthreads++;
702 703 if (flags & THR_DAEMON)
703 704 udp->ndaemons++;
704 705 if (flags & THR_NEW_LWP)
705 706 thr_concurrency++;
706 707 __libc_threaded = 1; /* inform stdio */
707 708 lmutex_unlock(&udp->link_lock);
708 709
709 710 if (__td_event_report(self, TD_CREATE, udp)) {
710 711 self->ul_td_evbuf.eventnum = TD_CREATE;
711 712 self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
712 713 tdb_event(TD_CREATE, udp);
713 714 }
714 715
715 716 exit_critical(self);
716 717
717 718 if (!(flags & THR_SUSPENDED))
718 719 (void) _thrp_continue(tid, TSTP_REGULAR);
719 720
720 721 return (0);
721 722 }
722 723
723 724 int
724 725 thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
725 726 long flags, thread_t *new_thread)
726 727 {
727 728 return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0));
728 729 }
729 730
730 731 /*
731 732 * A special cancellation cleanup hook for DCE.
732 733 * cleanuphndlr, when it is not NULL, will contain a callback
733 734 * function to be called before a thread is terminated in
734 735 * thr_exit() as a result of being cancelled.
735 736 */
736 737 static void (*cleanuphndlr)(void) = NULL;
737 738
738 739 /*
739 740 * _pthread_setcleanupinit: sets the cleanup hook.
740 741 */
741 742 int
742 743 _pthread_setcleanupinit(void (*func)(void))
743 744 {
744 745 cleanuphndlr = func;
745 746 return (0);
746 747 }
747 748
748 749 void
749 750 _thrp_exit()
750 751 {
751 752 ulwp_t *self = curthread;
752 753 uberdata_t *udp = self->ul_uberdata;
753 754 ulwp_t *replace = NULL;
754 755
755 756 if (__td_event_report(self, TD_DEATH, udp)) {
756 757 self->ul_td_evbuf.eventnum = TD_DEATH;
757 758 tdb_event(TD_DEATH, udp);
758 759 }
759 760
760 761 ASSERT(self->ul_sigdefer != 0);
761 762
762 763 lmutex_lock(&udp->link_lock);
763 764 udp->nthreads--;
764 765 if (self->ul_usropts & THR_NEW_LWP)
765 766 thr_concurrency--;
766 767 if (self->ul_usropts & THR_DAEMON)
767 768 udp->ndaemons--;
768 769 else if (udp->nthreads == udp->ndaemons) {
769 770 /*
↓ open down ↓ |
735 lines elided |
↑ open up ↑ |
770 771 * We are the last non-daemon thread exiting.
771 772 * Exit the process. We retain our TSD and TLS so
772 773 * that atexit() application functions can use them.
773 774 */
774 775 lmutex_unlock(&udp->link_lock);
775 776 exit(0);
776 777 thr_panic("_thrp_exit(): exit(0) returned");
777 778 }
778 779 lmutex_unlock(&udp->link_lock);
779 780
780 - tmem_exit(); /* deallocate tmem allocations */
781 + /*
782 + * tsd_exit() may call its destructor free(), thus depending on
783 + * tmem, therefore tmem_exit() needs to be called after tsd_exit()
784 + * and tls_exit().
785 + */
781 786 tsd_exit(); /* deallocate thread-specific data */
782 787 tls_exit(); /* deallocate thread-local storage */
788 + tmem_exit(); /* deallocate tmem allocations */
783 789 heldlock_exit(); /* deal with left-over held locks */
784 790
785 791 /* block all signals to finish exiting */
786 792 block_all_signals(self);
787 793 /* also prevent ourself from being suspended */
788 794 enter_critical(self);
789 795 rwl_free(self);
790 796 lmutex_lock(&udp->link_lock);
791 797 ulwp_free(self);
792 798 (void) ulwp_lock(self, udp);
793 799
794 800 if (self->ul_mapsiz && !self->ul_detached) {
795 801 /*
796 802 * We want to free the stack for reuse but must keep
797 803 * the ulwp_t struct for the benefit of thr_join().
798 804 * For this purpose we allocate a replacement ulwp_t.
799 805 */
800 806 if ((replace = udp->ulwp_replace_free) == NULL)
801 807 replace = lmalloc(REPLACEMENT_SIZE);
802 808 else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
803 809 udp->ulwp_replace_last = NULL;
804 810 }
805 811
806 812 if (udp->all_lwps == self)
807 813 udp->all_lwps = self->ul_forw;
808 814 if (udp->all_lwps == self)
809 815 udp->all_lwps = NULL;
810 816 else {
811 817 self->ul_forw->ul_back = self->ul_back;
812 818 self->ul_back->ul_forw = self->ul_forw;
813 819 }
814 820 self->ul_forw = self->ul_back = NULL;
815 821 #if defined(THREAD_DEBUG)
816 822 /* collect queue lock statistics before marking ourself dead */
817 823 record_spin_locks(self);
818 824 #endif
819 825 self->ul_dead = 1;
820 826 self->ul_pleasestop = 0;
821 827 if (replace != NULL) {
822 828 int ix = self->ul_ix; /* the hash index */
823 829 (void) memcpy(replace, self, REPLACEMENT_SIZE);
824 830 replace->ul_self = replace;
825 831 replace->ul_next = NULL; /* clone not on stack list */
826 832 replace->ul_mapsiz = 0; /* allows clone to be freed */
827 833 replace->ul_replace = 1; /* requires clone to be freed */
828 834 hash_out_unlocked(self, ix, udp);
829 835 hash_in_unlocked(replace, ix, udp);
830 836 ASSERT(!(self->ul_detached));
831 837 self->ul_detached = 1; /* this frees the stack */
832 838 self->ul_schedctl = NULL;
833 839 self->ul_schedctl_called = &udp->uberflags;
834 840 set_curthread(self = replace);
835 841 /*
836 842 * Having just changed the address of curthread, we
837 843 * must reset the ownership of the locks we hold so
838 844 * that assertions will not fire when we release them.
839 845 */
840 846 udp->link_lock.mutex_owner = (uintptr_t)self;
841 847 ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self;
842 848 /*
843 849 * NOTE:
844 850 * On i386, %gs still references the original, not the
845 851 * replacement, ulwp structure. Fetching the replacement
846 852 * curthread pointer via %gs:0 works correctly since the
847 853 * original ulwp structure will not be reallocated until
848 854 * this lwp has completed its lwp_exit() system call (see
849 855 * dead_and_buried()), but from here on out, we must make
850 856 * no references to %gs:<offset> other than %gs:0.
851 857 */
852 858 }
853 859 /*
854 860 * Put non-detached terminated threads in the all_zombies list.
855 861 */
856 862 if (!self->ul_detached) {
857 863 udp->nzombies++;
858 864 if (udp->all_zombies == NULL) {
859 865 ASSERT(udp->nzombies == 1);
860 866 udp->all_zombies = self->ul_forw = self->ul_back = self;
861 867 } else {
862 868 self->ul_forw = udp->all_zombies;
863 869 self->ul_back = udp->all_zombies->ul_back;
864 870 self->ul_back->ul_forw = self;
865 871 self->ul_forw->ul_back = self;
866 872 }
867 873 }
868 874 /*
869 875 * Notify everyone waiting for this thread.
870 876 */
871 877 ulwp_broadcast(self);
872 878 (void) ulwp_unlock(self, udp);
873 879 /*
874 880 * Prevent any more references to the schedctl data.
875 881 * We are exiting and continue_fork() may not find us.
876 882 * Do this just before dropping link_lock, since fork
877 883 * serializes on link_lock.
878 884 */
879 885 self->ul_schedctl = NULL;
880 886 self->ul_schedctl_called = &udp->uberflags;
881 887 lmutex_unlock(&udp->link_lock);
882 888
883 889 ASSERT(self->ul_critical == 1);
884 890 ASSERT(self->ul_preempt == 0);
885 891 _lwp_terminate(); /* never returns */
886 892 thr_panic("_thrp_exit(): _lwp_terminate() returned");
887 893 }
888 894
889 895 #if defined(THREAD_DEBUG)
890 896 void
891 897 collect_queue_statistics()
892 898 {
893 899 uberdata_t *udp = curthread->ul_uberdata;
894 900 ulwp_t *ulwp;
895 901
896 902 if (thread_queue_dump) {
897 903 lmutex_lock(&udp->link_lock);
898 904 if ((ulwp = udp->all_lwps) != NULL) {
899 905 do {
900 906 record_spin_locks(ulwp);
901 907 } while ((ulwp = ulwp->ul_forw) != udp->all_lwps);
902 908 }
903 909 lmutex_unlock(&udp->link_lock);
904 910 }
905 911 }
906 912 #endif
907 913
908 914 static void __NORETURN
909 915 _thrp_exit_common(void *status, int unwind)
910 916 {
911 917 ulwp_t *self = curthread;
912 918 int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED);
913 919
914 920 ASSERT(self->ul_critical == 0 && self->ul_preempt == 0);
915 921
916 922 /*
917 923 * Disable cancellation and call the special DCE cancellation
918 924 * cleanup hook if it is enabled. Do nothing else before calling
919 925 * the DCE cancellation cleanup hook; it may call longjmp() and
920 926 * never return here.
921 927 */
922 928 self->ul_cancel_disabled = 1;
923 929 self->ul_cancel_async = 0;
924 930 self->ul_save_async = 0;
925 931 self->ul_cancelable = 0;
926 932 self->ul_cancel_pending = 0;
927 933 set_cancel_pending_flag(self, 1);
928 934 if (cancelled && cleanuphndlr != NULL)
929 935 (*cleanuphndlr)();
930 936
931 937 /*
932 938 * Block application signals while we are exiting.
933 939 * We call out to C++, TSD, and TLS destructors while exiting
934 940 * and these are application-defined, so we cannot be assured
935 941 * that they won't reset the signal mask. We use sigoff() to
936 942 * defer any signals that may be received as a result of this
937 943 * bad behavior. Such signals will be lost to the process
938 944 * when the thread finishes exiting.
939 945 */
940 946 (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
941 947 sigoff(self);
942 948
943 949 self->ul_rval = status;
944 950
945 951 /*
946 952 * If thr_exit is being called from the places where
947 953 * C++ destructors are to be called such as cancellation
948 954 * points, then set this flag. It is checked in _t_cancel()
949 955 * to decide whether _ex_unwind() is to be called or not.
950 956 */
951 957 if (unwind)
952 958 self->ul_unwind = 1;
953 959
954 960 /*
955 961 * _thrp_unwind() will eventually call _thrp_exit().
956 962 * It never returns.
957 963 */
958 964 _thrp_unwind(NULL);
959 965 thr_panic("_thrp_exit_common(): _thrp_unwind() returned");
960 966
961 967 for (;;) /* to shut the compiler up about __NORETURN */
962 968 continue;
963 969 }
964 970
965 971 /*
966 972 * Called when a thread returns from its start function.
967 973 * We are at the top of the stack; no unwinding is necessary.
968 974 */
969 975 void
970 976 _thrp_terminate(void *status)
971 977 {
972 978 _thrp_exit_common(status, 0);
973 979 }
974 980
975 981 #pragma weak pthread_exit = thr_exit
976 982 #pragma weak _thr_exit = thr_exit
977 983 void
978 984 thr_exit(void *status)
979 985 {
980 986 _thrp_exit_common(status, 1);
981 987 }
982 988
983 989 int
984 990 _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel)
985 991 {
986 992 uberdata_t *udp = curthread->ul_uberdata;
987 993 mutex_t *mp;
988 994 void *rval;
989 995 thread_t found;
990 996 ulwp_t *ulwp;
991 997 ulwp_t **ulwpp;
992 998 int replace;
993 999 int error;
994 1000
995 1001 if (do_cancel)
996 1002 error = lwp_wait(tid, &found);
997 1003 else {
998 1004 while ((error = __lwp_wait(tid, &found)) == EINTR)
999 1005 ;
1000 1006 }
1001 1007 if (error)
1002 1008 return (error);
1003 1009
1004 1010 /*
1005 1011 * We must hold link_lock to avoid a race condition with find_stack().
1006 1012 */
1007 1013 lmutex_lock(&udp->link_lock);
1008 1014 if ((ulwpp = find_lwpp(found)) == NULL) {
1009 1015 /*
1010 1016 * lwp_wait() found an lwp that the library doesn't know
1011 1017 * about. It must have been created with _lwp_create().
1012 1018 * Just return its lwpid; we can't know its status.
1013 1019 */
1014 1020 lmutex_unlock(&udp->link_lock);
1015 1021 rval = NULL;
1016 1022 } else {
1017 1023 /*
1018 1024 * Remove ulwp from the hash table.
1019 1025 */
1020 1026 ulwp = *ulwpp;
1021 1027 *ulwpp = ulwp->ul_hash;
1022 1028 ulwp->ul_hash = NULL;
1023 1029 /*
1024 1030 * Remove ulwp from all_zombies list.
1025 1031 */
1026 1032 ASSERT(udp->nzombies >= 1);
1027 1033 if (udp->all_zombies == ulwp)
1028 1034 udp->all_zombies = ulwp->ul_forw;
1029 1035 if (udp->all_zombies == ulwp)
1030 1036 udp->all_zombies = NULL;
1031 1037 else {
1032 1038 ulwp->ul_forw->ul_back = ulwp->ul_back;
1033 1039 ulwp->ul_back->ul_forw = ulwp->ul_forw;
1034 1040 }
1035 1041 ulwp->ul_forw = ulwp->ul_back = NULL;
1036 1042 udp->nzombies--;
1037 1043 ASSERT(ulwp->ul_dead && !ulwp->ul_detached &&
1038 1044 !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON)));
1039 1045 /*
1040 1046 * We can't call ulwp_unlock(ulwp) after we set
1041 1047 * ulwp->ul_ix = -1 so we have to get a pointer to the
1042 1048 * ulwp's hash table mutex now in order to unlock it below.
1043 1049 */
1044 1050 mp = ulwp_mutex(ulwp, udp);
1045 1051 ulwp->ul_lwpid = (lwpid_t)(-1);
1046 1052 ulwp->ul_ix = -1;
1047 1053 rval = ulwp->ul_rval;
1048 1054 replace = ulwp->ul_replace;
1049 1055 lmutex_unlock(mp);
1050 1056 if (replace) {
1051 1057 ulwp->ul_next = NULL;
1052 1058 if (udp->ulwp_replace_free == NULL)
1053 1059 udp->ulwp_replace_free =
1054 1060 udp->ulwp_replace_last = ulwp;
1055 1061 else {
1056 1062 udp->ulwp_replace_last->ul_next = ulwp;
1057 1063 udp->ulwp_replace_last = ulwp;
1058 1064 }
1059 1065 }
1060 1066 lmutex_unlock(&udp->link_lock);
1061 1067 }
1062 1068
1063 1069 if (departed != NULL)
1064 1070 *departed = found;
1065 1071 if (status != NULL)
1066 1072 *status = rval;
1067 1073 return (0);
1068 1074 }
1069 1075
1070 1076 int
1071 1077 thr_join(thread_t tid, thread_t *departed, void **status)
1072 1078 {
1073 1079 int error = _thrp_join(tid, departed, status, 1);
1074 1080 return ((error == EINVAL)? ESRCH : error);
1075 1081 }
1076 1082
1077 1083 /*
1078 1084 * pthread_join() differs from Solaris thr_join():
1079 1085 * It does not return the departed thread's id
1080 1086 * and hence does not have a "departed" argument.
1081 1087 * It returns EINVAL if tid refers to a detached thread.
1082 1088 */
1083 1089 #pragma weak _pthread_join = pthread_join
1084 1090 int
1085 1091 pthread_join(pthread_t tid, void **status)
1086 1092 {
1087 1093 return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
1088 1094 }
1089 1095
1090 1096 int
1091 1097 pthread_detach(pthread_t tid)
1092 1098 {
1093 1099 uberdata_t *udp = curthread->ul_uberdata;
1094 1100 ulwp_t *ulwp;
1095 1101 ulwp_t **ulwpp;
1096 1102 int error = 0;
1097 1103
1098 1104 if ((ulwpp = find_lwpp(tid)) == NULL)
1099 1105 return (ESRCH);
1100 1106 ulwp = *ulwpp;
1101 1107
1102 1108 if (ulwp->ul_dead) {
1103 1109 ulwp_unlock(ulwp, udp);
1104 1110 error = _thrp_join(tid, NULL, NULL, 0);
1105 1111 } else {
1106 1112 error = __lwp_detach(tid);
1107 1113 ulwp->ul_detached = 1;
1108 1114 ulwp->ul_usropts |= THR_DETACHED;
1109 1115 ulwp_unlock(ulwp, udp);
1110 1116 }
1111 1117 return (error);
1112 1118 }
1113 1119
1114 1120 static const char *
1115 1121 ematch(const char *ev, const char *match)
1116 1122 {
1117 1123 int c;
1118 1124
1119 1125 while ((c = *match++) != '\0') {
1120 1126 if (*ev++ != c)
1121 1127 return (NULL);
1122 1128 }
1123 1129 if (*ev++ != '=')
1124 1130 return (NULL);
1125 1131 return (ev);
1126 1132 }
1127 1133
1128 1134 static int
1129 1135 envvar(const char *ev, const char *match, int limit)
1130 1136 {
1131 1137 int val = -1;
1132 1138 const char *ename;
1133 1139
1134 1140 if ((ename = ematch(ev, match)) != NULL) {
1135 1141 int c;
1136 1142 for (val = 0; (c = *ename) != '\0'; ename++) {
1137 1143 if (!isdigit(c)) {
1138 1144 val = -1;
1139 1145 break;
1140 1146 }
1141 1147 val = val * 10 + (c - '0');
1142 1148 if (val > limit) {
1143 1149 val = limit;
1144 1150 break;
1145 1151 }
1146 1152 }
1147 1153 }
1148 1154 return (val);
1149 1155 }
1150 1156
1151 1157 static void
1152 1158 etest(const char *ev)
1153 1159 {
1154 1160 int value;
1155 1161
1156 1162 if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0)
1157 1163 thread_queue_spin = value;
1158 1164 if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0)
1159 1165 thread_adaptive_spin = value;
1160 1166 if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0)
1161 1167 thread_max_spinners = value;
1162 1168 if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0)
1163 1169 thread_queue_fifo = value;
1164 1170 #if defined(THREAD_DEBUG)
1165 1171 if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0)
1166 1172 thread_queue_verify = value;
1167 1173 if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0)
1168 1174 thread_queue_dump = value;
1169 1175 #endif
1170 1176 if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0)
1171 1177 thread_stack_cache = value;
1172 1178 if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0)
1173 1179 thread_cond_wait_defer = value;
1174 1180 if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0)
1175 1181 thread_error_detection = value;
1176 1182 if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0)
1177 1183 thread_async_safe = value;
1178 1184 if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0)
1179 1185 thread_door_noreserve = value;
1180 1186 if ((value = envvar(ev, "LOCKS_MISALIGNED", 1)) >= 0)
1181 1187 thread_locks_misaligned = value;
1182 1188 }
1183 1189
1184 1190 /*
1185 1191 * Look for and evaluate environment variables of the form "_THREAD_*".
1186 1192 * For compatibility with the past, we also look for environment
1187 1193 * names of the form "LIBTHREAD_*".
1188 1194 */
1189 1195 static void
1190 1196 set_thread_vars()
1191 1197 {
1192 1198 extern const char **_environ;
1193 1199 const char **pev;
1194 1200 const char *ev;
1195 1201 char c;
1196 1202
1197 1203 if ((pev = _environ) == NULL)
1198 1204 return;
1199 1205 while ((ev = *pev++) != NULL) {
1200 1206 c = *ev;
1201 1207 if (c == '_' && strncmp(ev, "_THREAD_", 8) == 0)
1202 1208 etest(ev + 8);
1203 1209 if (c == 'L' && strncmp(ev, "LIBTHREAD_", 10) == 0)
1204 1210 etest(ev + 10);
1205 1211 }
1206 1212 }
1207 1213
1208 1214 /* PROBE_SUPPORT begin */
1209 1215 #pragma weak __tnf_probe_notify
1210 1216 extern void __tnf_probe_notify(void);
1211 1217 /* PROBE_SUPPORT end */
1212 1218
1213 1219 /* same as atexit() but private to the library */
1214 1220 extern int _atexit(void (*)(void));
1215 1221
1216 1222 /* same as _cleanup() but private to the library */
1217 1223 extern void __cleanup(void);
1218 1224
1219 1225 extern void atfork_init(void);
1220 1226
1221 1227 #ifdef __amd64
1222 1228 extern void __proc64id(void);
1223 1229 #endif
1224 1230
1225 1231 static void
1226 1232 init_auxv_data(uberdata_t *udp)
1227 1233 {
1228 1234 Dl_argsinfo_t args;
1229 1235
1230 1236 udp->ub_comm_page = NULL;
1231 1237 if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0)
1232 1238 return;
1233 1239
1234 1240 while (args.dla_auxv->a_type != AT_NULL) {
1235 1241 if (args.dla_auxv->a_type == AT_SUN_COMMPAGE) {
1236 1242 udp->ub_comm_page = args.dla_auxv->a_un.a_ptr;
1237 1243 }
1238 1244 args.dla_auxv++;
1239 1245 }
1240 1246 }
1241 1247
1242 1248 /*
1243 1249 * libc_init() is called by ld.so.1 for library initialization.
1244 1250 * We perform minimal initialization; enough to work with the main thread.
1245 1251 */
1246 1252 void
1247 1253 libc_init(void)
1248 1254 {
1249 1255 uberdata_t *udp = &__uberdata;
1250 1256 ulwp_t *oldself = __curthread();
1251 1257 ucontext_t uc;
1252 1258 ulwp_t *self;
1253 1259 struct rlimit rl;
1254 1260 caddr_t data;
1255 1261 size_t tls_size;
1256 1262 int setmask;
1257 1263
1258 1264 /*
1259 1265 * For the initial stage of initialization, we must be careful
1260 1266 * not to call any function that could possibly call _cerror().
1261 1267 * For this purpose, we call only the raw system call wrappers.
1262 1268 */
1263 1269
1264 1270 #ifdef __amd64
1265 1271 /*
1266 1272 * Gather information about cache layouts for optimized
1267 1273 * AMD and Intel assembler strfoo() and memfoo() functions.
1268 1274 */
1269 1275 __proc64id();
1270 1276 #endif
1271 1277
1272 1278 /*
1273 1279 * Every libc, regardless of which link map, must register __cleanup().
1274 1280 */
1275 1281 (void) _atexit(__cleanup);
1276 1282
1277 1283 /*
1278 1284 * Every libc, regardless of link map, needs to go through and check
1279 1285 * its aux vectors. Doing so will indicate whether or not this has
1280 1286 * been given a comm page (to optimize certain system actions).
1281 1287 */
1282 1288 init_auxv_data(udp);
1283 1289
1284 1290 /*
1285 1291 * We keep our uberdata on one of (a) the first alternate link map
1286 1292 * or (b) the primary link map. We switch to the primary link map
1287 1293 * and stay there once we see it. All intermediate link maps are
1288 1294 * subject to being unloaded at any time.
1289 1295 */
1290 1296 if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) {
1291 1297 __tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap;
1292 1298 mutex_setup();
1293 1299 atfork_init(); /* every link map needs atfork() processing */
1294 1300 init_progname();
1295 1301 return;
1296 1302 }
1297 1303
1298 1304 /*
1299 1305 * To establish the main stack information, we have to get our context.
1300 1306 * This is also convenient to use for getting our signal mask.
1301 1307 */
1302 1308 uc.uc_flags = UC_ALL;
1303 1309 (void) __getcontext(&uc);
1304 1310 ASSERT(uc.uc_link == NULL);
1305 1311
1306 1312 tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
1307 1313 ASSERT(primary_link_map || tls_size == 0);
1308 1314 data = lmalloc(sizeof (ulwp_t) + tls_size);
1309 1315 if (data == NULL)
1310 1316 thr_panic("cannot allocate thread structure for main thread");
1311 1317 /* LINTED pointer cast may result in improper alignment */
1312 1318 self = (ulwp_t *)(data + tls_size);
1313 1319 init_hash_table[0].hash_bucket = self;
1314 1320
1315 1321 self->ul_sigmask = uc.uc_sigmask;
1316 1322 delete_reserved_signals(&self->ul_sigmask);
1317 1323 /*
1318 1324 * Are the old and new sets different?
1319 1325 * (This can happen if we are currently blocking SIGCANCEL.)
1320 1326 * If so, we must explicitly set our signal mask, below.
1321 1327 */
1322 1328 setmask =
1323 1329 ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) |
1324 1330 (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]) |
1325 1331 (self->ul_sigmask.__sigbits[2] ^ uc.uc_sigmask.__sigbits[2]) |
1326 1332 (self->ul_sigmask.__sigbits[3] ^ uc.uc_sigmask.__sigbits[3]));
1327 1333
1328 1334 #ifdef __sparc
1329 1335 /*
1330 1336 * We cache several instructions in the thread structure for use
1331 1337 * by the fasttrap DTrace provider. When changing this, read the
1332 1338 * comment in fasttrap.h for the all the other places that must
1333 1339 * be changed.
1334 1340 */
1335 1341 self->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */
1336 1342 self->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */
1337 1343 self->ul_dftret = 0x91d0203a; /* ta 0x3a */
1338 1344 self->ul_dreturn = 0x81ca0000; /* return %o0 */
1339 1345 #endif
1340 1346
1341 1347 self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size;
1342 1348 (void) getrlimit(RLIMIT_STACK, &rl);
1343 1349 self->ul_stksiz = rl.rlim_cur;
1344 1350 self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz);
1345 1351
1346 1352 self->ul_forw = self->ul_back = self;
1347 1353 self->ul_hash = NULL;
1348 1354 self->ul_ix = 0;
1349 1355 self->ul_lwpid = 1; /* _lwp_self() */
1350 1356 self->ul_main = 1;
1351 1357 self->ul_self = self;
1352 1358 self->ul_policy = -1; /* initialize only when needed */
1353 1359 self->ul_pri = 0;
1354 1360 self->ul_cid = 0;
1355 1361 self->ul_rtclassid = -1;
1356 1362 self->ul_uberdata = udp;
1357 1363 if (oldself != NULL) {
1358 1364 int i;
1359 1365
1360 1366 ASSERT(primary_link_map);
1361 1367 ASSERT(oldself->ul_main == 1);
1362 1368 self->ul_stsd = oldself->ul_stsd;
1363 1369 for (i = 0; i < TSD_NFAST; i++)
1364 1370 self->ul_ftsd[i] = oldself->ul_ftsd[i];
1365 1371 self->ul_tls = oldself->ul_tls;
1366 1372 /*
1367 1373 * Retrieve all pointers to uberdata allocated
1368 1374 * while running on previous link maps.
1369 1375 * We would like to do a structure assignment here, but
1370 1376 * gcc turns structure assignments into calls to memcpy(),
1371 1377 * a function exported from libc. We can't call any such
1372 1378 * external functions until we establish curthread, below,
1373 1379 * so we just call our private version of memcpy().
1374 1380 */
1375 1381 (void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp));
1376 1382 /*
1377 1383 * These items point to global data on the primary link map.
1378 1384 */
1379 1385 udp->thr_hash_table = init_hash_table;
1380 1386 udp->sigacthandler = sigacthandler;
1381 1387 udp->tdb.tdb_events = tdb_events;
1382 1388 ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt);
1383 1389 ASSERT(udp->lwp_stacks == NULL);
1384 1390 ASSERT(udp->ulwp_freelist == NULL);
1385 1391 ASSERT(udp->ulwp_replace_free == NULL);
1386 1392 ASSERT(udp->hash_size == 1);
1387 1393 }
1388 1394 udp->all_lwps = self;
1389 1395 udp->ulwp_one = self;
1390 1396 udp->pid = getpid();
1391 1397 udp->nthreads = 1;
1392 1398 /*
1393 1399 * In every link map, tdb_bootstrap points to the same piece of
1394 1400 * allocated memory. When the primary link map is initialized,
1395 1401 * the allocated memory is assigned a pointer to the one true
1396 1402 * uberdata. This allows libc_db to initialize itself regardless
1397 1403 * of which instance of libc it finds in the address space.
1398 1404 */
1399 1405 if (udp->tdb_bootstrap == NULL)
1400 1406 udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *));
1401 1407 __tdb_bootstrap = udp->tdb_bootstrap;
1402 1408 if (primary_link_map) {
1403 1409 self->ul_primarymap = 1;
1404 1410 udp->primary_map = 1;
1405 1411 *udp->tdb_bootstrap = udp;
1406 1412 }
1407 1413 /*
1408 1414 * Cancellation can't happen until:
1409 1415 * pthread_cancel() is called
1410 1416 * or:
1411 1417 * another thread is created
1412 1418 * For now, as a single-threaded process, set the flag that tells
1413 1419 * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen.
1414 1420 */
1415 1421 self->ul_nocancel = 1;
1416 1422
1417 1423 #if defined(__amd64)
1418 1424 (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self);
1419 1425 #elif defined(__i386)
1420 1426 (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self);
1421 1427 #endif /* __i386 || __amd64 */
1422 1428 set_curthread(self); /* redundant on i386 */
1423 1429 /*
1424 1430 * Now curthread is established and it is safe to call any
1425 1431 * function in libc except one that uses thread-local storage.
1426 1432 */
1427 1433 self->ul_errnop = &errno;
1428 1434 if (oldself != NULL) {
1429 1435 /* tls_size was zero when oldself was allocated */
1430 1436 lfree(oldself, sizeof (ulwp_t));
1431 1437 }
1432 1438 mutex_setup();
1433 1439 atfork_init();
1434 1440 signal_init();
1435 1441
1436 1442 /*
1437 1443 * If the stack is unlimited, we set the size to zero to disable
1438 1444 * stack checking.
1439 1445 * XXX: Work harder here. Get the stack size from /proc/self/rmap
1440 1446 */
1441 1447 if (self->ul_stksiz == RLIM_INFINITY) {
1442 1448 self->ul_ustack.ss_sp = (void *)self->ul_stktop;
1443 1449 self->ul_ustack.ss_size = 0;
1444 1450 } else {
1445 1451 self->ul_ustack.ss_sp = self->ul_stk;
1446 1452 self->ul_ustack.ss_size = self->ul_stksiz;
1447 1453 }
1448 1454 self->ul_ustack.ss_flags = 0;
1449 1455 (void) setustack(&self->ul_ustack);
1450 1456
1451 1457 /*
1452 1458 * Get the variables that affect thread behavior from the environment.
1453 1459 */
1454 1460 set_thread_vars();
1455 1461 udp->uberflags.uf_thread_error_detection = (char)thread_error_detection;
1456 1462 udp->thread_stack_cache = thread_stack_cache;
1457 1463
1458 1464 /*
1459 1465 * Make per-thread copies of global variables, for speed.
1460 1466 */
1461 1467 self->ul_queue_fifo = (char)thread_queue_fifo;
1462 1468 self->ul_cond_wait_defer = (char)thread_cond_wait_defer;
1463 1469 self->ul_error_detection = (char)thread_error_detection;
1464 1470 self->ul_async_safe = (char)thread_async_safe;
1465 1471 self->ul_door_noreserve = (char)thread_door_noreserve;
1466 1472 self->ul_misaligned = (char)thread_locks_misaligned;
1467 1473 self->ul_max_spinners = (uint8_t)thread_max_spinners;
1468 1474 self->ul_adaptive_spin = thread_adaptive_spin;
1469 1475 self->ul_queue_spin = thread_queue_spin;
1470 1476
1471 1477 #if defined(__sparc) && !defined(_LP64)
1472 1478 if (self->ul_misaligned) {
1473 1479 /*
1474 1480 * Tell the kernel to fix up ldx/stx instructions that
1475 1481 * refer to non-8-byte aligned data instead of giving
1476 1482 * the process an alignment trap and generating SIGBUS.
1477 1483 *
1478 1484 * Programs compiled for 32-bit sparc with the Studio SS12
1479 1485 * compiler get this done for them automatically (in _init()).
1480 1486 * We do it here for the benefit of programs compiled with
1481 1487 * other compilers, like gcc.
1482 1488 *
1483 1489 * This is necessary for the _THREAD_LOCKS_MISALIGNED=1
1484 1490 * environment variable horrible hack to work.
1485 1491 */
1486 1492 extern void _do_fix_align(void);
1487 1493 _do_fix_align();
1488 1494 }
1489 1495 #endif
1490 1496
1491 1497 /*
1492 1498 * When we have initialized the primary link map, inform
1493 1499 * the dynamic linker about our interface functions.
1494 1500 * Set up our pointer to the program name.
1495 1501 */
1496 1502 if (self->ul_primarymap)
1497 1503 _ld_libc((void *)rtld_funcs);
1498 1504 init_progname();
1499 1505
1500 1506 /*
1501 1507 * Defer signals until TLS constructors have been called.
1502 1508 */
1503 1509 sigoff(self);
1504 1510 tls_setup();
1505 1511 sigon(self);
1506 1512 if (setmask)
1507 1513 (void) restore_signals(self);
1508 1514
1509 1515 /*
1510 1516 * Make private copies of __xpg4 and __xpg6 so libc can test
1511 1517 * them after this point without invoking the dynamic linker.
1512 1518 */
1513 1519 libc__xpg4 = __xpg4;
1514 1520 libc__xpg6 = __xpg6;
1515 1521
1516 1522 /* PROBE_SUPPORT begin */
1517 1523 if (self->ul_primarymap && __tnf_probe_notify != NULL)
1518 1524 __tnf_probe_notify();
1519 1525 /* PROBE_SUPPORT end */
1520 1526
1521 1527 init_sigev_thread();
1522 1528 init_aio();
1523 1529
1524 1530 /*
1525 1531 * We need to reset __threaded dynamically at runtime, so that
1526 1532 * __threaded can be bound to __threaded outside libc which may not
1527 1533 * have initial value of 1 (without a copy relocation in a.out).
1528 1534 */
1529 1535 __threaded = 1;
1530 1536 }
1531 1537
1532 1538 #pragma fini(libc_fini)
1533 1539 void
1534 1540 libc_fini()
1535 1541 {
1536 1542 /*
1537 1543 * If we are doing fini processing for the instance of libc
1538 1544 * on the first alternate link map (this happens only when
1539 1545 * the dynamic linker rejects a bad audit library), then clear
1540 1546 * __curthread(). We abandon whatever memory was allocated by
1541 1547 * lmalloc() while running on this alternate link-map but we
1542 1548 * don't care (and can't find the memory in any case); we just
1543 1549 * want to protect the application from this bad audit library.
1544 1550 * No fini processing is done by libc in the normal case.
1545 1551 */
1546 1552
1547 1553 uberdata_t *udp = curthread->ul_uberdata;
1548 1554
1549 1555 if (udp->primary_map == 0 && udp == &__uberdata)
1550 1556 set_curthread(NULL);
1551 1557 }
1552 1558
1553 1559 /*
1554 1560 * finish_init is called when we are about to become multi-threaded,
1555 1561 * that is, on the first call to thr_create().
1556 1562 */
1557 1563 void
1558 1564 finish_init()
1559 1565 {
1560 1566 ulwp_t *self = curthread;
1561 1567 uberdata_t *udp = self->ul_uberdata;
1562 1568 thr_hash_table_t *htp;
1563 1569 void *data;
1564 1570 int i;
1565 1571
1566 1572 /*
1567 1573 * No locks needed here; we are single-threaded on the first call.
1568 1574 * We can be called only after the primary link map has been set up.
1569 1575 */
1570 1576 ASSERT(self->ul_primarymap);
1571 1577 ASSERT(self == udp->ulwp_one);
1572 1578 ASSERT(!udp->uberflags.uf_mt);
1573 1579 ASSERT(udp->hash_size == 1);
1574 1580
1575 1581 /*
1576 1582 * Initialize self->ul_policy, self->ul_cid, and self->ul_pri.
1577 1583 */
1578 1584 update_sched(self);
1579 1585
1580 1586 /*
1581 1587 * Allocate the queue_head array if not already allocated.
1582 1588 */
1583 1589 if (udp->queue_head == NULL)
1584 1590 queue_alloc();
1585 1591
1586 1592 /*
1587 1593 * Now allocate the thread hash table.
1588 1594 */
1589 1595 if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t),
1590 1596 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0))
1591 1597 == MAP_FAILED)
1592 1598 thr_panic("cannot allocate thread hash table");
1593 1599
1594 1600 udp->thr_hash_table = htp = (thr_hash_table_t *)data;
1595 1601 udp->hash_size = HASHTBLSZ;
1596 1602 udp->hash_mask = HASHTBLSZ - 1;
1597 1603
1598 1604 for (i = 0; i < HASHTBLSZ; i++, htp++) {
1599 1605 htp->hash_lock.mutex_flag = LOCK_INITED;
1600 1606 htp->hash_lock.mutex_magic = MUTEX_MAGIC;
1601 1607 htp->hash_cond.cond_magic = COND_MAGIC;
1602 1608 }
1603 1609 hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
1604 1610
1605 1611 /*
1606 1612 * Set up the SIGCANCEL handler for threads cancellation.
1607 1613 */
1608 1614 setup_cancelsig(SIGCANCEL);
1609 1615
1610 1616 /*
1611 1617 * Arrange to do special things on exit --
1612 1618 * - collect queue statistics from all remaining active threads.
1613 1619 * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set.
1614 1620 * - grab assert_lock to ensure that assertion failures
1615 1621 * and a core dump take precedence over _exit().
1616 1622 * (Functions are called in the reverse order of their registration.)
1617 1623 */
1618 1624 (void) _atexit(grab_assert_lock);
1619 1625 #if defined(THREAD_DEBUG)
1620 1626 (void) _atexit(dump_queue_statistics);
1621 1627 (void) _atexit(collect_queue_statistics);
1622 1628 #endif
1623 1629 }
1624 1630
1625 1631 /*
1626 1632 * Used only by postfork1_child(), below.
1627 1633 */
1628 1634 static void
1629 1635 mark_dead_and_buried(ulwp_t *ulwp)
1630 1636 {
1631 1637 ulwp->ul_dead = 1;
1632 1638 ulwp->ul_lwpid = (lwpid_t)(-1);
1633 1639 ulwp->ul_hash = NULL;
1634 1640 ulwp->ul_ix = -1;
1635 1641 ulwp->ul_schedctl = NULL;
1636 1642 ulwp->ul_schedctl_called = NULL;
1637 1643 }
1638 1644
1639 1645 /*
1640 1646 * This is called from fork1() in the child.
1641 1647 * Reset our data structures to reflect one lwp.
1642 1648 */
1643 1649 void
1644 1650 postfork1_child()
1645 1651 {
1646 1652 ulwp_t *self = curthread;
1647 1653 uberdata_t *udp = self->ul_uberdata;
1648 1654 queue_head_t *qp;
1649 1655 ulwp_t *next;
1650 1656 ulwp_t *ulwp;
1651 1657 int i;
1652 1658
1653 1659 /* daemon threads shouldn't call fork1(), but oh well... */
1654 1660 self->ul_usropts &= ~THR_DAEMON;
1655 1661 udp->nthreads = 1;
1656 1662 udp->ndaemons = 0;
1657 1663 udp->uberflags.uf_mt = 0;
1658 1664 __libc_threaded = 0;
1659 1665 for (i = 0; i < udp->hash_size; i++)
1660 1666 udp->thr_hash_table[i].hash_bucket = NULL;
1661 1667 self->ul_lwpid = _lwp_self();
1662 1668 hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
1663 1669
1664 1670 /*
1665 1671 * Some thread in the parent might have been suspended
1666 1672 * while holding udp->callout_lock or udp->ld_lock.
1667 1673 * Reinitialize the child's copies.
1668 1674 */
1669 1675 (void) mutex_init(&udp->callout_lock,
1670 1676 USYNC_THREAD | LOCK_RECURSIVE, NULL);
1671 1677 (void) mutex_init(&udp->ld_lock,
1672 1678 USYNC_THREAD | LOCK_RECURSIVE, NULL);
1673 1679
1674 1680 /* no one in the child is on a sleep queue; reinitialize */
1675 1681 if ((qp = udp->queue_head) != NULL) {
1676 1682 (void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t));
1677 1683 for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
1678 1684 qp->qh_type = (i < QHASHSIZE)? MX : CV;
1679 1685 qp->qh_lock.mutex_flag = LOCK_INITED;
1680 1686 qp->qh_lock.mutex_magic = MUTEX_MAGIC;
1681 1687 qp->qh_hlist = &qp->qh_def_root;
1682 1688 #if defined(THREAD_DEBUG)
1683 1689 qp->qh_hlen = 1;
1684 1690 qp->qh_hmax = 1;
1685 1691 #endif
1686 1692 }
1687 1693 }
1688 1694
1689 1695 /*
1690 1696 * Do post-fork1 processing for subsystems that need it.
1691 1697 * We need to do this before unmapping all of the abandoned
1692 1698 * threads' stacks, below(), because the post-fork1 actions
1693 1699 * might require access to those stacks.
1694 1700 */
1695 1701 postfork1_child_sigev_aio();
1696 1702 postfork1_child_sigev_mq();
1697 1703 postfork1_child_sigev_timer();
1698 1704 postfork1_child_aio();
1699 1705 /*
1700 1706 * The above subsystems use thread pools, so this action
1701 1707 * must be performed after those actions.
1702 1708 */
1703 1709 postfork1_child_tpool();
1704 1710
1705 1711 /*
1706 1712 * All lwps except ourself are gone. Mark them so.
1707 1713 * First mark all of the lwps that have already been freed.
1708 1714 * Then mark and free all of the active lwps except ourself.
1709 1715 * Since we are single-threaded, no locks are required here.
1710 1716 */
1711 1717 for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next)
1712 1718 mark_dead_and_buried(ulwp);
1713 1719 for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next)
1714 1720 mark_dead_and_buried(ulwp);
1715 1721 for (ulwp = self->ul_forw; ulwp != self; ulwp = next) {
1716 1722 next = ulwp->ul_forw;
1717 1723 ulwp->ul_forw = ulwp->ul_back = NULL;
1718 1724 mark_dead_and_buried(ulwp);
1719 1725 tsd_free(ulwp);
1720 1726 tls_free(ulwp);
1721 1727 rwl_free(ulwp);
1722 1728 heldlock_free(ulwp);
1723 1729 ulwp_free(ulwp);
1724 1730 }
1725 1731 self->ul_forw = self->ul_back = udp->all_lwps = self;
1726 1732 if (self != udp->ulwp_one)
1727 1733 mark_dead_and_buried(udp->ulwp_one);
1728 1734 if ((ulwp = udp->all_zombies) != NULL) {
1729 1735 ASSERT(udp->nzombies != 0);
1730 1736 do {
1731 1737 next = ulwp->ul_forw;
1732 1738 ulwp->ul_forw = ulwp->ul_back = NULL;
1733 1739 mark_dead_and_buried(ulwp);
1734 1740 udp->nzombies--;
1735 1741 if (ulwp->ul_replace) {
1736 1742 ulwp->ul_next = NULL;
1737 1743 if (udp->ulwp_replace_free == NULL) {
1738 1744 udp->ulwp_replace_free =
1739 1745 udp->ulwp_replace_last = ulwp;
1740 1746 } else {
1741 1747 udp->ulwp_replace_last->ul_next = ulwp;
1742 1748 udp->ulwp_replace_last = ulwp;
1743 1749 }
1744 1750 }
1745 1751 } while ((ulwp = next) != udp->all_zombies);
1746 1752 ASSERT(udp->nzombies == 0);
1747 1753 udp->all_zombies = NULL;
1748 1754 udp->nzombies = 0;
1749 1755 }
1750 1756 trim_stack_cache(0);
1751 1757 }
1752 1758
1753 1759 lwpid_t
1754 1760 lwp_self(void)
1755 1761 {
1756 1762 return (curthread->ul_lwpid);
1757 1763 }
1758 1764
1759 1765 #pragma weak _ti_thr_self = thr_self
1760 1766 #pragma weak pthread_self = thr_self
1761 1767 thread_t
1762 1768 thr_self()
1763 1769 {
1764 1770 return (curthread->ul_lwpid);
1765 1771 }
1766 1772
1767 1773 int
1768 1774 thr_main()
1769 1775 {
1770 1776 ulwp_t *self = __curthread();
1771 1777
1772 1778 return ((self == NULL)? -1 : self->ul_main);
1773 1779 }
1774 1780
1775 1781 int
1776 1782 _thrp_cancelled(void)
1777 1783 {
1778 1784 return (curthread->ul_rval == PTHREAD_CANCELED);
1779 1785 }
1780 1786
1781 1787 int
1782 1788 _thrp_stksegment(ulwp_t *ulwp, stack_t *stk)
1783 1789 {
1784 1790 stk->ss_sp = (void *)ulwp->ul_stktop;
1785 1791 stk->ss_size = ulwp->ul_stksiz;
1786 1792 stk->ss_flags = 0;
1787 1793 return (0);
1788 1794 }
1789 1795
1790 1796 #pragma weak _thr_stksegment = thr_stksegment
1791 1797 int
1792 1798 thr_stksegment(stack_t *stk)
1793 1799 {
1794 1800 return (_thrp_stksegment(curthread, stk));
1795 1801 }
1796 1802
1797 1803 void
1798 1804 force_continue(ulwp_t *ulwp)
1799 1805 {
1800 1806 #if defined(THREAD_DEBUG)
1801 1807 ulwp_t *self = curthread;
1802 1808 uberdata_t *udp = self->ul_uberdata;
1803 1809 #endif
1804 1810 int error;
1805 1811 timespec_t ts;
1806 1812
1807 1813 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
1808 1814 ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
1809 1815
1810 1816 for (;;) {
1811 1817 error = _lwp_continue(ulwp->ul_lwpid);
1812 1818 if (error != 0 && error != EINTR)
1813 1819 break;
1814 1820 error = 0;
1815 1821 if (ulwp->ul_stopping) { /* it is stopping itsself */
1816 1822 ts.tv_sec = 0; /* give it a chance to run */
1817 1823 ts.tv_nsec = 100000; /* 100 usecs or clock tick */
1818 1824 (void) __nanosleep(&ts, NULL);
1819 1825 }
1820 1826 if (!ulwp->ul_stopping) /* it is running now */
1821 1827 break; /* so we are done */
1822 1828 /*
1823 1829 * It is marked as being in the process of stopping
1824 1830 * itself. Loop around and continue it again.
1825 1831 * It may not have been stopped the first time.
1826 1832 */
1827 1833 }
1828 1834 }
1829 1835
1830 1836 /*
1831 1837 * Suspend an lwp with lwp_suspend(), then move it to a safe point,
1832 1838 * that is, to a point where ul_critical and ul_rtld are both zero.
1833 1839 * On return, the ulwp_lock() is dropped as with ulwp_unlock().
1834 1840 * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry.
1835 1841 * If we have to drop link_lock, we store 1 through link_dropped.
1836 1842 * If the lwp exits before it can be suspended, we return ESRCH.
1837 1843 */
1838 1844 int
1839 1845 safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped)
1840 1846 {
1841 1847 ulwp_t *self = curthread;
1842 1848 uberdata_t *udp = self->ul_uberdata;
1843 1849 cond_t *cvp = ulwp_condvar(ulwp, udp);
1844 1850 mutex_t *mp = ulwp_mutex(ulwp, udp);
1845 1851 thread_t tid = ulwp->ul_lwpid;
1846 1852 int ix = ulwp->ul_ix;
1847 1853 int error = 0;
1848 1854
1849 1855 ASSERT(whystopped == TSTP_REGULAR ||
1850 1856 whystopped == TSTP_MUTATOR ||
1851 1857 whystopped == TSTP_FORK);
1852 1858 ASSERT(ulwp != self);
1853 1859 ASSERT(!ulwp->ul_stop);
1854 1860 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
1855 1861 ASSERT(MUTEX_OWNED(mp, self));
1856 1862
1857 1863 if (link_dropped != NULL)
1858 1864 *link_dropped = 0;
1859 1865
1860 1866 /*
1861 1867 * We must grab the target's spin lock before suspending it.
1862 1868 * See the comments below and in _thrp_suspend() for why.
1863 1869 */
1864 1870 spin_lock_set(&ulwp->ul_spinlock);
1865 1871 (void) ___lwp_suspend(tid);
1866 1872 spin_lock_clear(&ulwp->ul_spinlock);
1867 1873
1868 1874 top:
1869 1875 if ((ulwp->ul_critical == 0 && ulwp->ul_rtld == 0) ||
1870 1876 ulwp->ul_stopping) {
1871 1877 /* thread is already safe */
1872 1878 ulwp->ul_stop |= whystopped;
1873 1879 } else {
1874 1880 /*
1875 1881 * Setting ul_pleasestop causes the target thread to stop
1876 1882 * itself in _thrp_suspend(), below, after we drop its lock.
1877 1883 * We must continue the critical thread before dropping
1878 1884 * link_lock because the critical thread may be holding
1879 1885 * the queue lock for link_lock. This is delicate.
1880 1886 */
1881 1887 ulwp->ul_pleasestop |= whystopped;
1882 1888 force_continue(ulwp);
1883 1889 if (link_dropped != NULL) {
1884 1890 *link_dropped = 1;
1885 1891 lmutex_unlock(&udp->link_lock);
1886 1892 /* be sure to drop link_lock only once */
1887 1893 link_dropped = NULL;
1888 1894 }
1889 1895
1890 1896 /*
1891 1897 * The thread may disappear by calling thr_exit() so we
1892 1898 * cannot rely on the ulwp pointer after dropping the lock.
1893 1899 * Instead, we search the hash table to find it again.
1894 1900 * When we return, we may find that the thread has been
1895 1901 * continued by some other thread. The suspend/continue
1896 1902 * interfaces are prone to such race conditions by design.
1897 1903 */
1898 1904 while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop &&
1899 1905 (ulwp->ul_pleasestop & whystopped)) {
1900 1906 (void) __cond_wait(cvp, mp);
1901 1907 for (ulwp = udp->thr_hash_table[ix].hash_bucket;
1902 1908 ulwp != NULL; ulwp = ulwp->ul_hash) {
1903 1909 if (ulwp->ul_lwpid == tid)
1904 1910 break;
1905 1911 }
1906 1912 }
1907 1913
1908 1914 if (ulwp == NULL || ulwp->ul_dead)
1909 1915 error = ESRCH;
1910 1916 else {
1911 1917 /*
1912 1918 * Do another lwp_suspend() to make sure we don't
1913 1919 * return until the target thread is fully stopped
1914 1920 * in the kernel. Don't apply lwp_suspend() until
1915 1921 * we know that the target is not holding any
1916 1922 * queue locks, that is, that it has completed
1917 1923 * ulwp_unlock(self) and has, or at least is
1918 1924 * about to, call lwp_suspend() on itself. We do
1919 1925 * this by grabbing the target's spin lock.
1920 1926 */
1921 1927 ASSERT(ulwp->ul_lwpid == tid);
1922 1928 spin_lock_set(&ulwp->ul_spinlock);
1923 1929 (void) ___lwp_suspend(tid);
1924 1930 spin_lock_clear(&ulwp->ul_spinlock);
1925 1931 /*
1926 1932 * If some other thread did a thr_continue()
1927 1933 * on the target thread we have to start over.
1928 1934 */
1929 1935 if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped))
1930 1936 goto top;
1931 1937 }
1932 1938 }
1933 1939
1934 1940 (void) cond_broadcast(cvp);
1935 1941 lmutex_unlock(mp);
1936 1942 return (error);
1937 1943 }
1938 1944
1939 1945 int
1940 1946 _thrp_suspend(thread_t tid, uchar_t whystopped)
1941 1947 {
1942 1948 ulwp_t *self = curthread;
1943 1949 uberdata_t *udp = self->ul_uberdata;
1944 1950 ulwp_t *ulwp;
1945 1951 int error = 0;
1946 1952
1947 1953 ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0);
1948 1954 ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0);
1949 1955
1950 1956 /*
1951 1957 * We can't suspend anyone except ourself while
1952 1958 * some other thread is performing a fork.
1953 1959 * This also allows only one suspension at a time.
1954 1960 */
1955 1961 if (tid != self->ul_lwpid)
1956 1962 fork_lock_enter();
1957 1963
1958 1964 if ((ulwp = find_lwp(tid)) == NULL)
1959 1965 error = ESRCH;
1960 1966 else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) {
1961 1967 ulwp_unlock(ulwp, udp);
1962 1968 error = EINVAL;
1963 1969 } else if (ulwp->ul_stop) { /* already stopped */
1964 1970 ulwp->ul_stop |= whystopped;
1965 1971 ulwp_broadcast(ulwp);
1966 1972 ulwp_unlock(ulwp, udp);
1967 1973 } else if (ulwp != self) {
1968 1974 /*
1969 1975 * After suspending the other thread, move it out of a
1970 1976 * critical section and deal with the schedctl mappings.
1971 1977 * safe_suspend() suspends the other thread, calls
1972 1978 * ulwp_broadcast(ulwp) and drops the ulwp lock.
1973 1979 */
1974 1980 error = safe_suspend(ulwp, whystopped, NULL);
1975 1981 } else {
1976 1982 int schedctl_after_fork = 0;
1977 1983
1978 1984 /*
1979 1985 * We are suspending ourself. We must not take a signal
1980 1986 * until we return from lwp_suspend() and clear ul_stopping.
1981 1987 * This is to guard against siglongjmp().
1982 1988 */
1983 1989 enter_critical(self);
1984 1990 self->ul_sp = stkptr();
1985 1991 _flush_windows(); /* sparc */
1986 1992 self->ul_pleasestop = 0;
1987 1993 self->ul_stop |= whystopped;
1988 1994 /*
1989 1995 * Grab our spin lock before dropping ulwp_mutex(self).
1990 1996 * This prevents the suspending thread from applying
1991 1997 * lwp_suspend() to us before we emerge from
1992 1998 * lmutex_unlock(mp) and have dropped mp's queue lock.
1993 1999 */
1994 2000 spin_lock_set(&self->ul_spinlock);
1995 2001 self->ul_stopping = 1;
1996 2002 ulwp_broadcast(self);
1997 2003 ulwp_unlock(self, udp);
1998 2004 /*
1999 2005 * From this point until we return from lwp_suspend(),
2000 2006 * we must not call any function that might invoke the
2001 2007 * dynamic linker, that is, we can only call functions
2002 2008 * private to the library.
2003 2009 *
2004 2010 * Also, this is a nasty race condition for a process
2005 2011 * that is undergoing a forkall() operation:
2006 2012 * Once we clear our spinlock (below), we are vulnerable
2007 2013 * to being suspended by the forkall() thread before
2008 2014 * we manage to suspend ourself in ___lwp_suspend().
2009 2015 * See safe_suspend() and force_continue().
2010 2016 *
2011 2017 * To avoid a SIGSEGV due to the disappearance
2012 2018 * of the schedctl mappings in the child process,
2013 2019 * which can happen in spin_lock_clear() if we
2014 2020 * are suspended while we are in the middle of
2015 2021 * its call to preempt(), we preemptively clear
2016 2022 * our own schedctl pointer before dropping our
2017 2023 * spinlock. We reinstate it, in both the parent
2018 2024 * and (if this really is a forkall()) the child.
2019 2025 */
2020 2026 if (whystopped & TSTP_FORK) {
2021 2027 schedctl_after_fork = 1;
2022 2028 self->ul_schedctl = NULL;
2023 2029 self->ul_schedctl_called = &udp->uberflags;
2024 2030 }
2025 2031 spin_lock_clear(&self->ul_spinlock);
2026 2032 (void) ___lwp_suspend(tid);
2027 2033 /*
2028 2034 * Somebody else continued us.
2029 2035 * We can't grab ulwp_lock(self)
2030 2036 * until after clearing ul_stopping.
2031 2037 * force_continue() relies on this.
2032 2038 */
2033 2039 self->ul_stopping = 0;
2034 2040 self->ul_sp = 0;
2035 2041 if (schedctl_after_fork) {
2036 2042 self->ul_schedctl_called = NULL;
2037 2043 self->ul_schedctl = NULL;
2038 2044 (void) setup_schedctl();
2039 2045 }
2040 2046 ulwp_lock(self, udp);
2041 2047 ulwp_broadcast(self);
2042 2048 ulwp_unlock(self, udp);
2043 2049 exit_critical(self);
2044 2050 }
2045 2051
2046 2052 if (tid != self->ul_lwpid)
2047 2053 fork_lock_exit();
2048 2054
2049 2055 return (error);
2050 2056 }
2051 2057
2052 2058 /*
2053 2059 * Suspend all lwps other than ourself in preparation for fork.
2054 2060 */
2055 2061 void
2056 2062 suspend_fork()
2057 2063 {
2058 2064 ulwp_t *self = curthread;
2059 2065 uberdata_t *udp = self->ul_uberdata;
2060 2066 ulwp_t *ulwp;
2061 2067 int link_dropped;
2062 2068
2063 2069 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
2064 2070 top:
2065 2071 lmutex_lock(&udp->link_lock);
2066 2072
2067 2073 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2068 2074 ulwp_lock(ulwp, udp);
2069 2075 if (ulwp->ul_stop) { /* already stopped */
2070 2076 ulwp->ul_stop |= TSTP_FORK;
2071 2077 ulwp_broadcast(ulwp);
2072 2078 ulwp_unlock(ulwp, udp);
2073 2079 } else {
2074 2080 /*
2075 2081 * Move the stopped lwp out of a critical section.
2076 2082 */
2077 2083 if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) ||
2078 2084 link_dropped)
2079 2085 goto top;
2080 2086 }
2081 2087 }
2082 2088
2083 2089 lmutex_unlock(&udp->link_lock);
2084 2090 }
2085 2091
2086 2092 void
2087 2093 continue_fork(int child)
2088 2094 {
2089 2095 ulwp_t *self = curthread;
2090 2096 uberdata_t *udp = self->ul_uberdata;
2091 2097 ulwp_t *ulwp;
2092 2098
2093 2099 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
2094 2100
2095 2101 /*
2096 2102 * Clear the schedctl pointers in the child of forkall().
2097 2103 */
2098 2104 if (child) {
2099 2105 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2100 2106 ulwp->ul_schedctl_called =
2101 2107 ulwp->ul_dead? &udp->uberflags : NULL;
2102 2108 ulwp->ul_schedctl = NULL;
2103 2109 }
2104 2110 }
2105 2111
2106 2112 /*
2107 2113 * Set all lwps that were stopped for fork() running again.
2108 2114 */
2109 2115 lmutex_lock(&udp->link_lock);
2110 2116 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2111 2117 mutex_t *mp = ulwp_mutex(ulwp, udp);
2112 2118 lmutex_lock(mp);
2113 2119 ASSERT(ulwp->ul_stop & TSTP_FORK);
2114 2120 ulwp->ul_stop &= ~TSTP_FORK;
2115 2121 ulwp_broadcast(ulwp);
2116 2122 if (!ulwp->ul_stop)
2117 2123 force_continue(ulwp);
2118 2124 lmutex_unlock(mp);
2119 2125 }
2120 2126 lmutex_unlock(&udp->link_lock);
2121 2127 }
2122 2128
2123 2129 int
2124 2130 _thrp_continue(thread_t tid, uchar_t whystopped)
2125 2131 {
2126 2132 uberdata_t *udp = curthread->ul_uberdata;
2127 2133 ulwp_t *ulwp;
2128 2134 mutex_t *mp;
2129 2135 int error = 0;
2130 2136
2131 2137 ASSERT(whystopped == TSTP_REGULAR ||
2132 2138 whystopped == TSTP_MUTATOR);
2133 2139
2134 2140 /*
2135 2141 * We single-thread the entire thread suspend/continue mechanism.
2136 2142 */
2137 2143 fork_lock_enter();
2138 2144
2139 2145 if ((ulwp = find_lwp(tid)) == NULL) {
2140 2146 fork_lock_exit();
2141 2147 return (ESRCH);
2142 2148 }
2143 2149
2144 2150 mp = ulwp_mutex(ulwp, udp);
2145 2151 if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) {
2146 2152 error = EINVAL;
2147 2153 } else if (ulwp->ul_stop & whystopped) {
2148 2154 ulwp->ul_stop &= ~whystopped;
2149 2155 ulwp_broadcast(ulwp);
2150 2156 if (!ulwp->ul_stop) {
2151 2157 if (whystopped == TSTP_REGULAR && ulwp->ul_created) {
2152 2158 ulwp->ul_sp = 0;
2153 2159 ulwp->ul_created = 0;
2154 2160 }
2155 2161 force_continue(ulwp);
2156 2162 }
2157 2163 }
2158 2164 lmutex_unlock(mp);
2159 2165
2160 2166 fork_lock_exit();
2161 2167 return (error);
2162 2168 }
2163 2169
2164 2170 int
2165 2171 thr_suspend(thread_t tid)
2166 2172 {
2167 2173 return (_thrp_suspend(tid, TSTP_REGULAR));
2168 2174 }
2169 2175
2170 2176 int
2171 2177 thr_continue(thread_t tid)
2172 2178 {
2173 2179 return (_thrp_continue(tid, TSTP_REGULAR));
2174 2180 }
2175 2181
2176 2182 void
2177 2183 thr_yield()
2178 2184 {
2179 2185 yield();
2180 2186 }
2181 2187
2182 2188 #pragma weak pthread_kill = thr_kill
2183 2189 #pragma weak _thr_kill = thr_kill
2184 2190 int
2185 2191 thr_kill(thread_t tid, int sig)
2186 2192 {
2187 2193 if (sig == SIGCANCEL)
2188 2194 return (EINVAL);
2189 2195 return (_lwp_kill(tid, sig));
2190 2196 }
2191 2197
2192 2198 /*
2193 2199 * Exit a critical section, take deferred actions if necessary.
2194 2200 * Called from exit_critical() and from sigon().
2195 2201 */
2196 2202 void
2197 2203 do_exit_critical()
2198 2204 {
2199 2205 ulwp_t *self = curthread;
2200 2206 int sig;
2201 2207
2202 2208 ASSERT(self->ul_critical == 0);
2203 2209
2204 2210 /*
2205 2211 * Don't suspend ourself or take a deferred signal while dying
2206 2212 * or while executing inside the dynamic linker (ld.so.1).
2207 2213 */
2208 2214 if (self->ul_dead || self->ul_rtld)
2209 2215 return;
2210 2216
2211 2217 while (self->ul_pleasestop ||
2212 2218 (self->ul_cursig != 0 && self->ul_sigdefer == 0)) {
2213 2219 /*
2214 2220 * Avoid a recursive call to exit_critical() in _thrp_suspend()
2215 2221 * by keeping self->ul_critical == 1 here.
2216 2222 */
2217 2223 self->ul_critical++;
2218 2224 while (self->ul_pleasestop) {
2219 2225 /*
2220 2226 * Guard against suspending ourself while on a sleep
2221 2227 * queue. See the comments in call_user_handler().
2222 2228 */
2223 2229 unsleep_self();
2224 2230 set_parking_flag(self, 0);
2225 2231 (void) _thrp_suspend(self->ul_lwpid,
2226 2232 self->ul_pleasestop);
2227 2233 }
2228 2234 self->ul_critical--;
2229 2235
2230 2236 if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) {
2231 2237 /*
2232 2238 * Clear ul_cursig before proceeding.
2233 2239 * This protects us from the dynamic linker's
2234 2240 * calls to bind_guard()/bind_clear() in the
2235 2241 * event that it is invoked to resolve a symbol
2236 2242 * like take_deferred_signal() below.
2237 2243 */
2238 2244 self->ul_cursig = 0;
2239 2245 take_deferred_signal(sig);
2240 2246 ASSERT(self->ul_cursig == 0);
2241 2247 }
2242 2248 }
2243 2249 ASSERT(self->ul_critical == 0);
2244 2250 }
2245 2251
2246 2252 /*
2247 2253 * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker
2248 2254 * (ld.so.1) when it has do do something, like resolve a symbol to be called
2249 2255 * by the application or one of its libraries. _ti_bind_guard() is called
2250 2256 * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the
2251 2257 * application. The dynamic linker gets special dispensation from libc to
2252 2258 * run in a critical region (all signals deferred and no thread suspension
2253 2259 * or forking allowed), and to be immune from cancellation for the duration.
2254 2260 */
2255 2261 int
2256 2262 _ti_bind_guard(int flags)
2257 2263 {
2258 2264 ulwp_t *self = curthread;
2259 2265 uberdata_t *udp = self->ul_uberdata;
2260 2266 int bindflag = (flags & THR_FLG_RTLD);
2261 2267
2262 2268 if ((self->ul_bindflags & bindflag) == bindflag)
2263 2269 return (0);
2264 2270 self->ul_bindflags |= bindflag;
2265 2271 if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
2266 2272 sigoff(self); /* see no signals while holding ld_lock */
2267 2273 self->ul_rtld++; /* don't suspend while in ld.so.1 */
2268 2274 (void) mutex_lock(&udp->ld_lock);
2269 2275 }
2270 2276 enter_critical(self);
2271 2277 self->ul_save_state = self->ul_cancel_disabled;
2272 2278 self->ul_cancel_disabled = 1;
2273 2279 set_cancel_pending_flag(self, 0);
2274 2280 return (1);
2275 2281 }
2276 2282
2277 2283 int
2278 2284 _ti_bind_clear(int flags)
2279 2285 {
2280 2286 ulwp_t *self = curthread;
2281 2287 uberdata_t *udp = self->ul_uberdata;
2282 2288 int bindflag = (flags & THR_FLG_RTLD);
2283 2289
2284 2290 if ((self->ul_bindflags & bindflag) == 0)
2285 2291 return (self->ul_bindflags);
2286 2292 self->ul_bindflags &= ~bindflag;
2287 2293 self->ul_cancel_disabled = self->ul_save_state;
2288 2294 set_cancel_pending_flag(self, 0);
2289 2295 exit_critical(self);
2290 2296 if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
2291 2297 if (MUTEX_OWNED(&udp->ld_lock, self)) {
2292 2298 (void) mutex_unlock(&udp->ld_lock);
2293 2299 self->ul_rtld--;
2294 2300 sigon(self); /* reenable signals */
2295 2301 }
2296 2302 }
2297 2303 return (self->ul_bindflags);
2298 2304 }
2299 2305
2300 2306 /*
2301 2307 * Tell the dynamic linker (ld.so.1) whether or not it was entered from
2302 2308 * a critical region in libc. Return zero if not, else return non-zero.
2303 2309 */
2304 2310 int
2305 2311 _ti_critical(void)
2306 2312 {
2307 2313 ulwp_t *self = curthread;
2308 2314 int level = self->ul_critical;
2309 2315
2310 2316 if ((self->ul_bindflags & THR_FLG_RTLD) == 0 || level == 0)
2311 2317 return (level); /* ld.so.1 hasn't (yet) called enter() */
2312 2318 return (level - 1);
2313 2319 }
2314 2320
2315 2321 /*
2316 2322 * sigoff() and sigon() enable cond_wait() to behave (optionally) like
2317 2323 * it does in the old libthread (see the comments in cond_wait_queue()).
2318 2324 * Also, signals are deferred at thread startup until TLS constructors
2319 2325 * have all been called, at which time _thrp_setup() calls sigon().
2320 2326 *
2321 2327 * _sigoff() and _sigon() are external consolidation-private interfaces to
2322 2328 * sigoff() and sigon(), respectively, in libc. These are used in libnsl.
2323 2329 * Also, _sigoff() and _sigon() are called from dbx's run-time checking
2324 2330 * (librtc.so) to defer signals during its critical sections (not to be
2325 2331 * confused with libc critical sections [see exit_critical() above]).
2326 2332 */
2327 2333 void
2328 2334 _sigoff(void)
2329 2335 {
2330 2336 ulwp_t *self = curthread;
2331 2337
2332 2338 sigoff(self);
2333 2339 }
2334 2340
2335 2341 void
2336 2342 _sigon(void)
2337 2343 {
2338 2344 ulwp_t *self = curthread;
2339 2345
2340 2346 ASSERT(self->ul_sigdefer > 0);
2341 2347 sigon(self);
2342 2348 }
2343 2349
2344 2350 int
2345 2351 thr_getconcurrency()
2346 2352 {
2347 2353 return (thr_concurrency);
2348 2354 }
2349 2355
2350 2356 int
2351 2357 pthread_getconcurrency()
2352 2358 {
2353 2359 return (pthread_concurrency);
2354 2360 }
2355 2361
2356 2362 int
2357 2363 thr_setconcurrency(int new_level)
2358 2364 {
2359 2365 uberdata_t *udp = curthread->ul_uberdata;
2360 2366
2361 2367 if (new_level < 0)
2362 2368 return (EINVAL);
2363 2369 if (new_level > 65536) /* 65536 is totally arbitrary */
2364 2370 return (EAGAIN);
2365 2371 lmutex_lock(&udp->link_lock);
2366 2372 if (new_level > thr_concurrency)
2367 2373 thr_concurrency = new_level;
2368 2374 lmutex_unlock(&udp->link_lock);
2369 2375 return (0);
2370 2376 }
2371 2377
2372 2378 int
2373 2379 pthread_setconcurrency(int new_level)
2374 2380 {
2375 2381 if (new_level < 0)
2376 2382 return (EINVAL);
2377 2383 if (new_level > 65536) /* 65536 is totally arbitrary */
2378 2384 return (EAGAIN);
2379 2385 pthread_concurrency = new_level;
2380 2386 return (0);
2381 2387 }
2382 2388
2383 2389 size_t
2384 2390 thr_min_stack(void)
2385 2391 {
2386 2392 return (MINSTACK);
2387 2393 }
2388 2394
2389 2395 int
2390 2396 __nthreads(void)
2391 2397 {
2392 2398 return (curthread->ul_uberdata->nthreads);
2393 2399 }
2394 2400
2395 2401 /*
2396 2402 * XXX
2397 2403 * The remainder of this file implements the private interfaces to java for
2398 2404 * garbage collection. It is no longer used, at least by java 1.2.
2399 2405 * It can all go away once all old JVMs have disappeared.
2400 2406 */
2401 2407
2402 2408 int suspendingallmutators; /* when non-zero, suspending all mutators. */
2403 2409 int suspendedallmutators; /* when non-zero, all mutators suspended. */
2404 2410 int mutatorsbarrier; /* when non-zero, mutators barrier imposed. */
2405 2411 mutex_t mutatorslock = DEFAULTMUTEX; /* used to enforce mutators barrier. */
2406 2412 cond_t mutatorscv = DEFAULTCV; /* where non-mutators sleep. */
2407 2413
2408 2414 /*
2409 2415 * Get the available register state for the target thread.
2410 2416 * Return non-volatile registers: TRS_NONVOLATILE
2411 2417 */
2412 2418 #pragma weak _thr_getstate = thr_getstate
2413 2419 int
2414 2420 thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs)
2415 2421 {
2416 2422 ulwp_t *self = curthread;
2417 2423 uberdata_t *udp = self->ul_uberdata;
2418 2424 ulwp_t **ulwpp;
2419 2425 ulwp_t *ulwp;
2420 2426 int error = 0;
2421 2427 int trs_flag = TRS_LWPID;
2422 2428
2423 2429 if (tid == 0 || self->ul_lwpid == tid) {
2424 2430 ulwp = self;
2425 2431 ulwp_lock(ulwp, udp);
2426 2432 } else if ((ulwpp = find_lwpp(tid)) != NULL) {
2427 2433 ulwp = *ulwpp;
2428 2434 } else {
2429 2435 if (flag)
2430 2436 *flag = TRS_INVALID;
2431 2437 return (ESRCH);
2432 2438 }
2433 2439
2434 2440 if (ulwp->ul_dead) {
2435 2441 trs_flag = TRS_INVALID;
2436 2442 } else if (!ulwp->ul_stop && !suspendedallmutators) {
2437 2443 error = EINVAL;
2438 2444 trs_flag = TRS_INVALID;
2439 2445 } else if (ulwp->ul_stop) {
2440 2446 trs_flag = TRS_NONVOLATILE;
2441 2447 getgregs(ulwp, rs);
2442 2448 }
2443 2449
2444 2450 if (flag)
2445 2451 *flag = trs_flag;
2446 2452 if (lwp)
2447 2453 *lwp = tid;
2448 2454 if (ss != NULL)
2449 2455 (void) _thrp_stksegment(ulwp, ss);
2450 2456
2451 2457 ulwp_unlock(ulwp, udp);
2452 2458 return (error);
2453 2459 }
2454 2460
2455 2461 /*
2456 2462 * Set the appropriate register state for the target thread.
2457 2463 * This is not used by java. It exists solely for the MSTC test suite.
2458 2464 */
2459 2465 #pragma weak _thr_setstate = thr_setstate
2460 2466 int
2461 2467 thr_setstate(thread_t tid, int flag, gregset_t rs)
2462 2468 {
2463 2469 uberdata_t *udp = curthread->ul_uberdata;
2464 2470 ulwp_t *ulwp;
2465 2471 int error = 0;
2466 2472
2467 2473 if ((ulwp = find_lwp(tid)) == NULL)
2468 2474 return (ESRCH);
2469 2475
2470 2476 if (!ulwp->ul_stop && !suspendedallmutators)
2471 2477 error = EINVAL;
2472 2478 else if (rs != NULL) {
2473 2479 switch (flag) {
2474 2480 case TRS_NONVOLATILE:
2475 2481 /* do /proc stuff here? */
2476 2482 if (ulwp->ul_stop)
2477 2483 setgregs(ulwp, rs);
2478 2484 else
2479 2485 error = EINVAL;
2480 2486 break;
2481 2487 case TRS_LWPID: /* do /proc stuff here? */
2482 2488 default:
2483 2489 error = EINVAL;
2484 2490 break;
2485 2491 }
2486 2492 }
2487 2493
2488 2494 ulwp_unlock(ulwp, udp);
2489 2495 return (error);
2490 2496 }
2491 2497
2492 2498 int
2493 2499 getlwpstatus(thread_t tid, struct lwpstatus *sp)
2494 2500 {
2495 2501 extern ssize_t __pread(int, void *, size_t, off_t);
2496 2502 char buf[100];
2497 2503 int fd;
2498 2504
2499 2505 /* "/proc/self/lwp/%u/lwpstatus" w/o stdio */
2500 2506 (void) strcpy(buf, "/proc/self/lwp/");
2501 2507 ultos((uint64_t)tid, 10, buf + strlen(buf));
2502 2508 (void) strcat(buf, "/lwpstatus");
2503 2509 if ((fd = __open(buf, O_RDONLY, 0)) >= 0) {
2504 2510 while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) {
2505 2511 if (sp->pr_flags & PR_STOPPED) {
2506 2512 (void) __close(fd);
2507 2513 return (0);
2508 2514 }
2509 2515 yield(); /* give it a chance to stop */
2510 2516 }
2511 2517 (void) __close(fd);
2512 2518 }
2513 2519 return (-1);
2514 2520 }
2515 2521
2516 2522 int
2517 2523 putlwpregs(thread_t tid, prgregset_t prp)
2518 2524 {
2519 2525 extern ssize_t __writev(int, const struct iovec *, int);
2520 2526 char buf[100];
2521 2527 int fd;
2522 2528 long dstop_sreg[2];
2523 2529 long run_null[2];
2524 2530 iovec_t iov[3];
2525 2531
2526 2532 /* "/proc/self/lwp/%u/lwpctl" w/o stdio */
2527 2533 (void) strcpy(buf, "/proc/self/lwp/");
2528 2534 ultos((uint64_t)tid, 10, buf + strlen(buf));
2529 2535 (void) strcat(buf, "/lwpctl");
2530 2536 if ((fd = __open(buf, O_WRONLY, 0)) >= 0) {
2531 2537 dstop_sreg[0] = PCDSTOP; /* direct it to stop */
2532 2538 dstop_sreg[1] = PCSREG; /* set the registers */
2533 2539 iov[0].iov_base = (caddr_t)dstop_sreg;
2534 2540 iov[0].iov_len = sizeof (dstop_sreg);
2535 2541 iov[1].iov_base = (caddr_t)prp; /* from the register set */
2536 2542 iov[1].iov_len = sizeof (prgregset_t);
2537 2543 run_null[0] = PCRUN; /* make it runnable again */
2538 2544 run_null[1] = 0;
2539 2545 iov[2].iov_base = (caddr_t)run_null;
2540 2546 iov[2].iov_len = sizeof (run_null);
2541 2547 if (__writev(fd, iov, 3) >= 0) {
2542 2548 (void) __close(fd);
2543 2549 return (0);
2544 2550 }
2545 2551 (void) __close(fd);
2546 2552 }
2547 2553 return (-1);
2548 2554 }
2549 2555
2550 2556 static ulong_t
2551 2557 gettsp_slow(thread_t tid)
2552 2558 {
2553 2559 char buf[100];
2554 2560 struct lwpstatus status;
2555 2561
2556 2562 if (getlwpstatus(tid, &status) != 0) {
2557 2563 /* "__gettsp(%u): can't read lwpstatus" w/o stdio */
2558 2564 (void) strcpy(buf, "__gettsp(");
2559 2565 ultos((uint64_t)tid, 10, buf + strlen(buf));
2560 2566 (void) strcat(buf, "): can't read lwpstatus");
2561 2567 thr_panic(buf);
2562 2568 }
2563 2569 return (status.pr_reg[R_SP]);
2564 2570 }
2565 2571
2566 2572 ulong_t
2567 2573 __gettsp(thread_t tid)
2568 2574 {
2569 2575 uberdata_t *udp = curthread->ul_uberdata;
2570 2576 ulwp_t *ulwp;
2571 2577 ulong_t result;
2572 2578
2573 2579 if ((ulwp = find_lwp(tid)) == NULL)
2574 2580 return (0);
2575 2581
2576 2582 if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) {
2577 2583 ulwp_unlock(ulwp, udp);
2578 2584 return (result);
2579 2585 }
2580 2586
2581 2587 result = gettsp_slow(tid);
2582 2588 ulwp_unlock(ulwp, udp);
2583 2589 return (result);
2584 2590 }
2585 2591
2586 2592 /*
2587 2593 * This tells java stack walkers how to find the ucontext
2588 2594 * structure passed to signal handlers.
2589 2595 */
2590 2596 #pragma weak _thr_sighndlrinfo = thr_sighndlrinfo
2591 2597 void
2592 2598 thr_sighndlrinfo(void (**func)(), int *funcsize)
2593 2599 {
2594 2600 *func = &__sighndlr;
2595 2601 *funcsize = (char *)&__sighndlrend - (char *)&__sighndlr;
2596 2602 }
2597 2603
2598 2604 /*
2599 2605 * Mark a thread a mutator or reset a mutator to being a default,
2600 2606 * non-mutator thread.
2601 2607 */
2602 2608 #pragma weak _thr_setmutator = thr_setmutator
2603 2609 int
2604 2610 thr_setmutator(thread_t tid, int enabled)
2605 2611 {
2606 2612 ulwp_t *self = curthread;
2607 2613 uberdata_t *udp = self->ul_uberdata;
2608 2614 ulwp_t *ulwp;
2609 2615 int error;
2610 2616 int cancel_state;
2611 2617
2612 2618 enabled = enabled? 1 : 0;
2613 2619 top:
2614 2620 if (tid == 0) {
2615 2621 ulwp = self;
2616 2622 ulwp_lock(ulwp, udp);
2617 2623 } else if ((ulwp = find_lwp(tid)) == NULL) {
2618 2624 return (ESRCH);
2619 2625 }
2620 2626
2621 2627 /*
2622 2628 * The target thread should be the caller itself or a suspended thread.
2623 2629 * This prevents the target from also changing its ul_mutator field.
2624 2630 */
2625 2631 error = 0;
2626 2632 if (ulwp != self && !ulwp->ul_stop && enabled)
2627 2633 error = EINVAL;
2628 2634 else if (ulwp->ul_mutator != enabled) {
2629 2635 lmutex_lock(&mutatorslock);
2630 2636 if (mutatorsbarrier) {
2631 2637 ulwp_unlock(ulwp, udp);
2632 2638 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
2633 2639 &cancel_state);
2634 2640 while (mutatorsbarrier)
2635 2641 (void) cond_wait(&mutatorscv, &mutatorslock);
2636 2642 (void) pthread_setcancelstate(cancel_state, NULL);
2637 2643 lmutex_unlock(&mutatorslock);
2638 2644 goto top;
2639 2645 }
2640 2646 ulwp->ul_mutator = enabled;
2641 2647 lmutex_unlock(&mutatorslock);
2642 2648 }
2643 2649
2644 2650 ulwp_unlock(ulwp, udp);
2645 2651 return (error);
2646 2652 }
2647 2653
2648 2654 /*
2649 2655 * Establish a barrier against new mutators. Any non-mutator trying
2650 2656 * to become a mutator is suspended until the barrier is removed.
2651 2657 */
2652 2658 #pragma weak _thr_mutators_barrier = thr_mutators_barrier
2653 2659 void
2654 2660 thr_mutators_barrier(int enabled)
2655 2661 {
2656 2662 int oldvalue;
2657 2663 int cancel_state;
2658 2664
2659 2665 lmutex_lock(&mutatorslock);
2660 2666
2661 2667 /*
2662 2668 * Wait if trying to set the barrier while it is already set.
2663 2669 */
2664 2670 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2665 2671 while (mutatorsbarrier && enabled)
2666 2672 (void) cond_wait(&mutatorscv, &mutatorslock);
2667 2673 (void) pthread_setcancelstate(cancel_state, NULL);
2668 2674
2669 2675 oldvalue = mutatorsbarrier;
2670 2676 mutatorsbarrier = enabled;
2671 2677 /*
2672 2678 * Wakeup any blocked non-mutators when barrier is removed.
2673 2679 */
2674 2680 if (oldvalue && !enabled)
2675 2681 (void) cond_broadcast(&mutatorscv);
2676 2682 lmutex_unlock(&mutatorslock);
2677 2683 }
2678 2684
2679 2685 /*
2680 2686 * Suspend the set of all mutators except for the caller. The list
2681 2687 * of actively running threads is searched and only the mutators
2682 2688 * in this list are suspended. Actively running non-mutators remain
2683 2689 * running. Any other thread is suspended.
2684 2690 */
2685 2691 #pragma weak _thr_suspend_allmutators = thr_suspend_allmutators
2686 2692 int
2687 2693 thr_suspend_allmutators(void)
2688 2694 {
2689 2695 ulwp_t *self = curthread;
2690 2696 uberdata_t *udp = self->ul_uberdata;
2691 2697 ulwp_t *ulwp;
2692 2698 int link_dropped;
2693 2699
2694 2700 /*
2695 2701 * We single-thread the entire thread suspend/continue mechanism.
2696 2702 */
2697 2703 fork_lock_enter();
2698 2704
2699 2705 top:
2700 2706 lmutex_lock(&udp->link_lock);
2701 2707
2702 2708 if (suspendingallmutators || suspendedallmutators) {
2703 2709 lmutex_unlock(&udp->link_lock);
2704 2710 fork_lock_exit();
2705 2711 return (EINVAL);
2706 2712 }
2707 2713 suspendingallmutators = 1;
2708 2714
2709 2715 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2710 2716 ulwp_lock(ulwp, udp);
2711 2717 if (!ulwp->ul_mutator) {
2712 2718 ulwp_unlock(ulwp, udp);
2713 2719 } else if (ulwp->ul_stop) { /* already stopped */
2714 2720 ulwp->ul_stop |= TSTP_MUTATOR;
2715 2721 ulwp_broadcast(ulwp);
2716 2722 ulwp_unlock(ulwp, udp);
2717 2723 } else {
2718 2724 /*
2719 2725 * Move the stopped lwp out of a critical section.
2720 2726 */
2721 2727 if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) ||
2722 2728 link_dropped) {
2723 2729 suspendingallmutators = 0;
2724 2730 goto top;
2725 2731 }
2726 2732 }
2727 2733 }
2728 2734
2729 2735 suspendedallmutators = 1;
2730 2736 suspendingallmutators = 0;
2731 2737 lmutex_unlock(&udp->link_lock);
2732 2738 fork_lock_exit();
2733 2739 return (0);
2734 2740 }
2735 2741
2736 2742 /*
2737 2743 * Suspend the target mutator. The caller is permitted to suspend
2738 2744 * itself. If a mutator barrier is enabled, the caller will suspend
2739 2745 * itself as though it had been suspended by thr_suspend_allmutators().
2740 2746 * When the barrier is removed, this thread will be resumed. Any
2741 2747 * suspended mutator, whether suspended by thr_suspend_mutator(), or by
2742 2748 * thr_suspend_allmutators(), can be resumed by thr_continue_mutator().
2743 2749 */
2744 2750 #pragma weak _thr_suspend_mutator = thr_suspend_mutator
2745 2751 int
2746 2752 thr_suspend_mutator(thread_t tid)
2747 2753 {
2748 2754 if (tid == 0)
2749 2755 tid = curthread->ul_lwpid;
2750 2756 return (_thrp_suspend(tid, TSTP_MUTATOR));
2751 2757 }
2752 2758
2753 2759 /*
2754 2760 * Resume the set of all suspended mutators.
2755 2761 */
2756 2762 #pragma weak _thr_continue_allmutators = thr_continue_allmutators
2757 2763 int
2758 2764 thr_continue_allmutators()
2759 2765 {
2760 2766 ulwp_t *self = curthread;
2761 2767 uberdata_t *udp = self->ul_uberdata;
2762 2768 ulwp_t *ulwp;
2763 2769
2764 2770 /*
2765 2771 * We single-thread the entire thread suspend/continue mechanism.
2766 2772 */
2767 2773 fork_lock_enter();
2768 2774
2769 2775 lmutex_lock(&udp->link_lock);
2770 2776 if (!suspendedallmutators) {
2771 2777 lmutex_unlock(&udp->link_lock);
2772 2778 fork_lock_exit();
2773 2779 return (EINVAL);
2774 2780 }
2775 2781 suspendedallmutators = 0;
2776 2782
2777 2783 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2778 2784 mutex_t *mp = ulwp_mutex(ulwp, udp);
2779 2785 lmutex_lock(mp);
2780 2786 if (ulwp->ul_stop & TSTP_MUTATOR) {
2781 2787 ulwp->ul_stop &= ~TSTP_MUTATOR;
2782 2788 ulwp_broadcast(ulwp);
2783 2789 if (!ulwp->ul_stop)
2784 2790 force_continue(ulwp);
2785 2791 }
2786 2792 lmutex_unlock(mp);
2787 2793 }
2788 2794
2789 2795 lmutex_unlock(&udp->link_lock);
2790 2796 fork_lock_exit();
2791 2797 return (0);
2792 2798 }
2793 2799
2794 2800 /*
2795 2801 * Resume a suspended mutator.
2796 2802 */
2797 2803 #pragma weak _thr_continue_mutator = thr_continue_mutator
2798 2804 int
2799 2805 thr_continue_mutator(thread_t tid)
2800 2806 {
2801 2807 return (_thrp_continue(tid, TSTP_MUTATOR));
2802 2808 }
2803 2809
2804 2810 #pragma weak _thr_wait_mutator = thr_wait_mutator
2805 2811 int
2806 2812 thr_wait_mutator(thread_t tid, int dontwait)
2807 2813 {
2808 2814 uberdata_t *udp = curthread->ul_uberdata;
2809 2815 ulwp_t *ulwp;
2810 2816 int cancel_state;
2811 2817 int error = 0;
2812 2818
2813 2819 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2814 2820 top:
2815 2821 if ((ulwp = find_lwp(tid)) == NULL) {
2816 2822 (void) pthread_setcancelstate(cancel_state, NULL);
2817 2823 return (ESRCH);
2818 2824 }
2819 2825
2820 2826 if (!ulwp->ul_mutator)
2821 2827 error = EINVAL;
2822 2828 else if (dontwait) {
2823 2829 if (!(ulwp->ul_stop & TSTP_MUTATOR))
2824 2830 error = EWOULDBLOCK;
2825 2831 } else if (!(ulwp->ul_stop & TSTP_MUTATOR)) {
2826 2832 cond_t *cvp = ulwp_condvar(ulwp, udp);
2827 2833 mutex_t *mp = ulwp_mutex(ulwp, udp);
2828 2834
2829 2835 (void) cond_wait(cvp, mp);
2830 2836 (void) lmutex_unlock(mp);
2831 2837 goto top;
2832 2838 }
2833 2839
2834 2840 ulwp_unlock(ulwp, udp);
2835 2841 (void) pthread_setcancelstate(cancel_state, NULL);
2836 2842 return (error);
2837 2843 }
2838 2844
2839 2845 /* PROBE_SUPPORT begin */
2840 2846
2841 2847 void
2842 2848 thr_probe_setup(void *data)
2843 2849 {
2844 2850 curthread->ul_tpdp = data;
2845 2851 }
2846 2852
2847 2853 static void *
2848 2854 _thread_probe_getfunc()
2849 2855 {
2850 2856 return (curthread->ul_tpdp);
2851 2857 }
2852 2858
2853 2859 void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc;
2854 2860
2855 2861 /* ARGSUSED */
2856 2862 void
2857 2863 _resume(ulwp_t *ulwp, caddr_t sp, int dontsave)
2858 2864 {
2859 2865 /* never called */
2860 2866 }
2861 2867
2862 2868 /* ARGSUSED */
2863 2869 void
2864 2870 _resume_ret(ulwp_t *oldlwp)
2865 2871 {
2866 2872 /* never called */
2867 2873 }
2868 2874
2869 2875 /* PROBE_SUPPORT end */
↓ open down ↓ |
2077 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX