xref: /aoo41x/main/sal/osl/unx/conditn.c (revision 647f063d)
1*647f063dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*647f063dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*647f063dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*647f063dSAndrew Rist  * distributed with this work for additional information
6*647f063dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*647f063dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*647f063dSAndrew Rist  * "License"); you may not use this file except in compliance
9*647f063dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*647f063dSAndrew Rist  *
11*647f063dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*647f063dSAndrew Rist  *
13*647f063dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*647f063dSAndrew Rist  * software distributed under the License is distributed on an
15*647f063dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*647f063dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*647f063dSAndrew Rist  * specific language governing permissions and limitations
18*647f063dSAndrew Rist  * under the License.
19*647f063dSAndrew Rist  *
20*647f063dSAndrew Rist  *************************************************************/
21*647f063dSAndrew Rist 
22*647f063dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include "system.h"
26cdf0e10cSrcweir #include <sal/types.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <osl/conditn.h>
29cdf0e10cSrcweir #include <osl/diagnose.h>
30cdf0e10cSrcweir #include <osl/time.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir typedef struct _oslConditionImpl
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	pthread_cond_t	m_Condition;
36cdf0e10cSrcweir 	pthread_mutex_t	m_Lock;
37cdf0e10cSrcweir 	sal_Bool			m_State;
38cdf0e10cSrcweir } oslConditionImpl;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /*****************************************************************************/
42cdf0e10cSrcweir /* osl_createCondition */
43cdf0e10cSrcweir /*****************************************************************************/
osl_createCondition()44cdf0e10cSrcweir oslCondition SAL_CALL osl_createCondition()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir   	oslConditionImpl* pCond;
47cdf0e10cSrcweir     int nRet=0;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	pCond = (oslConditionImpl*) malloc(sizeof(oslConditionImpl));
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	OSL_ASSERT(pCond);
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	if ( pCond == 0 )
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		return 0;
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	pCond->m_State = sal_False;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	/* init condition variable with default attr. (PTHREAD_PROCESS_PRIVAT) */
61cdf0e10cSrcweir     nRet =  pthread_cond_init(&pCond->m_Condition, PTHREAD_CONDATTR_DEFAULT);
62cdf0e10cSrcweir 	if ( nRet != 0 )
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 	    OSL_TRACE("osl_createCondition : condition init failed. Errno: %d; '%s'\n",
65cdf0e10cSrcweir                   nRet, strerror(nRet));
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	    free(pCond);
68cdf0e10cSrcweir 		return 0;
69cdf0e10cSrcweir 	}
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     nRet = pthread_mutex_init(&pCond->m_Lock, PTHREAD_MUTEXATTR_DEFAULT);
72cdf0e10cSrcweir 	if ( nRet != 0 )
73cdf0e10cSrcweir 	{
74cdf0e10cSrcweir 	    OSL_TRACE("osl_createCondition : mutex init failed. Errno: %d; %s\n",
75cdf0e10cSrcweir                   nRet, strerror(nRet));
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         nRet = pthread_cond_destroy(&pCond->m_Condition);
78cdf0e10cSrcweir 	    if ( nRet != 0 )
79cdf0e10cSrcweir         {
80cdf0e10cSrcweir             OSL_TRACE("osl_createCondition : destroy condition failed. Errno: %d; '%s'\n",
81cdf0e10cSrcweir                       nRet, strerror(nRet));
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         free(pCond);
85cdf0e10cSrcweir 	    pCond = 0;
86cdf0e10cSrcweir 	}
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	return (oslCondition)pCond;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir /*****************************************************************************/
92cdf0e10cSrcweir /* osl_destroyCondition */
93cdf0e10cSrcweir /*****************************************************************************/
osl_destroyCondition(oslCondition Condition)94cdf0e10cSrcweir void SAL_CALL osl_destroyCondition(oslCondition Condition)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     oslConditionImpl* pCond;
97cdf0e10cSrcweir     int nRet = 0;
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	if ( Condition )
100cdf0e10cSrcweir 	{
101cdf0e10cSrcweir 	    pCond = (oslConditionImpl*)Condition;
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         nRet = pthread_cond_destroy(&pCond->m_Condition);
104cdf0e10cSrcweir 		if ( nRet != 0 )
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             OSL_TRACE("osl_destroyCondition : destroy condition failed. Errno: %d; '%s'\n",
107cdf0e10cSrcweir                       nRet, strerror(nRet));
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         nRet = pthread_mutex_destroy(&pCond->m_Lock);
110cdf0e10cSrcweir 		if ( nRet != 0 )
111cdf0e10cSrcweir         {
112cdf0e10cSrcweir             OSL_TRACE("osl_destroyCondition : destroy mutex failed. Errno: %d; '%s'\n",
113cdf0e10cSrcweir                       nRet, strerror(nRet));
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		free(Condition);
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir 
119cdf0e10cSrcweir     return;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir /*****************************************************************************/
123cdf0e10cSrcweir /* osl_setCondition */
124cdf0e10cSrcweir /*****************************************************************************/
osl_setCondition(oslCondition Condition)125cdf0e10cSrcweir sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir    oslConditionImpl* pCond;
128cdf0e10cSrcweir    int nRet=0;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir    OSL_ASSERT(Condition);
131cdf0e10cSrcweir    pCond = (oslConditionImpl*)Condition;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir    if ( pCond == 0 )
134cdf0e10cSrcweir    {
135cdf0e10cSrcweir 	   return sal_False;
136cdf0e10cSrcweir    }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir    nRet = pthread_mutex_lock(&pCond->m_Lock);
139cdf0e10cSrcweir    if ( nRet != 0 )
140cdf0e10cSrcweir    {
141cdf0e10cSrcweir        OSL_TRACE("osl_setCondition : mutex lock failed. Errno: %d; %s\n",
142cdf0e10cSrcweir                   nRet, strerror(nRet));
143cdf0e10cSrcweir 	   return sal_False;
144cdf0e10cSrcweir    }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir    pCond->m_State = sal_True;
147cdf0e10cSrcweir    nRet = pthread_cond_broadcast(&pCond->m_Condition);
148cdf0e10cSrcweir    if ( nRet != 0 )
149cdf0e10cSrcweir    {
150cdf0e10cSrcweir        OSL_TRACE("osl_setCondition : condition broadcast failed. Errno: %d; %s\n",
151cdf0e10cSrcweir                   nRet, strerror(nRet));
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 
179cdf0e10cSrcweir 	if ( pCond == 0 )
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 
217cdf0e10cSrcweir 	if ( pCond == 0 )
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 
322cdf0e10cSrcweir 	if ( pCond == 0 )
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 }
345cdf0e10cSrcweir 
346cdf0e10cSrcweir 
347