xref: /illumos-gate/usr/src/lib/libc/port/threads/pthr_rwlock.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "lint.h"
28 #include "thr_uberdata.h"
29 
30 /*
31  * UNIX98
32  * pthread_rwlockattr_init: allocates the mutex attribute object and
33  * initializes it with the default values.
34  */
35 int
36 pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
37 {
38 	rwlattr_t *ap;
39 
40 	if ((ap = lmalloc(sizeof (rwlattr_t))) == NULL)
41 		return (ENOMEM);
42 	ap->pshared = PTHREAD_PROCESS_PRIVATE;
43 	attr->__pthread_rwlockattrp = ap;
44 	return (0);
45 }
46 
47 /*
48  * UNIX98
49  * pthread_rwlockattr_destroy: frees the rwlock attribute object and
50  * invalidates it with NULL value.
51  */
52 int
53 pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
54 {
55 	if (attr == NULL || attr->__pthread_rwlockattrp == NULL)
56 		return (EINVAL);
57 	lfree(attr->__pthread_rwlockattrp, sizeof (rwlattr_t));
58 	attr->__pthread_rwlockattrp = NULL;
59 	return (0);
60 }
61 
62 /*
63  * UNIX98
64  * pthread_rwlockattr_setpshared: sets the shared attr to PRIVATE or SHARED.
65  */
66 int
67 pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared)
68 {
69 	rwlattr_t *ap;
70 
71 	if (attr != NULL && (ap = attr->__pthread_rwlockattrp) != NULL &&
72 	    (pshared == PTHREAD_PROCESS_PRIVATE ||
73 	    pshared == PTHREAD_PROCESS_SHARED)) {
74 		ap->pshared = pshared;
75 		return (0);
76 	}
77 	return (EINVAL);
78 }
79 
80 /*
81  * UNIX98
82  * pthread_rwlockattr_getpshared: gets the shared attr.
83  */
84 int
85 pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *attr, int *pshared)
86 {
87 	rwlattr_t *ap;
88 
89 	if (attr != NULL && (ap = attr->__pthread_rwlockattrp) != NULL &&
90 	    pshared != NULL) {
91 		*pshared = ap->pshared;
92 		return (0);
93 	}
94 	return (EINVAL);
95 }
96 
97 /*
98  * UNIX98
99  * pthread_rwlock_init: Initializes the rwlock object. It copies the
100  * pshared attr into type argument and calls rwlock_init().
101  */
102 int
103 pthread_rwlock_init(pthread_rwlock_t *_RESTRICT_KYWD rwlock,
104     const pthread_rwlockattr_t *_RESTRICT_KYWD attr)
105 {
106 	rwlattr_t *ap;
107 	int type;
108 
109 	if (attr == NULL)
110 		type = PTHREAD_PROCESS_PRIVATE;
111 	else if ((ap = attr->__pthread_rwlockattrp) != NULL)
112 		type = ap->pshared;
113 	else
114 		return (EINVAL);
115 
116 	return (rwlock_init((rwlock_t *)rwlock, type, NULL));
117 }
118