xref: /titanic_54/usr/src/lib/libnsl/common/common.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*61961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "mt.h"
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate void *
347c478bd9Sstevel@tonic-gate thr_get_storage(pthread_key_t *keyp, size_t size, void (*destructor)(void *))
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate 	extern mutex_t tsd_lock;
377c478bd9Sstevel@tonic-gate 	pthread_key_t key;
387c478bd9Sstevel@tonic-gate 	void *addr;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	if ((key = *keyp) == 0) {
41*61961e0fSrobinson 		(void) mutex_lock(&tsd_lock);
427c478bd9Sstevel@tonic-gate 		if ((key = *keyp) == 0) {
437c478bd9Sstevel@tonic-gate 			if (pthread_key_create(keyp, destructor) != 0) {
44*61961e0fSrobinson 				(void) mutex_unlock(&tsd_lock);
457c478bd9Sstevel@tonic-gate 				return (NULL);
467c478bd9Sstevel@tonic-gate 			}
477c478bd9Sstevel@tonic-gate 			key = *keyp;
487c478bd9Sstevel@tonic-gate 		}
49*61961e0fSrobinson 		(void) mutex_unlock(&tsd_lock);
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	addr = pthread_getspecific(key);
537c478bd9Sstevel@tonic-gate 	if (addr == NULL && size != 0) {
547c478bd9Sstevel@tonic-gate 		addr = calloc(1, size);
557c478bd9Sstevel@tonic-gate 		if (addr != NULL && pthread_setspecific(key, addr) != 0) {
567c478bd9Sstevel@tonic-gate 			free(addr);
577c478bd9Sstevel@tonic-gate 			return (NULL);
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	return (addr);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * sig_mutex_lock() and sig_mutex_unlock() are the same
667c478bd9Sstevel@tonic-gate  * as mutex_lock() and mutex_unlock() except that all
677c478bd9Sstevel@tonic-gate  * signals are deferred while the lock is held.  Likewise
687c478bd9Sstevel@tonic-gate  * for sig_rw_rdlock(), sig_rw_wrlock() and sig_rw_unlock().
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * _sigoff() and _sigon() are consolidation-private
717c478bd9Sstevel@tonic-gate  * interfaces in libc that defer and enable signals.
727c478bd9Sstevel@tonic-gate  * Calls to these can nest but must be balanced, so
737c478bd9Sstevel@tonic-gate  * nested calls to these functions work properly.
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate void
777c478bd9Sstevel@tonic-gate sig_mutex_lock(mutex_t *mp)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	_sigoff();
807c478bd9Sstevel@tonic-gate 	(void) mutex_lock(mp);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate void
847c478bd9Sstevel@tonic-gate sig_mutex_unlock(mutex_t *mp)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(mp);
877c478bd9Sstevel@tonic-gate 	_sigon();
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate void
917c478bd9Sstevel@tonic-gate sig_rw_rdlock(rwlock_t *rwlp)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	_sigoff();
947c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(rwlp);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate void
987c478bd9Sstevel@tonic-gate sig_rw_wrlock(rwlock_t *rwlp)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	_sigoff();
1017c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(rwlp);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate void
1057c478bd9Sstevel@tonic-gate sig_rw_unlock(rwlock_t *rwlp)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	(void) rw_unlock(rwlp);
1087c478bd9Sstevel@tonic-gate 	_sigon();
1097c478bd9Sstevel@tonic-gate }
110