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