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 18589 : oslCondition SAL_CALL osl_createCondition()
44 : {
45 : oslConditionImpl* pCond;
46 18589 : int nRet=0;
47 :
48 18589 : pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
49 :
50 18589 : if ( pCond == 0 )
51 : {
52 : SAL_WARN("sal.osl", "std::bad_alloc in C");
53 0 : return 0;
54 : }
55 :
56 18589 : pCond->m_State = sal_False;
57 :
58 : /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
59 18589 : nRet = pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
60 18589 : 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 18589 : nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
72 18589 : 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 18589 : return (oslCondition)pCond;
90 : }
91 :
92 : /*****************************************************************************/
93 : /* osl_destroyCondition */
94 : /*****************************************************************************/
95 17684 : void SAL_CALL osl_destroyCondition(oslCondition Condition)
96 : {
97 : oslConditionImpl* pCond;
98 17684 : int nRet = 0;
99 :
100 17684 : if ( Condition )
101 : {
102 17684 : pCond = (oslConditionImpl*)Condition;
103 :
104 17684 : 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 17684 : 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 17684 : free(Condition);
116 : }
117 :
118 17684 : return;
119 : }
120 :
121 : /*****************************************************************************/
122 : /* osl_setCondition */
123 : /*****************************************************************************/
124 70334 : sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
125 : {
126 : oslConditionImpl* pCond;
127 70334 : int nRet=0;
128 :
129 : assert(Condition);
130 70334 : pCond = (oslConditionImpl*)Condition;
131 :
132 70334 : if ( pCond == 0 )
133 : {
134 0 : return sal_False;
135 : }
136 :
137 70334 : nRet = pthread_mutex_lock(&pCond->m_Lock);
138 70334 : 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 70334 : pCond->m_State = sal_True;
148 70334 : nRet = pthread_cond_broadcast(&pCond->m_Condition);
149 70334 : if ( nRet != 0 )
150 : {
151 : SAL_WARN(
152 : "sal.osl",
153 : "pthread_cond_broadcast failed, errno " << nRet << ", \""
154 : << strerror(nRet) << '"');
155 0 : return sal_False;
156 : }
157 :
158 70334 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
159 70334 : if ( nRet != 0 )
160 : {
161 : SAL_WARN(
162 : "sal.osl",
163 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
164 : << strerror(nRet) << '"');
165 0 : return sal_False;
166 : }
167 :
168 70334 : return sal_True;
169 :
170 : }
171 :
172 : /*****************************************************************************/
173 : /* osl_resetCondition */
174 : /*****************************************************************************/
175 49658 : sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
176 : {
177 : oslConditionImpl* pCond;
178 49658 : int nRet=0;
179 :
180 : assert(Condition);
181 :
182 49658 : pCond = (oslConditionImpl*)Condition;
183 :
184 49658 : if ( pCond == 0 )
185 : {
186 0 : return sal_False;
187 : }
188 :
189 49658 : nRet = pthread_mutex_lock(&pCond->m_Lock);
190 49658 : if ( nRet != 0 )
191 : {
192 : SAL_WARN(
193 : "sal.osl",
194 : "pthread_mutex_lock failed, errno " << nRet << ", \""
195 : << strerror(nRet) << '"');
196 0 : return sal_False;
197 : }
198 :
199 49658 : pCond->m_State = sal_False;
200 :
201 49658 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
202 49658 : if ( nRet != 0 )
203 : {
204 : SAL_WARN(
205 : "sal.osl", "pthread_mutex_unlock failed, errno " << nRet <<", \""
206 : << strerror(nRet) << '"');
207 0 : return sal_False;
208 : }
209 :
210 49658 : return sal_True;
211 : }
212 :
213 : /*****************************************************************************/
214 : /* osl_waitCondition */
215 : /*****************************************************************************/
216 34693 : oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
217 : {
218 : oslConditionImpl* pCond;
219 34693 : int nRet=0;
220 34693 : oslConditionResult Result = osl_cond_result_ok;
221 :
222 : assert(Condition);
223 34693 : pCond = (oslConditionImpl*)Condition;
224 :
225 34693 : if ( pCond == 0 )
226 : {
227 0 : return osl_cond_result_error;
228 : }
229 :
230 34693 : nRet = pthread_mutex_lock(&pCond->m_Lock);
231 34693 : if ( nRet != 0 )
232 : {
233 : SAL_WARN(
234 : "sal.osl", "pthread_mutex_lock failed, errno " << nRet <<", \""
235 : << strerror(nRet) << '"');
236 0 : return osl_cond_result_error;
237 : }
238 :
239 34693 : if ( pTimeout )
240 : {
241 2181 : if ( ! pCond->m_State )
242 : {
243 : int ret;
244 : struct timeval tp;
245 : struct timespec to;
246 :
247 2179 : gettimeofday(&tp, NULL);
248 :
249 2179 : SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
250 : tp.tv_usec * 1000 + pTimeout->Nanosec );
251 :
252 : /* spurious wake up prevention */
253 0 : do
254 : {
255 2179 : ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
256 2179 : if ( ret != 0 )
257 : {
258 2179 : if ( ret == ETIME || ret == ETIMEDOUT )
259 : {
260 2179 : Result = osl_cond_result_timeout;
261 2179 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
262 : SAL_WARN_IF(
263 : nRet != 0, "sal.osl",
264 : "pthread_mutex_unlock failed, errno " << nRet
265 : << ", \"" << strerror(nRet) << '"');
266 :
267 2179 : return Result;
268 : }
269 0 : else if ( ret != EINTR )
270 : {
271 0 : Result = osl_cond_result_error;
272 0 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
273 : SAL_WARN_IF(
274 : nRet != 0, "sal.osl",
275 : "pthread_mutex_unlock failed, errno " << nRet
276 : << ", \"" << strerror(nRet) << '"');
277 0 : return Result;
278 : }
279 : }
280 : }
281 0 : while ( !pCond->m_State );
282 : }
283 : }
284 : else
285 : {
286 75336 : while ( !pCond->m_State )
287 : {
288 10312 : nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
289 10312 : if ( nRet != 0 )
290 : {
291 : SAL_WARN(
292 : "sal.osl",
293 : "pthread_cond_wait failed, errno " << nRet << ", \""
294 : << strerror(nRet) << '"');
295 0 : Result = osl_cond_result_error;
296 0 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
297 : SAL_WARN_IF(
298 : nRet != 0, "sal.osl",
299 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
300 : << strerror(nRet) << '"');
301 :
302 0 : return Result;
303 : }
304 : }
305 : }
306 :
307 32514 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
308 : SAL_WARN_IF(
309 : nRet != 0, "sal.osl",
310 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
311 : << strerror(nRet) << '"');
312 :
313 32514 : return Result;
314 : }
315 :
316 : /*****************************************************************************/
317 : /* osl_checkCondition */
318 : /*****************************************************************************/
319 95907 : sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
320 : {
321 : sal_Bool State;
322 : oslConditionImpl* pCond;
323 95907 : int nRet=0;
324 :
325 : assert(Condition);
326 95907 : pCond = (oslConditionImpl*)Condition;
327 :
328 95907 : if ( pCond == 0 )
329 : {
330 0 : return sal_False;
331 : }
332 :
333 95907 : nRet = pthread_mutex_lock(&pCond->m_Lock);
334 : SAL_WARN_IF(
335 : nRet != 0, "sal.osl",
336 : "pthread_mutex_lock failed, errno " << nRet << ", \"" << strerror(nRet)
337 : << '"');
338 :
339 95907 : State = pCond->m_State;
340 :
341 95907 : nRet = pthread_mutex_unlock(&pCond->m_Lock);
342 : SAL_WARN_IF(
343 : nRet != 0, "sal.osl",
344 : "pthread_mutex_unlock failed, errno " << nRet << ", \""
345 : << strerror(nRet) << '"');
346 :
347 95907 : return State;
348 : }
349 :
350 :
351 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|