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