xref: /aoo41x/main/salhelper/source/condition.cxx (revision f3cac8d6)
1*f3cac8d6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f3cac8d6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f3cac8d6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f3cac8d6SAndrew Rist  * distributed with this work for additional information
6*f3cac8d6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f3cac8d6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f3cac8d6SAndrew Rist  * "License"); you may not use this file except in compliance
9*f3cac8d6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f3cac8d6SAndrew Rist  *
11*f3cac8d6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f3cac8d6SAndrew Rist  *
13*f3cac8d6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f3cac8d6SAndrew Rist  * software distributed under the License is distributed on an
15*f3cac8d6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f3cac8d6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f3cac8d6SAndrew Rist  * specific language governing permissions and limitations
18*f3cac8d6SAndrew Rist  * under the License.
19*f3cac8d6SAndrew Rist  *
20*f3cac8d6SAndrew Rist  *************************************************************/
21*f3cac8d6SAndrew Rist 
22*f3cac8d6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <salhelper/condition.hxx>
26cdf0e10cSrcweir #include <osl/time.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using namespace salhelper;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /******************************************************************
33cdf0e10cSrcweir  *                                                                *
34cdf0e10cSrcweir  *                     Condition                                  *
35cdf0e10cSrcweir  *                                                                *
36cdf0e10cSrcweir  ******************************************************************/
37cdf0e10cSrcweir 
Condition(osl::Mutex & aMutex)38cdf0e10cSrcweir Condition::Condition(osl::Mutex& aMutex)
39cdf0e10cSrcweir 	: m_aMutex(aMutex),
40cdf0e10cSrcweir 	  m_aCondition(osl_createCondition())
41cdf0e10cSrcweir {
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
~Condition()45cdf0e10cSrcweir Condition::~Condition()
46cdf0e10cSrcweir {
47cdf0e10cSrcweir 	osl_destroyCondition(m_aCondition);
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /******************************************************************
52cdf0e10cSrcweir  *                                                                *
53cdf0e10cSrcweir  *              ConditionModifier                                 *
54cdf0e10cSrcweir  *                                                                *
55cdf0e10cSrcweir  ******************************************************************/
56cdf0e10cSrcweir 
ConditionModifier(Condition & aCond)57cdf0e10cSrcweir ConditionModifier::ConditionModifier(Condition& aCond)
58cdf0e10cSrcweir 	: m_aCond(aCond)
59cdf0e10cSrcweir {
60cdf0e10cSrcweir     m_aCond.m_aMutex.acquire();
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
~ConditionModifier()64cdf0e10cSrcweir ConditionModifier::~ConditionModifier()
65cdf0e10cSrcweir {
66cdf0e10cSrcweir 	if(m_aCond.applies())
67cdf0e10cSrcweir 		osl_setCondition(m_aCond.m_aCondition);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 	m_aCond.m_aMutex.release();
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
74cdf0e10cSrcweir /******************************************************************
75cdf0e10cSrcweir  *                                                                *
76cdf0e10cSrcweir  *              ConditionWaiter                                   *
77cdf0e10cSrcweir  *                                                                *
78cdf0e10cSrcweir  ******************************************************************/
79cdf0e10cSrcweir 
timedout()80cdf0e10cSrcweir ConditionWaiter::timedout::timedout() {}
81cdf0e10cSrcweir 
timedout(timedout const &)82cdf0e10cSrcweir ConditionWaiter::timedout::timedout(timedout const &) {}
83cdf0e10cSrcweir 
~timedout()84cdf0e10cSrcweir ConditionWaiter::timedout::~timedout() {}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir ConditionWaiter::timedout &
operator =(timedout const &)87cdf0e10cSrcweir ConditionWaiter::timedout::operator =(timedout const &) { return *this; }
88cdf0e10cSrcweir 
ConditionWaiter(Condition & aCond)89cdf0e10cSrcweir ConditionWaiter::ConditionWaiter(Condition& aCond)
90cdf0e10cSrcweir 	: m_aCond(aCond)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	while(true) {
93cdf0e10cSrcweir 		osl_waitCondition(m_aCond.m_aCondition,0);
94cdf0e10cSrcweir 		m_aCond.m_aMutex.acquire();
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 		if(m_aCond.applies())
97cdf0e10cSrcweir 			break;
98cdf0e10cSrcweir 		else {
99cdf0e10cSrcweir 			osl_resetCondition(m_aCond.m_aCondition);
100cdf0e10cSrcweir 			m_aCond.m_aMutex.release();
101cdf0e10cSrcweir 		}
102cdf0e10cSrcweir 	}
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 
ConditionWaiter(Condition & aCond,sal_uInt32 milliSec)106cdf0e10cSrcweir ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec)
107cdf0e10cSrcweir 	throw(
108cdf0e10cSrcweir 		ConditionWaiter::timedout
109cdf0e10cSrcweir 	)
110cdf0e10cSrcweir 	: m_aCond(aCond)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	TimeValue aTime;
113cdf0e10cSrcweir 	aTime.Seconds = milliSec / 1000;
114cdf0e10cSrcweir 	aTime.Nanosec = 1000000 * ( milliSec % 1000 );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	while(true) {
117cdf0e10cSrcweir 		if( osl_waitCondition(m_aCond.m_aCondition,&aTime) ==
118cdf0e10cSrcweir 			osl_cond_result_timeout )
119cdf0e10cSrcweir 			throw timedout();
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		m_aCond.m_aMutex.acquire();
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 		if(m_aCond.applies())
124cdf0e10cSrcweir 			break;
125cdf0e10cSrcweir 		else {
126cdf0e10cSrcweir 			osl_resetCondition(m_aCond.m_aCondition);
127cdf0e10cSrcweir 			m_aCond.m_aMutex.release();
128cdf0e10cSrcweir 		}
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
~ConditionWaiter()133cdf0e10cSrcweir ConditionWaiter::~ConditionWaiter()
134cdf0e10cSrcweir {
135cdf0e10cSrcweir 	if(! m_aCond.applies())
136cdf0e10cSrcweir 		osl_resetCondition(m_aCond.m_aCondition);
137cdf0e10cSrcweir 	m_aCond.m_aMutex.release();
138cdf0e10cSrcweir }
139