Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / osl / unx / conditn.cxx
blob1daea54b68e245a9a3153eb15c5fa7ffc05ce249
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include <assert.h>
24 #include "system.h"
25 #include <sal/log.hxx>
26 #include <sal/types.h>
28 #include <osl/conditn.h>
29 #include <osl/time.h>
32 typedef struct _oslConditionImpl
34 pthread_cond_t m_Condition;
35 pthread_mutex_t m_Lock;
36 sal_Bool m_State;
37 } oslConditionImpl;
40 oslCondition SAL_CALL osl_createCondition()
42 oslConditionImpl* pCond;
43 int nRet=0;
45 pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
47 if ( pCond == 0 )
49 SAL_WARN("sal.osl", "std::bad_alloc in C");
50 return 0;
53 pCond->m_State = sal_False;
55 /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
56 nRet = pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
57 if ( nRet != 0 )
59 SAL_WARN(
60 "sal.osl",
61 "pthread_cond_init failed, errno " << nRet << ", \""
62 << strerror(nRet) << '"');
64 free(pCond);
65 return 0;
68 nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
69 if ( nRet != 0 )
71 SAL_WARN(
72 "sal.osl",
73 "pthread_mutex_init failed, errno " << nRet << ", \""
74 << strerror(nRet) << '"');
76 nRet = pthread_cond_destroy(&pCond->m_Condition);
77 SAL_WARN_IF(
78 nRet != 0, "sal.osl",
79 "pthread_cond_destroy failed, errno " << nRet << ", \""
80 << strerror(nRet) << '"');
82 free(pCond);
83 pCond = 0;
86 return (oslCondition)pCond;
89 void SAL_CALL osl_destroyCondition(oslCondition Condition)
91 oslConditionImpl* pCond;
92 int nRet = 0;
94 if ( Condition )
96 pCond = (oslConditionImpl*)Condition;
98 nRet = pthread_cond_destroy(&pCond->m_Condition);
99 SAL_WARN_IF(
100 nRet != 0, "sal.osl",
101 "pthread_cond_destroy failed, errno " << nRet << ", \""
102 << strerror(nRet) << '"');
103 nRet = pthread_mutex_destroy(&pCond->m_Lock);
104 SAL_WARN_IF(
105 nRet != 0, "sal.osl",
106 "pthread_mutex_destroy failed, errno " << nRet << ", \""
107 << strerror(nRet) << '"');
109 free(Condition);
112 return;
115 sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
117 oslConditionImpl* pCond;
118 int nRet=0;
120 assert(Condition);
121 pCond = (oslConditionImpl*)Condition;
123 if ( pCond == 0 )
125 return sal_False;
128 nRet = pthread_mutex_lock(&pCond->m_Lock);
129 if ( nRet != 0 )
131 SAL_WARN(
132 "sal.osl",
133 "pthread_mutex_lock failed, errno " << nRet << ", \""
134 << strerror(nRet) << '"');
135 return sal_False;
138 pCond->m_State = sal_True;
139 nRet = pthread_cond_broadcast(&pCond->m_Condition);
140 if ( nRet != 0 )
142 SAL_WARN(
143 "sal.osl",
144 "pthread_cond_broadcast failed, errno " << nRet << ", \""
145 << strerror(nRet) << '"');
146 // try to unlock the mutex
147 pthread_mutex_unlock(&pCond->m_Lock);
148 return sal_False;
151 nRet = pthread_mutex_unlock(&pCond->m_Lock);
152 if ( nRet != 0 )
154 SAL_WARN(
155 "sal.osl",
156 "pthread_mutex_unlock failed, errno " << nRet << ", \""
157 << strerror(nRet) << '"');
158 return sal_False;
161 return sal_True;
165 sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
167 oslConditionImpl* pCond;
168 int nRet=0;
170 assert(Condition);
172 pCond = (oslConditionImpl*)Condition;
174 if ( pCond == 0 )
176 return sal_False;
179 nRet = pthread_mutex_lock(&pCond->m_Lock);
180 if ( nRet != 0 )
182 SAL_WARN(
183 "sal.osl",
184 "pthread_mutex_lock failed, errno " << nRet << ", \""
185 << strerror(nRet) << '"');
186 return sal_False;
189 pCond->m_State = sal_False;
191 nRet = pthread_mutex_unlock(&pCond->m_Lock);
192 if ( nRet != 0 )
194 SAL_WARN(
195 "sal.osl", "pthread_mutex_unlock failed, errno " << nRet <<", \""
196 << strerror(nRet) << '"');
197 return sal_False;
200 return sal_True;
203 oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
205 oslConditionImpl* pCond;
206 int nRet=0;
207 oslConditionResult Result = osl_cond_result_ok;
209 assert(Condition);
210 pCond = (oslConditionImpl*)Condition;
212 if ( pCond == 0 )
214 return osl_cond_result_error;
217 nRet = pthread_mutex_lock(&pCond->m_Lock);
218 if ( nRet != 0 )
220 SAL_WARN(
221 "sal.osl", "pthread_mutex_lock failed, errno " << nRet <<", \""
222 << strerror(nRet) << '"');
223 return osl_cond_result_error;
226 if ( pTimeout )
228 if ( ! pCond->m_State )
230 int ret;
231 struct timeval tp;
232 struct timespec to;
234 gettimeofday(&tp, NULL);
236 SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
237 tp.tv_usec * 1000 + pTimeout->Nanosec );
239 /* spurious wake up prevention */
242 ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
243 if ( ret != 0 )
245 if ( ret == ETIME || ret == ETIMEDOUT )
247 Result = osl_cond_result_timeout;
248 nRet = pthread_mutex_unlock(&pCond->m_Lock);
249 SAL_WARN_IF(
250 nRet != 0, "sal.osl",
251 "pthread_mutex_unlock failed, errno " << nRet
252 << ", \"" << strerror(nRet) << '"');
254 return Result;
256 else if ( ret != EINTR )
258 Result = osl_cond_result_error;
259 nRet = pthread_mutex_unlock(&pCond->m_Lock);
260 SAL_WARN_IF(
261 nRet != 0, "sal.osl",
262 "pthread_mutex_unlock failed, errno " << nRet
263 << ", \"" << strerror(nRet) << '"');
264 return Result;
268 while ( !pCond->m_State );
271 else
273 while ( !pCond->m_State )
275 nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
276 if ( nRet != 0 )
278 SAL_WARN(
279 "sal.osl",
280 "pthread_cond_wait failed, errno " << nRet << ", \""
281 << strerror(nRet) << '"');
282 Result = osl_cond_result_error;
283 nRet = pthread_mutex_unlock(&pCond->m_Lock);
284 SAL_WARN_IF(
285 nRet != 0, "sal.osl",
286 "pthread_mutex_unlock failed, errno " << nRet << ", \""
287 << strerror(nRet) << '"');
289 return Result;
294 nRet = pthread_mutex_unlock(&pCond->m_Lock);
295 SAL_WARN_IF(
296 nRet != 0, "sal.osl",
297 "pthread_mutex_unlock failed, errno " << nRet << ", \""
298 << strerror(nRet) << '"');
300 return Result;
303 sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
305 sal_Bool State;
306 oslConditionImpl* pCond;
307 int nRet=0;
309 assert(Condition);
310 pCond = (oslConditionImpl*)Condition;
312 if ( pCond == 0 )
314 return sal_False;
317 nRet = pthread_mutex_lock(&pCond->m_Lock);
318 SAL_WARN_IF(
319 nRet != 0, "sal.osl",
320 "pthread_mutex_lock failed, errno " << nRet << ", \"" << strerror(nRet)
321 << '"');
323 State = pCond->m_State;
325 nRet = pthread_mutex_unlock(&pCond->m_Lock);
326 SAL_WARN_IF(
327 nRet != 0, "sal.osl",
328 "pthread_mutex_unlock failed, errno " << nRet << ", \""
329 << strerror(nRet) << '"');
331 return State;
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */