xref: /freebsd/lib/libthr/thread/thr_condattr.c (revision 7b7ba7857ce8be0bf6ab905d936d8ba1363e4ec2)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4a091d823SDavid Xu  * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
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 
3237a6356bSDavid Xu #include "namespace.h"
33a091d823SDavid Xu #include <stdlib.h>
34a091d823SDavid Xu #include <string.h>
35a091d823SDavid Xu #include <errno.h>
36a091d823SDavid Xu #include <pthread.h>
3737a6356bSDavid Xu #include "un-namespace.h"
38a091d823SDavid Xu 
39a091d823SDavid Xu #include "thr_private.h"
40a091d823SDavid Xu 
41a091d823SDavid Xu __weak_reference(_pthread_condattr_init, pthread_condattr_init);
42a091d823SDavid Xu __weak_reference(_pthread_condattr_destroy, pthread_condattr_destroy);
43a091d823SDavid Xu __weak_reference(_pthread_condattr_getclock, pthread_condattr_getclock);
44a091d823SDavid Xu __weak_reference(_pthread_condattr_setclock, pthread_condattr_setclock);
45a091d823SDavid Xu __weak_reference(_pthread_condattr_getpshared, pthread_condattr_getpshared);
46a091d823SDavid Xu __weak_reference(_pthread_condattr_setpshared, pthread_condattr_setpshared);
47a091d823SDavid Xu 
48a091d823SDavid Xu int
_pthread_condattr_init(pthread_condattr_t * attr)49a091d823SDavid Xu _pthread_condattr_init(pthread_condattr_t *attr)
50a091d823SDavid Xu {
51a091d823SDavid Xu 	pthread_condattr_t pattr;
52a091d823SDavid Xu 	int ret;
53a091d823SDavid Xu 
54a091d823SDavid Xu 	if ((pattr = (pthread_condattr_t)
55a091d823SDavid Xu 	    malloc(sizeof(struct pthread_cond_attr))) == NULL) {
56a091d823SDavid Xu 		ret = ENOMEM;
57a091d823SDavid Xu 	} else {
58a091d823SDavid Xu 		memcpy(pattr, &_pthread_condattr_default,
59a091d823SDavid Xu 		    sizeof(struct pthread_cond_attr));
60a091d823SDavid Xu 		*attr = pattr;
61a091d823SDavid Xu 		ret = 0;
62a091d823SDavid Xu 	}
63a091d823SDavid Xu 	return (ret);
64a091d823SDavid Xu }
65a091d823SDavid Xu 
66a091d823SDavid Xu int
_pthread_condattr_destroy(pthread_condattr_t * attr)67a091d823SDavid Xu _pthread_condattr_destroy(pthread_condattr_t *attr)
68a091d823SDavid Xu {
69a091d823SDavid Xu 	int	ret;
70a091d823SDavid Xu 
71a091d823SDavid Xu 	if (attr == NULL || *attr == NULL) {
72a091d823SDavid Xu 		ret = EINVAL;
73a091d823SDavid Xu 	} else {
74a091d823SDavid Xu 		free(*attr);
75a091d823SDavid Xu 		*attr = NULL;
76a091d823SDavid Xu 		ret = 0;
77a091d823SDavid Xu 	}
78a091d823SDavid Xu 	return(ret);
79a091d823SDavid Xu }
80a091d823SDavid Xu 
81a091d823SDavid Xu int
_pthread_condattr_getclock(const pthread_condattr_t * __restrict attr,clockid_t * __restrict clock_id)82b6413b6dSPedro F. Giffuni _pthread_condattr_getclock(const pthread_condattr_t * __restrict attr,
83b6413b6dSPedro F. Giffuni     clockid_t * __restrict clock_id)
84a091d823SDavid Xu {
85a091d823SDavid Xu 	if (attr == NULL || *attr == NULL)
86a091d823SDavid Xu 		return (EINVAL);
87a091d823SDavid Xu 	*clock_id = (*attr)->c_clockid;
88a091d823SDavid Xu 	return (0);
89a091d823SDavid Xu }
90a091d823SDavid Xu 
91a091d823SDavid Xu int
_pthread_condattr_setclock(pthread_condattr_t * attr,clockid_t clock_id)929ed8360eSDaniel Eischen _pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
93a091d823SDavid Xu {
94a091d823SDavid Xu 	if (attr == NULL || *attr == NULL)
95a091d823SDavid Xu 		return (EINVAL);
96a091d823SDavid Xu 	if (clock_id != CLOCK_REALTIME &&
97*7b7ba785SNathan Whitehorn 	    clock_id != CLOCK_TAI &&
98a091d823SDavid Xu 	    clock_id != CLOCK_VIRTUAL &&
99a091d823SDavid Xu 	    clock_id != CLOCK_PROF &&
100a091d823SDavid Xu 	    clock_id != CLOCK_MONOTONIC) {
101a091d823SDavid Xu 		return  (EINVAL);
102a091d823SDavid Xu 	}
103a091d823SDavid Xu 	(*attr)->c_clockid = clock_id;
104a091d823SDavid Xu 	return (0);
105a091d823SDavid Xu }
106a091d823SDavid Xu 
107a091d823SDavid Xu int
_pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,int * __restrict pshared)108b6413b6dSPedro F. Giffuni _pthread_condattr_getpshared(const pthread_condattr_t * __restrict attr,
109b6413b6dSPedro F. Giffuni     int * __restrict pshared)
110a091d823SDavid Xu {
1111bdbd705SKonstantin Belousov 
112a091d823SDavid Xu 	if (attr == NULL || *attr == NULL)
113a091d823SDavid Xu 		return (EINVAL);
1141bdbd705SKonstantin Belousov 	*pshared = (*attr)->c_pshared;
115a091d823SDavid Xu 	return (0);
116a091d823SDavid Xu }
117a091d823SDavid Xu 
118a091d823SDavid Xu int
_pthread_condattr_setpshared(pthread_condattr_t * attr,int pshared)119a091d823SDavid Xu _pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
120a091d823SDavid Xu {
121a091d823SDavid Xu 
1221bdbd705SKonstantin Belousov 	if (attr == NULL || *attr == NULL ||
1231bdbd705SKonstantin Belousov 	    (pshared != PTHREAD_PROCESS_PRIVATE &&
1241bdbd705SKonstantin Belousov 	    pshared != PTHREAD_PROCESS_SHARED))
125a091d823SDavid Xu 		return (EINVAL);
1261bdbd705SKonstantin Belousov 	(*attr)->c_pshared = pshared;
127a091d823SDavid Xu 	return (0);
128a091d823SDavid Xu }
129