xref: /illumos-gate/usr/src/lib/libc/port/threads/pthr_rwlock.c (revision bb5e3b2f129cc39517b925419c22f69a378ec023)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999-2003 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 "lint.h"
30 #include "thr_uberdata.h"
31 
32 /*
33  * UNIX98
34  * pthread_rwlockattr_init: allocates the mutex attribute object and
35  * initializes it with the default values.
36  */
37 #pragma weak pthread_rwlockattr_init = _pthread_rwlockattr_init
38 int
39 _pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
40 {
41 	rwlattr_t *ap;
42 
43 	if ((ap = lmalloc(sizeof (rwlattr_t))) == NULL)
44 		return (ENOMEM);
45 	ap->pshared = DEFAULT_TYPE;
46 	attr->__pthread_rwlockattrp = ap;
47 	return (0);
48 }
49 
50 /*
51  * UNIX98
52  * pthread_rwlockattr_destroy: frees the rwlock attribute object and
53  * invalidates it with NULL value.
54  */
55 #pragma weak pthread_rwlockattr_destroy =  _pthread_rwlockattr_destroy
56 int
57 _pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
58 {
59 	if (attr == NULL || attr->__pthread_rwlockattrp == NULL)
60 		return (EINVAL);
61 	lfree(attr->__pthread_rwlockattrp, sizeof (rwlattr_t));
62 	attr->__pthread_rwlockattrp = NULL;
63 	return (0);
64 }
65 
66 /*
67  * UNIX98
68  * pthread_rwlockattr_setpshared: sets the shared attr to PRIVATE or SHARED.
69  */
70 #pragma weak pthread_rwlockattr_setpshared =  _pthread_rwlockattr_setpshared
71 int
72 _pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
73 {
74 	rwlattr_t *ap;
75 
76 	if (attr != NULL && (ap = attr->__pthread_rwlockattrp) != NULL &&
77 	    (pshared == PTHREAD_PROCESS_PRIVATE ||
78 	    pshared == PTHREAD_PROCESS_SHARED)) {
79 		ap->pshared = pshared;
80 		return (0);
81 	}
82 	return (EINVAL);
83 }
84 
85 /*
86  * UNIX98
87  * pthread_rwlockattr_getpshared: gets the shared attr.
88  */
89 #pragma weak pthread_rwlockattr_getpshared =  _pthread_rwlockattr_getpshared
90 int
91 _pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared)
92 {
93 	rwlattr_t *ap;
94 
95 	if (attr != NULL && (ap = attr->__pthread_rwlockattrp) != NULL &&
96 	    pshared != NULL) {
97 		*pshared = ap->pshared;
98 		return (0);
99 	}
100 	return (EINVAL);
101 }
102 
103 /*
104  * UNIX98
105  * pthread_rwlock_init: Initializes the rwlock object. It copies the
106  * pshared attr into type argument and calls rwlock_init().
107  */
108 #pragma weak pthread_rwlock_init = _pthread_rwlock_init
109 int
110 _pthread_rwlock_init(pthread_rwlock_t *rwlock, pthread_rwlockattr_t *attr)
111 {
112 	rwlattr_t *ap;
113 	int type;
114 
115 	if (attr == NULL)
116 		type = DEFAULT_TYPE;
117 	else if ((ap = attr->__pthread_rwlockattrp) != NULL)
118 		type = ap->pshared;
119 	else
120 		return (EINVAL);
121 
122 	return (__rwlock_init((rwlock_t *)rwlock, type, NULL));
123 }
124