xref: /freebsd/lib/libthr/thread/thr_rwlockattr.c (revision 7cc1fde083b1fe9d55388045ba3df7d770d4f3bf)
1 /*-
2  * Copyright (c) 1998 Alex Nash
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "namespace.h"
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <pthread.h>
33 #include "un-namespace.h"
34 
35 #include "thr_private.h"
36 
37 __weak_reference(_pthread_rwlockattr_destroy, pthread_rwlockattr_destroy);
38 __weak_reference(_pthread_rwlockattr_getpshared, pthread_rwlockattr_getpshared);
39 __weak_reference(_pthread_rwlockattr_getkind_np, pthread_rwlockattr_getkind_np);
40 __weak_reference(_pthread_rwlockattr_init, pthread_rwlockattr_init);
41 __weak_reference(_pthread_rwlockattr_setpshared, pthread_rwlockattr_setpshared);
42 __weak_reference(_pthread_rwlockattr_setkind_np, pthread_rwlockattr_setkind_np);
43 
44 int
45 _pthread_rwlockattr_destroy(pthread_rwlockattr_t *rwlockattr)
46 {
47 	pthread_rwlockattr_t prwlockattr;
48 
49 	if (rwlockattr == NULL)
50 		return(EINVAL);
51 
52 	prwlockattr = *rwlockattr;
53 
54 	if (prwlockattr == NULL)
55 		return(EINVAL);
56 
57 	free(prwlockattr);
58 
59 	return(0);
60 }
61 
62 int
63 _pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *rwlockattr,
64 	int *pshared)
65 {
66 	*pshared = (*rwlockattr)->pshared;
67 
68 	return(0);
69 }
70 
71 int
72 _pthread_rwlockattr_init(pthread_rwlockattr_t *rwlockattr)
73 {
74 	pthread_rwlockattr_t prwlockattr;
75 
76 	if (rwlockattr == NULL)
77 		return(EINVAL);
78 
79 	prwlockattr = (pthread_rwlockattr_t)
80 		malloc(sizeof(struct pthread_rwlockattr));
81 
82 	if (prwlockattr == NULL)
83 		return(ENOMEM);
84 
85 	prwlockattr->pshared 	= PTHREAD_PROCESS_PRIVATE;
86 	prwlockattr->kind	= PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP;
87 	*rwlockattr		= prwlockattr;
88 
89 	return(0);
90 }
91 
92 int
93 _pthread_rwlockattr_setpshared(pthread_rwlockattr_t *rwlockattr, int pshared)
94 {
95 	/* Only PTHREAD_PROCESS_PRIVATE is supported. */
96 	if (pshared != PTHREAD_PROCESS_PRIVATE)
97 		return(EINVAL);
98 
99 	(*rwlockattr)->pshared = pshared;
100 
101 	return(0);
102 }
103 
104 int
105 _pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, int kind)
106 {
107 	if (kind != PTHREAD_RWLOCK_PREFER_READER_NP &&
108 	    kind != PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP &&
109 	    kind != PTHREAD_RWLOCK_PREFER_WRITER_NP) {
110 		return (EINVAL);
111 	}
112 	(*attr)->kind = kind;
113 	return (0);
114 }
115 
116 int
117 _pthread_rwlockattr_getkind_np(const pthread_rwlockattr_t *attr, int *kind)
118 {
119 	*kind = (*attr)->kind;
120 	return (0);
121 }
122