Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "sal/config.h"
30 : :
31 : : #include <assert.h>
32 : :
33 : : #include "system.h"
34 : : #include <sal/log.hxx>
35 : : #include <sal/types.h>
36 : :
37 : : #include <osl/conditn.h>
38 : : #include <osl/time.h>
39 : :
40 : :
41 : : typedef struct _oslConditionImpl
42 : : {
43 : : pthread_cond_t m_Condition;
44 : : pthread_mutex_t m_Lock;
45 : : sal_Bool m_State;
46 : : } oslConditionImpl;
47 : :
48 : :
49 : : /*****************************************************************************/
50 : : /* osl_createCondition */
51 : : /*****************************************************************************/
52 : 694589 : oslCondition SAL_CALL osl_createCondition()
53 : : {
54 : : oslConditionImpl* pCond;
55 : 694589 : int nRet=0;
56 : :
57 : 694589 : pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
58 : :
59 [ - + ]: 694589 : if ( pCond == 0 )
60 : : {
61 : : SAL_WARN("sal", "std::bad_alloc in C");
62 : 0 : return 0;
63 : : }
64 : :
65 : 694589 : pCond->m_State = sal_False;
66 : :
67 : : /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
68 : 694589 : nRet = pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
69 [ - + ]: 694589 : if ( nRet != 0 )
70 : : {
71 : : SAL_WARN(
72 : : "sal",
73 : : "pthread_cond_init failed, errno " << nRet << ", \""
74 : : << strerror(nRet) << '"');
75 : :
76 : 0 : free(pCond);
77 : 0 : return 0;
78 : : }
79 : :
80 : 694589 : nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
81 [ - + ]: 694589 : if ( nRet != 0 )
82 : : {
83 : : SAL_WARN(
84 : : "sal",
85 : : "pthread_mutex_init failed, errno " << nRet << ", \""
86 : : << strerror(nRet) << '"');
87 : :
88 : 0 : nRet = pthread_cond_destroy(&pCond->m_Condition);
89 : : SAL_WARN_IF(
90 : : nRet != 0, "sal",
91 : : "pthread_cond_destroy failed, errno " << nRet << ", \""
92 : : << strerror(nRet) << '"');
93 : :
94 : 0 : free(pCond);
95 : 0 : pCond = 0;
96 : : }
97 : :
98 : 694589 : return (oslCondition)pCond;
99 : : }
100 : :
101 : : /*****************************************************************************/
102 : : /* osl_destroyCondition */
103 : : /*****************************************************************************/
104 : 693409 : void SAL_CALL osl_destroyCondition(oslCondition Condition)
105 : : {
106 : : oslConditionImpl* pCond;
107 : 693409 : int nRet = 0;
108 : :
109 [ + - ]: 693409 : if ( Condition )
110 : : {
111 : 693409 : pCond = (oslConditionImpl*)Condition;
112 : :
113 : 693409 : nRet = pthread_cond_destroy(&pCond->m_Condition);
114 : : SAL_WARN_IF(
115 : : nRet != 0, "sal",
116 : : "pthread_cond_destroy failed, errno " << nRet << ", \""
117 : : << strerror(nRet) << '"');
118 : 693408 : nRet = pthread_mutex_destroy(&pCond->m_Lock);
119 : : SAL_WARN_IF(
120 : : nRet != 0, "sal",
121 : : "pthread_mutex_destroy failed, errno " << nRet << ", \""
122 : : << strerror(nRet) << '"');
123 : :
124 : 693409 : free(Condition);
125 : : }
126 : :
127 : 693409 : return;
128 : : }
129 : :
130 : : /*****************************************************************************/
131 : : /* osl_setCondition */
132 : : /*****************************************************************************/
133 : 2198670 : sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
134 : : {
135 : : oslConditionImpl* pCond;
136 : 2198670 : int nRet=0;
137 : :
138 : : assert(Condition);
139 : 2198670 : pCond = (oslConditionImpl*)Condition;
140 : :
141 [ - + ]: 2198670 : if ( pCond == 0 )
142 : : {
143 : 0 : return sal_False;
144 : : }
145 : :
146 : 2198670 : nRet = pthread_mutex_lock(&pCond->m_Lock);
147 [ - + ]: 2198670 : if ( nRet != 0 )
148 : : {
149 : : SAL_WARN(
150 : : "sal",
151 : : "pthread_mutex_lock failed, errno " << nRet << ", \""
152 : : << strerror(nRet) << '"');
153 : 0 : return sal_False;
154 : : }
155 : :
156 : 2198670 : pCond->m_State = sal_True;
157 : 2198670 : nRet = pthread_cond_broadcast(&pCond->m_Condition);
158 [ - + ]: 2198668 : if ( nRet != 0 )
159 : : {
160 : : SAL_WARN(
161 : : "sal",
162 : : "pthread_cond_broadcast failed, errno " << nRet << ", \""
163 : : << strerror(nRet) << '"');
164 : 0 : return sal_False;
165 : : }
166 : :
167 : 2198668 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
168 [ - + ]: 2198671 : if ( nRet != 0 )
169 : : {
170 : : SAL_WARN(
171 : : "sal",
172 : : "pthread_mutex_unlock failed, errno " << nRet << ", \""
173 : : << strerror(nRet) << '"');
174 : 0 : return sal_False;
175 : : }
176 : :
177 : 2198671 : return sal_True;
178 : :
179 : : }
180 : :
181 : : /*****************************************************************************/
182 : : /* osl_resetCondition */
183 : : /*****************************************************************************/
184 : 2078273 : sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
185 : : {
186 : : oslConditionImpl* pCond;
187 : 2078273 : int nRet=0;
188 : :
189 : : assert(Condition);
190 : :
191 : 2078273 : pCond = (oslConditionImpl*)Condition;
192 : :
193 [ + + ]: 2078273 : if ( pCond == 0 )
194 : : {
195 : 3 : return sal_False;
196 : : }
197 : :
198 : 2078270 : nRet = pthread_mutex_lock(&pCond->m_Lock);
199 [ - + ]: 2078269 : if ( nRet != 0 )
200 : : {
201 : : SAL_WARN(
202 : : "sal",
203 : : "pthread_mutex_lock failed, errno " << nRet << ", \""
204 : : << strerror(nRet) << '"');
205 : 0 : return sal_False;
206 : : }
207 : :
208 : 2078269 : pCond->m_State = sal_False;
209 : :
210 : 2078269 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
211 [ - + ]: 2078273 : if ( nRet != 0 )
212 : : {
213 : : SAL_WARN(
214 : : "sal", "pthread_mutex_unlock failed, errno " << nRet <<", \""
215 : : << strerror(nRet) << '"');
216 : 0 : return sal_False;
217 : : }
218 : :
219 : 2078276 : return sal_True;
220 : : }
221 : :
222 : : /*****************************************************************************/
223 : : /* osl_waitCondition */
224 : : /*****************************************************************************/
225 : 1327320 : oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
226 : : {
227 : : oslConditionImpl* pCond;
228 : 1327320 : int nRet=0;
229 : 1327320 : oslConditionResult Result = osl_cond_result_ok;
230 : :
231 : : assert(Condition);
232 : 1327320 : pCond = (oslConditionImpl*)Condition;
233 : :
234 [ + + ]: 1327320 : if ( pCond == 0 )
235 : : {
236 : 2 : return osl_cond_result_error;
237 : : }
238 : :
239 : 1327318 : nRet = pthread_mutex_lock(&pCond->m_Lock);
240 [ - + ]: 1327325 : if ( nRet != 0 )
241 : : {
242 : : SAL_WARN(
243 : : "sal", "pthread_mutex_lock failed, errno " << nRet <<", \""
244 : : << strerror(nRet) << '"');
245 : 0 : return osl_cond_result_error;
246 : : }
247 : :
248 [ + + ]: 1327325 : if ( pTimeout )
249 : : {
250 [ + + ]: 320875 : if ( ! pCond->m_State )
251 : : {
252 : : int ret;
253 : : struct timeval tp;
254 : : struct timespec to;
255 : :
256 : 320745 : gettimeofday(&tp, NULL);
257 : :
258 : 320745 : SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
259 : : tp.tv_usec * 1000 + pTimeout->Nanosec );
260 : :
261 : : /* spurious wake up prevention */
262 [ + ]: 320740 : do
263 : : {
264 [ + - ]: 320741 : ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
265 [ + + ]: 320740 : if ( ret != 0 )
266 : : {
267 [ + - ][ + - ]: 13921 : if ( ret == ETIME || ret == ETIMEDOUT )
268 : : {
269 : 13921 : Result = osl_cond_result_timeout;
270 : 13921 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
271 : : SAL_WARN_IF(
272 : : nRet != 0, "sal",
273 : : "pthread_mutex_unlock failed, errno " << nRet
274 : : << ", \"" << strerror(nRet) << '"');
275 : :
276 : 13921 : return Result;
277 : : }
278 [ # # ]: 0 : else if ( ret != EINTR )
279 : : {
280 : 0 : Result = osl_cond_result_error;
281 : 0 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
282 : : SAL_WARN_IF(
283 : : nRet != 0, "sal",
284 : : "pthread_mutex_unlock failed, errno " << nRet
285 : : << ", \"" << strerror(nRet) << '"');
286 : 0 : return Result;
287 : : }
288 : : }
289 : : }
290 : 306819 : while ( !pCond->m_State );
291 : : }
292 : : }
293 : : else
294 : : {
295 [ + + ]: 1425443 : while ( !pCond->m_State )
296 : : {
297 [ + - ]: 418995 : nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
298 [ - + ]: 418993 : if ( nRet != 0 )
299 : : {
300 : : SAL_WARN(
301 : : "sal",
302 : : "pthread_cond_wait failed, errno " << nRet << ", \""
303 : : << strerror(nRet) << '"');
304 : 0 : Result = osl_cond_result_error;
305 : 0 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
306 : : SAL_WARN_IF(
307 : : nRet != 0, "sal",
308 : : "pthread_mutex_unlock failed, errno " << nRet << ", \""
309 : : << strerror(nRet) << '"');
310 : :
311 : 0 : return Result;
312 : : }
313 : : }
314 : : }
315 : :
316 : 1313401 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
317 : : SAL_WARN_IF(
318 : : nRet != 0, "sal",
319 : : "pthread_mutex_unlock failed, errno " << nRet << ", \""
320 : : << strerror(nRet) << '"');
321 : :
322 : 1327319 : return Result;
323 : : }
324 : :
325 : : /*****************************************************************************/
326 : : /* osl_checkCondition */
327 : : /*****************************************************************************/
328 : 1121264 : sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
329 : : {
330 : : sal_Bool State;
331 : : oslConditionImpl* pCond;
332 : 1121264 : int nRet=0;
333 : :
334 : : assert(Condition);
335 : 1121264 : pCond = (oslConditionImpl*)Condition;
336 : :
337 [ - + ]: 1121264 : if ( pCond == 0 )
338 : : {
339 : 0 : return sal_False;
340 : : }
341 : :
342 : 1121264 : nRet = pthread_mutex_lock(&pCond->m_Lock);
343 : : SAL_WARN_IF(
344 : : nRet != 0, "sal",
345 : : "pthread_mutex_lock failed, errno " << nRet << ", \"" << strerror(nRet)
346 : : << '"');
347 : :
348 : 1121264 : State = pCond->m_State;
349 : :
350 : 1121264 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
351 : : SAL_WARN_IF(
352 : : nRet != 0, "sal",
353 : : "pthread_mutex_unlock failed, errno " << nRet << ", \""
354 : : << strerror(nRet) << '"');
355 : :
356 : 1121264 : return State;
357 : : }
358 : :
359 : :
360 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|