xref: /dragonfly/lib/libthread_xu/thread/thr_mutexattr.c (revision cf8046a92768d53e67d2533fb51b137d5506248d)
1 /*
2  * Copyright (c) 1996 Jeffrey Hsu <hsu@freebsd.org>.
3  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include "namespace.h"
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <pthread.h>
37 #include <pthread_np.h>
38 #include "un-namespace.h"
39 
40 #include "thr_private.h"
41 
42 int
_pthread_mutexattr_init(pthread_mutexattr_t * attr)43 _pthread_mutexattr_init(pthread_mutexattr_t *attr)
44 {
45           int ret;
46           pthread_mutexattr_t pattr;
47 
48           pattr = __malloc(sizeof(struct __pthread_mutexattr_s));
49           if (pattr == NULL) {
50                     ret = ENOMEM;
51           } else {
52                     memcpy(pattr, &_pthread_mutexattr_default,
53                            sizeof(struct __pthread_mutexattr_s));
54                     *attr = pattr;
55                     ret = 0;
56           }
57           return (ret);
58 }
59 
60 int
_pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,int kind)61 _pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
62 {
63           int       ret;
64           if (attr == NULL || *attr == NULL) {
65                     errno = EINVAL;
66                     ret = -1;
67           } else {
68                     (*attr)->m_type = kind;
69                     ret = 0;
70           }
71           return(ret);
72 }
73 
74 int
_pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)75 _pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)
76 {
77           int       ret;
78 
79           if (attr == NULL) {
80                     errno = EINVAL;
81                     ret = -1;
82           } else {
83                     ret = attr->m_type;
84           }
85           return (ret);
86 }
87 
88 int
_pthread_mutexattr_settype(pthread_mutexattr_t * attr,int type)89 _pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
90 {
91           int       ret;
92 
93           if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) {
94                     errno = EINVAL;
95                     ret = -1;
96           } else {
97                     (*attr)->m_type = type;
98                     ret = 0;
99           }
100           return (ret);
101 }
102 
103 int
_pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict attr,int * __restrict type)104 _pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict attr,
105     int * __restrict type)
106 {
107           int       ret;
108 
109           if (attr == NULL || *attr == NULL || (*attr)->m_type >=
110               PTHREAD_MUTEX_TYPE_MAX) {
111                     ret = EINVAL;
112           } else {
113                     *type = (*attr)->m_type;
114                     ret = 0;
115           }
116           return (ret);
117 }
118 
119 int
_pthread_mutexattr_destroy(pthread_mutexattr_t * attr)120 _pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
121 {
122           int       ret;
123           if (attr == NULL || *attr == NULL) {
124                     ret = EINVAL;
125           } else {
126                     __free(*attr);
127                     *attr = NULL;
128                     ret = 0;
129           }
130           return (ret);
131 }
132 
133 int
_pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr,int * __restrict pshared)134 _pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict attr,
135     int * __restrict pshared)
136 {
137 
138           if (attr == NULL || *attr == NULL)
139                     return (EINVAL);
140           *pshared = PTHREAD_PROCESS_PRIVATE;
141           return (0);
142 }
143 
144 int
_pthread_mutexattr_setpshared(pthread_mutexattr_t * attr,int pshared)145 _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
146 {
147 
148           if (attr == NULL || *attr == NULL)
149                     return (EINVAL);
150 
151           /* Only PTHREAD_PROCESS_PRIVATE is supported. */
152           if (pshared != PTHREAD_PROCESS_PRIVATE)
153                     return (EINVAL);
154 
155           return (0);
156 }
157 
158 int
_pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict mattr,int * __restrict protocol)159 _pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict mattr,
160     int * __restrict protocol)
161 {
162           int ret = 0;
163 
164           if ((mattr == NULL) || (*mattr == NULL))
165                     ret = EINVAL;
166           else
167                     *protocol = (*mattr)->m_protocol;
168 
169           return (ret);
170 }
171 
172 int
_pthread_mutexattr_setprotocol(pthread_mutexattr_t * mattr,int protocol)173 _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol)
174 {
175           int ret = 0;
176 
177           if ((mattr == NULL) || (*mattr == NULL) ||
178               (protocol < PTHREAD_PRIO_NONE) || (protocol > PTHREAD_PRIO_PROTECT))
179                     ret = EINVAL;
180           else {
181                     (*mattr)->m_protocol = protocol;
182                     (*mattr)->m_ceiling = THR_MUTEX_CEIL_PRIORITY;
183           }
184           return (ret);
185 }
186 
187 int
_pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict mattr,int * __restrict prioceiling)188 _pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict mattr,
189     int * __restrict prioceiling)
190 {
191           int ret = 0;
192 
193           if ((mattr == NULL) || (*mattr == NULL))
194                     ret = EINVAL;
195           else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
196                     ret = EINVAL;
197           else
198                     *prioceiling = (*mattr)->m_ceiling;
199 
200           return (ret);
201 }
202 
203 int
_pthread_mutexattr_setprioceiling(pthread_mutexattr_t * mattr,int prioceiling)204 _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling)
205 {
206           int ret = 0;
207 
208           if ((mattr == NULL) || (*mattr == NULL))
209                     ret = EINVAL;
210           else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
211                     ret = EINVAL;
212           else
213                     (*mattr)->m_ceiling = prioceiling;
214 
215           return (ret);
216 }
217 
218 __strong_reference(_pthread_mutexattr_init, pthread_mutexattr_init);
219 __strong_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
220 __strong_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
221 __strong_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype);
222 __strong_reference(_pthread_mutexattr_settype, pthread_mutexattr_settype);
223 __strong_reference(_pthread_mutexattr_destroy, pthread_mutexattr_destroy);
224 __strong_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared);
225 __strong_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared);
226 __strong_reference(_pthread_mutexattr_getprotocol, pthread_mutexattr_getprotocol);
227 __strong_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol);
228 __strong_reference(_pthread_mutexattr_getprioceiling, pthread_mutexattr_getprioceiling);
229 __strong_reference(_pthread_mutexattr_setprioceiling, pthread_mutexattr_setprioceiling);
230