merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / unx / conditn.c
blobea701d221e55c62cd3bfffa9d177b94cff6fe146
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 ************************************************************************/
29 #include "system.h"
30 #include <sal/types.h>
32 #include <osl/conditn.h>
33 #include <osl/diagnose.h>
34 #include <osl/time.h>
37 typedef struct _oslConditionImpl
39 pthread_cond_t m_Condition;
40 pthread_mutex_t m_Lock;
41 sal_Bool m_State;
42 } oslConditionImpl;
45 /*****************************************************************************/
46 /* osl_createCondition */
47 /*****************************************************************************/
48 oslCondition SAL_CALL osl_createCondition()
50 oslConditionImpl* pCond;
51 int nRet=0;
53 pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
55 OSL_ASSERT(pCond);
57 if ( pCond == 0 )
59 return 0;
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);
66 if ( nRet != 0 )
68 OSL_TRACE("osl_createCondition : condition init failed. Errno: %d; '%s'\n",
69 nRet, strerror(nRet));
71 free(pCond);
72 return 0;
75 nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
76 if ( nRet != 0 )
78 OSL_TRACE("osl_createCondition : mutex init failed. Errno: %d; %s\n",
79 nRet, strerror(nRet));
81 nRet = pthread_cond_destroy(&pCond->m_Condition);
82 if ( nRet != 0 )
84 OSL_TRACE("osl_createCondition : destroy condition failed. Errno: %d; '%s'\n",
85 nRet, strerror(nRet));
88 free(pCond);
89 pCond = 0;
92 return (oslCondition)pCond;
95 /*****************************************************************************/
96 /* osl_destroyCondition */
97 /*****************************************************************************/
98 void SAL_CALL osl_destroyCondition(oslCondition Condition)
100 oslConditionImpl* pCond;
101 int nRet = 0;
103 if ( Condition )
105 pCond = (oslConditionImpl*)Condition;
107 nRet = pthread_cond_destroy(&pCond->m_Condition);
108 if ( nRet != 0 )
110 OSL_TRACE("osl_destroyCondition : destroy condition failed. Errno: %d; '%s'\n",
111 nRet, strerror(nRet));
113 nRet = pthread_mutex_destroy(&pCond->m_Lock);
114 if ( nRet != 0 )
116 OSL_TRACE("osl_destroyCondition : destroy mutex failed. Errno: %d; '%s'\n",
117 nRet, strerror(nRet));
120 free(Condition);
123 return;
126 /*****************************************************************************/
127 /* osl_setCondition */
128 /*****************************************************************************/
129 sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
131 oslConditionImpl* pCond;
132 int nRet=0;
134 OSL_ASSERT(Condition);
135 pCond = (oslConditionImpl*)Condition;
137 if ( pCond == 0 )
139 return sal_False;
142 nRet = pthread_mutex_lock(&pCond->m_Lock);
143 if ( nRet != 0 )
145 OSL_TRACE("osl_setCondition : mutex lock failed. Errno: %d; %s\n",
146 nRet, strerror(nRet));
147 return sal_False;
150 pCond->m_State = sal_True;
151 nRet = pthread_cond_broadcast(&pCond->m_Condition);
152 if ( nRet != 0 )
154 OSL_TRACE("osl_setCondition : condition broadcast failed. Errno: %d; %s\n",
155 nRet, strerror(nRet));
156 return sal_False;
159 nRet = pthread_mutex_unlock(&pCond->m_Lock);
160 if ( nRet != 0 )
162 OSL_TRACE("osl_setCondition : mutex unlock failed. Errno: %d; %s\n",
163 nRet, strerror(nRet));
164 return sal_False;
167 return sal_True;
171 /*****************************************************************************/
172 /* osl_resetCondition */
173 /*****************************************************************************/
174 sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
176 oslConditionImpl* pCond;
177 int nRet=0;
179 OSL_ASSERT(Condition);
181 pCond = (oslConditionImpl*)Condition;
183 if ( pCond == 0 )
185 return sal_False;
188 nRet = pthread_mutex_lock(&pCond->m_Lock);
189 if ( nRet != 0 )
191 OSL_TRACE("osl_resetCondition : mutex lock failed. Errno: %d; %s\n",
192 nRet, strerror(nRet));
193 return sal_False;
196 pCond->m_State = sal_False;
198 nRet = pthread_mutex_unlock(&pCond->m_Lock);
199 if ( nRet != 0 )
201 OSL_TRACE("osl_resetCondition : mutex unlock failed. Errno: %d; %s\n",
202 nRet, strerror(nRet));
203 return sal_False;
206 return sal_True;
209 /*****************************************************************************/
210 /* osl_waitCondition */
211 /*****************************************************************************/
212 oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
214 oslConditionImpl* pCond;
215 int nRet=0;
216 oslConditionResult Result = osl_cond_result_ok;
218 OSL_ASSERT(Condition);
219 pCond = (oslConditionImpl*)Condition;
221 if ( pCond == 0 )
223 return osl_cond_result_error;
226 nRet = pthread_mutex_lock(&pCond->m_Lock);
227 if ( nRet != 0 )
229 OSL_TRACE("osl_waitCondition : mutex lock failed. Errno: %d; %s\n",
230 nRet, strerror(nRet));
231 return osl_cond_result_error;
234 if ( pTimeout )
236 if ( ! pCond->m_State )
238 int ret;
239 struct timeval tp;
240 struct timespec to;
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);
251 if ( ret != 0 )
253 if ( ret == ETIME || ret == ETIMEDOUT )
255 Result = osl_cond_result_timeout;
256 nRet = pthread_mutex_unlock(&pCond->m_Lock);
257 if (nRet != 0)
259 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
260 nRet, strerror(nRet));
263 return Result;
265 else if ( ret != EINTR )
267 Result = osl_cond_result_error;
268 nRet = pthread_mutex_unlock(&pCond->m_Lock);
269 if ( nRet != 0 )
271 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
272 nRet, strerror(nRet));
274 return Result;
276 /* OSL_TRACE("EINTR\n");*/
279 while ( !pCond->m_State );
282 else
284 while ( !pCond->m_State )
286 nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
287 if ( nRet != 0 )
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);
293 if ( nRet != 0 )
295 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
296 nRet, strerror(nRet));
299 return Result;
304 nRet = pthread_mutex_unlock(&pCond->m_Lock);
305 if ( nRet != 0 )
307 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
308 nRet, strerror(nRet));
311 return Result;
314 /*****************************************************************************/
315 /* osl_checkCondition */
316 /*****************************************************************************/
317 sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
319 sal_Bool State;
320 oslConditionImpl* pCond;
321 int nRet=0;
323 OSL_ASSERT(Condition);
324 pCond = (oslConditionImpl*)Condition;
326 if ( pCond == 0 )
328 return sal_False;
331 nRet = pthread_mutex_lock(&pCond->m_Lock);
332 if ( nRet != 0 )
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);
341 if ( nRet != 0 )
343 OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
344 nRet, strerror(nRet));
347 return State;