18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
4a091d823SDavid Xu * Copyright (c) 1996 Jeffrey Hsu <hsu@freebsd.org>.
5a091d823SDavid Xu * All rights reserved.
6a091d823SDavid Xu *
7a091d823SDavid Xu * Redistribution and use in source and binary forms, with or without
8a091d823SDavid Xu * modification, are permitted provided that the following conditions
9a091d823SDavid Xu * are met:
10a091d823SDavid Xu * 1. Redistributions of source code must retain the above copyright
11a091d823SDavid Xu * notice, this list of conditions and the following disclaimer.
12a091d823SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright
13a091d823SDavid Xu * notice, this list of conditions and the following disclaimer in the
14a091d823SDavid Xu * documentation and/or other materials provided with the distribution.
15fed32d75SWarner Losh * 3. Neither the name of the author nor the names of any co-contributors
16a091d823SDavid Xu * may be used to endorse or promote products derived from this software
17a091d823SDavid Xu * without specific prior written permission.
18a091d823SDavid Xu *
19a091d823SDavid Xu * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20a091d823SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a091d823SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a091d823SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23a091d823SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a091d823SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a091d823SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a091d823SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a091d823SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a091d823SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a091d823SDavid Xu * SUCH DAMAGE.
30a091d823SDavid Xu */
31a091d823SDavid Xu
32a091d823SDavid Xu /*
33a091d823SDavid Xu * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
34a091d823SDavid Xu * All rights reserved.
35a091d823SDavid Xu *
36a091d823SDavid Xu * Redistribution and use in source and binary forms, with or without
37a091d823SDavid Xu * modification, are permitted provided that the following conditions
38a091d823SDavid Xu * are met:
39a091d823SDavid Xu * 1. Redistributions of source code must retain the above copyright
40a091d823SDavid Xu * notice, this list of conditions and the following disclaimer.
41a091d823SDavid Xu * 2. Redistributions in binary form must reproduce the above copyright
42a091d823SDavid Xu * notice, this list of conditions and the following disclaimer in the
43a091d823SDavid Xu * documentation and/or other materials provided with the distribution.
44fed32d75SWarner Losh * 3. Neither the name of the author nor the names of any co-contributors
45a091d823SDavid Xu * may be used to endorse or promote products derived from this software
46a091d823SDavid Xu * without specific prior written permission.
47a091d823SDavid Xu *
48a091d823SDavid Xu * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
49a091d823SDavid Xu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50a091d823SDavid Xu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51a091d823SDavid Xu * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
52a091d823SDavid Xu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53a091d823SDavid Xu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54a091d823SDavid Xu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55a091d823SDavid Xu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56a091d823SDavid Xu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57a091d823SDavid Xu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58a091d823SDavid Xu * SUCH DAMAGE.
59a091d823SDavid Xu *
60a091d823SDavid Xu */
61a091d823SDavid Xu
6237a6356bSDavid Xu #include "namespace.h"
63a091d823SDavid Xu #include <string.h>
64a091d823SDavid Xu #include <stdlib.h>
65a091d823SDavid Xu #include <errno.h>
66a091d823SDavid Xu #include <pthread.h>
6737a6356bSDavid Xu #include <pthread_np.h>
6837a6356bSDavid Xu #include "un-namespace.h"
6937a6356bSDavid Xu
70a091d823SDavid Xu #include "thr_private.h"
71a091d823SDavid Xu
720ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_init, pthread_mutexattr_init);
730ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_init, _pthread_mutexattr_init);
74a091d823SDavid Xu __weak_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
75a091d823SDavid Xu __weak_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
76a091d823SDavid Xu __weak_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype);
770ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_settype, pthread_mutexattr_settype);
780ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_settype, _pthread_mutexattr_settype);
790ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_destroy, pthread_mutexattr_destroy);
800ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_destroy, _pthread_mutexattr_destroy);
81c3d1b896SDavid Xu __weak_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared);
82c3d1b896SDavid Xu __weak_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared);
839ad4b644SDavid Xu __weak_reference(_pthread_mutexattr_getprotocol, pthread_mutexattr_getprotocol);
849ad4b644SDavid Xu __weak_reference(_pthread_mutexattr_setprotocol, pthread_mutexattr_setprotocol);
852a339d9eSKonstantin Belousov __weak_reference(_pthread_mutexattr_getprioceiling,
862a339d9eSKonstantin Belousov pthread_mutexattr_getprioceiling);
872a339d9eSKonstantin Belousov __weak_reference(_pthread_mutexattr_setprioceiling,
882a339d9eSKonstantin Belousov pthread_mutexattr_setprioceiling);
890ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_getrobust, pthread_mutexattr_getrobust);
900ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_getrobust, _pthread_mutexattr_getrobust);
910ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_setrobust, pthread_mutexattr_setrobust);
920ab1bfc7SKonstantin Belousov __weak_reference(_thr_mutexattr_setrobust, _pthread_mutexattr_setrobust);
93a091d823SDavid Xu
94a091d823SDavid Xu int
_thr_mutexattr_init(pthread_mutexattr_t * attr)950ab1bfc7SKonstantin Belousov _thr_mutexattr_init(pthread_mutexattr_t *attr)
96a091d823SDavid Xu {
97a091d823SDavid Xu int ret;
98a091d823SDavid Xu pthread_mutexattr_t pattr;
99a091d823SDavid Xu
100a091d823SDavid Xu if ((pattr = (pthread_mutexattr_t)
101a091d823SDavid Xu malloc(sizeof(struct pthread_mutex_attr))) == NULL) {
102a091d823SDavid Xu ret = ENOMEM;
103a091d823SDavid Xu } else {
104a091d823SDavid Xu memcpy(pattr, &_pthread_mutexattr_default,
105a091d823SDavid Xu sizeof(struct pthread_mutex_attr));
106a091d823SDavid Xu *attr = pattr;
107a091d823SDavid Xu ret = 0;
108a091d823SDavid Xu }
109a091d823SDavid Xu return (ret);
110a091d823SDavid Xu }
111a091d823SDavid Xu
112a091d823SDavid Xu int
_pthread_mutexattr_setkind_np(pthread_mutexattr_t * attr,int kind)113a091d823SDavid Xu _pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
114a091d823SDavid Xu {
115a091d823SDavid Xu int ret;
116a091d823SDavid Xu if (attr == NULL || *attr == NULL) {
117a091d823SDavid Xu errno = EINVAL;
118a091d823SDavid Xu ret = -1;
119a091d823SDavid Xu } else {
120a091d823SDavid Xu (*attr)->m_type = kind;
121a091d823SDavid Xu ret = 0;
122a091d823SDavid Xu }
123a091d823SDavid Xu return(ret);
124a091d823SDavid Xu }
125a091d823SDavid Xu
126a091d823SDavid Xu int
_pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)127a091d823SDavid Xu _pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)
128a091d823SDavid Xu {
129a091d823SDavid Xu int ret;
1302a339d9eSKonstantin Belousov
131a091d823SDavid Xu if (attr == NULL) {
132a091d823SDavid Xu errno = EINVAL;
133a091d823SDavid Xu ret = -1;
134a091d823SDavid Xu } else {
135a091d823SDavid Xu ret = attr->m_type;
136a091d823SDavid Xu }
137a091d823SDavid Xu return (ret);
138a091d823SDavid Xu }
139a091d823SDavid Xu
140a091d823SDavid Xu int
_thr_mutexattr_settype(pthread_mutexattr_t * attr,int type)1410ab1bfc7SKonstantin Belousov _thr_mutexattr_settype(pthread_mutexattr_t *attr, int type)
142a091d823SDavid Xu {
143a091d823SDavid Xu int ret;
1442a339d9eSKonstantin Belousov
145ad7c4916SStefan Farfeleder if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) {
14648ebe2ebSDavid Xu ret = EINVAL;
147a091d823SDavid Xu } else {
148a091d823SDavid Xu (*attr)->m_type = type;
149a091d823SDavid Xu ret = 0;
150a091d823SDavid Xu }
151a091d823SDavid Xu return (ret);
152a091d823SDavid Xu }
153a091d823SDavid Xu
154a091d823SDavid Xu int
_pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict attr,int * __restrict type)155b6413b6dSPedro F. Giffuni _pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict attr,
156b6413b6dSPedro F. Giffuni int * __restrict type)
157a091d823SDavid Xu {
158a091d823SDavid Xu int ret;
159a091d823SDavid Xu
160a091d823SDavid Xu if (attr == NULL || *attr == NULL || (*attr)->m_type >=
161ad7c4916SStefan Farfeleder PTHREAD_MUTEX_TYPE_MAX) {
162a091d823SDavid Xu ret = EINVAL;
163a091d823SDavid Xu } else {
164a091d823SDavid Xu *type = (*attr)->m_type;
165a091d823SDavid Xu ret = 0;
166a091d823SDavid Xu }
1672a339d9eSKonstantin Belousov return (ret);
168a091d823SDavid Xu }
169a091d823SDavid Xu
170a091d823SDavid Xu int
_thr_mutexattr_destroy(pthread_mutexattr_t * attr)1710ab1bfc7SKonstantin Belousov _thr_mutexattr_destroy(pthread_mutexattr_t *attr)
172a091d823SDavid Xu {
173a091d823SDavid Xu int ret;
174a091d823SDavid Xu if (attr == NULL || *attr == NULL) {
175a091d823SDavid Xu ret = EINVAL;
176a091d823SDavid Xu } else {
177a091d823SDavid Xu free(*attr);
178a091d823SDavid Xu *attr = NULL;
179a091d823SDavid Xu ret = 0;
180a091d823SDavid Xu }
181a1292a02SRuslan Ermilov return (ret);
182a091d823SDavid Xu }
183c3d1b896SDavid Xu
184c3d1b896SDavid Xu int
_pthread_mutexattr_getpshared(const pthread_mutexattr_t * attr,int * pshared)185c3d1b896SDavid Xu _pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
186c3d1b896SDavid Xu int *pshared)
187c3d1b896SDavid Xu {
188c3d1b896SDavid Xu
189c3d1b896SDavid Xu if (attr == NULL || *attr == NULL)
190c3d1b896SDavid Xu return (EINVAL);
1911bdbd705SKonstantin Belousov *pshared = (*attr)->m_pshared;
192c3d1b896SDavid Xu return (0);
193c3d1b896SDavid Xu }
194c3d1b896SDavid Xu
195c3d1b896SDavid Xu int
_pthread_mutexattr_setpshared(pthread_mutexattr_t * attr,int pshared)196c3d1b896SDavid Xu _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
197c3d1b896SDavid Xu {
198c3d1b896SDavid Xu
1991bdbd705SKonstantin Belousov if (attr == NULL || *attr == NULL ||
2001bdbd705SKonstantin Belousov (pshared != PTHREAD_PROCESS_PRIVATE &&
2011bdbd705SKonstantin Belousov pshared != PTHREAD_PROCESS_SHARED))
202c3d1b896SDavid Xu return (EINVAL);
2031bdbd705SKonstantin Belousov (*attr)->m_pshared = pshared;
204c3d1b896SDavid Xu return (0);
205c3d1b896SDavid Xu }
2069ad4b644SDavid Xu
2079ad4b644SDavid Xu int
_pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict mattr,int * __restrict protocol)208b6413b6dSPedro F. Giffuni _pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict mattr,
209b6413b6dSPedro F. Giffuni int * __restrict protocol)
2109ad4b644SDavid Xu {
2119ad4b644SDavid Xu int ret = 0;
2129ad4b644SDavid Xu
2132a339d9eSKonstantin Belousov if (mattr == NULL || *mattr == NULL)
2149ad4b644SDavid Xu ret = EINVAL;
2159ad4b644SDavid Xu else
2169ad4b644SDavid Xu *protocol = (*mattr)->m_protocol;
2179ad4b644SDavid Xu
2189ad4b644SDavid Xu return (ret);
2199ad4b644SDavid Xu }
2209ad4b644SDavid Xu
2219ad4b644SDavid Xu int
_pthread_mutexattr_setprotocol(pthread_mutexattr_t * mattr,int protocol)2229ad4b644SDavid Xu _pthread_mutexattr_setprotocol(pthread_mutexattr_t *mattr, int protocol)
2239ad4b644SDavid Xu {
2249ad4b644SDavid Xu int ret = 0;
2259ad4b644SDavid Xu
2262a339d9eSKonstantin Belousov if (mattr == NULL || *mattr == NULL ||
2272a339d9eSKonstantin Belousov protocol < PTHREAD_PRIO_NONE || protocol > PTHREAD_PRIO_PROTECT)
2289ad4b644SDavid Xu ret = EINVAL;
2299ad4b644SDavid Xu else {
2309ad4b644SDavid Xu (*mattr)->m_protocol = protocol;
231245116caSDavid Xu (*mattr)->m_ceiling = THR_MAX_RR_PRIORITY;
2329ad4b644SDavid Xu }
2339ad4b644SDavid Xu return (ret);
2349ad4b644SDavid Xu }
2359ad4b644SDavid Xu
2369ad4b644SDavid Xu int
_pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict mattr,int * __restrict prioceiling)237b6413b6dSPedro F. Giffuni _pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict mattr,
238b6413b6dSPedro F. Giffuni int * __restrict prioceiling)
2399ad4b644SDavid Xu {
2409ad4b644SDavid Xu int ret = 0;
2419ad4b644SDavid Xu
2422a339d9eSKonstantin Belousov if (mattr == NULL || *mattr == NULL)
2439ad4b644SDavid Xu ret = EINVAL;
2449ad4b644SDavid Xu else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
2459ad4b644SDavid Xu ret = EINVAL;
2469ad4b644SDavid Xu else
2479ad4b644SDavid Xu *prioceiling = (*mattr)->m_ceiling;
2489ad4b644SDavid Xu
2499ad4b644SDavid Xu return (ret);
2509ad4b644SDavid Xu }
2519ad4b644SDavid Xu
2529ad4b644SDavid Xu int
_pthread_mutexattr_setprioceiling(pthread_mutexattr_t * mattr,int prioceiling)2539ad4b644SDavid Xu _pthread_mutexattr_setprioceiling(pthread_mutexattr_t *mattr, int prioceiling)
2549ad4b644SDavid Xu {
2559ad4b644SDavid Xu int ret = 0;
2569ad4b644SDavid Xu
2572a339d9eSKonstantin Belousov if (mattr == NULL || *mattr == NULL)
2589ad4b644SDavid Xu ret = EINVAL;
2599ad4b644SDavid Xu else if ((*mattr)->m_protocol != PTHREAD_PRIO_PROTECT)
2609ad4b644SDavid Xu ret = EINVAL;
2619ad4b644SDavid Xu else
2629ad4b644SDavid Xu (*mattr)->m_ceiling = prioceiling;
2639ad4b644SDavid Xu
2649ad4b644SDavid Xu return (ret);
2659ad4b644SDavid Xu }
2669ad4b644SDavid Xu
2672a339d9eSKonstantin Belousov int
_thr_mutexattr_getrobust(pthread_mutexattr_t * mattr,int * robust)2680ab1bfc7SKonstantin Belousov _thr_mutexattr_getrobust(pthread_mutexattr_t *mattr, int *robust)
2692a339d9eSKonstantin Belousov {
2702a339d9eSKonstantin Belousov int ret;
2712a339d9eSKonstantin Belousov
2722a339d9eSKonstantin Belousov if (mattr == NULL || *mattr == NULL) {
2732a339d9eSKonstantin Belousov ret = EINVAL;
2742a339d9eSKonstantin Belousov } else {
2752a339d9eSKonstantin Belousov ret = 0;
2762a339d9eSKonstantin Belousov *robust = (*mattr)->m_robust;
2772a339d9eSKonstantin Belousov }
2782a339d9eSKonstantin Belousov return (ret);
2792a339d9eSKonstantin Belousov }
2802a339d9eSKonstantin Belousov
2812a339d9eSKonstantin Belousov int
_thr_mutexattr_setrobust(pthread_mutexattr_t * mattr,int robust)2820ab1bfc7SKonstantin Belousov _thr_mutexattr_setrobust(pthread_mutexattr_t *mattr, int robust)
2832a339d9eSKonstantin Belousov {
2842a339d9eSKonstantin Belousov int ret;
2852a339d9eSKonstantin Belousov
2862a339d9eSKonstantin Belousov if (mattr == NULL || *mattr == NULL) {
2872a339d9eSKonstantin Belousov ret = EINVAL;
2882a339d9eSKonstantin Belousov } else if (robust != PTHREAD_MUTEX_STALLED &&
2892a339d9eSKonstantin Belousov robust != PTHREAD_MUTEX_ROBUST) {
2902a339d9eSKonstantin Belousov ret = EINVAL;
2912a339d9eSKonstantin Belousov } else {
2922a339d9eSKonstantin Belousov ret = 0;
2932a339d9eSKonstantin Belousov (*mattr)->m_robust = robust;
2942a339d9eSKonstantin Belousov }
2952a339d9eSKonstantin Belousov return (ret);
2962a339d9eSKonstantin Belousov }
2972a339d9eSKonstantin Belousov
298