1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: conditn.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 #include <sal/types.h>
35 #include <osl/conditn.h>
36 #include <osl/diagnose.h>
40 typedef struct _oslConditionImpl
42 pthread_cond_t m_Condition
;
43 pthread_mutex_t m_Lock
;
48 /*****************************************************************************/
49 /* osl_createCondition */
50 /*****************************************************************************/
51 oslCondition SAL_CALL
osl_createCondition()
53 oslConditionImpl
* pCond
;
56 pCond
= (oslConditionImpl
*) malloc(sizeof(oslConditionImpl
));
65 pCond
->m_State
= sal_False
;
67 /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
68 nRet
= pthread_cond_init(&pCond
->m_Condition
, PTHREAD_CONDATTR_DEFAULT
);
71 OSL_TRACE("osl_createCondition : condition init failed. Errno: %d; '%s'\n",
72 nRet
, strerror(nRet
));
78 nRet
= pthread_mutex_init(&pCond
->m_Lock
, PTHREAD_MUTEXATTR_DEFAULT
);
81 OSL_TRACE("osl_createCondition : mutex init failed. Errno: %d; %s\n",
82 nRet
, strerror(nRet
));
84 nRet
= pthread_cond_destroy(&pCond
->m_Condition
);
87 OSL_TRACE("osl_createCondition : destroy condition failed. Errno: %d; '%s'\n",
88 nRet
, strerror(nRet
));
95 return (oslCondition
)pCond
;
98 /*****************************************************************************/
99 /* osl_destroyCondition */
100 /*****************************************************************************/
101 void SAL_CALL
osl_destroyCondition(oslCondition Condition
)
103 oslConditionImpl
* pCond
;
108 pCond
= (oslConditionImpl
*)Condition
;
110 nRet
= pthread_cond_destroy(&pCond
->m_Condition
);
113 OSL_TRACE("osl_destroyCondition : destroy condition failed. Errno: %d; '%s'\n",
114 nRet
, strerror(nRet
));
116 nRet
= pthread_mutex_destroy(&pCond
->m_Lock
);
119 OSL_TRACE("osl_destroyCondition : destroy mutex failed. Errno: %d; '%s'\n",
120 nRet
, strerror(nRet
));
129 /*****************************************************************************/
130 /* osl_setCondition */
131 /*****************************************************************************/
132 sal_Bool SAL_CALL
osl_setCondition(oslCondition Condition
)
134 oslConditionImpl
* pCond
;
137 OSL_ASSERT(Condition
);
138 pCond
= (oslConditionImpl
*)Condition
;
145 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
148 OSL_TRACE("osl_setCondition : mutex lock failed. Errno: %d; %s\n",
149 nRet
, strerror(nRet
));
153 pCond
->m_State
= sal_True
;
154 nRet
= pthread_cond_broadcast(&pCond
->m_Condition
);
157 OSL_TRACE("osl_setCondition : condition broadcast failed. Errno: %d; %s\n",
158 nRet
, strerror(nRet
));
162 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
165 OSL_TRACE("osl_setCondition : mutex unlock failed. Errno: %d; %s\n",
166 nRet
, strerror(nRet
));
174 /*****************************************************************************/
175 /* osl_resetCondition */
176 /*****************************************************************************/
177 sal_Bool SAL_CALL
osl_resetCondition(oslCondition Condition
)
179 oslConditionImpl
* pCond
;
182 OSL_ASSERT(Condition
);
184 pCond
= (oslConditionImpl
*)Condition
;
191 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
194 OSL_TRACE("osl_resetCondition : mutex lock failed. Errno: %d; %s\n",
195 nRet
, strerror(nRet
));
199 pCond
->m_State
= sal_False
;
201 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
204 OSL_TRACE("osl_resetCondition : mutex unlock failed. Errno: %d; %s\n",
205 nRet
, strerror(nRet
));
212 /*****************************************************************************/
213 /* osl_waitCondition */
214 /*****************************************************************************/
215 oslConditionResult SAL_CALL
osl_waitCondition(oslCondition Condition
, const TimeValue
* pTimeout
)
217 oslConditionImpl
* pCond
;
219 oslConditionResult Result
= osl_cond_result_ok
;
221 OSL_ASSERT(Condition
);
222 pCond
= (oslConditionImpl
*)Condition
;
226 return osl_cond_result_error
;
229 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
232 OSL_TRACE("osl_waitCondition : mutex lock failed. Errno: %d; %s\n",
233 nRet
, strerror(nRet
));
234 return osl_cond_result_error
;
239 if ( ! pCond
->m_State
)
245 gettimeofday(&tp
, NULL
);
247 SET_TIMESPEC( to
, tp
.tv_sec
+ pTimeout
->Seconds
,
248 tp
.tv_usec
* 1000 + pTimeout
->Nanosec
);
250 /* spurious wake up prevention */
253 ret
= pthread_cond_timedwait(&pCond
->m_Condition
, &pCond
->m_Lock
, &to
);
256 if ( ret
== ETIME
|| ret
== ETIMEDOUT
)
258 Result
= osl_cond_result_timeout
;
259 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
262 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
263 nRet
, strerror(nRet
));
268 else if ( ret
!= EINTR
)
270 Result
= osl_cond_result_error
;
271 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
274 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
275 nRet
, strerror(nRet
));
279 /* OSL_TRACE("EINTR\n");*/
282 while ( !pCond
->m_State
);
287 while ( !pCond
->m_State
)
289 nRet
= pthread_cond_wait(&pCond
->m_Condition
, &pCond
->m_Lock
);
292 OSL_TRACE("osl_waitCondition : condition wait failed. Errno: %d; %s\n",
293 nRet
, strerror(nRet
));
294 Result
= osl_cond_result_error
;
295 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
298 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
299 nRet
, strerror(nRet
));
307 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
310 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
311 nRet
, strerror(nRet
));
317 /*****************************************************************************/
318 /* osl_checkCondition */
319 /*****************************************************************************/
320 sal_Bool SAL_CALL
osl_checkCondition(oslCondition Condition
)
323 oslConditionImpl
* pCond
;
326 OSL_ASSERT(Condition
);
327 pCond
= (oslConditionImpl
*)Condition
;
334 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
337 OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
338 nRet
, strerror(nRet
));
341 State
= pCond
->m_State
;
343 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
346 OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
347 nRet
, strerror(nRet
));