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