xref: /illumos-gate/usr/src/lib/libc/port/threads/pthr_cond.c (revision eb0cc229f19c437a6b538d3ac0d0443268290b7e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "thr_uberdata.h"
31 
32 /*
33  * pthread_condattr_init: allocates the cond attribute object and
34  * initializes it with the default values.
35  */
36 #pragma weak pthread_condattr_init = _pthread_condattr_init
37 int
38 _pthread_condattr_init(pthread_condattr_t *attr)
39 {
40 	cvattr_t *ap;
41 
42 	if ((ap = lmalloc(sizeof (cvattr_t))) == NULL)
43 		return (ENOMEM);
44 	ap->pshared = DEFAULT_TYPE;
45 	ap->clockid = CLOCK_REALTIME;
46 	attr->__pthread_condattrp = ap;
47 	return (0);
48 }
49 
50 /*
51  * pthread_condattr_destroy: frees the cond attribute object and
52  * invalidates it with NULL value.
53  */
54 #pragma weak pthread_condattr_destroy = _pthread_condattr_destroy
55 int
56 _pthread_condattr_destroy(pthread_condattr_t *attr)
57 {
58 	if (attr == NULL || attr->__pthread_condattrp == NULL)
59 		return (EINVAL);
60 	lfree(attr->__pthread_condattrp, sizeof (cvattr_t));
61 	attr->__pthread_condattrp = NULL;
62 	return (0);
63 }
64 
65 /*
66  * pthread_condattr_setclock: sets the clockid attribute.
67  */
68 #pragma weak pthread_condattr_setclock = _pthread_condattr_setclock
69 int
70 _pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
71 {
72 	cvattr_t *ap;
73 
74 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
75 	    (clock_id == CLOCK_REALTIME || clock_id == CLOCK_HIGHRES)) {
76 		ap->clockid = clock_id;
77 		return (0);
78 	}
79 	return (EINVAL);
80 }
81 
82 /*
83  * pthread_condattr_getclock: gets the shared attr.
84  */
85 #pragma weak pthread_condattr_getclock = _pthread_condattr_getclock
86 int
87 _pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
88 {
89 	cvattr_t *ap;
90 
91 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
92 	    clock_id != NULL) {
93 		*clock_id = ap->clockid;
94 		return (0);
95 	}
96 	return (EINVAL);
97 }
98 
99 
100 /*
101  * pthread_condattr_setpshared: sets the shared attr to PRIVATE or SHARED.
102  * This is equivalent to setting USYNC_PROCESS/USYNC_THREAD flag in cond_init().
103  */
104 #pragma weak pthread_condattr_setpshared = _pthread_condattr_setpshared
105 int
106 _pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
107 {
108 	cvattr_t *ap;
109 
110 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
111 	    (pshared == PTHREAD_PROCESS_PRIVATE ||
112 	    pshared == PTHREAD_PROCESS_SHARED)) {
113 		ap->pshared = pshared;
114 		return (0);
115 	}
116 	return (EINVAL);
117 }
118 
119 /*
120  * pthread_condattr_getpshared: gets the shared attr.
121  */
122 #pragma weak pthread_condattr_getpshared = _pthread_condattr_getpshared
123 int
124 _pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
125 {
126 	cvattr_t *ap;
127 
128 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
129 	    pshared != NULL) {
130 		*pshared = ap->pshared;
131 		return (0);
132 	}
133 	return (EINVAL);
134 }
135 
136 /*
137  * pthread_cond_init: Initializes the cond object. It copies the
138  * pshared attr into type argument and calls cond_init().
139  */
140 #pragma weak pthread_cond_init = _pthread_cond_init
141 int
142 _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
143 {
144 	cvattr_t *ap;
145 	int type;
146 	clockid_t clock_id;
147 	int error;
148 
149 	if (attr == NULL) {
150 		type = DEFAULT_TYPE;
151 		clock_id = CLOCK_REALTIME;
152 	} else if ((ap = attr->__pthread_condattrp) != NULL) {
153 		type = ap->pshared;
154 		clock_id = ap->clockid;
155 	} else {
156 		return (EINVAL);
157 	}
158 
159 	if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_HIGHRES)
160 		error = EINVAL;
161 	else if ((error = _cond_init((cond_t *)cond, type, NULL)) == 0)
162 		((cond_t *)cond)->cond_clockid = (uint8_t)clock_id;
163 
164 	return (error);
165 }
166