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