xref: /aoo4110/main/sal/osl/w32/conditn.c (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "system.h"
25 
26 #include <osl/conditn.h>
27 #include <osl/diagnose.h>
28 #include <osl/time.h>
29 
30 /*
31 	under WIN32, we use the void* oslCondition
32 	as a WIN32 HANDLE (which is also a 32-bit value)
33 */
34 
35 /*****************************************************************************/
36 /* osl_createCondition */
37 /*****************************************************************************/
osl_createCondition(void)38 oslCondition SAL_CALL osl_createCondition(void)
39 {
40 	oslCondition Condition;
41 
42 	Condition= (oslCondition)CreateEvent(0,			/* no security */
43 										 sal_True,		/* manual reset */
44 										 sal_False,		/* initial state not signaled */
45 										 0);		/* automatic name */
46 
47 	return Condition;
48 
49 }
50 
51 /*****************************************************************************/
52 /* osl_destroyCondition */
53 /*****************************************************************************/
osl_destroyCondition(oslCondition Condition)54 void SAL_CALL osl_destroyCondition(oslCondition Condition)
55 {
56 	if(Condition)
57 	{
58 		OSL_VERIFY(CloseHandle(Condition));
59 	}
60 }
61 
62 /*****************************************************************************/
63 /* osl_setCondition */
64 /*****************************************************************************/
osl_setCondition(oslCondition Condition)65 sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
66 {
67 	OSL_ASSERT(Condition);
68 
69 	return (sal_Bool)(SetEvent((HANDLE)Condition) != FALSE);
70 }
71 
72 /*****************************************************************************/
73 /* osl_resetCondition */
74 /*****************************************************************************/
osl_resetCondition(oslCondition Condition)75 sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
76 {
77 	OSL_ASSERT(Condition);
78 
79 	return (sal_Bool)(ResetEvent((HANDLE)Condition) != FALSE);
80 }
81 
82 /*****************************************************************************/
83 /* osl_waitCondition */
84 /*****************************************************************************/
osl_waitCondition(oslCondition Condition,const TimeValue * pTimeout)85 oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition,
86 									 const TimeValue* pTimeout)
87 {
88 	DWORD timeout;
89 
90 	OSL_ASSERT(Condition);
91 
92 	if (pTimeout)
93 		timeout = pTimeout->Seconds * 1000 + pTimeout->Nanosec / 1000000L;
94 	else
95 		timeout = INFINITE;
96 
97 	/* It's necessary to process SendMessage calls to the current thread to give other threads
98 		access to COM objects instatiated in this thread */
99 
100 	while ( 1 )
101 	{
102 		/* Only wake up if a SendMessage call to the threads message loop is detected */
103         switch( MsgWaitForMultipleObjects( 1, (HANDLE *)(&Condition), FALSE, timeout, QS_SENDMESSAGE ) )
104 		{
105 			case WAIT_OBJECT_0 + 1:
106 				{
107 				MSG	msg;
108 
109 				/* We Must not dispatch the message. PM_NOREMOVE leaves the message queue untouched
110 				 but dispatches SendMessage calls automatically */
111 
112 				PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE );
113 				}
114 				break;
115 
116 			case WAIT_OBJECT_0:
117 				return (osl_cond_result_ok);
118 
119 			case WAIT_TIMEOUT:
120 				return (osl_cond_result_timeout);
121 
122 			default:
123 				return (osl_cond_result_error);
124 		}
125 	}
126 }
127 
128 /*****************************************************************************/
129 /* osl_checkCondition */
130 /*****************************************************************************/
osl_checkCondition(oslCondition Condition)131 sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
132 {
133 	OSL_ASSERT(Condition);
134 
135 	return (sal_Bool)(WaitForSingleObject((HANDLE)Condition, 0) == WAIT_OBJECT_0);
136 }
137 
138