xref: /illumos-gate/usr/src/lib/libnsl/common/common.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "mt.h"
28 #include <stdlib.h>
29 
30 void *
31 thr_get_storage(pthread_key_t *keyp, size_t size, void (*destructor)(void *))
32 {
33 	void *addr;
34 
35 	if (pthread_key_create_once_np(keyp, destructor) != 0)
36 		return (NULL);
37 	addr = pthread_getspecific(*keyp);
38 	if (addr == NULL && size != 0) {
39 		addr = calloc(1, size);
40 		if (addr != NULL && pthread_setspecific(*keyp, addr) != 0) {
41 			free(addr);
42 			return (NULL);
43 		}
44 	}
45 
46 	return (addr);
47 }
48 
49 /*
50  * sig_mutex_lock() and sig_mutex_unlock() are the same
51  * as mutex_lock() and mutex_unlock() except that all
52  * signals are deferred while the lock is held.  Likewise
53  * for sig_rw_rdlock(), sig_rw_wrlock() and sig_rw_unlock().
54  *
55  * _sigoff() and _sigon() are consolidation-private
56  * interfaces in libc that defer and enable signals.
57  * Calls to these can nest but must be balanced, so
58  * nested calls to these functions work properly.
59  */
60 
61 void
62 sig_mutex_lock(mutex_t *mp)
63 {
64 	_sigoff();
65 	(void) mutex_lock(mp);
66 }
67 
68 void
69 sig_mutex_unlock(mutex_t *mp)
70 {
71 	(void) mutex_unlock(mp);
72 	_sigon();
73 }
74 
75 void
76 sig_rw_rdlock(rwlock_t *rwlp)
77 {
78 	_sigoff();
79 	(void) rw_rdlock(rwlp);
80 }
81 
82 void
83 sig_rw_wrlock(rwlock_t *rwlp)
84 {
85 	_sigoff();
86 	(void) rw_wrlock(rwlp);
87 }
88 
89 void
90 sig_rw_unlock(rwlock_t *rwlp)
91 {
92 	(void) rw_unlock(rwlp);
93 	_sigon();
94 }
95