xref: /aoo41x/main/sal/osl/w32/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 #include "system.h"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <osl/conditn.h>
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/time.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /*
35*cdf0e10cSrcweir 	under WIN32, we use the void* oslCondition
36*cdf0e10cSrcweir 	as a WIN32 HANDLE (which is also a 32-bit value)
37*cdf0e10cSrcweir */
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir /*****************************************************************************/
40*cdf0e10cSrcweir /* osl_createCondition */
41*cdf0e10cSrcweir /*****************************************************************************/
42*cdf0e10cSrcweir oslCondition SAL_CALL osl_createCondition(void)
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 	oslCondition Condition;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 	Condition= (oslCondition)CreateEvent(0,			/* no security */
47*cdf0e10cSrcweir 										 sal_True,		/* manual reset */
48*cdf0e10cSrcweir 										 sal_False,		/* initial state not signaled */
49*cdf0e10cSrcweir 										 0);		/* automatic name */
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	return Condition;
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir /*****************************************************************************/
56*cdf0e10cSrcweir /* osl_destroyCondition */
57*cdf0e10cSrcweir /*****************************************************************************/
58*cdf0e10cSrcweir void SAL_CALL osl_destroyCondition(oslCondition Condition)
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	if(Condition)
61*cdf0e10cSrcweir 	{
62*cdf0e10cSrcweir 		OSL_VERIFY(CloseHandle(Condition));
63*cdf0e10cSrcweir 	}
64*cdf0e10cSrcweir }
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir /*****************************************************************************/
67*cdf0e10cSrcweir /* osl_setCondition */
68*cdf0e10cSrcweir /*****************************************************************************/
69*cdf0e10cSrcweir sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir 	OSL_ASSERT(Condition);
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	return (sal_Bool)(SetEvent((HANDLE)Condition) != FALSE);
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir /*****************************************************************************/
77*cdf0e10cSrcweir /* osl_resetCondition */
78*cdf0e10cSrcweir /*****************************************************************************/
79*cdf0e10cSrcweir sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	OSL_ASSERT(Condition);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	return (sal_Bool)(ResetEvent((HANDLE)Condition) != FALSE);
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir /*****************************************************************************/
87*cdf0e10cSrcweir /* osl_waitCondition */
88*cdf0e10cSrcweir /*****************************************************************************/
89*cdf0e10cSrcweir oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition,
90*cdf0e10cSrcweir 									 const TimeValue* pTimeout)
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	DWORD timeout;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	OSL_ASSERT(Condition);
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 	if (pTimeout)
97*cdf0e10cSrcweir 		timeout = pTimeout->Seconds * 1000 + pTimeout->Nanosec / 1000000L;
98*cdf0e10cSrcweir 	else
99*cdf0e10cSrcweir 		timeout = INFINITE;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	/* It's necessary to process SendMessage calls to the current thread to give other threads
102*cdf0e10cSrcweir 		access to COM objects instatiated in this thread */
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	while ( 1 )
105*cdf0e10cSrcweir 	{
106*cdf0e10cSrcweir 		/* Only wake up if a SendMessage call to the threads message loop is detected */
107*cdf0e10cSrcweir         switch( MsgWaitForMultipleObjects( 1, (HANDLE *)(&Condition), FALSE, timeout, QS_SENDMESSAGE ) )
108*cdf0e10cSrcweir 		{
109*cdf0e10cSrcweir 			case WAIT_OBJECT_0 + 1:
110*cdf0e10cSrcweir 				{
111*cdf0e10cSrcweir 				MSG	msg;
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 				/* We Must not dispatch the message. PM_NOREMOVE leaves the message queue untouched
114*cdf0e10cSrcweir 				 but dispatches SendMessage calls automatically */
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 				PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE );
117*cdf0e10cSrcweir 				}
118*cdf0e10cSrcweir 				break;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 			case WAIT_OBJECT_0:
121*cdf0e10cSrcweir 				return (osl_cond_result_ok);
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 			case WAIT_TIMEOUT:
124*cdf0e10cSrcweir 				return (osl_cond_result_timeout);
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 			default:
127*cdf0e10cSrcweir 				return (osl_cond_result_error);
128*cdf0e10cSrcweir 		}
129*cdf0e10cSrcweir 	}
130*cdf0e10cSrcweir }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir /*****************************************************************************/
133*cdf0e10cSrcweir /* osl_checkCondition */
134*cdf0e10cSrcweir /*****************************************************************************/
135*cdf0e10cSrcweir sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	OSL_ASSERT(Condition);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	return (sal_Bool)(WaitForSingleObject((HANDLE)Condition, 0) == WAIT_OBJECT_0);
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142