Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include <assert.h>
23 :
24 : #include "system.h"
25 : #include <sal/log.hxx>
26 : #include <sal/types.h>
27 :
28 : #include <osl/conditn.h>
29 : #include <osl/time.h>
30 :
31 :
32 : typedef struct _oslConditionImpl
33 : {
34 : pthread_cond_t m_Condition;
35 : pthread_mutex_t m_Lock;
36 : sal_Bool m_State;
37 : } oslConditionImpl;
38 :
39 :
40 : /*****************************************************************************/
41 : /* osl_createCondition */
42 : /*****************************************************************************/
43 359970 : oslCondition SAL_CALL osl_createCondition()
44 : {
45 : oslConditionImpl* pCond;
46 359970 : int nRet=0;
47 :
48 359970 : pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
49 :
50 359970 : if ( pCond == 0 )
51 : {
52 : SAL_WARN("sal.osl", "std::bad_alloc in C");
53 0 : return 0;
54 : }
55 :
56 359970 : pCond->m_State = sal_False;
57 :
58 : /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
59 359970 : nRet = pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
60 359970 : if ( nRet != 0 )
61 : {
62 : SAL_WARN(
63 : "sal.osl",
64 : "pthread_cond_init failed, errno " << nRet << ", \""
65 : << strerror(nRet) << '"');
66 :
67 0 : free(pCond);
68 0 : return 0;
69 : }
70 :
71 359970 : nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
72 359970 : if ( nRet != 0 )
73 : {
74 : SAL_WARN(
75 : "sal.osl",
76 : "pthread_mutex_init failed, errno " << nRet << ", \""
77 : << strerror(nRet) << '"');
78 :
79 0 : nRet = pthread_cond_destroy(&pCond->m_Condition);
80 : SAL_WARN_IF(
81 : nRet != 0, "sal.osl",
82 : "pthread_cond_destroy failed, errno " << nRet << ", \""
83 : << strerror(nRet) << '"');
84 :
85 0 : free(pCond);
86 0 : pCond = 0;
87 : }
88 :
89 359970 : return (oslCondition)pCond;
90 : }
91 :
92 : /*****************************************************************************/
93 : /* osl_destroyCondition */
94 : /*****************************************************************************/
95 359780 : void SAL_CALL osl_destroyCondition(oslCondition Condition)
96 : {
97 : oslConditionImpl* pCond;
98 359780 : int nRet = 0;
99 :
100 359780 : if ( Condition )
101 : {
102 359780 : pCond = (oslConditionImpl*)Condition;
103 :
104 359780 : nRet = pthread_cond_destroy(&pCond->m_Condition);
105 : SAL_WARN_IF(
106 : nRet != 0, "sal.osl",
107 : "pthread_cond_destroy failed, errno " << nRet << ", \""
108 : << strerror(nRet) << '"');
109 359779 : nRet = pthread_mutex_destroy(&pCond->m_Lock);
110 : SAL_WARN_IF(
111 : nRet != 0, "sal.osl",
112 : "pthread_mutex_destroy failed, errno " << nRet << ", \""
113 : << strerror(nRet) << '"');
114 :
115 359781 : free(Condition);
116 : }
117 :
118 359781 : return;
119 : }
120 :
121 : /*****************************************************************************/
122 : /* osl_setCondition */
123 : /*****************************************************************************/
124 1168601 : sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
125 : {
126 : oslConditionImpl* pCond;
127 1168601 : int nRet=0;
128 :
129 : assert(Condition);
130 1168601 : pCond = (oslConditionImpl*)Condition;
131 :
132 1168601 : if ( pCond == 0 )
133 : {
134 0 : return sal_False;
135 : }
136 :
137 1168601 : nRet = pthread_mutex_lock(&pCond->m_Lock);
138 1168604 : if ( nRet != 0 )
139 : {
140 : SAL_WARN(
141 : "sal.osl",
142 : "pthread_mutex_lock failed, errno " << nRet << ", \""
143 : << strerror(nRet) << '"');
144 0 : return sal_False;
145 : }
146 :
147 1168604 : pCond->m_State = sal_True;
148 1168604 : nRet = pthread_cond_broadcast(&pCond->m_Condition);
149 1168604 : if ( nRet != 0 )
150 : {
151 : SAL_WARN(
152 : "sal.osl",
153 : "pthread_cond_broadcast failed, errno " << nRet << ", \""
154 : << strerror(nRet) << '"');
155 : // try to unlock the mutex
156 0 : pthread_mutex_unlock(&pCond->m_Lock);
157 0 : return sal_False;
158 : }
159 :
160 1168604 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
161 1168604 : if ( nRet != 0 )
162 : {
163 : SAL_WARN(
164 : "sal.osl",
165 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
166 : << strerror(nRet) << '"');
167 0 : return sal_False;
168 : }
169 :
170 1168604 : return sal_True;
171 :
172 : }
173 :
174 : /*****************************************************************************/
175 : /* osl_resetCondition */
176 : /*****************************************************************************/
177 1094091 : sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
178 : {
179 : oslConditionImpl* pCond;
180 1094091 : int nRet=0;
181 :
182 : assert(Condition);
183 :
184 1094091 : pCond = (oslConditionImpl*)Condition;
185 :
186 1094091 : if ( pCond == 0 )
187 : {
188 1 : return sal_False;
189 : }
190 :
191 1094090 : nRet = pthread_mutex_lock(&pCond->m_Lock);
192 1094094 : if ( nRet != 0 )
193 : {
194 : SAL_WARN(
195 : "sal.osl",
196 : "pthread_mutex_lock failed, errno " << nRet << ", \""
197 : << strerror(nRet) << '"');
198 0 : return sal_False;
199 : }
200 :
201 1094094 : pCond->m_State = sal_False;
202 :
203 1094094 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
204 1094097 : if ( nRet != 0 )
205 : {
206 : SAL_WARN(
207 : "sal.osl", "pthread_mutex_unlock failed, errno " << nRet <<", \""
208 : << strerror(nRet) << '"');
209 0 : return sal_False;
210 : }
211 :
212 1094097 : return sal_True;
213 : }
214 :
215 : /*****************************************************************************/
216 : /* osl_waitCondition */
217 : /*****************************************************************************/
218 704374 : oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
219 : {
220 : oslConditionImpl* pCond;
221 704374 : int nRet=0;
222 704374 : oslConditionResult Result = osl_cond_result_ok;
223 :
224 : assert(Condition);
225 704374 : pCond = (oslConditionImpl*)Condition;
226 :
227 704374 : if ( pCond == 0 )
228 : {
229 2 : return osl_cond_result_error;
230 : }
231 :
232 704372 : nRet = pthread_mutex_lock(&pCond->m_Lock);
233 704375 : if ( nRet != 0 )
234 : {
235 : SAL_WARN(
236 : "sal.osl", "pthread_mutex_lock failed, errno " << nRet <<", \""
237 : << strerror(nRet) << '"');
238 0 : return osl_cond_result_error;
239 : }
240 :
241 704375 : if ( pTimeout )
242 : {
243 160994 : if ( ! pCond->m_State )
244 : {
245 : int ret;
246 : struct timeval tp;
247 : struct timespec to;
248 :
249 160904 : gettimeofday(&tp, NULL);
250 :
251 160904 : SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
252 : tp.tv_usec * 1000 + pTimeout->Nanosec );
253 :
254 : /* spurious wake up prevention */
255 155825 : do
256 : {
257 160902 : ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
258 160902 : if ( ret != 0 )
259 : {
260 5077 : if ( ret == ETIME || ret == ETIMEDOUT )
261 : {
262 5077 : Result = osl_cond_result_timeout;
263 5077 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
264 : SAL_WARN_IF(
265 : nRet != 0, "sal.osl",
266 : "pthread_mutex_unlock failed, errno " << nRet
267 : << ", \"" << strerror(nRet) << '"');
268 :
269 10154 : return Result;
270 : }
271 0 : else if ( ret != EINTR )
272 : {
273 0 : Result = osl_cond_result_error;
274 0 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
275 : SAL_WARN_IF(
276 : nRet != 0, "sal.osl",
277 : "pthread_mutex_unlock failed, errno " << nRet
278 : << ", \"" << strerror(nRet) << '"');
279 0 : return Result;
280 : }
281 : }
282 : }
283 155825 : while ( !pCond->m_State );
284 : }
285 : }
286 : else
287 : {
288 1312156 : while ( !pCond->m_State )
289 : {
290 225395 : nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
291 225394 : if ( nRet != 0 )
292 : {
293 : SAL_WARN(
294 : "sal.osl",
295 : "pthread_cond_wait failed, errno " << nRet << ", \""
296 : << strerror(nRet) << '"');
297 0 : Result = osl_cond_result_error;
298 0 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
299 : SAL_WARN_IF(
300 : nRet != 0, "sal.osl",
301 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
302 : << strerror(nRet) << '"');
303 :
304 0 : return Result;
305 : }
306 : }
307 : }
308 :
309 699297 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
310 : SAL_WARN_IF(
311 : nRet != 0, "sal.osl",
312 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
313 : << strerror(nRet) << '"');
314 :
315 699294 : return Result;
316 : }
317 :
318 : /*****************************************************************************/
319 : /* osl_checkCondition */
320 : /*****************************************************************************/
321 598631 : sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
322 : {
323 : sal_Bool State;
324 : oslConditionImpl* pCond;
325 598631 : int nRet=0;
326 :
327 : assert(Condition);
328 598631 : pCond = (oslConditionImpl*)Condition;
329 :
330 598631 : if ( pCond == 0 )
331 : {
332 0 : return sal_False;
333 : }
334 :
335 598631 : nRet = pthread_mutex_lock(&pCond->m_Lock);
336 : SAL_WARN_IF(
337 : nRet != 0, "sal.osl",
338 : "pthread_mutex_lock failed, errno " << nRet << ", \"" << strerror(nRet)
339 : << '"');
340 :
341 598631 : State = pCond->m_State;
342 :
343 598631 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
344 : SAL_WARN_IF(
345 : nRet != 0, "sal.osl",
346 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
347 : << strerror(nRet) << '"');
348 :
349 598631 : return State;
350 : }
351 :
352 :
353 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|