xref: /trunk/main/salhelper/source/condition.cxx (revision f3cac8d6)
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 
25 #include <salhelper/condition.hxx>
26 #include <osl/time.h>
27 
28 
29 using namespace salhelper;
30 
31 
32 /******************************************************************
33  *                                                                *
34  *                     Condition                                  *
35  *                                                                *
36  ******************************************************************/
37 
Condition(osl::Mutex & aMutex)38 Condition::Condition(osl::Mutex& aMutex)
39 	: m_aMutex(aMutex),
40 	  m_aCondition(osl_createCondition())
41 {
42 }
43 
44 
~Condition()45 Condition::~Condition()
46 {
47 	osl_destroyCondition(m_aCondition);
48 }
49 
50 
51 /******************************************************************
52  *                                                                *
53  *              ConditionModifier                                 *
54  *                                                                *
55  ******************************************************************/
56 
ConditionModifier(Condition & aCond)57 ConditionModifier::ConditionModifier(Condition& aCond)
58 	: m_aCond(aCond)
59 {
60     m_aCond.m_aMutex.acquire();
61 }
62 
63 
~ConditionModifier()64 ConditionModifier::~ConditionModifier()
65 {
66 	if(m_aCond.applies())
67 		osl_setCondition(m_aCond.m_aCondition);
68 
69 	m_aCond.m_aMutex.release();
70 }
71 
72 
73 
74 /******************************************************************
75  *                                                                *
76  *              ConditionWaiter                                   *
77  *                                                                *
78  ******************************************************************/
79 
timedout()80 ConditionWaiter::timedout::timedout() {}
81 
timedout(timedout const &)82 ConditionWaiter::timedout::timedout(timedout const &) {}
83 
~timedout()84 ConditionWaiter::timedout::~timedout() {}
85 
86 ConditionWaiter::timedout &
operator =(timedout const &)87 ConditionWaiter::timedout::operator =(timedout const &) { return *this; }
88 
ConditionWaiter(Condition & aCond)89 ConditionWaiter::ConditionWaiter(Condition& aCond)
90 	: m_aCond(aCond)
91 {
92 	while(true) {
93 		osl_waitCondition(m_aCond.m_aCondition,0);
94 		m_aCond.m_aMutex.acquire();
95 
96 		if(m_aCond.applies())
97 			break;
98 		else {
99 			osl_resetCondition(m_aCond.m_aCondition);
100 			m_aCond.m_aMutex.release();
101 		}
102 	}
103 }
104 
105 
ConditionWaiter(Condition & aCond,sal_uInt32 milliSec)106 ConditionWaiter::ConditionWaiter(Condition& aCond,sal_uInt32 milliSec)
107 	throw(
108 		ConditionWaiter::timedout
109 	)
110 	: m_aCond(aCond)
111 {
112 	TimeValue aTime;
113 	aTime.Seconds = milliSec / 1000;
114 	aTime.Nanosec = 1000000 * ( milliSec % 1000 );
115 
116 	while(true) {
117 		if( osl_waitCondition(m_aCond.m_aCondition,&aTime) ==
118 			osl_cond_result_timeout )
119 			throw timedout();
120 
121 		m_aCond.m_aMutex.acquire();
122 
123 		if(m_aCond.applies())
124 			break;
125 		else {
126 			osl_resetCondition(m_aCond.m_aCondition);
127 			m_aCond.m_aMutex.release();
128 		}
129 	}
130 }
131 
132 
~ConditionWaiter()133 ConditionWaiter::~ConditionWaiter()
134 {
135 	if(! m_aCond.applies())
136 		osl_resetCondition(m_aCond.m_aCondition);
137 	m_aCond.m_aMutex.release();
138 }
139