update dev300-m57
[ooovba.git] / sal / osl / unx / conditn.c
blob9db658f91daa3dace200cd681331ce00d60e281f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: conditn.c,v $
10 * $Revision: 1.8 $
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 ************************************************************************/
32 #include "system.h"
33 #include <sal/types.h>
35 #include <osl/conditn.h>
36 #include <osl/diagnose.h>
37 #include <osl/time.h>
40 typedef struct _oslConditionImpl
42 pthread_cond_t m_Condition;
43 pthread_mutex_t m_Lock;
44 sal_Bool m_State;
45 } oslConditionImpl;
48 /*****************************************************************************/
49 /* osl_createCondition */
50 /*****************************************************************************/
51 oslCondition SAL_CALL osl_createCondition()
53 oslConditionImpl* pCond;
54 int nRet=0;
56 pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
58 OSL_ASSERT(pCond);
60 if ( pCond == 0 )
62 return 0;
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);
69 if ( nRet != 0 )
71 OSL_TRACE("osl_createCondition : condition init failed. Errno: %d; '%s'\n",
72 nRet, strerror(nRet));
74 free(pCond);
75 return 0;
78 nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
79 if ( nRet != 0 )
81 OSL_TRACE("osl_createCondition : mutex init failed. Errno: %d; %s\n",
82 nRet, strerror(nRet));
84 nRet = pthread_cond_destroy(&pCond->m_Condition);
85 if ( nRet != 0 )
87 OSL_TRACE("osl_createCondition : destroy condition failed. Errno: %d; '%s'\n",
88 nRet, strerror(nRet));
91 free(pCond);
92 pCond = 0;
95 return (oslCondition)pCond;
98 /*****************************************************************************/
99 /* osl_destroyCondition */
100 /*****************************************************************************/
101 void SAL_CALL osl_destroyCondition(oslCondition Condition)
103 oslConditionImpl* pCond;
104 int nRet = 0;
106 if ( Condition )
108 pCond = (oslConditionImpl*)Condition;
110 nRet = pthread_cond_destroy(&pCond->m_Condition);
111 if ( nRet != 0 )
113 OSL_TRACE("osl_destroyCondition : destroy condition failed. Errno: %d; '%s'\n",
114 nRet, strerror(nRet));
116 nRet = pthread_mutex_destroy(&pCond->m_Lock);
117 if ( nRet != 0 )
119 OSL_TRACE("osl_destroyCondition : destroy mutex failed. Errno: %d; '%s'\n",
120 nRet, strerror(nRet));
123 free(Condition);
126 return;
129 /*****************************************************************************/
130 /* osl_setCondition */
131 /*****************************************************************************/
132 sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
134 oslConditionImpl* pCond;
135 int nRet=0;
137 OSL_ASSERT(Condition);
138 pCond = (oslConditionImpl*)Condition;
140 if ( pCond == 0 )
142 return sal_False;
145 nRet = pthread_mutex_lock(&pCond->m_Lock);
146 if ( nRet != 0 )
148 OSL_TRACE("osl_setCondition : mutex lock failed. Errno: %d; %s\n",
149 nRet, strerror(nRet));
150 return sal_False;
153 pCond->m_State = sal_True;
154 nRet = pthread_cond_broadcast(&pCond->m_Condition);
155 if ( nRet != 0 )
157 OSL_TRACE("osl_setCondition : condition broadcast failed. Errno: %d; %s\n",
158 nRet, strerror(nRet));
159 return sal_False;
162 nRet = pthread_mutex_unlock(&pCond->m_Lock);
163 if ( nRet != 0 )
165 OSL_TRACE("osl_setCondition : mutex unlock failed. Errno: %d; %s\n",
166 nRet, strerror(nRet));
167 return sal_False;
170 return sal_True;
174 /*****************************************************************************/
175 /* osl_resetCondition */
176 /*****************************************************************************/
177 sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
179 oslConditionImpl* pCond;
180 int nRet=0;
182 OSL_ASSERT(Condition);
184 pCond = (oslConditionImpl*)Condition;
186 if ( pCond == 0 )
188 return sal_False;
191 nRet = pthread_mutex_lock(&pCond->m_Lock);
192 if ( nRet != 0 )
194 OSL_TRACE("osl_resetCondition : mutex lock failed. Errno: %d; %s\n",
195 nRet, strerror(nRet));
196 return sal_False;
199 pCond->m_State = sal_False;
201 nRet = pthread_mutex_unlock(&pCond->m_Lock);
202 if ( nRet != 0 )
204 OSL_TRACE("osl_resetCondition : mutex unlock failed. Errno: %d; %s\n",
205 nRet, strerror(nRet));
206 return sal_False;
209 return sal_True;
212 /*****************************************************************************/
213 /* osl_waitCondition */
214 /*****************************************************************************/
215 oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
217 oslConditionImpl* pCond;
218 int nRet=0;
219 oslConditionResult Result = osl_cond_result_ok;
221 OSL_ASSERT(Condition);
222 pCond = (oslConditionImpl*)Condition;
224 if ( pCond == 0 )
226 return osl_cond_result_error;
229 nRet = pthread_mutex_lock(&pCond->m_Lock);
230 if ( nRet != 0 )
232 OSL_TRACE("osl_waitCondition : mutex lock failed. Errno: %d; %s\n",
233 nRet, strerror(nRet));
234 return osl_cond_result_error;
237 if ( pTimeout )
239 if ( ! pCond->m_State )
241 int ret;
242 struct timeval tp;
243 struct timespec to;
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);
254 if ( ret != 0 )
256 if ( ret == ETIME || ret == ETIMEDOUT )
258 Result = osl_cond_result_timeout;
259 nRet = pthread_mutex_unlock(&pCond->m_Lock);
260 if (nRet != 0)
262 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
263 nRet, strerror(nRet));
266 return Result;
268 else if ( ret != EINTR )
270 Result = osl_cond_result_error;
271 nRet = pthread_mutex_unlock(&pCond->m_Lock);
272 if ( nRet != 0 )
274 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
275 nRet, strerror(nRet));
277 return Result;
279 /* OSL_TRACE("EINTR\n");*/
282 while ( !pCond->m_State );
285 else
287 while ( !pCond->m_State )
289 nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
290 if ( nRet != 0 )
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);
296 if ( nRet != 0 )
298 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
299 nRet, strerror(nRet));
302 return Result;
307 nRet = pthread_mutex_unlock(&pCond->m_Lock);
308 if ( nRet != 0 )
310 OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
311 nRet, strerror(nRet));
314 return Result;
317 /*****************************************************************************/
318 /* osl_checkCondition */
319 /*****************************************************************************/
320 sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
322 sal_Bool State;
323 oslConditionImpl* pCond;
324 int nRet=0;
326 OSL_ASSERT(Condition);
327 pCond = (oslConditionImpl*)Condition;
329 if ( pCond == 0 )
331 return sal_False;
334 nRet = pthread_mutex_lock(&pCond->m_Lock);
335 if ( nRet != 0 )
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);
344 if ( nRet != 0 )
346 OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
347 nRet, strerror(nRet));
350 return State;