xref: /illumos-gate/usr/src/lib/libc/port/gen/sh_locks.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52333b8a2Sraf  * Common Development and Distribution License (the "License").
62333b8a2Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212333b8a2Sraf 
227c478bd9Sstevel@tonic-gate /*
238cd45542Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*7257d1b4Sraf #include "lint.h"
287c478bd9Sstevel@tonic-gate #include <mtlib.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <synch.h>
317c478bd9Sstevel@tonic-gate #include <thread.h>
328cd45542Sraf #include <pthread.h>
337c478bd9Sstevel@tonic-gate #include "libc.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * fork1-safety.
377c478bd9Sstevel@tonic-gate  * These three routines were used to make some libc interfaces fork1-safe.
387c478bd9Sstevel@tonic-gate  * With the merge of libthread into libc, almost all libc internal-only
397c478bd9Sstevel@tonic-gate  * locks are acquired via lmutex_lock() or lrw_rdlock()/lrw_wrlock(),
407c478bd9Sstevel@tonic-gate  * which makes them automatically fork1-safe (as well as async-signal
417c478bd9Sstevel@tonic-gate  * safe), so these functions are now used only for the locks that cannot
427c478bd9Sstevel@tonic-gate  * be used with lmutex_lock or lrw_rdlock()/lrw_wrlock().
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern void atexit_locks(void);
467c478bd9Sstevel@tonic-gate extern void atexit_unlocks(void);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate extern void stdio_locks(void);
497c478bd9Sstevel@tonic-gate extern void stdio_unlocks(void);
507c478bd9Sstevel@tonic-gate 
512333b8a2Sraf extern void malloc_locks(void);
522333b8a2Sraf extern void malloc_unlocks(void);
532333b8a2Sraf 
547c478bd9Sstevel@tonic-gate static void
libc_prepare_atfork(void)557c478bd9Sstevel@tonic-gate libc_prepare_atfork(void)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	atexit_locks();
587c478bd9Sstevel@tonic-gate 	stdio_locks();
592333b8a2Sraf 	malloc_locks();
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void
libc_child_atfork(void)637c478bd9Sstevel@tonic-gate libc_child_atfork(void)
647c478bd9Sstevel@tonic-gate {
652333b8a2Sraf 	malloc_unlocks();
667c478bd9Sstevel@tonic-gate 	stdio_unlocks();
677c478bd9Sstevel@tonic-gate 	atexit_unlocks();
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static void
libc_parent_atfork(void)717c478bd9Sstevel@tonic-gate libc_parent_atfork(void)
727c478bd9Sstevel@tonic-gate {
732333b8a2Sraf 	malloc_unlocks();
747c478bd9Sstevel@tonic-gate 	stdio_unlocks();
757c478bd9Sstevel@tonic-gate 	atexit_unlocks();
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* called from libc_init() */
797c478bd9Sstevel@tonic-gate void
atfork_init(void)807c478bd9Sstevel@tonic-gate atfork_init(void)
817c478bd9Sstevel@tonic-gate {
828cd45542Sraf 	(void) pthread_atfork(libc_prepare_atfork,
838cd45542Sraf 	    libc_parent_atfork, libc_child_atfork);
847c478bd9Sstevel@tonic-gate }
85