xref: /illumos-gate/usr/src/lib/libnsl/common/common.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
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 #include "mt.h"
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate void *
thr_get_storage(pthread_key_t * keyp,size_t size,void (* destructor)(void *))317c478bd9Sstevel@tonic-gate thr_get_storage(pthread_key_t *keyp, size_t size, void (*destructor)(void *))
327c478bd9Sstevel@tonic-gate {
337c478bd9Sstevel@tonic-gate 	void *addr;
347c478bd9Sstevel@tonic-gate 
35*cb620785Sraf 	if (pthread_key_create_once_np(keyp, destructor) != 0)
36*cb620785Sraf 		return (NULL);
37*cb620785Sraf 	addr = pthread_getspecific(*keyp);
387c478bd9Sstevel@tonic-gate 	if (addr == NULL && size != 0) {
397c478bd9Sstevel@tonic-gate 		addr = calloc(1, size);
40*cb620785Sraf 		if (addr != NULL && pthread_setspecific(*keyp, addr) != 0) {
417c478bd9Sstevel@tonic-gate 			free(addr);
427c478bd9Sstevel@tonic-gate 			return (NULL);
437c478bd9Sstevel@tonic-gate 		}
447c478bd9Sstevel@tonic-gate 	}
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	return (addr);
477c478bd9Sstevel@tonic-gate }
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * sig_mutex_lock() and sig_mutex_unlock() are the same
517c478bd9Sstevel@tonic-gate  * as mutex_lock() and mutex_unlock() except that all
527c478bd9Sstevel@tonic-gate  * signals are deferred while the lock is held.  Likewise
537c478bd9Sstevel@tonic-gate  * for sig_rw_rdlock(), sig_rw_wrlock() and sig_rw_unlock().
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * _sigoff() and _sigon() are consolidation-private
567c478bd9Sstevel@tonic-gate  * interfaces in libc that defer and enable signals.
577c478bd9Sstevel@tonic-gate  * Calls to these can nest but must be balanced, so
587c478bd9Sstevel@tonic-gate  * nested calls to these functions work properly.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate void
sig_mutex_lock(mutex_t * mp)627c478bd9Sstevel@tonic-gate sig_mutex_lock(mutex_t *mp)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	_sigoff();
657c478bd9Sstevel@tonic-gate 	(void) mutex_lock(mp);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
sig_mutex_unlock(mutex_t * mp)697c478bd9Sstevel@tonic-gate sig_mutex_unlock(mutex_t *mp)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(mp);
727c478bd9Sstevel@tonic-gate 	_sigon();
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate void
sig_rw_rdlock(rwlock_t * rwlp)767c478bd9Sstevel@tonic-gate sig_rw_rdlock(rwlock_t *rwlp)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	_sigoff();
797c478bd9Sstevel@tonic-gate 	(void) rw_rdlock(rwlp);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
sig_rw_wrlock(rwlock_t * rwlp)837c478bd9Sstevel@tonic-gate sig_rw_wrlock(rwlock_t *rwlp)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	_sigoff();
867c478bd9Sstevel@tonic-gate 	(void) rw_wrlock(rwlp);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate void
sig_rw_unlock(rwlock_t * rwlp)907c478bd9Sstevel@tonic-gate sig_rw_unlock(rwlock_t *rwlp)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	(void) rw_unlock(rwlp);
937c478bd9Sstevel@tonic-gate 	_sigon();
947c478bd9Sstevel@tonic-gate }
95