import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / threads / thr.c
blobd86d441532859bb0aa0bad9e735908aaf31e6912
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2016 by Delphix. All rights reserved.
25 * Copyright (c) 2017 by The MathWorks, Inc. All rights reserved.
28 * Copyright 2016 Joyent, Inc.
31 #include "lint.h"
32 #include "thr_uberdata.h"
33 #include <pthread.h>
34 #include <procfs.h>
35 #include <sys/uio.h>
36 #include <ctype.h>
37 #include "libc.h"
40 * These symbols should not be exported from libc, but
41 * /lib/libm.so.2 references _thr_main. libm needs to be fixed.
42 * Also, some older versions of the Studio compiler/debugger
43 * components reference them. These need to be fixed, too.
45 #pragma weak _thr_main = thr_main
46 #pragma weak _thr_create = thr_create
47 #pragma weak _thr_join = thr_join
48 #pragma weak _thr_self = thr_self
50 #undef errno
51 extern int errno;
54 * Between Solaris 2.5 and Solaris 9, __threaded was used to indicate
55 * "we are linked with libthread". The Sun Workshop 6 update 1 compilation
56 * system used it illegally (it is a consolidation private symbol).
57 * To accommodate this and possibly other abusers of the symbol,
58 * we make it always equal to 1 now that libthread has been folded
59 * into libc. The new __libc_threaded symbol is used to indicate
60 * the new meaning, "more than one thread exists".
62 int __threaded = 1; /* always equal to 1 */
63 int __libc_threaded = 0; /* zero until first thr_create() */
66 * thr_concurrency and pthread_concurrency are not used by the library.
67 * They exist solely to hold and return the values set by calls to
68 * thr_setconcurrency() and pthread_setconcurrency().
69 * Because thr_concurrency is affected by the THR_NEW_LWP flag
70 * to thr_create(), thr_concurrency is protected by link_lock.
72 static int thr_concurrency = 1;
73 static int pthread_concurrency;
75 #define HASHTBLSZ 1024 /* must be a power of two */
76 #define TIDHASH(tid, udp) (tid & (udp)->hash_mask)
78 /* initial allocation, just enough for one lwp */
79 #pragma align 64(init_hash_table)
80 thr_hash_table_t init_hash_table[1] = {
81 { DEFAULTMUTEX, DEFAULTCV, NULL },
84 extern const Lc_interface rtld_funcs[];
87 * The weak version is known to libc_db and mdb.
89 #pragma weak _uberdata = __uberdata
90 uberdata_t __uberdata = {
91 { DEFAULTMUTEX, 0, 0 }, /* link_lock */
92 { RECURSIVEMUTEX, 0, 0 }, /* ld_lock */
93 { RECURSIVEMUTEX, 0, 0 }, /* fork_lock */
94 { RECURSIVEMUTEX, 0, 0 }, /* atfork_lock */
95 { RECURSIVEMUTEX, 0, 0 }, /* callout_lock */
96 { DEFAULTMUTEX, 0, 0 }, /* tdb_hash_lock */
97 { 0, }, /* tdb_hash_lock_stats */
98 { { 0 }, }, /* siguaction[NSIG] */
99 {{ DEFAULTMUTEX, NULL, 0 }, /* bucket[NBUCKETS] */
100 { DEFAULTMUTEX, NULL, 0 },
101 { DEFAULTMUTEX, NULL, 0 },
102 { DEFAULTMUTEX, NULL, 0 },
103 { DEFAULTMUTEX, NULL, 0 },
104 { DEFAULTMUTEX, NULL, 0 },
105 { DEFAULTMUTEX, NULL, 0 },
106 { DEFAULTMUTEX, NULL, 0 },
107 { DEFAULTMUTEX, NULL, 0 },
108 { DEFAULTMUTEX, NULL, 0 }},
109 { RECURSIVEMUTEX, NULL, NULL }, /* atexit_root */
110 { RECURSIVEMUTEX, NULL }, /* quickexit_root */
111 { DEFAULTMUTEX, 0, 0, NULL }, /* tsd_metadata */
112 { DEFAULTMUTEX, {0, 0}, {0, 0} }, /* tls_metadata */
113 0, /* primary_map */
114 0, /* bucket_init */
115 0, /* pad[0] */
116 0, /* pad[1] */
117 { 0 }, /* uberflags */
118 NULL, /* queue_head */
119 init_hash_table, /* thr_hash_table */
120 1, /* hash_size: size of the hash table */
121 0, /* hash_mask: hash_size - 1 */
122 NULL, /* ulwp_one */
123 NULL, /* all_lwps */
124 NULL, /* all_zombies */
125 0, /* nthreads */
126 0, /* nzombies */
127 0, /* ndaemons */
128 0, /* pid */
129 sigacthandler, /* sigacthandler */
130 NULL, /* lwp_stacks */
131 NULL, /* lwp_laststack */
132 0, /* nfreestack */
133 10, /* thread_stack_cache */
134 NULL, /* ulwp_freelist */
135 NULL, /* ulwp_lastfree */
136 NULL, /* ulwp_replace_free */
137 NULL, /* ulwp_replace_last */
138 NULL, /* atforklist */
139 NULL, /* robustlocks */
140 NULL, /* robustlist */
141 NULL, /* progname */
142 NULL, /* ub_comm_page */
143 NULL, /* __tdb_bootstrap */
144 { /* tdb */
145 NULL, /* tdb_sync_addr_hash */
146 0, /* tdb_register_count */
147 0, /* tdb_hash_alloc_failed */
148 NULL, /* tdb_sync_addr_free */
149 NULL, /* tdb_sync_addr_last */
150 0, /* tdb_sync_alloc */
151 { 0, 0 }, /* tdb_ev_global_mask */
152 tdb_events, /* tdb_events array */
157 * The weak version is known to libc_db and mdb.
159 #pragma weak _tdb_bootstrap = __tdb_bootstrap
160 uberdata_t **__tdb_bootstrap = NULL;
162 int thread_queue_fifo = 4;
163 int thread_queue_dump = 0;
164 int thread_cond_wait_defer = 0;
165 int thread_error_detection = 0;
166 int thread_async_safe = 0;
167 int thread_stack_cache = 10;
168 int thread_door_noreserve = 0;
169 int thread_locks_misaligned = 0;
171 static ulwp_t *ulwp_alloc(void);
172 static void ulwp_free(ulwp_t *);
175 * Insert the lwp into the hash table.
177 void
178 hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
180 ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket;
181 udp->thr_hash_table[ix].hash_bucket = ulwp;
182 ulwp->ul_ix = ix;
185 void
186 hash_in(ulwp_t *ulwp, uberdata_t *udp)
188 int ix = TIDHASH(ulwp->ul_lwpid, udp);
189 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
191 lmutex_lock(mp);
192 hash_in_unlocked(ulwp, ix, udp);
193 lmutex_unlock(mp);
197 * Delete the lwp from the hash table.
199 void
200 hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
202 ulwp_t **ulwpp;
204 for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
205 ulwp != *ulwpp;
206 ulwpp = &(*ulwpp)->ul_hash)
208 *ulwpp = ulwp->ul_hash;
209 ulwp->ul_hash = NULL;
210 ulwp->ul_ix = -1;
213 void
214 hash_out(ulwp_t *ulwp, uberdata_t *udp)
216 int ix;
218 if ((ix = ulwp->ul_ix) >= 0) {
219 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
221 lmutex_lock(mp);
222 hash_out_unlocked(ulwp, ix, udp);
223 lmutex_unlock(mp);
228 * Retain stack information for thread structures that are being recycled for
229 * new threads. All other members of the thread structure should be zeroed.
231 static void
232 ulwp_clean(ulwp_t *ulwp)
234 caddr_t stk = ulwp->ul_stk;
235 size_t mapsiz = ulwp->ul_mapsiz;
236 size_t guardsize = ulwp->ul_guardsize;
237 uintptr_t stktop = ulwp->ul_stktop;
238 size_t stksiz = ulwp->ul_stksiz;
240 (void) memset(ulwp, 0, sizeof (*ulwp));
242 ulwp->ul_stk = stk;
243 ulwp->ul_mapsiz = mapsiz;
244 ulwp->ul_guardsize = guardsize;
245 ulwp->ul_stktop = stktop;
246 ulwp->ul_stksiz = stksiz;
249 static int stackprot;
252 * Answer the question, "Is the lwp in question really dead?"
253 * We must inquire of the operating system to be really sure
254 * because the lwp may have called lwp_exit() but it has not
255 * yet completed the exit.
257 static int
258 dead_and_buried(ulwp_t *ulwp)
260 if (ulwp->ul_lwpid == (lwpid_t)(-1))
261 return (1);
262 if (ulwp->ul_dead && ulwp->ul_detached &&
263 _lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) {
264 ulwp->ul_lwpid = (lwpid_t)(-1);
265 return (1);
267 return (0);
271 * Attempt to keep the stack cache within the specified cache limit.
273 static void
274 trim_stack_cache(int cache_limit)
276 ulwp_t *self = curthread;
277 uberdata_t *udp = self->ul_uberdata;
278 ulwp_t *prev = NULL;
279 ulwp_t **ulwpp = &udp->lwp_stacks;
280 ulwp_t *ulwp;
282 ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self));
284 while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) {
285 if (dead_and_buried(ulwp)) {
286 *ulwpp = ulwp->ul_next;
287 if (ulwp == udp->lwp_laststack)
288 udp->lwp_laststack = prev;
289 hash_out(ulwp, udp);
290 udp->nfreestack--;
291 (void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz);
293 * Now put the free ulwp on the ulwp freelist.
295 ulwp->ul_mapsiz = 0;
296 ulwp->ul_next = NULL;
297 if (udp->ulwp_freelist == NULL)
298 udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
299 else {
300 udp->ulwp_lastfree->ul_next = ulwp;
301 udp->ulwp_lastfree = ulwp;
303 } else {
304 prev = ulwp;
305 ulwpp = &ulwp->ul_next;
311 * Find an unused stack of the requested size
312 * or create a new stack of the requested size.
313 * Return a pointer to the ulwp_t structure referring to the stack, or NULL.
314 * thr_exit() stores 1 in the ul_dead member.
315 * thr_join() stores -1 in the ul_lwpid member.
317 static ulwp_t *
318 find_stack(size_t stksize, size_t guardsize)
320 static size_t pagesize = 0;
322 uberdata_t *udp = curthread->ul_uberdata;
323 size_t mapsize;
324 ulwp_t *prev;
325 ulwp_t *ulwp;
326 ulwp_t **ulwpp;
327 void *stk;
330 * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC
331 * unless overridden by the system's configuration.
333 if (stackprot == 0) { /* do this once */
334 long lprot = _sysconf(_SC_STACK_PROT);
335 if (lprot <= 0)
336 lprot = (PROT_READ|PROT_WRITE|PROT_EXEC);
337 stackprot = (int)lprot;
339 if (pagesize == 0) /* do this once */
340 pagesize = _sysconf(_SC_PAGESIZE);
343 * One megabyte stacks by default, but subtract off
344 * two pages for the system-created red zones.
345 * Round up a non-zero stack size to a pagesize multiple.
347 if (stksize == 0)
348 stksize = DEFAULTSTACK - 2 * pagesize;
349 else
350 stksize = ((stksize + pagesize - 1) & -pagesize);
353 * Round up the mapping size to a multiple of pagesize.
354 * Note: mmap() provides at least one page of red zone
355 * so we deduct that from the value of guardsize.
357 if (guardsize != 0)
358 guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize;
359 mapsize = stksize + guardsize;
361 lmutex_lock(&udp->link_lock);
362 for (prev = NULL, ulwpp = &udp->lwp_stacks;
363 (ulwp = *ulwpp) != NULL;
364 prev = ulwp, ulwpp = &ulwp->ul_next) {
365 if (ulwp->ul_mapsiz == mapsize &&
366 ulwp->ul_guardsize == guardsize &&
367 dead_and_buried(ulwp)) {
369 * The previous lwp is gone; reuse the stack.
370 * Remove the ulwp from the stack list.
372 *ulwpp = ulwp->ul_next;
373 ulwp->ul_next = NULL;
374 if (ulwp == udp->lwp_laststack)
375 udp->lwp_laststack = prev;
376 hash_out(ulwp, udp);
377 udp->nfreestack--;
378 lmutex_unlock(&udp->link_lock);
379 ulwp_clean(ulwp);
380 return (ulwp);
385 * None of the cached stacks matched our mapping size.
386 * Reduce the stack cache to get rid of possibly
387 * very old stacks that will never be reused.
389 if (udp->nfreestack > udp->thread_stack_cache)
390 trim_stack_cache(udp->thread_stack_cache);
391 else if (udp->nfreestack > 0)
392 trim_stack_cache(udp->nfreestack - 1);
393 lmutex_unlock(&udp->link_lock);
396 * Create a new stack.
398 if ((stk = mmap(NULL, mapsize, stackprot,
399 MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) {
401 * We have allocated our stack. Now allocate the ulwp.
403 ulwp = ulwp_alloc();
404 if (ulwp == NULL)
405 (void) munmap(stk, mapsize);
406 else {
407 ulwp->ul_stk = stk;
408 ulwp->ul_mapsiz = mapsize;
409 ulwp->ul_guardsize = guardsize;
410 ulwp->ul_stktop = (uintptr_t)stk + mapsize;
411 ulwp->ul_stksiz = stksize;
412 if (guardsize) /* protect the extra red zone */
413 (void) mprotect(stk, guardsize, PROT_NONE);
416 return (ulwp);
420 * Get a ulwp_t structure from the free list or allocate a new one.
421 * Such ulwp_t's do not have a stack allocated by the library.
423 static ulwp_t *
424 ulwp_alloc(void)
426 ulwp_t *self = curthread;
427 uberdata_t *udp = self->ul_uberdata;
428 size_t tls_size;
429 ulwp_t *prev;
430 ulwp_t *ulwp;
431 ulwp_t **ulwpp;
432 caddr_t data;
434 lmutex_lock(&udp->link_lock);
435 for (prev = NULL, ulwpp = &udp->ulwp_freelist;
436 (ulwp = *ulwpp) != NULL;
437 prev = ulwp, ulwpp = &ulwp->ul_next) {
438 if (dead_and_buried(ulwp)) {
439 *ulwpp = ulwp->ul_next;
440 ulwp->ul_next = NULL;
441 if (ulwp == udp->ulwp_lastfree)
442 udp->ulwp_lastfree = prev;
443 hash_out(ulwp, udp);
444 lmutex_unlock(&udp->link_lock);
445 ulwp_clean(ulwp);
446 return (ulwp);
449 lmutex_unlock(&udp->link_lock);
451 tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
452 data = lmalloc(sizeof (*ulwp) + tls_size);
453 if (data != NULL) {
454 /* LINTED pointer cast may result in improper alignment */
455 ulwp = (ulwp_t *)(data + tls_size);
457 return (ulwp);
461 * Free a ulwp structure.
462 * If there is an associated stack, put it on the stack list and
463 * munmap() previously freed stacks up to the residual cache limit.
464 * Else put it on the ulwp free list and never call lfree() on it.
466 static void
467 ulwp_free(ulwp_t *ulwp)
469 uberdata_t *udp = curthread->ul_uberdata;
471 ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread));
472 ulwp->ul_next = NULL;
473 if (ulwp == udp->ulwp_one) /* don't reuse the primoridal stack */
474 /*EMPTY*/;
475 else if (ulwp->ul_mapsiz != 0) {
476 if (udp->lwp_stacks == NULL)
477 udp->lwp_stacks = udp->lwp_laststack = ulwp;
478 else {
479 udp->lwp_laststack->ul_next = ulwp;
480 udp->lwp_laststack = ulwp;
482 if (++udp->nfreestack > udp->thread_stack_cache)
483 trim_stack_cache(udp->thread_stack_cache);
484 } else {
485 if (udp->ulwp_freelist == NULL)
486 udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
487 else {
488 udp->ulwp_lastfree->ul_next = ulwp;
489 udp->ulwp_lastfree = ulwp;
495 * Find a named lwp and return a pointer to its hash list location.
496 * On success, returns with the hash lock held.
498 ulwp_t **
499 find_lwpp(thread_t tid)
501 uberdata_t *udp = curthread->ul_uberdata;
502 int ix = TIDHASH(tid, udp);
503 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
504 ulwp_t *ulwp;
505 ulwp_t **ulwpp;
507 if (tid == 0)
508 return (NULL);
510 lmutex_lock(mp);
511 for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
512 (ulwp = *ulwpp) != NULL;
513 ulwpp = &ulwp->ul_hash) {
514 if (ulwp->ul_lwpid == tid)
515 return (ulwpp);
517 lmutex_unlock(mp);
518 return (NULL);
522 * Wake up all lwps waiting on this lwp for some reason.
524 void
525 ulwp_broadcast(ulwp_t *ulwp)
527 ulwp_t *self = curthread;
528 uberdata_t *udp = self->ul_uberdata;
530 ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
531 (void) cond_broadcast(ulwp_condvar(ulwp, udp));
535 * Find a named lwp and return a pointer to it.
536 * Returns with the hash lock held.
538 ulwp_t *
539 find_lwp(thread_t tid)
541 ulwp_t *self = curthread;
542 uberdata_t *udp = self->ul_uberdata;
543 ulwp_t *ulwp = NULL;
544 ulwp_t **ulwpp;
546 if (self->ul_lwpid == tid) {
547 ulwp = self;
548 ulwp_lock(ulwp, udp);
549 } else if ((ulwpp = find_lwpp(tid)) != NULL) {
550 ulwp = *ulwpp;
553 if (ulwp && ulwp->ul_dead) {
554 ulwp_unlock(ulwp, udp);
555 ulwp = NULL;
558 return (ulwp);
562 _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
563 long flags, thread_t *new_thread, size_t guardsize)
565 ulwp_t *self = curthread;
566 uberdata_t *udp = self->ul_uberdata;
567 ucontext_t uc;
568 uint_t lwp_flags;
569 thread_t tid;
570 int error;
571 ulwp_t *ulwp;
574 * Enforce the restriction of not creating any threads
575 * until the primary link map has been initialized.
576 * Also, disallow thread creation to a child of vfork().
578 if (!self->ul_primarymap || self->ul_vfork)
579 return (ENOTSUP);
581 if (udp->hash_size == 1)
582 finish_init();
584 if ((stk || stksize) && stksize < MINSTACK)
585 return (EINVAL);
587 if (stk == NULL) {
588 if ((ulwp = find_stack(stksize, guardsize)) == NULL)
589 return (ENOMEM);
590 stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize;
591 } else {
592 /* initialize the private stack */
593 if ((ulwp = ulwp_alloc()) == NULL)
594 return (ENOMEM);
595 ulwp->ul_stk = stk;
596 ulwp->ul_stktop = (uintptr_t)stk + stksize;
597 ulwp->ul_stksiz = stksize;
599 /* ulwp is not in the hash table; make sure hash_out() doesn't fail */
600 ulwp->ul_ix = -1;
601 ulwp->ul_errnop = &ulwp->ul_errno;
603 lwp_flags = LWP_SUSPENDED;
604 if (flags & (THR_DETACHED|THR_DAEMON)) {
605 flags |= THR_DETACHED;
606 lwp_flags |= LWP_DETACHED;
608 if (flags & THR_DAEMON)
609 lwp_flags |= LWP_DAEMON;
611 /* creating a thread: enforce mt-correctness in mutex_lock() */
612 self->ul_async_safe = 1;
614 /* per-thread copies of global variables, for speed */
615 ulwp->ul_queue_fifo = self->ul_queue_fifo;
616 ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer;
617 ulwp->ul_error_detection = self->ul_error_detection;
618 ulwp->ul_async_safe = self->ul_async_safe;
619 ulwp->ul_max_spinners = self->ul_max_spinners;
620 ulwp->ul_adaptive_spin = self->ul_adaptive_spin;
621 ulwp->ul_queue_spin = self->ul_queue_spin;
622 ulwp->ul_door_noreserve = self->ul_door_noreserve;
623 ulwp->ul_misaligned = self->ul_misaligned;
625 /* new thread inherits creating thread's scheduling parameters */
626 ulwp->ul_policy = self->ul_policy;
627 ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri);
628 ulwp->ul_cid = self->ul_cid;
629 ulwp->ul_rtclassid = self->ul_rtclassid;
631 ulwp->ul_primarymap = self->ul_primarymap;
632 ulwp->ul_self = ulwp;
633 ulwp->ul_uberdata = udp;
635 /* debugger support */
636 ulwp->ul_usropts = flags;
639 ulwp->ul_startpc = func;
640 ulwp->ul_startarg = arg;
641 _fpinherit(ulwp);
643 * Defer signals on the new thread until its TLS constructors
644 * have been called. _thrp_setup() will call sigon() after
645 * it has called tls_setup().
647 ulwp->ul_sigdefer = 1;
649 error = setup_context(&uc, _thrp_setup, ulwp,
650 (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize);
651 if (error != 0 && stk != NULL) /* inaccessible stack */
652 error = EFAULT;
655 * Call enter_critical() to avoid being suspended until we
656 * have linked the new thread into the proper lists.
657 * This is necessary because forkall() and fork1() must
658 * suspend all threads and they must see a complete list.
660 enter_critical(self);
661 uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask;
662 if (error != 0 ||
663 (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) {
664 exit_critical(self);
665 ulwp->ul_lwpid = (lwpid_t)(-1);
666 ulwp->ul_dead = 1;
667 ulwp->ul_detached = 1;
668 lmutex_lock(&udp->link_lock);
669 ulwp_free(ulwp);
670 lmutex_unlock(&udp->link_lock);
671 return (error);
673 self->ul_nocancel = 0; /* cancellation is now possible */
674 udp->uberflags.uf_mt = 1;
675 if (new_thread)
676 *new_thread = tid;
677 if (flags & THR_DETACHED)
678 ulwp->ul_detached = 1;
679 ulwp->ul_lwpid = tid;
680 ulwp->ul_stop = TSTP_REGULAR;
681 if (flags & THR_SUSPENDED)
682 ulwp->ul_created = 1;
684 lmutex_lock(&udp->link_lock);
685 ulwp->ul_forw = udp->all_lwps;
686 ulwp->ul_back = udp->all_lwps->ul_back;
687 ulwp->ul_back->ul_forw = ulwp;
688 ulwp->ul_forw->ul_back = ulwp;
689 hash_in(ulwp, udp);
690 udp->nthreads++;
691 if (flags & THR_DAEMON)
692 udp->ndaemons++;
693 if (flags & THR_NEW_LWP)
694 thr_concurrency++;
695 __libc_threaded = 1; /* inform stdio */
696 lmutex_unlock(&udp->link_lock);
698 if (__td_event_report(self, TD_CREATE, udp)) {
699 self->ul_td_evbuf.eventnum = TD_CREATE;
700 self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
701 tdb_event(TD_CREATE, udp);
704 exit_critical(self);
706 if (!(flags & THR_SUSPENDED))
707 (void) _thrp_continue(tid, TSTP_REGULAR);
709 return (0);
713 thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
714 long flags, thread_t *new_thread)
716 return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0));
720 * A special cancellation cleanup hook for DCE.
721 * cleanuphndlr, when it is not NULL, will contain a callback
722 * function to be called before a thread is terminated in
723 * thr_exit() as a result of being cancelled.
725 static void (*cleanuphndlr)(void) = NULL;
728 * _pthread_setcleanupinit: sets the cleanup hook.
731 _pthread_setcleanupinit(void (*func)(void))
733 cleanuphndlr = func;
734 return (0);
737 void
738 _thrp_exit()
740 ulwp_t *self = curthread;
741 uberdata_t *udp = self->ul_uberdata;
742 ulwp_t *replace = NULL;
744 if (__td_event_report(self, TD_DEATH, udp)) {
745 self->ul_td_evbuf.eventnum = TD_DEATH;
746 tdb_event(TD_DEATH, udp);
749 ASSERT(self->ul_sigdefer != 0);
751 lmutex_lock(&udp->link_lock);
752 udp->nthreads--;
753 if (self->ul_usropts & THR_NEW_LWP)
754 thr_concurrency--;
755 if (self->ul_usropts & THR_DAEMON)
756 udp->ndaemons--;
757 else if (udp->nthreads == udp->ndaemons) {
759 * We are the last non-daemon thread exiting.
760 * Exit the process. We retain our TSD and TLS so
761 * that atexit() application functions can use them.
763 lmutex_unlock(&udp->link_lock);
764 exit(0);
765 thr_panic("_thrp_exit(): exit(0) returned");
767 lmutex_unlock(&udp->link_lock);
770 * tsd_exit() may call its destructor free(), thus depending on
771 * tmem, therefore tmem_exit() needs to be called after tsd_exit()
772 * and tls_exit().
774 tsd_exit(); /* deallocate thread-specific data */
775 tls_exit(); /* deallocate thread-local storage */
776 tmem_exit(); /* deallocate tmem allocations */
777 heldlock_exit(); /* deal with left-over held locks */
779 /* block all signals to finish exiting */
780 block_all_signals(self);
781 /* also prevent ourself from being suspended */
782 enter_critical(self);
783 rwl_free(self);
784 lmutex_lock(&udp->link_lock);
785 ulwp_free(self);
786 (void) ulwp_lock(self, udp);
788 if (self->ul_mapsiz && !self->ul_detached) {
790 * We want to free the stack for reuse but must keep
791 * the ulwp_t struct for the benefit of thr_join().
792 * For this purpose we allocate a replacement ulwp_t.
794 if ((replace = udp->ulwp_replace_free) == NULL)
795 replace = lmalloc(REPLACEMENT_SIZE);
796 else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
797 udp->ulwp_replace_last = NULL;
800 if (udp->all_lwps == self)
801 udp->all_lwps = self->ul_forw;
802 if (udp->all_lwps == self)
803 udp->all_lwps = NULL;
804 else {
805 self->ul_forw->ul_back = self->ul_back;
806 self->ul_back->ul_forw = self->ul_forw;
808 self->ul_forw = self->ul_back = NULL;
809 #if defined(THREAD_DEBUG)
810 /* collect queue lock statistics before marking ourself dead */
811 record_spin_locks(self);
812 #endif
813 self->ul_dead = 1;
814 self->ul_pleasestop = 0;
815 if (replace != NULL) {
816 int ix = self->ul_ix; /* the hash index */
817 (void) memcpy(replace, self, REPLACEMENT_SIZE);
818 replace->ul_self = replace;
819 replace->ul_next = NULL; /* clone not on stack list */
820 replace->ul_mapsiz = 0; /* allows clone to be freed */
821 replace->ul_replace = 1; /* requires clone to be freed */
822 hash_out_unlocked(self, ix, udp);
823 hash_in_unlocked(replace, ix, udp);
824 ASSERT(!(self->ul_detached));
825 self->ul_detached = 1; /* this frees the stack */
826 self->ul_schedctl = NULL;
827 self->ul_schedctl_called = &udp->uberflags;
828 set_curthread(self = replace);
830 * Having just changed the address of curthread, we
831 * must reset the ownership of the locks we hold so
832 * that assertions will not fire when we release them.
834 udp->link_lock.mutex_owner = (uintptr_t)self;
835 ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self;
837 * NOTE:
838 * On i386, %gs still references the original, not the
839 * replacement, ulwp structure. Fetching the replacement
840 * curthread pointer via %gs:0 works correctly since the
841 * original ulwp structure will not be reallocated until
842 * this lwp has completed its lwp_exit() system call (see
843 * dead_and_buried()), but from here on out, we must make
844 * no references to %gs:<offset> other than %gs:0.
848 * Put non-detached terminated threads in the all_zombies list.
850 if (!self->ul_detached) {
851 udp->nzombies++;
852 if (udp->all_zombies == NULL) {
853 ASSERT(udp->nzombies == 1);
854 udp->all_zombies = self->ul_forw = self->ul_back = self;
855 } else {
856 self->ul_forw = udp->all_zombies;
857 self->ul_back = udp->all_zombies->ul_back;
858 self->ul_back->ul_forw = self;
859 self->ul_forw->ul_back = self;
863 * Notify everyone waiting for this thread.
865 ulwp_broadcast(self);
866 (void) ulwp_unlock(self, udp);
868 * Prevent any more references to the schedctl data.
869 * We are exiting and continue_fork() may not find us.
870 * Do this just before dropping link_lock, since fork
871 * serializes on link_lock.
873 self->ul_schedctl = NULL;
874 self->ul_schedctl_called = &udp->uberflags;
875 lmutex_unlock(&udp->link_lock);
877 ASSERT(self->ul_critical == 1);
878 ASSERT(self->ul_preempt == 0);
879 _lwp_terminate(); /* never returns */
880 thr_panic("_thrp_exit(): _lwp_terminate() returned");
883 #if defined(THREAD_DEBUG)
884 void
885 collect_queue_statistics()
887 uberdata_t *udp = curthread->ul_uberdata;
888 ulwp_t *ulwp;
890 if (thread_queue_dump) {
891 lmutex_lock(&udp->link_lock);
892 if ((ulwp = udp->all_lwps) != NULL) {
893 do {
894 record_spin_locks(ulwp);
895 } while ((ulwp = ulwp->ul_forw) != udp->all_lwps);
897 lmutex_unlock(&udp->link_lock);
900 #endif
902 static void __NORETURN
903 _thrp_exit_common(void *status, int unwind)
905 ulwp_t *self = curthread;
906 int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED);
908 ASSERT(self->ul_critical == 0 && self->ul_preempt == 0);
911 * Disable cancellation and call the special DCE cancellation
912 * cleanup hook if it is enabled. Do nothing else before calling
913 * the DCE cancellation cleanup hook; it may call longjmp() and
914 * never return here.
916 self->ul_cancel_disabled = 1;
917 self->ul_cancel_async = 0;
918 self->ul_save_async = 0;
919 self->ul_cancelable = 0;
920 self->ul_cancel_pending = 0;
921 set_cancel_pending_flag(self, 1);
922 if (cancelled && cleanuphndlr != NULL)
923 (*cleanuphndlr)();
926 * Block application signals while we are exiting.
927 * We call out to C++, TSD, and TLS destructors while exiting
928 * and these are application-defined, so we cannot be assured
929 * that they won't reset the signal mask. We use sigoff() to
930 * defer any signals that may be received as a result of this
931 * bad behavior. Such signals will be lost to the process
932 * when the thread finishes exiting.
934 (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
935 sigoff(self);
937 self->ul_rval = status;
940 * If thr_exit is being called from the places where
941 * C++ destructors are to be called such as cancellation
942 * points, then set this flag. It is checked in _t_cancel()
943 * to decide whether _ex_unwind() is to be called or not.
945 if (unwind)
946 self->ul_unwind = 1;
949 * _thrp_unwind() will eventually call _thrp_exit().
950 * It never returns.
952 _thrp_unwind(NULL);
953 thr_panic("_thrp_exit_common(): _thrp_unwind() returned");
955 for (;;) /* to shut the compiler up about __NORETURN */
956 continue;
960 * Called when a thread returns from its start function.
961 * We are at the top of the stack; no unwinding is necessary.
963 void
964 _thrp_terminate(void *status)
966 _thrp_exit_common(status, 0);
969 #pragma weak pthread_exit = thr_exit
970 #pragma weak _thr_exit = thr_exit
971 void
972 thr_exit(void *status)
974 _thrp_exit_common(status, 1);
978 _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel)
980 uberdata_t *udp = curthread->ul_uberdata;
981 mutex_t *mp;
982 void *rval;
983 thread_t found;
984 ulwp_t *ulwp;
985 ulwp_t **ulwpp;
986 int replace;
987 int error;
989 if (do_cancel)
990 error = lwp_wait(tid, &found);
991 else {
992 while ((error = __lwp_wait(tid, &found)) == EINTR)
995 if (error)
996 return (error);
999 * We must hold link_lock to avoid a race condition with find_stack().
1001 lmutex_lock(&udp->link_lock);
1002 if ((ulwpp = find_lwpp(found)) == NULL) {
1004 * lwp_wait() found an lwp that the library doesn't know
1005 * about. It must have been created with _lwp_create().
1006 * Just return its lwpid; we can't know its status.
1008 lmutex_unlock(&udp->link_lock);
1009 rval = NULL;
1010 } else {
1012 * Remove ulwp from the hash table.
1014 ulwp = *ulwpp;
1015 *ulwpp = ulwp->ul_hash;
1016 ulwp->ul_hash = NULL;
1018 * Remove ulwp from all_zombies list.
1020 ASSERT(udp->nzombies >= 1);
1021 if (udp->all_zombies == ulwp)
1022 udp->all_zombies = ulwp->ul_forw;
1023 if (udp->all_zombies == ulwp)
1024 udp->all_zombies = NULL;
1025 else {
1026 ulwp->ul_forw->ul_back = ulwp->ul_back;
1027 ulwp->ul_back->ul_forw = ulwp->ul_forw;
1029 ulwp->ul_forw = ulwp->ul_back = NULL;
1030 udp->nzombies--;
1031 ASSERT(ulwp->ul_dead && !ulwp->ul_detached &&
1032 !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON)));
1034 * We can't call ulwp_unlock(ulwp) after we set
1035 * ulwp->ul_ix = -1 so we have to get a pointer to the
1036 * ulwp's hash table mutex now in order to unlock it below.
1038 mp = ulwp_mutex(ulwp, udp);
1039 ulwp->ul_lwpid = (lwpid_t)(-1);
1040 ulwp->ul_ix = -1;
1041 rval = ulwp->ul_rval;
1042 replace = ulwp->ul_replace;
1043 lmutex_unlock(mp);
1044 if (replace) {
1045 ulwp->ul_next = NULL;
1046 if (udp->ulwp_replace_free == NULL)
1047 udp->ulwp_replace_free =
1048 udp->ulwp_replace_last = ulwp;
1049 else {
1050 udp->ulwp_replace_last->ul_next = ulwp;
1051 udp->ulwp_replace_last = ulwp;
1054 lmutex_unlock(&udp->link_lock);
1057 if (departed != NULL)
1058 *departed = found;
1059 if (status != NULL)
1060 *status = rval;
1061 return (0);
1065 thr_join(thread_t tid, thread_t *departed, void **status)
1067 int error = _thrp_join(tid, departed, status, 1);
1068 return ((error == EINVAL)? ESRCH : error);
1072 * pthread_join() differs from Solaris thr_join():
1073 * It does not return the departed thread's id
1074 * and hence does not have a "departed" argument.
1075 * It returns EINVAL if tid refers to a detached thread.
1077 #pragma weak _pthread_join = pthread_join
1079 pthread_join(pthread_t tid, void **status)
1081 return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
1085 pthread_detach(pthread_t tid)
1087 uberdata_t *udp = curthread->ul_uberdata;
1088 ulwp_t *ulwp;
1089 ulwp_t **ulwpp;
1090 int error = 0;
1092 if ((ulwpp = find_lwpp(tid)) == NULL)
1093 return (ESRCH);
1094 ulwp = *ulwpp;
1096 if (ulwp->ul_dead) {
1097 ulwp_unlock(ulwp, udp);
1098 error = _thrp_join(tid, NULL, NULL, 0);
1099 } else {
1100 error = __lwp_detach(tid);
1101 ulwp->ul_detached = 1;
1102 ulwp->ul_usropts |= THR_DETACHED;
1103 ulwp_unlock(ulwp, udp);
1105 return (error);
1108 static const char *
1109 ematch(const char *ev, const char *match)
1111 int c;
1113 while ((c = *match++) != '\0') {
1114 if (*ev++ != c)
1115 return (NULL);
1117 if (*ev++ != '=')
1118 return (NULL);
1119 return (ev);
1122 static int
1123 envvar(const char *ev, const char *match, int limit)
1125 int val = -1;
1126 const char *ename;
1128 if ((ename = ematch(ev, match)) != NULL) {
1129 int c;
1130 for (val = 0; (c = *ename) != '\0'; ename++) {
1131 if (!isdigit(c)) {
1132 val = -1;
1133 break;
1135 val = val * 10 + (c - '0');
1136 if (val > limit) {
1137 val = limit;
1138 break;
1142 return (val);
1145 static void
1146 etest(const char *ev)
1148 int value;
1150 if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0)
1151 thread_queue_spin = value;
1152 if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0)
1153 thread_adaptive_spin = value;
1154 if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0)
1155 thread_max_spinners = value;
1156 if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0)
1157 thread_queue_fifo = value;
1158 #if defined(THREAD_DEBUG)
1159 if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0)
1160 thread_queue_verify = value;
1161 if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0)
1162 thread_queue_dump = value;
1163 #endif
1164 if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0)
1165 thread_stack_cache = value;
1166 if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0)
1167 thread_cond_wait_defer = value;
1168 if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0)
1169 thread_error_detection = value;
1170 if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0)
1171 thread_async_safe = value;
1172 if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0)
1173 thread_door_noreserve = value;
1174 if ((value = envvar(ev, "LOCKS_MISALIGNED", 1)) >= 0)
1175 thread_locks_misaligned = value;
1179 * Look for and evaluate environment variables of the form "_THREAD_*".
1180 * For compatibility with the past, we also look for environment
1181 * names of the form "LIBTHREAD_*".
1183 static void
1184 set_thread_vars()
1186 extern const char **_environ;
1187 const char **pev;
1188 const char *ev;
1189 char c;
1191 if ((pev = _environ) == NULL)
1192 return;
1193 while ((ev = *pev++) != NULL) {
1194 c = *ev;
1195 if (c == '_' && strncmp(ev, "_THREAD_", 8) == 0)
1196 etest(ev + 8);
1197 if (c == 'L' && strncmp(ev, "LIBTHREAD_", 10) == 0)
1198 etest(ev + 10);
1202 /* PROBE_SUPPORT begin */
1203 #pragma weak __tnf_probe_notify
1204 extern void __tnf_probe_notify(void);
1205 /* PROBE_SUPPORT end */
1207 /* same as atexit() but private to the library */
1208 extern int _atexit(void (*)(void));
1210 /* same as _cleanup() but private to the library */
1211 extern void __cleanup(void);
1213 extern void atfork_init(void);
1215 #ifdef __amd64
1216 extern void __proc64id(void);
1217 #endif
1219 static void
1220 init_auxv_data(uberdata_t *udp)
1222 Dl_argsinfo_t args;
1224 udp->ub_comm_page = NULL;
1225 if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0)
1226 return;
1228 while (args.dla_auxv->a_type != AT_NULL) {
1229 if (args.dla_auxv->a_type == AT_SUN_COMMPAGE) {
1230 udp->ub_comm_page = args.dla_auxv->a_un.a_ptr;
1232 args.dla_auxv++;
1237 * libc_init() is called by ld.so.1 for library initialization.
1238 * We perform minimal initialization; enough to work with the main thread.
1240 void
1241 libc_init(void)
1243 uberdata_t *udp = &__uberdata;
1244 ulwp_t *oldself = __curthread();
1245 ucontext_t uc;
1246 ulwp_t *self;
1247 struct rlimit rl;
1248 caddr_t data;
1249 size_t tls_size;
1250 int setmask;
1253 * For the initial stage of initialization, we must be careful
1254 * not to call any function that could possibly call _cerror().
1255 * For this purpose, we call only the raw system call wrappers.
1258 #ifdef __amd64
1260 * Gather information about cache layouts for optimized
1261 * AMD and Intel assembler strfoo() and memfoo() functions.
1263 __proc64id();
1264 #endif
1267 * Every libc, regardless of which link map, must register __cleanup().
1269 (void) _atexit(__cleanup);
1272 * Every libc, regardless of link map, needs to go through and check
1273 * its aux vectors. Doing so will indicate whether or not this has
1274 * been given a comm page (to optimize certain system actions).
1276 init_auxv_data(udp);
1279 * We keep our uberdata on one of (a) the first alternate link map
1280 * or (b) the primary link map. We switch to the primary link map
1281 * and stay there once we see it. All intermediate link maps are
1282 * subject to being unloaded at any time.
1284 if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) {
1285 __tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap;
1286 mutex_setup();
1287 atfork_init(); /* every link map needs atfork() processing */
1288 init_progname();
1289 return;
1293 * To establish the main stack information, we have to get our context.
1294 * This is also convenient to use for getting our signal mask.
1296 uc.uc_flags = UC_ALL;
1297 (void) __getcontext(&uc);
1298 ASSERT(uc.uc_link == NULL);
1300 tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
1301 ASSERT(primary_link_map || tls_size == 0);
1302 data = lmalloc(sizeof (ulwp_t) + tls_size);
1303 if (data == NULL)
1304 thr_panic("cannot allocate thread structure for main thread");
1305 /* LINTED pointer cast may result in improper alignment */
1306 self = (ulwp_t *)(data + tls_size);
1307 init_hash_table[0].hash_bucket = self;
1309 self->ul_sigmask = uc.uc_sigmask;
1310 delete_reserved_signals(&self->ul_sigmask);
1312 * Are the old and new sets different?
1313 * (This can happen if we are currently blocking SIGCANCEL.)
1314 * If so, we must explicitly set our signal mask, below.
1316 setmask =
1317 ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) |
1318 (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]) |
1319 (self->ul_sigmask.__sigbits[2] ^ uc.uc_sigmask.__sigbits[2]) |
1320 (self->ul_sigmask.__sigbits[3] ^ uc.uc_sigmask.__sigbits[3]));
1323 self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size;
1324 (void) getrlimit(RLIMIT_STACK, &rl);
1325 self->ul_stksiz = rl.rlim_cur;
1326 self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz);
1328 self->ul_forw = self->ul_back = self;
1329 self->ul_hash = NULL;
1330 self->ul_ix = 0;
1331 self->ul_lwpid = 1; /* _lwp_self() */
1332 self->ul_main = 1;
1333 self->ul_self = self;
1334 self->ul_policy = -1; /* initialize only when needed */
1335 self->ul_pri = 0;
1336 self->ul_cid = 0;
1337 self->ul_rtclassid = -1;
1338 self->ul_uberdata = udp;
1339 if (oldself != NULL) {
1340 int i;
1342 ASSERT(primary_link_map);
1343 ASSERT(oldself->ul_main == 1);
1344 self->ul_stsd = oldself->ul_stsd;
1345 for (i = 0; i < TSD_NFAST; i++)
1346 self->ul_ftsd[i] = oldself->ul_ftsd[i];
1347 self->ul_tls = oldself->ul_tls;
1349 * Retrieve all pointers to uberdata allocated
1350 * while running on previous link maps.
1351 * We would like to do a structure assignment here, but
1352 * gcc turns structure assignments into calls to memcpy(),
1353 * a function exported from libc. We can't call any such
1354 * external functions until we establish curthread, below,
1355 * so we just call our private version of memcpy().
1357 (void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp));
1359 * These items point to global data on the primary link map.
1361 udp->thr_hash_table = init_hash_table;
1362 udp->sigacthandler = sigacthandler;
1363 udp->tdb.tdb_events = tdb_events;
1364 ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt);
1365 ASSERT(udp->lwp_stacks == NULL);
1366 ASSERT(udp->ulwp_freelist == NULL);
1367 ASSERT(udp->ulwp_replace_free == NULL);
1368 ASSERT(udp->hash_size == 1);
1370 udp->all_lwps = self;
1371 udp->ulwp_one = self;
1372 udp->pid = getpid();
1373 udp->nthreads = 1;
1375 * In every link map, tdb_bootstrap points to the same piece of
1376 * allocated memory. When the primary link map is initialized,
1377 * the allocated memory is assigned a pointer to the one true
1378 * uberdata. This allows libc_db to initialize itself regardless
1379 * of which instance of libc it finds in the address space.
1381 if (udp->tdb_bootstrap == NULL)
1382 udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *));
1383 __tdb_bootstrap = udp->tdb_bootstrap;
1384 if (primary_link_map) {
1385 self->ul_primarymap = 1;
1386 udp->primary_map = 1;
1387 *udp->tdb_bootstrap = udp;
1390 * Cancellation can't happen until:
1391 * pthread_cancel() is called
1392 * or:
1393 * another thread is created
1394 * For now, as a single-threaded process, set the flag that tells
1395 * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen.
1397 self->ul_nocancel = 1;
1399 #if defined(__amd64)
1400 (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self);
1401 #elif defined(__i386)
1402 (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self);
1403 #endif /* __i386 || __amd64 */
1404 set_curthread(self); /* redundant on i386 */
1406 * Now curthread is established and it is safe to call any
1407 * function in libc except one that uses thread-local storage.
1409 self->ul_errnop = &errno;
1410 if (oldself != NULL) {
1411 /* tls_size was zero when oldself was allocated */
1412 lfree(oldself, sizeof (ulwp_t));
1414 mutex_setup();
1415 atfork_init();
1416 signal_init();
1419 * If the stack is unlimited, we set the size to zero to disable
1420 * stack checking.
1421 * XXX: Work harder here. Get the stack size from /proc/self/rmap
1423 if (self->ul_stksiz == RLIM_INFINITY) {
1424 self->ul_ustack.ss_sp = (void *)self->ul_stktop;
1425 self->ul_ustack.ss_size = 0;
1426 } else {
1427 self->ul_ustack.ss_sp = self->ul_stk;
1428 self->ul_ustack.ss_size = self->ul_stksiz;
1430 self->ul_ustack.ss_flags = 0;
1431 (void) setustack(&self->ul_ustack);
1434 * Get the variables that affect thread behavior from the environment.
1436 set_thread_vars();
1437 udp->uberflags.uf_thread_error_detection = (char)thread_error_detection;
1438 udp->thread_stack_cache = thread_stack_cache;
1441 * Make per-thread copies of global variables, for speed.
1443 self->ul_queue_fifo = (char)thread_queue_fifo;
1444 self->ul_cond_wait_defer = (char)thread_cond_wait_defer;
1445 self->ul_error_detection = (char)thread_error_detection;
1446 self->ul_async_safe = (char)thread_async_safe;
1447 self->ul_door_noreserve = (char)thread_door_noreserve;
1448 self->ul_misaligned = (char)thread_locks_misaligned;
1449 self->ul_max_spinners = (uint8_t)thread_max_spinners;
1450 self->ul_adaptive_spin = thread_adaptive_spin;
1451 self->ul_queue_spin = thread_queue_spin;
1453 #if defined(__sparc) && !defined(_LP64)
1454 if (self->ul_misaligned) {
1456 * Tell the kernel to fix up ldx/stx instructions that
1457 * refer to non-8-byte aligned data instead of giving
1458 * the process an alignment trap and generating SIGBUS.
1460 * Programs compiled for 32-bit sparc with the Studio SS12
1461 * compiler get this done for them automatically (in _init()).
1462 * We do it here for the benefit of programs compiled with
1463 * other compilers, like gcc.
1465 * This is necessary for the _THREAD_LOCKS_MISALIGNED=1
1466 * environment variable horrible hack to work.
1468 extern void _do_fix_align(void);
1469 _do_fix_align();
1471 #endif
1474 * When we have initialized the primary link map, inform
1475 * the dynamic linker about our interface functions.
1476 * Set up our pointer to the program name.
1478 if (self->ul_primarymap)
1479 _ld_libc((void *)rtld_funcs);
1480 init_progname();
1483 * Defer signals until TLS constructors have been called.
1485 sigoff(self);
1486 tls_setup();
1487 sigon(self);
1488 if (setmask)
1489 (void) restore_signals(self);
1492 * Make private copies of __xpg4 and __xpg6 so libc can test
1493 * them after this point without invoking the dynamic linker.
1495 libc__xpg4 = __xpg4;
1496 libc__xpg6 = __xpg6;
1498 /* PROBE_SUPPORT begin */
1499 if (self->ul_primarymap && __tnf_probe_notify != NULL)
1500 __tnf_probe_notify();
1501 /* PROBE_SUPPORT end */
1503 init_sigev_thread();
1504 init_aio();
1507 * We need to reset __threaded dynamically at runtime, so that
1508 * __threaded can be bound to __threaded outside libc which may not
1509 * have initial value of 1 (without a copy relocation in a.out).
1511 __threaded = 1;
1514 #pragma fini(libc_fini)
1515 void
1516 libc_fini()
1519 * If we are doing fini processing for the instance of libc
1520 * on the first alternate link map (this happens only when
1521 * the dynamic linker rejects a bad audit library), then clear
1522 * __curthread(). We abandon whatever memory was allocated by
1523 * lmalloc() while running on this alternate link-map but we
1524 * don't care (and can't find the memory in any case); we just
1525 * want to protect the application from this bad audit library.
1526 * No fini processing is done by libc in the normal case.
1529 uberdata_t *udp = curthread->ul_uberdata;
1531 if (udp->primary_map == 0 && udp == &__uberdata)
1532 set_curthread(NULL);
1536 * finish_init is called when we are about to become multi-threaded,
1537 * that is, on the first call to thr_create().
1539 void
1540 finish_init()
1542 ulwp_t *self = curthread;
1543 uberdata_t *udp = self->ul_uberdata;
1544 thr_hash_table_t *htp;
1545 void *data;
1546 int i;
1549 * No locks needed here; we are single-threaded on the first call.
1550 * We can be called only after the primary link map has been set up.
1552 ASSERT(self->ul_primarymap);
1553 ASSERT(self == udp->ulwp_one);
1554 ASSERT(!udp->uberflags.uf_mt);
1555 ASSERT(udp->hash_size == 1);
1558 * Initialize self->ul_policy, self->ul_cid, and self->ul_pri.
1560 update_sched(self);
1563 * Allocate the queue_head array if not already allocated.
1565 if (udp->queue_head == NULL)
1566 queue_alloc();
1569 * Now allocate the thread hash table.
1571 if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t),
1572 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0))
1573 == MAP_FAILED)
1574 thr_panic("cannot allocate thread hash table");
1576 udp->thr_hash_table = htp = (thr_hash_table_t *)data;
1577 udp->hash_size = HASHTBLSZ;
1578 udp->hash_mask = HASHTBLSZ - 1;
1580 for (i = 0; i < HASHTBLSZ; i++, htp++) {
1581 htp->hash_lock.mutex_flag = LOCK_INITED;
1582 htp->hash_lock.mutex_magic = MUTEX_MAGIC;
1583 htp->hash_cond.cond_magic = COND_MAGIC;
1585 hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
1588 * Set up the SIGCANCEL handler for threads cancellation.
1590 setup_cancelsig(SIGCANCEL);
1593 * Arrange to do special things on exit --
1594 * - collect queue statistics from all remaining active threads.
1595 * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set.
1596 * - grab assert_lock to ensure that assertion failures
1597 * and a core dump take precedence over _exit().
1598 * (Functions are called in the reverse order of their registration.)
1600 (void) _atexit(grab_assert_lock);
1601 #if defined(THREAD_DEBUG)
1602 (void) _atexit(dump_queue_statistics);
1603 (void) _atexit(collect_queue_statistics);
1604 #endif
1608 * Used only by postfork1_child(), below.
1610 static void
1611 mark_dead_and_buried(ulwp_t *ulwp)
1613 ulwp->ul_dead = 1;
1614 ulwp->ul_lwpid = (lwpid_t)(-1);
1615 ulwp->ul_hash = NULL;
1616 ulwp->ul_ix = -1;
1617 ulwp->ul_schedctl = NULL;
1618 ulwp->ul_schedctl_called = NULL;
1622 * This is called from fork1() in the child.
1623 * Reset our data structures to reflect one lwp.
1625 void
1626 postfork1_child()
1628 ulwp_t *self = curthread;
1629 uberdata_t *udp = self->ul_uberdata;
1630 queue_head_t *qp;
1631 ulwp_t *next;
1632 ulwp_t *ulwp;
1633 int i;
1635 /* daemon threads shouldn't call fork1(), but oh well... */
1636 self->ul_usropts &= ~THR_DAEMON;
1637 udp->nthreads = 1;
1638 udp->ndaemons = 0;
1639 udp->uberflags.uf_mt = 0;
1640 __libc_threaded = 0;
1641 for (i = 0; i < udp->hash_size; i++)
1642 udp->thr_hash_table[i].hash_bucket = NULL;
1643 self->ul_lwpid = _lwp_self();
1644 hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
1647 * Some thread in the parent might have been suspended
1648 * while holding udp->callout_lock or udp->ld_lock.
1649 * Reinitialize the child's copies.
1651 (void) mutex_init(&udp->callout_lock,
1652 USYNC_THREAD | LOCK_RECURSIVE, NULL);
1653 (void) mutex_init(&udp->ld_lock,
1654 USYNC_THREAD | LOCK_RECURSIVE, NULL);
1656 /* no one in the child is on a sleep queue; reinitialize */
1657 if ((qp = udp->queue_head) != NULL) {
1658 (void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t));
1659 for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
1660 qp->qh_type = (i < QHASHSIZE)? MX : CV;
1661 qp->qh_lock.mutex_flag = LOCK_INITED;
1662 qp->qh_lock.mutex_magic = MUTEX_MAGIC;
1663 qp->qh_hlist = &qp->qh_def_root;
1664 #if defined(THREAD_DEBUG)
1665 qp->qh_hlen = 1;
1666 qp->qh_hmax = 1;
1667 #endif
1672 * Do post-fork1 processing for subsystems that need it.
1673 * We need to do this before unmapping all of the abandoned
1674 * threads' stacks, below(), because the post-fork1 actions
1675 * might require access to those stacks.
1677 postfork1_child_sigev_aio();
1678 postfork1_child_sigev_mq();
1679 postfork1_child_sigev_timer();
1680 postfork1_child_aio();
1682 * The above subsystems use thread pools, so this action
1683 * must be performed after those actions.
1685 postfork1_child_tpool();
1688 * All lwps except ourself are gone. Mark them so.
1689 * First mark all of the lwps that have already been freed.
1690 * Then mark and free all of the active lwps except ourself.
1691 * Since we are single-threaded, no locks are required here.
1693 for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next)
1694 mark_dead_and_buried(ulwp);
1695 for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next)
1696 mark_dead_and_buried(ulwp);
1697 for (ulwp = self->ul_forw; ulwp != self; ulwp = next) {
1698 next = ulwp->ul_forw;
1699 ulwp->ul_forw = ulwp->ul_back = NULL;
1700 mark_dead_and_buried(ulwp);
1701 tsd_free(ulwp);
1702 tls_free(ulwp);
1703 rwl_free(ulwp);
1704 heldlock_free(ulwp);
1705 ulwp_free(ulwp);
1707 self->ul_forw = self->ul_back = udp->all_lwps = self;
1708 if (self != udp->ulwp_one)
1709 mark_dead_and_buried(udp->ulwp_one);
1710 if ((ulwp = udp->all_zombies) != NULL) {
1711 ASSERT(udp->nzombies != 0);
1712 do {
1713 next = ulwp->ul_forw;
1714 ulwp->ul_forw = ulwp->ul_back = NULL;
1715 mark_dead_and_buried(ulwp);
1716 udp->nzombies--;
1717 if (ulwp->ul_replace) {
1718 ulwp->ul_next = NULL;
1719 if (udp->ulwp_replace_free == NULL) {
1720 udp->ulwp_replace_free =
1721 udp->ulwp_replace_last = ulwp;
1722 } else {
1723 udp->ulwp_replace_last->ul_next = ulwp;
1724 udp->ulwp_replace_last = ulwp;
1727 } while ((ulwp = next) != udp->all_zombies);
1728 ASSERT(udp->nzombies == 0);
1729 udp->all_zombies = NULL;
1730 udp->nzombies = 0;
1732 trim_stack_cache(0);
1735 lwpid_t
1736 lwp_self(void)
1738 return (curthread->ul_lwpid);
1741 #pragma weak _ti_thr_self = thr_self
1742 #pragma weak pthread_self = thr_self
1743 thread_t
1744 thr_self()
1746 return (curthread->ul_lwpid);
1750 thr_main()
1752 ulwp_t *self = __curthread();
1754 return ((self == NULL)? -1 : self->ul_main);
1758 _thrp_cancelled(void)
1760 return (curthread->ul_rval == PTHREAD_CANCELED);
1764 _thrp_stksegment(ulwp_t *ulwp, stack_t *stk)
1766 stk->ss_sp = (void *)ulwp->ul_stktop;
1767 stk->ss_size = ulwp->ul_stksiz;
1768 stk->ss_flags = 0;
1769 return (0);
1772 #pragma weak _thr_stksegment = thr_stksegment
1774 thr_stksegment(stack_t *stk)
1776 return (_thrp_stksegment(curthread, stk));
1779 void
1780 force_continue(ulwp_t *ulwp)
1782 #if defined(THREAD_DEBUG)
1783 ulwp_t *self = curthread;
1784 uberdata_t *udp = self->ul_uberdata;
1785 #endif
1786 int error;
1787 timespec_t ts;
1789 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
1790 ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
1792 for (;;) {
1793 error = _lwp_continue(ulwp->ul_lwpid);
1794 if (error != 0 && error != EINTR)
1795 break;
1796 error = 0;
1797 if (ulwp->ul_stopping) { /* it is stopping itsself */
1798 ts.tv_sec = 0; /* give it a chance to run */
1799 ts.tv_nsec = 100000; /* 100 usecs or clock tick */
1800 (void) __nanosleep(&ts, NULL);
1802 if (!ulwp->ul_stopping) /* it is running now */
1803 break; /* so we are done */
1805 * It is marked as being in the process of stopping
1806 * itself. Loop around and continue it again.
1807 * It may not have been stopped the first time.
1813 * Suspend an lwp with lwp_suspend(), then move it to a safe point,
1814 * that is, to a point where ul_critical and ul_rtld are both zero.
1815 * On return, the ulwp_lock() is dropped as with ulwp_unlock().
1816 * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry.
1817 * If we have to drop link_lock, we store 1 through link_dropped.
1818 * If the lwp exits before it can be suspended, we return ESRCH.
1821 safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped)
1823 ulwp_t *self = curthread;
1824 uberdata_t *udp = self->ul_uberdata;
1825 cond_t *cvp = ulwp_condvar(ulwp, udp);
1826 mutex_t *mp = ulwp_mutex(ulwp, udp);
1827 thread_t tid = ulwp->ul_lwpid;
1828 int ix = ulwp->ul_ix;
1829 int error = 0;
1831 ASSERT(whystopped == TSTP_REGULAR ||
1832 whystopped == TSTP_MUTATOR ||
1833 whystopped == TSTP_FORK);
1834 ASSERT(ulwp != self);
1835 ASSERT(!ulwp->ul_stop);
1836 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
1837 ASSERT(MUTEX_OWNED(mp, self));
1839 if (link_dropped != NULL)
1840 *link_dropped = 0;
1843 * We must grab the target's spin lock before suspending it.
1844 * See the comments below and in _thrp_suspend() for why.
1846 spin_lock_set(&ulwp->ul_spinlock);
1847 (void) ___lwp_suspend(tid);
1848 spin_lock_clear(&ulwp->ul_spinlock);
1850 top:
1851 if ((ulwp->ul_critical == 0 && ulwp->ul_rtld == 0) ||
1852 ulwp->ul_stopping) {
1853 /* thread is already safe */
1854 ulwp->ul_stop |= whystopped;
1855 } else {
1857 * Setting ul_pleasestop causes the target thread to stop
1858 * itself in _thrp_suspend(), below, after we drop its lock.
1859 * We must continue the critical thread before dropping
1860 * link_lock because the critical thread may be holding
1861 * the queue lock for link_lock. This is delicate.
1863 ulwp->ul_pleasestop |= whystopped;
1864 force_continue(ulwp);
1865 if (link_dropped != NULL) {
1866 *link_dropped = 1;
1867 lmutex_unlock(&udp->link_lock);
1868 /* be sure to drop link_lock only once */
1869 link_dropped = NULL;
1873 * The thread may disappear by calling thr_exit() so we
1874 * cannot rely on the ulwp pointer after dropping the lock.
1875 * Instead, we search the hash table to find it again.
1876 * When we return, we may find that the thread has been
1877 * continued by some other thread. The suspend/continue
1878 * interfaces are prone to such race conditions by design.
1880 while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop &&
1881 (ulwp->ul_pleasestop & whystopped)) {
1882 (void) __cond_wait(cvp, mp);
1883 for (ulwp = udp->thr_hash_table[ix].hash_bucket;
1884 ulwp != NULL; ulwp = ulwp->ul_hash) {
1885 if (ulwp->ul_lwpid == tid)
1886 break;
1890 if (ulwp == NULL || ulwp->ul_dead)
1891 error = ESRCH;
1892 else {
1894 * Do another lwp_suspend() to make sure we don't
1895 * return until the target thread is fully stopped
1896 * in the kernel. Don't apply lwp_suspend() until
1897 * we know that the target is not holding any
1898 * queue locks, that is, that it has completed
1899 * ulwp_unlock(self) and has, or at least is
1900 * about to, call lwp_suspend() on itself. We do
1901 * this by grabbing the target's spin lock.
1903 ASSERT(ulwp->ul_lwpid == tid);
1904 spin_lock_set(&ulwp->ul_spinlock);
1905 (void) ___lwp_suspend(tid);
1906 spin_lock_clear(&ulwp->ul_spinlock);
1908 * If some other thread did a thr_continue()
1909 * on the target thread we have to start over.
1911 if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped))
1912 goto top;
1916 (void) cond_broadcast(cvp);
1917 lmutex_unlock(mp);
1918 return (error);
1922 _thrp_suspend(thread_t tid, uchar_t whystopped)
1924 ulwp_t *self = curthread;
1925 uberdata_t *udp = self->ul_uberdata;
1926 ulwp_t *ulwp;
1927 int error = 0;
1929 ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0);
1930 ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0);
1933 * We can't suspend anyone except ourself while
1934 * some other thread is performing a fork.
1935 * This also allows only one suspension at a time.
1937 if (tid != self->ul_lwpid)
1938 fork_lock_enter();
1940 if ((ulwp = find_lwp(tid)) == NULL)
1941 error = ESRCH;
1942 else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) {
1943 ulwp_unlock(ulwp, udp);
1944 error = EINVAL;
1945 } else if (ulwp->ul_stop) { /* already stopped */
1946 ulwp->ul_stop |= whystopped;
1947 ulwp_broadcast(ulwp);
1948 ulwp_unlock(ulwp, udp);
1949 } else if (ulwp != self) {
1951 * After suspending the other thread, move it out of a
1952 * critical section and deal with the schedctl mappings.
1953 * safe_suspend() suspends the other thread, calls
1954 * ulwp_broadcast(ulwp) and drops the ulwp lock.
1956 error = safe_suspend(ulwp, whystopped, NULL);
1957 } else {
1958 int schedctl_after_fork = 0;
1961 * We are suspending ourself. We must not take a signal
1962 * until we return from lwp_suspend() and clear ul_stopping.
1963 * This is to guard against siglongjmp().
1965 enter_critical(self);
1966 self->ul_sp = stkptr();
1967 _flush_windows(); /* sparc */
1968 self->ul_pleasestop = 0;
1969 self->ul_stop |= whystopped;
1971 * Grab our spin lock before dropping ulwp_mutex(self).
1972 * This prevents the suspending thread from applying
1973 * lwp_suspend() to us before we emerge from
1974 * lmutex_unlock(mp) and have dropped mp's queue lock.
1976 spin_lock_set(&self->ul_spinlock);
1977 self->ul_stopping = 1;
1978 ulwp_broadcast(self);
1979 ulwp_unlock(self, udp);
1981 * From this point until we return from lwp_suspend(),
1982 * we must not call any function that might invoke the
1983 * dynamic linker, that is, we can only call functions
1984 * private to the library.
1986 * Also, this is a nasty race condition for a process
1987 * that is undergoing a forkall() operation:
1988 * Once we clear our spinlock (below), we are vulnerable
1989 * to being suspended by the forkall() thread before
1990 * we manage to suspend ourself in ___lwp_suspend().
1991 * See safe_suspend() and force_continue().
1993 * To avoid a SIGSEGV due to the disappearance
1994 * of the schedctl mappings in the child process,
1995 * which can happen in spin_lock_clear() if we
1996 * are suspended while we are in the middle of
1997 * its call to preempt(), we preemptively clear
1998 * our own schedctl pointer before dropping our
1999 * spinlock. We reinstate it, in both the parent
2000 * and (if this really is a forkall()) the child.
2002 if (whystopped & TSTP_FORK) {
2003 schedctl_after_fork = 1;
2004 self->ul_schedctl = NULL;
2005 self->ul_schedctl_called = &udp->uberflags;
2007 spin_lock_clear(&self->ul_spinlock);
2008 (void) ___lwp_suspend(tid);
2010 * Somebody else continued us.
2011 * We can't grab ulwp_lock(self)
2012 * until after clearing ul_stopping.
2013 * force_continue() relies on this.
2015 self->ul_stopping = 0;
2016 self->ul_sp = 0;
2017 if (schedctl_after_fork) {
2018 self->ul_schedctl_called = NULL;
2019 self->ul_schedctl = NULL;
2020 (void) setup_schedctl();
2022 ulwp_lock(self, udp);
2023 ulwp_broadcast(self);
2024 ulwp_unlock(self, udp);
2025 exit_critical(self);
2028 if (tid != self->ul_lwpid)
2029 fork_lock_exit();
2031 return (error);
2035 * Suspend all lwps other than ourself in preparation for fork.
2037 void
2038 suspend_fork()
2040 ulwp_t *self = curthread;
2041 uberdata_t *udp = self->ul_uberdata;
2042 ulwp_t *ulwp;
2043 int link_dropped;
2045 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
2046 top:
2047 lmutex_lock(&udp->link_lock);
2049 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2050 ulwp_lock(ulwp, udp);
2051 if (ulwp->ul_stop) { /* already stopped */
2052 ulwp->ul_stop |= TSTP_FORK;
2053 ulwp_broadcast(ulwp);
2054 ulwp_unlock(ulwp, udp);
2055 } else {
2057 * Move the stopped lwp out of a critical section.
2059 if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) ||
2060 link_dropped)
2061 goto top;
2065 lmutex_unlock(&udp->link_lock);
2068 void
2069 continue_fork(int child)
2071 ulwp_t *self = curthread;
2072 uberdata_t *udp = self->ul_uberdata;
2073 ulwp_t *ulwp;
2075 ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
2078 * Clear the schedctl pointers in the child of forkall().
2080 if (child) {
2081 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2082 ulwp->ul_schedctl_called =
2083 ulwp->ul_dead? &udp->uberflags : NULL;
2084 ulwp->ul_schedctl = NULL;
2089 * Set all lwps that were stopped for fork() running again.
2091 lmutex_lock(&udp->link_lock);
2092 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2093 mutex_t *mp = ulwp_mutex(ulwp, udp);
2094 lmutex_lock(mp);
2095 ASSERT(ulwp->ul_stop & TSTP_FORK);
2096 ulwp->ul_stop &= ~TSTP_FORK;
2097 ulwp_broadcast(ulwp);
2098 if (!ulwp->ul_stop)
2099 force_continue(ulwp);
2100 lmutex_unlock(mp);
2102 lmutex_unlock(&udp->link_lock);
2106 _thrp_continue(thread_t tid, uchar_t whystopped)
2108 uberdata_t *udp = curthread->ul_uberdata;
2109 ulwp_t *ulwp;
2110 mutex_t *mp;
2111 int error = 0;
2113 ASSERT(whystopped == TSTP_REGULAR ||
2114 whystopped == TSTP_MUTATOR);
2117 * We single-thread the entire thread suspend/continue mechanism.
2119 fork_lock_enter();
2121 if ((ulwp = find_lwp(tid)) == NULL) {
2122 fork_lock_exit();
2123 return (ESRCH);
2126 mp = ulwp_mutex(ulwp, udp);
2127 if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) {
2128 error = EINVAL;
2129 } else if (ulwp->ul_stop & whystopped) {
2130 ulwp->ul_stop &= ~whystopped;
2131 ulwp_broadcast(ulwp);
2132 if (!ulwp->ul_stop) {
2133 if (whystopped == TSTP_REGULAR && ulwp->ul_created) {
2134 ulwp->ul_sp = 0;
2135 ulwp->ul_created = 0;
2137 force_continue(ulwp);
2140 lmutex_unlock(mp);
2142 fork_lock_exit();
2143 return (error);
2147 thr_suspend(thread_t tid)
2149 return (_thrp_suspend(tid, TSTP_REGULAR));
2153 thr_continue(thread_t tid)
2155 return (_thrp_continue(tid, TSTP_REGULAR));
2158 void
2159 thr_yield()
2161 yield();
2164 #pragma weak pthread_kill = thr_kill
2165 #pragma weak _thr_kill = thr_kill
2167 thr_kill(thread_t tid, int sig)
2169 if (sig == SIGCANCEL)
2170 return (EINVAL);
2171 return (_lwp_kill(tid, sig));
2175 * Exit a critical section, take deferred actions if necessary.
2176 * Called from exit_critical() and from sigon().
2178 void
2179 do_exit_critical()
2181 ulwp_t *self = curthread;
2182 int sig;
2184 ASSERT(self->ul_critical == 0);
2187 * Don't suspend ourself or take a deferred signal while dying
2188 * or while executing inside the dynamic linker (ld.so.1).
2190 if (self->ul_dead || self->ul_rtld)
2191 return;
2193 while (self->ul_pleasestop ||
2194 (self->ul_cursig != 0 && self->ul_sigdefer == 0)) {
2196 * Avoid a recursive call to exit_critical() in _thrp_suspend()
2197 * by keeping self->ul_critical == 1 here.
2199 self->ul_critical++;
2200 while (self->ul_pleasestop) {
2202 * Guard against suspending ourself while on a sleep
2203 * queue. See the comments in call_user_handler().
2205 unsleep_self();
2206 set_parking_flag(self, 0);
2207 (void) _thrp_suspend(self->ul_lwpid,
2208 self->ul_pleasestop);
2210 self->ul_critical--;
2212 if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) {
2214 * Clear ul_cursig before proceeding.
2215 * This protects us from the dynamic linker's
2216 * calls to bind_guard()/bind_clear() in the
2217 * event that it is invoked to resolve a symbol
2218 * like take_deferred_signal() below.
2220 self->ul_cursig = 0;
2221 take_deferred_signal(sig);
2222 ASSERT(self->ul_cursig == 0);
2225 ASSERT(self->ul_critical == 0);
2229 * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker
2230 * (ld.so.1) when it has do do something, like resolve a symbol to be called
2231 * by the application or one of its libraries. _ti_bind_guard() is called
2232 * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the
2233 * application. The dynamic linker gets special dispensation from libc to
2234 * run in a critical region (all signals deferred and no thread suspension
2235 * or forking allowed), and to be immune from cancellation for the duration.
2238 _ti_bind_guard(int flags)
2240 ulwp_t *self = curthread;
2241 uberdata_t *udp = self->ul_uberdata;
2242 int bindflag = (flags & THR_FLG_RTLD);
2244 if ((self->ul_bindflags & bindflag) == bindflag)
2245 return (0);
2246 self->ul_bindflags |= bindflag;
2247 if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
2248 sigoff(self); /* see no signals while holding ld_lock */
2249 self->ul_rtld++; /* don't suspend while in ld.so.1 */
2250 (void) mutex_lock(&udp->ld_lock);
2252 enter_critical(self);
2253 self->ul_save_state = self->ul_cancel_disabled;
2254 self->ul_cancel_disabled = 1;
2255 set_cancel_pending_flag(self, 0);
2256 return (1);
2260 _ti_bind_clear(int flags)
2262 ulwp_t *self = curthread;
2263 uberdata_t *udp = self->ul_uberdata;
2264 int bindflag = (flags & THR_FLG_RTLD);
2266 if ((self->ul_bindflags & bindflag) == 0)
2267 return (self->ul_bindflags);
2268 self->ul_bindflags &= ~bindflag;
2269 self->ul_cancel_disabled = self->ul_save_state;
2270 set_cancel_pending_flag(self, 0);
2271 exit_critical(self);
2272 if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
2273 if (MUTEX_OWNED(&udp->ld_lock, self)) {
2274 (void) mutex_unlock(&udp->ld_lock);
2275 self->ul_rtld--;
2276 sigon(self); /* reenable signals */
2279 return (self->ul_bindflags);
2283 * Tell the dynamic linker (ld.so.1) whether or not it was entered from
2284 * a critical region in libc. Return zero if not, else return non-zero.
2287 _ti_critical(void)
2289 ulwp_t *self = curthread;
2290 int level = self->ul_critical;
2292 if ((self->ul_bindflags & THR_FLG_RTLD) == 0 || level == 0)
2293 return (level); /* ld.so.1 hasn't (yet) called enter() */
2294 return (level - 1);
2298 * sigoff() and sigon() enable cond_wait() to behave (optionally) like
2299 * it does in the old libthread (see the comments in cond_wait_queue()).
2300 * Also, signals are deferred at thread startup until TLS constructors
2301 * have all been called, at which time _thrp_setup() calls sigon().
2303 * _sigoff() and _sigon() are external consolidation-private interfaces to
2304 * sigoff() and sigon(), respectively, in libc. These are used in libnsl.
2305 * Also, _sigoff() and _sigon() are called from dbx's run-time checking
2306 * (librtc.so) to defer signals during its critical sections (not to be
2307 * confused with libc critical sections [see exit_critical() above]).
2309 void
2310 _sigoff(void)
2312 ulwp_t *self = curthread;
2314 sigoff(self);
2317 void
2318 _sigon(void)
2320 ulwp_t *self = curthread;
2322 ASSERT(self->ul_sigdefer > 0);
2323 sigon(self);
2327 thr_getconcurrency()
2329 return (thr_concurrency);
2333 pthread_getconcurrency()
2335 return (pthread_concurrency);
2339 thr_setconcurrency(int new_level)
2341 uberdata_t *udp = curthread->ul_uberdata;
2343 if (new_level < 0)
2344 return (EINVAL);
2345 if (new_level > 65536) /* 65536 is totally arbitrary */
2346 return (EAGAIN);
2347 lmutex_lock(&udp->link_lock);
2348 if (new_level > thr_concurrency)
2349 thr_concurrency = new_level;
2350 lmutex_unlock(&udp->link_lock);
2351 return (0);
2355 pthread_setconcurrency(int new_level)
2357 if (new_level < 0)
2358 return (EINVAL);
2359 if (new_level > 65536) /* 65536 is totally arbitrary */
2360 return (EAGAIN);
2361 pthread_concurrency = new_level;
2362 return (0);
2365 size_t
2366 thr_min_stack(void)
2368 return (MINSTACK);
2372 __nthreads(void)
2374 return (curthread->ul_uberdata->nthreads);
2378 * XXX
2379 * The remainder of this file implements the private interfaces to java for
2380 * garbage collection. It is no longer used, at least by java 1.2.
2381 * It can all go away once all old JVMs have disappeared.
2384 int suspendingallmutators; /* when non-zero, suspending all mutators. */
2385 int suspendedallmutators; /* when non-zero, all mutators suspended. */
2386 int mutatorsbarrier; /* when non-zero, mutators barrier imposed. */
2387 mutex_t mutatorslock = DEFAULTMUTEX; /* used to enforce mutators barrier. */
2388 cond_t mutatorscv = DEFAULTCV; /* where non-mutators sleep. */
2391 * Get the available register state for the target thread.
2392 * Return non-volatile registers: TRS_NONVOLATILE
2394 #pragma weak _thr_getstate = thr_getstate
2396 thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs)
2398 ulwp_t *self = curthread;
2399 uberdata_t *udp = self->ul_uberdata;
2400 ulwp_t **ulwpp;
2401 ulwp_t *ulwp;
2402 int error = 0;
2403 int trs_flag = TRS_LWPID;
2405 if (tid == 0 || self->ul_lwpid == tid) {
2406 ulwp = self;
2407 ulwp_lock(ulwp, udp);
2408 } else if ((ulwpp = find_lwpp(tid)) != NULL) {
2409 ulwp = *ulwpp;
2410 } else {
2411 if (flag)
2412 *flag = TRS_INVALID;
2413 return (ESRCH);
2416 if (ulwp->ul_dead) {
2417 trs_flag = TRS_INVALID;
2418 } else if (!ulwp->ul_stop && !suspendedallmutators) {
2419 error = EINVAL;
2420 trs_flag = TRS_INVALID;
2421 } else if (ulwp->ul_stop) {
2422 trs_flag = TRS_NONVOLATILE;
2423 getgregs(ulwp, rs);
2426 if (flag)
2427 *flag = trs_flag;
2428 if (lwp)
2429 *lwp = tid;
2430 if (ss != NULL)
2431 (void) _thrp_stksegment(ulwp, ss);
2433 ulwp_unlock(ulwp, udp);
2434 return (error);
2438 * Set the appropriate register state for the target thread.
2439 * This is not used by java. It exists solely for the MSTC test suite.
2441 #pragma weak _thr_setstate = thr_setstate
2443 thr_setstate(thread_t tid, int flag, gregset_t rs)
2445 uberdata_t *udp = curthread->ul_uberdata;
2446 ulwp_t *ulwp;
2447 int error = 0;
2449 if ((ulwp = find_lwp(tid)) == NULL)
2450 return (ESRCH);
2452 if (!ulwp->ul_stop && !suspendedallmutators)
2453 error = EINVAL;
2454 else if (rs != NULL) {
2455 switch (flag) {
2456 case TRS_NONVOLATILE:
2457 /* do /proc stuff here? */
2458 if (ulwp->ul_stop)
2459 setgregs(ulwp, rs);
2460 else
2461 error = EINVAL;
2462 break;
2463 case TRS_LWPID: /* do /proc stuff here? */
2464 default:
2465 error = EINVAL;
2466 break;
2470 ulwp_unlock(ulwp, udp);
2471 return (error);
2475 getlwpstatus(thread_t tid, struct lwpstatus *sp)
2477 extern ssize_t __pread(int, void *, size_t, off_t);
2478 char buf[100];
2479 int fd;
2481 /* "/proc/self/lwp/%u/lwpstatus" w/o stdio */
2482 (void) strcpy(buf, "/proc/self/lwp/");
2483 ultos((uint64_t)tid, 10, buf + strlen(buf));
2484 (void) strcat(buf, "/lwpstatus");
2485 if ((fd = __open(buf, O_RDONLY, 0)) >= 0) {
2486 while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) {
2487 if (sp->pr_flags & PR_STOPPED) {
2488 (void) __close(fd);
2489 return (0);
2491 yield(); /* give it a chance to stop */
2493 (void) __close(fd);
2495 return (-1);
2499 putlwpregs(thread_t tid, prgregset_t prp)
2501 extern ssize_t __writev(int, const struct iovec *, int);
2502 char buf[100];
2503 int fd;
2504 long dstop_sreg[2];
2505 long run_null[2];
2506 iovec_t iov[3];
2508 /* "/proc/self/lwp/%u/lwpctl" w/o stdio */
2509 (void) strcpy(buf, "/proc/self/lwp/");
2510 ultos((uint64_t)tid, 10, buf + strlen(buf));
2511 (void) strcat(buf, "/lwpctl");
2512 if ((fd = __open(buf, O_WRONLY, 0)) >= 0) {
2513 dstop_sreg[0] = PCDSTOP; /* direct it to stop */
2514 dstop_sreg[1] = PCSREG; /* set the registers */
2515 iov[0].iov_base = (caddr_t)dstop_sreg;
2516 iov[0].iov_len = sizeof (dstop_sreg);
2517 iov[1].iov_base = (caddr_t)prp; /* from the register set */
2518 iov[1].iov_len = sizeof (prgregset_t);
2519 run_null[0] = PCRUN; /* make it runnable again */
2520 run_null[1] = 0;
2521 iov[2].iov_base = (caddr_t)run_null;
2522 iov[2].iov_len = sizeof (run_null);
2523 if (__writev(fd, iov, 3) >= 0) {
2524 (void) __close(fd);
2525 return (0);
2527 (void) __close(fd);
2529 return (-1);
2532 static ulong_t
2533 gettsp_slow(thread_t tid)
2535 char buf[100];
2536 struct lwpstatus status;
2538 if (getlwpstatus(tid, &status) != 0) {
2539 /* "__gettsp(%u): can't read lwpstatus" w/o stdio */
2540 (void) strcpy(buf, "__gettsp(");
2541 ultos((uint64_t)tid, 10, buf + strlen(buf));
2542 (void) strcat(buf, "): can't read lwpstatus");
2543 thr_panic(buf);
2545 return (status.pr_reg[R_SP]);
2548 ulong_t
2549 __gettsp(thread_t tid)
2551 uberdata_t *udp = curthread->ul_uberdata;
2552 ulwp_t *ulwp;
2553 ulong_t result;
2555 if ((ulwp = find_lwp(tid)) == NULL)
2556 return (0);
2558 if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) {
2559 ulwp_unlock(ulwp, udp);
2560 return (result);
2563 result = gettsp_slow(tid);
2564 ulwp_unlock(ulwp, udp);
2565 return (result);
2569 * This tells java stack walkers how to find the ucontext
2570 * structure passed to signal handlers.
2572 #pragma weak _thr_sighndlrinfo = thr_sighndlrinfo
2573 void
2574 thr_sighndlrinfo(void (**func)(), int *funcsize)
2576 *func = &__sighndlr;
2577 *funcsize = (char *)&__sighndlrend - (char *)&__sighndlr;
2581 * Mark a thread a mutator or reset a mutator to being a default,
2582 * non-mutator thread.
2584 #pragma weak _thr_setmutator = thr_setmutator
2586 thr_setmutator(thread_t tid, int enabled)
2588 ulwp_t *self = curthread;
2589 uberdata_t *udp = self->ul_uberdata;
2590 ulwp_t *ulwp;
2591 int error;
2592 int cancel_state;
2594 enabled = enabled? 1 : 0;
2595 top:
2596 if (tid == 0) {
2597 ulwp = self;
2598 ulwp_lock(ulwp, udp);
2599 } else if ((ulwp = find_lwp(tid)) == NULL) {
2600 return (ESRCH);
2604 * The target thread should be the caller itself or a suspended thread.
2605 * This prevents the target from also changing its ul_mutator field.
2607 error = 0;
2608 if (ulwp != self && !ulwp->ul_stop && enabled)
2609 error = EINVAL;
2610 else if (ulwp->ul_mutator != enabled) {
2611 lmutex_lock(&mutatorslock);
2612 if (mutatorsbarrier) {
2613 ulwp_unlock(ulwp, udp);
2614 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
2615 &cancel_state);
2616 while (mutatorsbarrier)
2617 (void) cond_wait(&mutatorscv, &mutatorslock);
2618 (void) pthread_setcancelstate(cancel_state, NULL);
2619 lmutex_unlock(&mutatorslock);
2620 goto top;
2622 ulwp->ul_mutator = enabled;
2623 lmutex_unlock(&mutatorslock);
2626 ulwp_unlock(ulwp, udp);
2627 return (error);
2631 * Establish a barrier against new mutators. Any non-mutator trying
2632 * to become a mutator is suspended until the barrier is removed.
2634 #pragma weak _thr_mutators_barrier = thr_mutators_barrier
2635 void
2636 thr_mutators_barrier(int enabled)
2638 int oldvalue;
2639 int cancel_state;
2641 lmutex_lock(&mutatorslock);
2644 * Wait if trying to set the barrier while it is already set.
2646 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2647 while (mutatorsbarrier && enabled)
2648 (void) cond_wait(&mutatorscv, &mutatorslock);
2649 (void) pthread_setcancelstate(cancel_state, NULL);
2651 oldvalue = mutatorsbarrier;
2652 mutatorsbarrier = enabled;
2654 * Wakeup any blocked non-mutators when barrier is removed.
2656 if (oldvalue && !enabled)
2657 (void) cond_broadcast(&mutatorscv);
2658 lmutex_unlock(&mutatorslock);
2662 * Suspend the set of all mutators except for the caller. The list
2663 * of actively running threads is searched and only the mutators
2664 * in this list are suspended. Actively running non-mutators remain
2665 * running. Any other thread is suspended.
2667 #pragma weak _thr_suspend_allmutators = thr_suspend_allmutators
2669 thr_suspend_allmutators(void)
2671 ulwp_t *self = curthread;
2672 uberdata_t *udp = self->ul_uberdata;
2673 ulwp_t *ulwp;
2674 int link_dropped;
2677 * We single-thread the entire thread suspend/continue mechanism.
2679 fork_lock_enter();
2681 top:
2682 lmutex_lock(&udp->link_lock);
2684 if (suspendingallmutators || suspendedallmutators) {
2685 lmutex_unlock(&udp->link_lock);
2686 fork_lock_exit();
2687 return (EINVAL);
2689 suspendingallmutators = 1;
2691 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2692 ulwp_lock(ulwp, udp);
2693 if (!ulwp->ul_mutator) {
2694 ulwp_unlock(ulwp, udp);
2695 } else if (ulwp->ul_stop) { /* already stopped */
2696 ulwp->ul_stop |= TSTP_MUTATOR;
2697 ulwp_broadcast(ulwp);
2698 ulwp_unlock(ulwp, udp);
2699 } else {
2701 * Move the stopped lwp out of a critical section.
2703 if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) ||
2704 link_dropped) {
2705 suspendingallmutators = 0;
2706 goto top;
2711 suspendedallmutators = 1;
2712 suspendingallmutators = 0;
2713 lmutex_unlock(&udp->link_lock);
2714 fork_lock_exit();
2715 return (0);
2719 * Suspend the target mutator. The caller is permitted to suspend
2720 * itself. If a mutator barrier is enabled, the caller will suspend
2721 * itself as though it had been suspended by thr_suspend_allmutators().
2722 * When the barrier is removed, this thread will be resumed. Any
2723 * suspended mutator, whether suspended by thr_suspend_mutator(), or by
2724 * thr_suspend_allmutators(), can be resumed by thr_continue_mutator().
2726 #pragma weak _thr_suspend_mutator = thr_suspend_mutator
2728 thr_suspend_mutator(thread_t tid)
2730 if (tid == 0)
2731 tid = curthread->ul_lwpid;
2732 return (_thrp_suspend(tid, TSTP_MUTATOR));
2736 * Resume the set of all suspended mutators.
2738 #pragma weak _thr_continue_allmutators = thr_continue_allmutators
2740 thr_continue_allmutators()
2742 ulwp_t *self = curthread;
2743 uberdata_t *udp = self->ul_uberdata;
2744 ulwp_t *ulwp;
2747 * We single-thread the entire thread suspend/continue mechanism.
2749 fork_lock_enter();
2751 lmutex_lock(&udp->link_lock);
2752 if (!suspendedallmutators) {
2753 lmutex_unlock(&udp->link_lock);
2754 fork_lock_exit();
2755 return (EINVAL);
2757 suspendedallmutators = 0;
2759 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2760 mutex_t *mp = ulwp_mutex(ulwp, udp);
2761 lmutex_lock(mp);
2762 if (ulwp->ul_stop & TSTP_MUTATOR) {
2763 ulwp->ul_stop &= ~TSTP_MUTATOR;
2764 ulwp_broadcast(ulwp);
2765 if (!ulwp->ul_stop)
2766 force_continue(ulwp);
2768 lmutex_unlock(mp);
2771 lmutex_unlock(&udp->link_lock);
2772 fork_lock_exit();
2773 return (0);
2777 * Resume a suspended mutator.
2779 #pragma weak _thr_continue_mutator = thr_continue_mutator
2781 thr_continue_mutator(thread_t tid)
2783 return (_thrp_continue(tid, TSTP_MUTATOR));
2786 #pragma weak _thr_wait_mutator = thr_wait_mutator
2788 thr_wait_mutator(thread_t tid, int dontwait)
2790 uberdata_t *udp = curthread->ul_uberdata;
2791 ulwp_t *ulwp;
2792 int cancel_state;
2793 int error = 0;
2795 (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2796 top:
2797 if ((ulwp = find_lwp(tid)) == NULL) {
2798 (void) pthread_setcancelstate(cancel_state, NULL);
2799 return (ESRCH);
2802 if (!ulwp->ul_mutator)
2803 error = EINVAL;
2804 else if (dontwait) {
2805 if (!(ulwp->ul_stop & TSTP_MUTATOR))
2806 error = EWOULDBLOCK;
2807 } else if (!(ulwp->ul_stop & TSTP_MUTATOR)) {
2808 cond_t *cvp = ulwp_condvar(ulwp, udp);
2809 mutex_t *mp = ulwp_mutex(ulwp, udp);
2811 (void) cond_wait(cvp, mp);
2812 (void) lmutex_unlock(mp);
2813 goto top;
2816 ulwp_unlock(ulwp, udp);
2817 (void) pthread_setcancelstate(cancel_state, NULL);
2818 return (error);
2821 /* PROBE_SUPPORT begin */
2823 void
2824 thr_probe_setup(void *data)
2826 curthread->ul_tpdp = data;
2829 static void *
2830 _thread_probe_getfunc()
2832 return (curthread->ul_tpdp);
2835 void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc;
2837 /* ARGSUSED */
2838 void
2839 _resume(ulwp_t *ulwp, caddr_t sp, int dontsave)
2841 /* never called */
2844 /* ARGSUSED */
2845 void
2846 _resume_ret(ulwp_t *oldlwp)
2848 /* never called */
2851 /* PROBE_SUPPORT end */