xref: /dflybsd-src/lib/libthread_xu/thread/thr_mutexattr.c (revision d4b0d6715086a6d05c21e8e4dd1cfd5da335cf79)
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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by John Birrell.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34 
35 #include "namespace.h"
36 #include <string.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <pthread.h>
40 #include <pthread_np.h>
41 #include "un-namespace.h"
42 
43 #include "thr_private.h"
44 
45 /* Default mutex attributes. */
46 struct pthread_mutex_attr _pthread_mutexattr_default = {
47 	.m_type = PTHREAD_MUTEX_DEFAULT,
48 	.m_protocol = PTHREAD_PRIO_NONE,
49 	.m_ceiling = 0,
50 	.m_flags = 0
51 };
52 
53 int
54 _pthread_mutexattr_init(pthread_mutexattr_t *attr)
55 {
56 	int ret;
57 	pthread_mutexattr_t pattr;
58 
59 	if ((pattr = (pthread_mutexattr_t)
60 	    malloc(sizeof(struct pthread_mutex_attr))) == NULL) {
61 		ret = ENOMEM;
62 	} else {
63 		memcpy(pattr, &_pthread_mutexattr_default,
64 		    sizeof(struct pthread_mutex_attr));
65 		*attr = pattr;
66 		ret = 0;
67 	}
68 	return (ret);
69 }
70 
71 int
72 _pthread_mutexattr_setkind_np(pthread_mutexattr_t *attr, int kind)
73 {
74 	int	ret;
75 	if (attr == NULL || *attr == NULL) {
76 		errno = EINVAL;
77 		ret = -1;
78 	} else {
79 		(*attr)->m_type = kind;
80 		ret = 0;
81 	}
82 	return(ret);
83 }
84 
85 int
86 _pthread_mutexattr_getkind_np(pthread_mutexattr_t attr)
87 {
88 	int	ret;
89 	if (attr == NULL) {
90 		errno = EINVAL;
91 		ret = -1;
92 	} else {
93 		ret = attr->m_type;
94 	}
95 	return(ret);
96 }
97 
98 int
99 _pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
100 {
101 	int	ret;
102 	if (attr == NULL || *attr == NULL || type >= PTHREAD_MUTEX_TYPE_MAX) {
103 		errno = EINVAL;
104 		ret = -1;
105 	} else {
106 		(*attr)->m_type = type;
107 		ret = 0;
108 	}
109 	return(ret);
110 }
111 
112 int
113 _pthread_mutexattr_gettype(pthread_mutexattr_t *attr, int *type)
114 {
115 	int	ret;
116 
117 	if (attr == NULL || *attr == NULL || (*attr)->m_type >=
118 	    PTHREAD_MUTEX_TYPE_MAX) {
119 		ret = EINVAL;
120 	} else {
121 		*type = (*attr)->m_type;
122 		ret = 0;
123 	}
124 	return ret;
125 }
126 
127 int
128 _pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
129 {
130 	int	ret;
131 	if (attr == NULL || *attr == NULL) {
132 		ret = EINVAL;
133 	} else {
134 		free(*attr);
135 		*attr = NULL;
136 		ret = 0;
137 	}
138 	return(ret);
139 }
140 
141 int
142 _pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr,
143 	int *pshared)
144 {
145 
146 	if (attr == NULL || *attr == NULL)
147 		return (EINVAL);
148 
149 	*pshared = PTHREAD_PROCESS_PRIVATE;
150 	return (0);
151 }
152 
153 int
154 _pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
155 {
156 
157 	if (attr == NULL || *attr == NULL)
158 		return (EINVAL);
159 
160 	/* Only PTHREAD_PROCESS_PRIVATE is supported. */
161 	if (pshared != PTHREAD_PROCESS_PRIVATE)
162 		return (EINVAL);
163 
164 	return (0);
165 }
166 
167 __strong_reference(_pthread_mutexattr_init, pthread_mutexattr_init);
168 __strong_reference(_pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np);
169 __strong_reference(_pthread_mutexattr_getkind_np, pthread_mutexattr_getkind_np);
170 __strong_reference(_pthread_mutexattr_gettype, pthread_mutexattr_gettype);
171 __strong_reference(_pthread_mutexattr_settype, pthread_mutexattr_settype);
172 __strong_reference(_pthread_mutexattr_destroy, pthread_mutexattr_destroy);
173 __strong_reference(_pthread_mutexattr_getpshared, pthread_mutexattr_getpshared);
174 __strong_reference(_pthread_mutexattr_setpshared, pthread_mutexattr_setpshared);
175