xref: /illumos-gate/usr/src/lib/libc/port/gen/sh_locks.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
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.
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "lint.h"
28 #include <mtlib.h>
29 #include <sys/types.h>
30 #include <synch.h>
31 #include <thread.h>
32 #include <pthread.h>
33 #include "libc.h"
34 
35 /*
36  * fork1-safety.
37  * These three routines were used to make some libc interfaces fork1-safe.
38  * With the merge of libthread into libc, almost all libc internal-only
39  * locks are acquired via lmutex_lock() or lrw_rdlock()/lrw_wrlock(),
40  * which makes them automatically fork1-safe (as well as async-signal
41  * safe), so these functions are now used only for the locks that cannot
42  * be used with lmutex_lock or lrw_rdlock()/lrw_wrlock().
43  */
44 
45 extern void atexit_locks(void);
46 extern void atexit_unlocks(void);
47 
48 extern void stdio_locks(void);
49 extern void stdio_unlocks(void);
50 
51 extern void malloc_locks(void);
52 extern void malloc_unlocks(void);
53 
54 static void
55 libc_prepare_atfork(void)
56 {
57 	atexit_locks();
58 	stdio_locks();
59 	malloc_locks();
60 }
61 
62 static void
63 libc_child_atfork(void)
64 {
65 	malloc_unlocks();
66 	stdio_unlocks();
67 	atexit_unlocks();
68 }
69 
70 static void
71 libc_parent_atfork(void)
72 {
73 	malloc_unlocks();
74 	stdio_unlocks();
75 	atexit_unlocks();
76 }
77 
78 /* called from libc_init() */
79 void
80 atfork_init(void)
81 {
82 	(void) pthread_atfork(libc_prepare_atfork,
83 	    libc_parent_atfork, libc_child_atfork);
84 }
85