1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_FRAMEWORK_INC_THREADHELP_GATE_HXX
21 #define INCLUDED_FRAMEWORK_INC_THREADHELP_GATE_HXX
23 #include <boost/noncopyable.hpp>
25 #include <osl/mutex.hxx>
26 #include <osl/conditn.hxx>
30 /*-************************************************************************************************************
31 @short implement a gate to block multiple threads at same time or unblock all
32 @descr A gate can be used as a negative-condition! You can open a "door" - wait() will not block ...
33 or you can close it - wait() blocks till open() is called again.
34 Then all currently waiting threads are running immediately - but new ones are blocked!
36 @attention To prevent us against wrong using, the default ctor, copy ctor and the =operator are marked private!
38 @devstatus ready to use
39 *//*-*************************************************************************************************************/
40 class Gate
: private boost::noncopyable
47 /*-****************************************************************************************************
49 @descr These initialize the object right as an open gate.
50 *//*-*****************************************************************************************************/
57 /*-****************************************************************************************************
59 @descr Is user forget it - we open the gate ...
60 blocked threads can running ... but I don't know
61 if it's right - we are destroyed yet!?
62 *//*-*****************************************************************************************************/
68 /*-****************************************************************************************************
70 @descr A wait() call will not block then.
72 @seealso method close()
73 *//*-*****************************************************************************************************/
76 // We must safe access to our internal member!
77 ::osl::MutexGuard
aLock( m_aAccessLock
);
78 // Set condition -> wait don't block any longer -> gate is open
80 // Check if operation was successful!
81 // Check returns false if condition isn't set => m_bClosed will be true then => we must return false; opening failed
82 m_bClosed
= !m_aPassage
.check();
85 /*-****************************************************************************************************
87 @descr A wait() call will block then.
89 @seealso method open()
90 *//*-*****************************************************************************************************/
93 // We must safe access to our internal member!
94 ::osl::MutexGuard
aLock( m_aAccessLock
);
95 // Reset condition -> wait blocks now -> gate is closed
97 // Check if operation was successful!
98 // Check returns false if condition was reseted => m_bClosed will be true then => we can return true; closing ok
99 m_bClosed
= !m_aPassage
.check();
102 /*-****************************************************************************************************
103 @short must be called to pass the gate
104 @descr If gate "open" => wait() will not block.
105 If gate "closed" => wait() will block till somewhere open it again.
107 @seealso method wait()
108 @seealso method open()
110 @param "pTimeOut", optional parameter to wait a certain time
111 @return true, if wait was successful (gate was opened)
112 false, if condition has an error or timeout was reached!
114 @onerror We return false.
115 *//*-*****************************************************************************************************/
116 bool wait(const TimeValue
* pTimeOut
= nullptr)
118 // We must safe access to our internal member!
119 ::osl::ClearableMutexGuard
aLock( m_aAccessLock
);
120 // If gate not closed - caller can pass it.
121 bool bSuccessful
= true;
124 // Then we must release used access lock -
125 // because next call will block ...
126 // and if we hold the access lock nobody else can use this object without a dadlock!
128 // Wait for opening gate ...
129 bSuccessful
= ( m_aPassage
.wait( pTimeOut
) == ::osl::Condition::result_ok
);
139 ::osl::Mutex m_aAccessLock
;
140 ::osl::Condition m_aPassage
;
145 } // namespace framework
147 #endif // INCLUDED_FRAMEWORK_INC_THREADHELP_GATE_HXX
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */