xref: /illumos-gate/usr/src/lib/libc/port/threads/pthr_cond.c (revision 438b5f69eb4f998015cc0835f19f2f80663f2f53)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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 int
55 pthread_condattr_destroy(pthread_condattr_t *attr)
56 {
57 	if (attr == NULL || attr->__pthread_condattrp == NULL)
58 		return (EINVAL);
59 	lfree(attr->__pthread_condattrp, sizeof (cvattr_t));
60 	attr->__pthread_condattrp = NULL;
61 	return (0);
62 }
63 
64 /*
65  * pthread_condattr_setclock: sets the clockid attribute.
66  */
67 int
68 pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
69 {
70 	cvattr_t *ap;
71 
72 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
73 	    (clock_id == CLOCK_REALTIME || clock_id == CLOCK_HIGHRES)) {
74 		ap->clockid = clock_id;
75 		return (0);
76 	}
77 	return (EINVAL);
78 }
79 
80 /*
81  * pthread_condattr_getclock: gets the shared attr.
82  */
83 int
84 pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
85 {
86 	cvattr_t *ap;
87 
88 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
89 	    clock_id != NULL) {
90 		*clock_id = ap->clockid;
91 		return (0);
92 	}
93 	return (EINVAL);
94 }
95 
96 
97 /*
98  * pthread_condattr_setpshared: sets the shared attr to PRIVATE or SHARED.
99  * This is equivalent to setting USYNC_PROCESS/USYNC_THREAD flag in cond_init().
100  */
101 int
102 pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
103 {
104 	cvattr_t *ap;
105 
106 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
107 	    (pshared == PTHREAD_PROCESS_PRIVATE ||
108 	    pshared == PTHREAD_PROCESS_SHARED)) {
109 		ap->pshared = pshared;
110 		return (0);
111 	}
112 	return (EINVAL);
113 }
114 
115 /*
116  * pthread_condattr_getpshared: gets the shared attr.
117  */
118 #pragma weak _pthread_condattr_getpshared = pthread_condattr_getpshared
119 int
120 pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
121 {
122 	cvattr_t *ap;
123 
124 	if (attr != NULL && (ap = attr->__pthread_condattrp) != NULL &&
125 	    pshared != NULL) {
126 		*pshared = ap->pshared;
127 		return (0);
128 	}
129 	return (EINVAL);
130 }
131 
132 /*
133  * pthread_cond_init: Initializes the cond object. It copies the
134  * pshared attr into type argument and calls cond_init().
135  */
136 #pragma weak _pthread_cond_init = pthread_cond_init
137 int
138 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
139 {
140 	cvattr_t *ap;
141 	int type;
142 	clockid_t clock_id;
143 	int error;
144 
145 	if (attr == NULL) {
146 		type = DEFAULT_TYPE;
147 		clock_id = CLOCK_REALTIME;
148 	} else if ((ap = attr->__pthread_condattrp) != NULL) {
149 		type = ap->pshared;
150 		clock_id = ap->clockid;
151 	} else {
152 		return (EINVAL);
153 	}
154 
155 	if (clock_id != CLOCK_REALTIME && clock_id != CLOCK_HIGHRES)
156 		error = EINVAL;
157 	else if ((error = cond_init((cond_t *)cond, type, NULL)) == 0)
158 		((cond_t *)cond)->cond_clockid = (uint8_t)clock_id;
159 
160 	return (error);
161 }
162