1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 #include <sal/types.h>
32 #include <osl/conditn.h>
33 #include <osl/diagnose.h>
37 typedef struct _oslConditionImpl
39 pthread_cond_t m_Condition
;
40 pthread_mutex_t m_Lock
;
45 /*****************************************************************************/
46 /* osl_createCondition */
47 /*****************************************************************************/
48 oslCondition SAL_CALL
osl_createCondition()
50 oslConditionImpl
* pCond
;
53 pCond
= (oslConditionImpl
*) malloc(sizeof(oslConditionImpl
));
62 pCond
->m_State
= sal_False
;
64 /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
65 nRet
= pthread_cond_init(&pCond
->m_Condition
, PTHREAD_CONDATTR_DEFAULT
);
68 OSL_TRACE("osl_createCondition : condition init failed. Errno: %d; '%s'\n",
69 nRet
, strerror(nRet
));
75 nRet
= pthread_mutex_init(&pCond
->m_Lock
, PTHREAD_MUTEXATTR_DEFAULT
);
78 OSL_TRACE("osl_createCondition : mutex init failed. Errno: %d; %s\n",
79 nRet
, strerror(nRet
));
81 nRet
= pthread_cond_destroy(&pCond
->m_Condition
);
84 OSL_TRACE("osl_createCondition : destroy condition failed. Errno: %d; '%s'\n",
85 nRet
, strerror(nRet
));
92 return (oslCondition
)pCond
;
95 /*****************************************************************************/
96 /* osl_destroyCondition */
97 /*****************************************************************************/
98 void SAL_CALL
osl_destroyCondition(oslCondition Condition
)
100 oslConditionImpl
* pCond
;
105 pCond
= (oslConditionImpl
*)Condition
;
107 nRet
= pthread_cond_destroy(&pCond
->m_Condition
);
110 OSL_TRACE("osl_destroyCondition : destroy condition failed. Errno: %d; '%s'\n",
111 nRet
, strerror(nRet
));
113 nRet
= pthread_mutex_destroy(&pCond
->m_Lock
);
116 OSL_TRACE("osl_destroyCondition : destroy mutex failed. Errno: %d; '%s'\n",
117 nRet
, strerror(nRet
));
126 /*****************************************************************************/
127 /* osl_setCondition */
128 /*****************************************************************************/
129 sal_Bool SAL_CALL
osl_setCondition(oslCondition Condition
)
131 oslConditionImpl
* pCond
;
134 OSL_ASSERT(Condition
);
135 pCond
= (oslConditionImpl
*)Condition
;
142 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
145 OSL_TRACE("osl_setCondition : mutex lock failed. Errno: %d; %s\n",
146 nRet
, strerror(nRet
));
150 pCond
->m_State
= sal_True
;
151 nRet
= pthread_cond_broadcast(&pCond
->m_Condition
);
154 OSL_TRACE("osl_setCondition : condition broadcast failed. Errno: %d; %s\n",
155 nRet
, strerror(nRet
));
159 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
162 OSL_TRACE("osl_setCondition : mutex unlock failed. Errno: %d; %s\n",
163 nRet
, strerror(nRet
));
171 /*****************************************************************************/
172 /* osl_resetCondition */
173 /*****************************************************************************/
174 sal_Bool SAL_CALL
osl_resetCondition(oslCondition Condition
)
176 oslConditionImpl
* pCond
;
179 OSL_ASSERT(Condition
);
181 pCond
= (oslConditionImpl
*)Condition
;
188 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
191 OSL_TRACE("osl_resetCondition : mutex lock failed. Errno: %d; %s\n",
192 nRet
, strerror(nRet
));
196 pCond
->m_State
= sal_False
;
198 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
201 OSL_TRACE("osl_resetCondition : mutex unlock failed. Errno: %d; %s\n",
202 nRet
, strerror(nRet
));
209 /*****************************************************************************/
210 /* osl_waitCondition */
211 /*****************************************************************************/
212 oslConditionResult SAL_CALL
osl_waitCondition(oslCondition Condition
, const TimeValue
* pTimeout
)
214 oslConditionImpl
* pCond
;
216 oslConditionResult Result
= osl_cond_result_ok
;
218 OSL_ASSERT(Condition
);
219 pCond
= (oslConditionImpl
*)Condition
;
223 return osl_cond_result_error
;
226 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
229 OSL_TRACE("osl_waitCondition : mutex lock failed. Errno: %d; %s\n",
230 nRet
, strerror(nRet
));
231 return osl_cond_result_error
;
236 if ( ! pCond
->m_State
)
242 gettimeofday(&tp
, NULL
);
244 SET_TIMESPEC( to
, tp
.tv_sec
+ pTimeout
->Seconds
,
245 tp
.tv_usec
* 1000 + pTimeout
->Nanosec
);
247 /* spurious wake up prevention */
250 ret
= pthread_cond_timedwait(&pCond
->m_Condition
, &pCond
->m_Lock
, &to
);
253 if ( ret
== ETIME
|| ret
== ETIMEDOUT
)
255 Result
= osl_cond_result_timeout
;
256 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
259 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
260 nRet
, strerror(nRet
));
265 else if ( ret
!= EINTR
)
267 Result
= osl_cond_result_error
;
268 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
271 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
272 nRet
, strerror(nRet
));
276 /* OSL_TRACE("EINTR\n");*/
279 while ( !pCond
->m_State
);
284 while ( !pCond
->m_State
)
286 nRet
= pthread_cond_wait(&pCond
->m_Condition
, &pCond
->m_Lock
);
289 OSL_TRACE("osl_waitCondition : condition wait failed. Errno: %d; %s\n",
290 nRet
, strerror(nRet
));
291 Result
= osl_cond_result_error
;
292 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
295 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
296 nRet
, strerror(nRet
));
304 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
307 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
308 nRet
, strerror(nRet
));
314 /*****************************************************************************/
315 /* osl_checkCondition */
316 /*****************************************************************************/
317 sal_Bool SAL_CALL
osl_checkCondition(oslCondition Condition
)
320 oslConditionImpl
* pCond
;
323 OSL_ASSERT(Condition
);
324 pCond
= (oslConditionImpl
*)Condition
;
331 nRet
= pthread_mutex_lock(&pCond
->m_Lock
);
334 OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
335 nRet
, strerror(nRet
));
338 State
= pCond
->m_State
;
340 nRet
= pthread_mutex_unlock(&pCond
->m_Lock
);
343 OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
344 nRet
, strerror(nRet
));