xref: /titanic_52/usr/src/lib/libnsl/common/common.c (revision cb6207858a9fcc2feaee22e626912fba281ac969)
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
5*cb620785Sraf  * Common Development and Distribution License (the "License").
6*cb620785Sraf  * 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  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
23*cb620785Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "mt.h"
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate void *
337c478bd9Sstevel@tonic-gate thr_get_storage(pthread_key_t *keyp, size_t size, void (*destructor)(void *))
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate 	void *addr;
367c478bd9Sstevel@tonic-gate 
37*cb620785Sraf 	if (pthread_key_create_once_np(keyp, destructor) != 0)
387c478bd9Sstevel@tonic-gate 		return (NULL);
39*cb620785Sraf 	addr = pthread_getspecific(*keyp);
407c478bd9Sstevel@tonic-gate 	if (addr == NULL && size != 0) {
417c478bd9Sstevel@tonic-gate 		addr = calloc(1, size);
42*cb620785Sraf 		if (addr != NULL && pthread_setspecific(*keyp, addr) != 0) {
437c478bd9Sstevel@tonic-gate 			free(addr);
447c478bd9Sstevel@tonic-gate 			return (NULL);
457c478bd9Sstevel@tonic-gate 		}
467c478bd9Sstevel@tonic-gate 	}
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	return (addr);
497c478bd9Sstevel@tonic-gate }
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * sig_mutex_lock() and sig_mutex_unlock() are the same
537c478bd9Sstevel@tonic-gate  * as mutex_lock() and mutex_unlock() except that all
547c478bd9Sstevel@tonic-gate  * signals are deferred while the lock is held.  Likewise
557c478bd9Sstevel@tonic-gate  * for sig_rw_rdlock(), sig_rw_wrlock() and sig_rw_unlock().
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * _sigoff() and _sigon() are consolidation-private
587c478bd9Sstevel@tonic-gate  * interfaces in libc that defer and enable signals.
597c478bd9Sstevel@tonic-gate  * Calls to these can nest but must be balanced, so
607c478bd9Sstevel@tonic-gate  * nested calls to these functions work properly.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate void
647c478bd9Sstevel@tonic-gate sig_mutex_lock(mutex_t *mp)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	_sigoff();
677c478bd9Sstevel@tonic-gate 	(void) mutex_lock(mp);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate void
717c478bd9Sstevel@tonic-gate sig_mutex_unlock(mutex_t *mp)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(mp);
747c478bd9Sstevel@tonic-gate 	_sigon();
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate void
787c478bd9Sstevel@tonic-gate sig_rw_rdlock(rwlock_t *rwlp)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	_sigoff();
817c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(rwlp);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate void
857c478bd9Sstevel@tonic-gate sig_rw_wrlock(rwlock_t *rwlp)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	_sigoff();
887c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(rwlp);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate void
927c478bd9Sstevel@tonic-gate sig_rw_unlock(rwlock_t *rwlp)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	(void) rw_unlock(rwlp);
957c478bd9Sstevel@tonic-gate 	_sigon();
967c478bd9Sstevel@tonic-gate }
97