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
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.
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]
23 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * xsem.c: to provide a semaphore system (used by the smq routines)
32 * these routines come from the libxposix library.
42 xsem_init(xsem_t
*sem
, int pshared
, unsigned int value
)
47 pthread_mutex_init(&sem
->semMutex
, NULL
);
48 pthread_cond_init(&sem
->semCV
, NULL
);
49 sem
->semaphore
= value
;
55 xsem_destroy(xsem_t
*sem
)
57 pthread_mutex_destroy(&sem
->semMutex
);
58 pthread_cond_destroy(&sem
->semCV
);
63 xsem_wait(xsem_t
*sem
)
65 pthread_mutex_lock(&sem
->semMutex
);
67 if (sem
->semaphore
< 0) {
69 pthread_mutex_unlock(&sem
->semMutex
);
73 if (sem
->semaphore
> 0) {
76 while (sem
->semaphore
== 0)
77 pthread_cond_wait(&sem
->semCV
, &sem
->semMutex
);
79 if (sem
->semaphore
!= 0) {
82 pthread_mutex_unlock(&sem
->semMutex
);
87 pthread_mutex_unlock(&sem
->semMutex
);
93 xsem_trywait(xsem_t
*sem
)
95 pthread_mutex_lock(&sem
->semMutex
);
97 if (sem
->semaphore
< 0) {
99 pthread_mutex_unlock(&sem
->semMutex
);
103 if (sem
->semaphore
== 0) {
104 pthread_mutex_unlock(&sem
->semMutex
);
110 pthread_mutex_unlock(&sem
->semMutex
);
116 xsem_post(xsem_t
*sem
)
118 pthread_mutex_lock(&sem
->semMutex
);
120 pthread_cond_signal(&sem
->semCV
);
121 pthread_mutex_unlock(&sem
->semMutex
);
128 xsem_getvalue(xsem_t
*sem
, int *sval
)
130 *sval
= sem
->semaphore
;
136 xsem_xwait(xsem_t
*sem
, int timeout
, timestruc_t
*mytime
)
142 return (xsem_wait(sem
));
144 pthread_mutex_lock(&sem
->semMutex
);
146 if (sem
->semaphore
< 0) {
148 pthread_mutex_unlock(&sem
->semMutex
);
152 if (sem
->semaphore
> 0) {
158 delay
.tv_sec
= delay
.tv_sec
+ time(NULL
);
159 while ((sem
->semaphore
== 0) && (status
== 0)) {
160 status
= pthread_cond_timedwait(&sem
->semCV
,
161 &sem
->semMutex
, &delay
);
165 * Check one more time in case thread didn't have a
166 * chance to check before timeout ??? TBD
170 pthread_mutex_unlock(&sem
->semMutex
);
172 } else if (sem
->semaphore
!= 0) {
175 pthread_mutex_unlock(&sem
->semMutex
);
180 pthread_mutex_unlock(&sem
->semMutex
);