5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2016 by Delphix. All rights reserved.
25 */
26 /*
27 * Copyright 2016 Joyent, Inc.
28 */
29
30 #include "lint.h"
31 #include "thr_uberdata.h"
32 #include <pthread.h>
33 #include <procfs.h>
34 #include <sys/uio.h>
35 #include <ctype.h>
36 #include "libc.h"
37
38 /*
39 * These symbols should not be exported from libc, but
40 * /lib/libm.so.2 references _thr_main. libm needs to be fixed.
41 * Also, some older versions of the Studio compiler/debugger
42 * components reference them. These need to be fixed, too.
43 */
44 #pragma weak _thr_main = thr_main
760 ASSERT(self->ul_sigdefer != 0);
761
762 lmutex_lock(&udp->link_lock);
763 udp->nthreads--;
764 if (self->ul_usropts & THR_NEW_LWP)
765 thr_concurrency--;
766 if (self->ul_usropts & THR_DAEMON)
767 udp->ndaemons--;
768 else if (udp->nthreads == udp->ndaemons) {
769 /*
770 * We are the last non-daemon thread exiting.
771 * Exit the process. We retain our TSD and TLS so
772 * that atexit() application functions can use them.
773 */
774 lmutex_unlock(&udp->link_lock);
775 exit(0);
776 thr_panic("_thrp_exit(): exit(0) returned");
777 }
778 lmutex_unlock(&udp->link_lock);
779
780 tmem_exit(); /* deallocate tmem allocations */
781 tsd_exit(); /* deallocate thread-specific data */
782 tls_exit(); /* deallocate thread-local storage */
783 heldlock_exit(); /* deal with left-over held locks */
784
785 /* block all signals to finish exiting */
786 block_all_signals(self);
787 /* also prevent ourself from being suspended */
788 enter_critical(self);
789 rwl_free(self);
790 lmutex_lock(&udp->link_lock);
791 ulwp_free(self);
792 (void) ulwp_lock(self, udp);
793
794 if (self->ul_mapsiz && !self->ul_detached) {
795 /*
796 * We want to free the stack for reuse but must keep
797 * the ulwp_t struct for the benefit of thr_join().
798 * For this purpose we allocate a replacement ulwp_t.
799 */
800 if ((replace = udp->ulwp_replace_free) == NULL)
801 replace = lmalloc(REPLACEMENT_SIZE);
802 else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
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.
26 */
27 /*
28 * Copyright 2016 Joyent, Inc.
29 */
30
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"
38
39 /*
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.
44 */
45 #pragma weak _thr_main = thr_main
761 ASSERT(self->ul_sigdefer != 0);
762
763 lmutex_lock(&udp->link_lock);
764 udp->nthreads--;
765 if (self->ul_usropts & THR_NEW_LWP)
766 thr_concurrency--;
767 if (self->ul_usropts & THR_DAEMON)
768 udp->ndaemons--;
769 else if (udp->nthreads == udp->ndaemons) {
770 /*
771 * We are the last non-daemon thread exiting.
772 * Exit the process. We retain our TSD and TLS so
773 * that atexit() application functions can use them.
774 */
775 lmutex_unlock(&udp->link_lock);
776 exit(0);
777 thr_panic("_thrp_exit(): exit(0) returned");
778 }
779 lmutex_unlock(&udp->link_lock);
780
781 /*
782 * tsd_exit() may call its destructor free(), thus depending on
783 * tmem, therefore tmem_exit() needs to be called after tsd_exit()
784 * and tls_exit().
785 */
786 tsd_exit(); /* deallocate thread-specific data */
787 tls_exit(); /* deallocate thread-local storage */
788 tmem_exit(); /* deallocate tmem allocations */
789 heldlock_exit(); /* deal with left-over held locks */
790
791 /* block all signals to finish exiting */
792 block_all_signals(self);
793 /* also prevent ourself from being suspended */
794 enter_critical(self);
795 rwl_free(self);
796 lmutex_lock(&udp->link_lock);
797 ulwp_free(self);
798 (void) ulwp_lock(self, udp);
799
800 if (self->ul_mapsiz && !self->ul_detached) {
801 /*
802 * We want to free the stack for reuse but must keep
803 * the ulwp_t struct for the benefit of thr_join().
804 * For this purpose we allocate a replacement ulwp_t.
805 */
806 if ((replace = udp->ulwp_replace_free) == NULL)
807 replace = lmalloc(REPLACEMENT_SIZE);
808 else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
|