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