1647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3647f063dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4647f063dSAndrew Rist * or more contributor license agreements. See the NOTICE file
5647f063dSAndrew Rist * distributed with this work for additional information
6647f063dSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7647f063dSAndrew Rist * to you under the Apache License, Version 2.0 (the
8647f063dSAndrew Rist * "License"); you may not use this file except in compliance
9647f063dSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11647f063dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13647f063dSAndrew Rist * Unless required by applicable law or agreed to in writing,
14647f063dSAndrew Rist * software distributed under the License is distributed on an
15647f063dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16647f063dSAndrew Rist * KIND, either express or implied. See the License for the
17647f063dSAndrew Rist * specific language governing permissions and limitations
18647f063dSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20647f063dSAndrew Rist *************************************************************/
21647f063dSAndrew Rist
22647f063dSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir #include <sal/types.h>
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <osl/conditn.h>
28cdf0e10cSrcweir #include <osl/diagnose.h>
29cdf0e10cSrcweir #include <osl/time.h>
30cdf0e10cSrcweir
31cdf0e10cSrcweir
32cdf0e10cSrcweir typedef struct _oslConditionImpl
33cdf0e10cSrcweir {
34cdf0e10cSrcweir pthread_cond_t m_Condition;
35cdf0e10cSrcweir pthread_mutex_t m_Lock;
36cdf0e10cSrcweir sal_Bool m_State;
37cdf0e10cSrcweir } oslConditionImpl;
38cdf0e10cSrcweir
39cdf0e10cSrcweir
40cdf0e10cSrcweir /*****************************************************************************/
41cdf0e10cSrcweir /* osl_createCondition */
42cdf0e10cSrcweir /*****************************************************************************/
osl_createCondition()43cdf0e10cSrcweir oslCondition SAL_CALL osl_createCondition()
44cdf0e10cSrcweir {
45cdf0e10cSrcweir oslConditionImpl* pCond;
46cdf0e10cSrcweir int nRet=0;
47cdf0e10cSrcweir
48cdf0e10cSrcweir pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
49cdf0e10cSrcweir
50cdf0e10cSrcweir OSL_ASSERT(pCond);
51cdf0e10cSrcweir
52*509a48ffSpfg if ( pCond == NULL )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir return 0;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir
57cdf0e10cSrcweir pCond->m_State = sal_False;
58cdf0e10cSrcweir
59cdf0e10cSrcweir /* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
60cdf0e10cSrcweir nRet = pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
61cdf0e10cSrcweir if ( nRet != 0 )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir OSL_TRACE("osl_createCondition : condition init failed. Errno: %d; '%s'\n",
64cdf0e10cSrcweir nRet, strerror(nRet));
65cdf0e10cSrcweir
66cdf0e10cSrcweir free(pCond);
67cdf0e10cSrcweir return 0;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
70cdf0e10cSrcweir nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
71cdf0e10cSrcweir if ( nRet != 0 )
72cdf0e10cSrcweir {
73cdf0e10cSrcweir OSL_TRACE("osl_createCondition : mutex init failed. Errno: %d; %s\n",
74cdf0e10cSrcweir nRet, strerror(nRet));
75cdf0e10cSrcweir
76cdf0e10cSrcweir nRet = pthread_cond_destroy(&pCond->m_Condition);
77cdf0e10cSrcweir if ( nRet != 0 )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir OSL_TRACE("osl_createCondition : destroy condition failed. Errno: %d; '%s'\n",
80cdf0e10cSrcweir nRet, strerror(nRet));
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
83cdf0e10cSrcweir free(pCond);
84*509a48ffSpfg pCond = NULL;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir
87cdf0e10cSrcweir return (oslCondition)pCond;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir /*****************************************************************************/
91cdf0e10cSrcweir /* osl_destroyCondition */
92cdf0e10cSrcweir /*****************************************************************************/
osl_destroyCondition(oslCondition Condition)93cdf0e10cSrcweir void SAL_CALL osl_destroyCondition(oslCondition Condition)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir oslConditionImpl* pCond;
96cdf0e10cSrcweir int nRet = 0;
97cdf0e10cSrcweir
98cdf0e10cSrcweir if ( Condition )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir pCond = (oslConditionImpl*)Condition;
101cdf0e10cSrcweir
102cdf0e10cSrcweir nRet = pthread_cond_destroy(&pCond->m_Condition);
103cdf0e10cSrcweir if ( nRet != 0 )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir OSL_TRACE("osl_destroyCondition : destroy condition failed. Errno: %d; '%s'\n",
106cdf0e10cSrcweir nRet, strerror(nRet));
107cdf0e10cSrcweir }
108cdf0e10cSrcweir nRet = pthread_mutex_destroy(&pCond->m_Lock);
109cdf0e10cSrcweir if ( nRet != 0 )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir OSL_TRACE("osl_destroyCondition : destroy mutex failed. Errno: %d; '%s'\n",
112cdf0e10cSrcweir nRet, strerror(nRet));
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir free(Condition);
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir return;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
121cdf0e10cSrcweir /*****************************************************************************/
122cdf0e10cSrcweir /* osl_setCondition */
123cdf0e10cSrcweir /*****************************************************************************/
osl_setCondition(oslCondition Condition)124cdf0e10cSrcweir sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir oslConditionImpl* pCond;
127cdf0e10cSrcweir int nRet=0;
128cdf0e10cSrcweir
129cdf0e10cSrcweir OSL_ASSERT(Condition);
130cdf0e10cSrcweir pCond = (oslConditionImpl*)Condition;
131cdf0e10cSrcweir
132*509a48ffSpfg if ( pCond == NULL )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir return sal_False;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir nRet = pthread_mutex_lock(&pCond->m_Lock);
138cdf0e10cSrcweir if ( nRet != 0 )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir OSL_TRACE("osl_setCondition : mutex lock failed. Errno: %d; %s\n",
141cdf0e10cSrcweir nRet, strerror(nRet));
142cdf0e10cSrcweir return sal_False;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir pCond->m_State = sal_True;
146cdf0e10cSrcweir nRet = pthread_cond_broadcast(&pCond->m_Condition);
147cdf0e10cSrcweir if ( nRet != 0 )
148cdf0e10cSrcweir {
149cdf0e10cSrcweir OSL_TRACE("osl_setCondition : condition broadcast failed. Errno: %d; %s\n",
150cdf0e10cSrcweir nRet, strerror(nRet));
1519b7ebb41SPedro Giffuni (void)pthread_mutex_unlock(&pCond->m_Lock);
152cdf0e10cSrcweir return sal_False;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir
155cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
156cdf0e10cSrcweir if ( nRet != 0 )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir OSL_TRACE("osl_setCondition : mutex unlock failed. Errno: %d; %s\n",
159cdf0e10cSrcweir nRet, strerror(nRet));
160cdf0e10cSrcweir return sal_False;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
163cdf0e10cSrcweir return sal_True;
164cdf0e10cSrcweir
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
167cdf0e10cSrcweir /*****************************************************************************/
168cdf0e10cSrcweir /* osl_resetCondition */
169cdf0e10cSrcweir /*****************************************************************************/
osl_resetCondition(oslCondition Condition)170cdf0e10cSrcweir sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
171cdf0e10cSrcweir {
172cdf0e10cSrcweir oslConditionImpl* pCond;
173cdf0e10cSrcweir int nRet=0;
174cdf0e10cSrcweir
175cdf0e10cSrcweir OSL_ASSERT(Condition);
176cdf0e10cSrcweir
177cdf0e10cSrcweir pCond = (oslConditionImpl*)Condition;
178cdf0e10cSrcweir
179*509a48ffSpfg if ( pCond == NULL )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir return sal_False;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir
184cdf0e10cSrcweir nRet = pthread_mutex_lock(&pCond->m_Lock);
185cdf0e10cSrcweir if ( nRet != 0 )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir OSL_TRACE("osl_resetCondition : mutex lock failed. Errno: %d; %s\n",
188cdf0e10cSrcweir nRet, strerror(nRet));
189cdf0e10cSrcweir return sal_False;
190cdf0e10cSrcweir }
191cdf0e10cSrcweir
192cdf0e10cSrcweir pCond->m_State = sal_False;
193cdf0e10cSrcweir
194cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
195cdf0e10cSrcweir if ( nRet != 0 )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir OSL_TRACE("osl_resetCondition : mutex unlock failed. Errno: %d; %s\n",
198cdf0e10cSrcweir nRet, strerror(nRet));
199cdf0e10cSrcweir return sal_False;
200cdf0e10cSrcweir }
201cdf0e10cSrcweir
202cdf0e10cSrcweir return sal_True;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir /*****************************************************************************/
206cdf0e10cSrcweir /* osl_waitCondition */
207cdf0e10cSrcweir /*****************************************************************************/
osl_waitCondition(oslCondition Condition,const TimeValue * pTimeout)208cdf0e10cSrcweir oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir oslConditionImpl* pCond;
211cdf0e10cSrcweir int nRet=0;
212cdf0e10cSrcweir oslConditionResult Result = osl_cond_result_ok;
213cdf0e10cSrcweir
214cdf0e10cSrcweir OSL_ASSERT(Condition);
215cdf0e10cSrcweir pCond = (oslConditionImpl*)Condition;
216cdf0e10cSrcweir
217*509a48ffSpfg if ( pCond == NULL )
218cdf0e10cSrcweir {
219cdf0e10cSrcweir return osl_cond_result_error;
220cdf0e10cSrcweir }
221cdf0e10cSrcweir
222cdf0e10cSrcweir nRet = pthread_mutex_lock(&pCond->m_Lock);
223cdf0e10cSrcweir if ( nRet != 0 )
224cdf0e10cSrcweir {
225cdf0e10cSrcweir OSL_TRACE("osl_waitCondition : mutex lock failed. Errno: %d; %s\n",
226cdf0e10cSrcweir nRet, strerror(nRet));
227cdf0e10cSrcweir return osl_cond_result_error;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir
230cdf0e10cSrcweir if ( pTimeout )
231cdf0e10cSrcweir {
232cdf0e10cSrcweir if ( ! pCond->m_State )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir int ret;
235cdf0e10cSrcweir struct timeval tp;
236cdf0e10cSrcweir struct timespec to;
237cdf0e10cSrcweir
238cdf0e10cSrcweir gettimeofday(&tp, NULL);
239cdf0e10cSrcweir
240cdf0e10cSrcweir SET_TIMESPEC( to, tp.tv_sec + pTimeout->Seconds,
241cdf0e10cSrcweir tp.tv_usec * 1000 + pTimeout->Nanosec );
242cdf0e10cSrcweir
243cdf0e10cSrcweir /* spurious wake up prevention */
244cdf0e10cSrcweir do
245cdf0e10cSrcweir {
246cdf0e10cSrcweir ret = pthread_cond_timedwait(&pCond->m_Condition, &pCond->m_Lock, &to);
247cdf0e10cSrcweir if ( ret != 0 )
248cdf0e10cSrcweir {
249cdf0e10cSrcweir if ( ret == ETIME || ret == ETIMEDOUT )
250cdf0e10cSrcweir {
251cdf0e10cSrcweir Result = osl_cond_result_timeout;
252cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
253cdf0e10cSrcweir if (nRet != 0)
254cdf0e10cSrcweir {
255cdf0e10cSrcweir OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
256cdf0e10cSrcweir nRet, strerror(nRet));
257cdf0e10cSrcweir }
258cdf0e10cSrcweir
259cdf0e10cSrcweir return Result;
260cdf0e10cSrcweir }
261cdf0e10cSrcweir else if ( ret != EINTR )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir Result = osl_cond_result_error;
264cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
265cdf0e10cSrcweir if ( nRet != 0 )
266cdf0e10cSrcweir {
267cdf0e10cSrcweir OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
268cdf0e10cSrcweir nRet, strerror(nRet));
269cdf0e10cSrcweir }
270cdf0e10cSrcweir return Result;
271cdf0e10cSrcweir }
272cdf0e10cSrcweir /* OSL_TRACE("EINTR\n");*/
273cdf0e10cSrcweir }
274cdf0e10cSrcweir }
275cdf0e10cSrcweir while ( !pCond->m_State );
276cdf0e10cSrcweir }
277cdf0e10cSrcweir }
278cdf0e10cSrcweir else
279cdf0e10cSrcweir {
280cdf0e10cSrcweir while ( !pCond->m_State )
281cdf0e10cSrcweir {
282cdf0e10cSrcweir nRet = pthread_cond_wait(&pCond->m_Condition, &pCond->m_Lock);
283cdf0e10cSrcweir if ( nRet != 0 )
284cdf0e10cSrcweir {
285cdf0e10cSrcweir OSL_TRACE("osl_waitCondition : condition wait failed. Errno: %d; %s\n",
286cdf0e10cSrcweir nRet, strerror(nRet));
287cdf0e10cSrcweir Result = osl_cond_result_error;
288cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
289cdf0e10cSrcweir if ( nRet != 0 )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
292cdf0e10cSrcweir nRet, strerror(nRet));
293cdf0e10cSrcweir }
294cdf0e10cSrcweir
295cdf0e10cSrcweir return Result;
296cdf0e10cSrcweir }
297cdf0e10cSrcweir }
298cdf0e10cSrcweir }
299cdf0e10cSrcweir
300cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
301cdf0e10cSrcweir if ( nRet != 0 )
302cdf0e10cSrcweir {
303cdf0e10cSrcweir OSL_TRACE("osl_waitCondition : mutex unlock failed. Errno: %d; %s\n",
304cdf0e10cSrcweir nRet, strerror(nRet));
305cdf0e10cSrcweir }
306cdf0e10cSrcweir
307cdf0e10cSrcweir return Result;
308cdf0e10cSrcweir }
309cdf0e10cSrcweir
310cdf0e10cSrcweir /*****************************************************************************/
311cdf0e10cSrcweir /* osl_checkCondition */
312cdf0e10cSrcweir /*****************************************************************************/
osl_checkCondition(oslCondition Condition)313cdf0e10cSrcweir sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
314cdf0e10cSrcweir {
315cdf0e10cSrcweir sal_Bool State;
316cdf0e10cSrcweir oslConditionImpl* pCond;
317cdf0e10cSrcweir int nRet=0;
318cdf0e10cSrcweir
319cdf0e10cSrcweir OSL_ASSERT(Condition);
320cdf0e10cSrcweir pCond = (oslConditionImpl*)Condition;
321cdf0e10cSrcweir
322*509a48ffSpfg if ( pCond == NULL )
323cdf0e10cSrcweir {
324cdf0e10cSrcweir return sal_False;
325cdf0e10cSrcweir }
326cdf0e10cSrcweir
327cdf0e10cSrcweir nRet = pthread_mutex_lock(&pCond->m_Lock);
328cdf0e10cSrcweir if ( nRet != 0 )
329cdf0e10cSrcweir {
330cdf0e10cSrcweir OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
331cdf0e10cSrcweir nRet, strerror(nRet));
332cdf0e10cSrcweir }
333cdf0e10cSrcweir
334cdf0e10cSrcweir State = pCond->m_State;
335cdf0e10cSrcweir
336cdf0e10cSrcweir nRet = pthread_mutex_unlock(&pCond->m_Lock);
337cdf0e10cSrcweir if ( nRet != 0 )
338cdf0e10cSrcweir {
339cdf0e10cSrcweir OSL_TRACE("osl_checkCondition : mutex unlock failed. Errno: %d; %s\n",
340cdf0e10cSrcweir nRet, strerror(nRet));
341cdf0e10cSrcweir }
342cdf0e10cSrcweir
343cdf0e10cSrcweir return State;
344cdf0e10cSrcweir }
345