xref: /trunk/main/sal/osl/w32/conditn.c (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "system.h"
29 
30 #include <osl/conditn.h>
31 #include <osl/diagnose.h>
32 #include <osl/time.h>
33 
34 /*
35 	under WIN32, we use the void* oslCondition
36 	as a WIN32 HANDLE (which is also a 32-bit value)
37 */
38 
39 /*****************************************************************************/
40 /* osl_createCondition */
41 /*****************************************************************************/
42 oslCondition SAL_CALL osl_createCondition(void)
43 {
44 	oslCondition Condition;
45 
46 	Condition= (oslCondition)CreateEvent(0,			/* no security */
47 										 sal_True,		/* manual reset */
48 										 sal_False,		/* initial state not signaled */
49 										 0);		/* automatic name */
50 
51 	return Condition;
52 
53 }
54 
55 /*****************************************************************************/
56 /* osl_destroyCondition */
57 /*****************************************************************************/
58 void SAL_CALL osl_destroyCondition(oslCondition Condition)
59 {
60 	if(Condition)
61 	{
62 		OSL_VERIFY(CloseHandle(Condition));
63 	}
64 }
65 
66 /*****************************************************************************/
67 /* osl_setCondition */
68 /*****************************************************************************/
69 sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
70 {
71 	OSL_ASSERT(Condition);
72 
73 	return (sal_Bool)(SetEvent((HANDLE)Condition) != FALSE);
74 }
75 
76 /*****************************************************************************/
77 /* osl_resetCondition */
78 /*****************************************************************************/
79 sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
80 {
81 	OSL_ASSERT(Condition);
82 
83 	return (sal_Bool)(ResetEvent((HANDLE)Condition) != FALSE);
84 }
85 
86 /*****************************************************************************/
87 /* osl_waitCondition */
88 /*****************************************************************************/
89 oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition,
90 									 const TimeValue* pTimeout)
91 {
92 	DWORD timeout;
93 
94 	OSL_ASSERT(Condition);
95 
96 	if (pTimeout)
97 		timeout = pTimeout->Seconds * 1000 + pTimeout->Nanosec / 1000000L;
98 	else
99 		timeout = INFINITE;
100 
101 	/* It's necessary to process SendMessage calls to the current thread to give other threads
102 		access to COM objects instatiated in this thread */
103 
104 	while ( 1 )
105 	{
106 		/* Only wake up if a SendMessage call to the threads message loop is detected */
107         switch( MsgWaitForMultipleObjects( 1, (HANDLE *)(&Condition), FALSE, timeout, QS_SENDMESSAGE ) )
108 		{
109 			case WAIT_OBJECT_0 + 1:
110 				{
111 				MSG	msg;
112 
113 				/* We Must not dispatch the message. PM_NOREMOVE leaves the message queue untouched
114 				 but dispatches SendMessage calls automatically */
115 
116 				PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE );
117 				}
118 				break;
119 
120 			case WAIT_OBJECT_0:
121 				return (osl_cond_result_ok);
122 
123 			case WAIT_TIMEOUT:
124 				return (osl_cond_result_timeout);
125 
126 			default:
127 				return (osl_cond_result_error);
128 		}
129 	}
130 }
131 
132 /*****************************************************************************/
133 /* osl_checkCondition */
134 /*****************************************************************************/
135 sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
136 {
137 	OSL_ASSERT(Condition);
138 
139 	return (sal_Bool)(WaitForSingleObject((HANDLE)Condition, 0) == WAIT_OBJECT_0);
140 }
141 
142