Update ooo320-m1
[ooovba.git] / framework / inc / threadhelp / gate.hxx
blobc48e26476808ea6ac3dc0473c1938fffd0336784
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gate.hxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_THREADHELP_GATE_HXX_
32 #define __FRAMEWORK_THREADHELP_GATE_HXX_
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
38 #include <threadhelp/inoncopyable.h>
39 #include <threadhelp/igate.h>
41 //_________________________________________________________________________________________________________________
42 // interface includes
43 //_________________________________________________________________________________________________________________
45 //_________________________________________________________________________________________________________________
46 // other includes
47 //_________________________________________________________________________________________________________________
48 #include <osl/mutex.hxx>
49 #include <osl/conditn.hxx>
51 //_________________________________________________________________________________________________________________
52 // namespace
53 //_________________________________________________________________________________________________________________
55 namespace framework{
57 //_________________________________________________________________________________________________________________
58 // const
59 //_________________________________________________________________________________________________________________
61 //_________________________________________________________________________________________________________________
62 // declarations
63 //_________________________________________________________________________________________________________________
65 /*-************************************************************************************************************//**
66 @short implement a gate to block multiple threads at same time or unblock all
67 @descr A gate can be used as a negative-condition! You can open a "door" - wait() will not block ...
68 or you can close it - wait() blocks till open() is called again.
69 As a special feature you can open the gate a little bit by sing openGap().
70 Then all currently waiting threads are running immediately - but new ones are blocked!
72 @attention To prevent us against wrong using, the default ctor, copy ctor and the =operator are maked private!
74 @implements IGate
75 @base IGate
76 INonCopyable
78 @devstatus ready to use
79 *//*-*************************************************************************************************************/
80 class Gate : public IGate
81 , private INonCopyable
83 //-------------------------------------------------------------------------------------------------------------
84 // public methods
85 //-------------------------------------------------------------------------------------------------------------
86 public:
88 /*-****************************************************************************************************//**
89 @short ctor
90 @descr These initialize the object right as an open gate.
92 @seealso -
94 @param -
95 @return -
97 @onerror -
98 *//*-*****************************************************************************************************/
99 inline Gate()
100 : m_bClosed ( sal_False )
101 , m_bGapOpen ( sal_False )
103 open();
106 /*-****************************************************************************************************//**
107 @short dtor
108 @descr Is user forget it - we open the gate ...
109 blocked threads can running ... but I don't know
110 if it's right - we are destroyed yet!?
112 @seealso -
114 @param -
115 @return -
117 @onerror -
118 *//*-*****************************************************************************************************/
119 inline virtual ~Gate()
121 open();
124 /*-****************************************************************************************************//**
125 @interface IGate
126 @short open the gate
127 @descr A wait() call will not block then.
129 @seealso method close()
131 @param -
132 @return -
134 @onerror -
135 *//*-*****************************************************************************************************/
136 inline virtual void open()
138 // We must safe access to our internal member!
139 ::osl::MutexGuard aLock( m_aAccessLock );
140 // Set condition -> wait don't block any longer -> gate is open
141 m_aPassage.set();
142 // Check if operation was successful!
143 // Check returns false if condition isn't set => m_bClosed will be true then => we must return false; opening failed
144 m_bClosed = ( m_aPassage.check() == sal_False );
147 /*-****************************************************************************************************//**
148 @interface IGate
149 @short close the gate
150 @descr A wait() call will block then.
152 @seealso method open()
154 @param -
155 @return -
157 @onerror -
158 *//*-*****************************************************************************************************/
159 inline virtual void close()
161 // We must safe access to our internal member!
162 ::osl::MutexGuard aLock( m_aAccessLock );
163 // Reset condition -> wait blocks now -> gate is closed
164 m_aPassage.reset();
165 // Check if operation was successful!
166 // Check returns false if condition was reseted => m_bClosed will be true then => we can return true; closing ok
167 m_bClosed = ( m_aPassage.check() == sal_False );
170 /*-****************************************************************************************************//**
171 @interface IGate
172 @short open gate for current waiting threads
173 @descr All current waiting threads stand in wait() at line "m_aPassage.wait()" ...
174 With this call you can open the passage for these waiting ones.
175 The "gap" is closed by any new thread which call wait() automaticly!
177 @seealso method wait()
178 @seealso method open()
180 @param -
181 @return -
183 @onerror -
184 *//*-*****************************************************************************************************/
185 inline virtual void openGap()
187 // We must safe access to our internal member!
188 ::osl::MutexGuard aLock( m_aAccessLock );
189 // Open passage for current waiting threads.
190 m_aPassage.set();
191 // Check state of condition.
192 // If condition is set check() returns true => m_bGapOpen will be true too => we can use it as return value.
193 m_bGapOpen = ( m_aPassage.check() == sal_True );
196 /*-****************************************************************************************************//**
197 @interface IGate
198 @short must be called to pass the gate
199 @descr If gate "open" => wait() will not block.
200 If gate "closed" => wait() will block till somewhere open it again.
201 If gap "open" => currently waiting threads unblocked, new ones blocked
203 @seealso method wait()
204 @seealso method open()
206 @param "pTimeOut", optional parameter to wait a certain time
207 @return true, if wait was successful (gate was opened)
208 false, if condition has an error or timeout was reached!
210 @onerror We return false.
211 *//*-*****************************************************************************************************/
212 inline virtual sal_Bool wait( const TimeValue* pTimeOut = NULL )
214 // We must safe access to our internal member!
215 ::osl::ClearableMutexGuard aLock( m_aAccessLock );
216 // If gate not closed - caller can pass it.
217 sal_Bool bSuccessful = sal_True;
218 if( m_bClosed == sal_True )
220 // Otherwise first new thread must close an open gap!
221 if( m_bGapOpen == sal_True )
223 m_bGapOpen = sal_False;
224 m_aPassage.reset();
226 // Then we must release used access lock -
227 // because next call will block ...
228 // and if we hold the access lock nobody else can use this object without a dadlock!
229 aLock.clear();
230 // Wait for opening gate ...
231 bSuccessful = ( m_aPassage.wait( pTimeOut ) == ::osl::Condition::result_ok );
234 return bSuccessful;
237 //-------------------------------------------------------------------------------------------------------------
238 // private member
239 //-------------------------------------------------------------------------------------------------------------
240 private:
242 ::osl::Mutex m_aAccessLock ;
243 ::osl::Condition m_aPassage ;
244 sal_Bool m_bClosed ;
245 sal_Bool m_bGapOpen ;
247 }; // class Gate
249 } // namespace framework
251 #endif // #ifndef __FRAMEWORK_THREADHELP_GATE_HXX_