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 #ifndef _LIFETIME_HXX 24 #define _LIFETIME_HXX 25 26 #include <osl/mutex.hxx> 27 #include <osl/conditn.hxx> 28 #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HDL_ 29 #include <com/sun/star/uno/Exception.hdl> 30 #endif 31 #include <cppuhelper/interfacecontainer.hxx> 32 #include <com/sun/star/util/XCloseListener.hpp> 33 #include <com/sun/star/util/XCloseable.hpp> 34 #include <com/sun/star/lang/XComponent.hpp> 35 #include <cppuhelper/weakref.hxx> 36 #include "charttoolsdllapi.hxx" 37 38 namespace apphelper 39 { 40 41 class LifeTimeGuard; 42 class LifeTimeManager 43 { 44 friend class LifeTimeGuard; 45 protected: 46 mutable ::osl::Mutex m_aAccessMutex; 47 public: 48 OOO_DLLPUBLIC_CHARTTOOLS LifeTimeManager( ::com::sun::star::lang::XComponent* pComponent, sal_Bool bLongLastingCallsCancelable = sal_False ); 49 OOO_DLLPUBLIC_CHARTTOOLS virtual ~LifeTimeManager(); 50 51 OOO_DLLPUBLIC_CHARTTOOLS bool impl_isDisposed( bool bAssert=true ); 52 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool dispose(); 53 54 public: 55 ::cppu::OMultiTypeInterfaceContainerHelper m_aListenerContainer; 56 57 protected: 58 virtual sal_Bool impl_canStartApiCall(); impl_apiCallCountReachedNull()59 virtual void impl_apiCallCountReachedNull(){} 60 61 void impl_registerApiCall(sal_Bool bLongLastingCall); 62 void impl_unregisterApiCall(sal_Bool bLongLastingCall); 63 64 void impl_init(); 65 66 protected: 67 ::com::sun::star::lang::XComponent* m_pComponent; 68 69 ::osl::Condition m_aNoAccessCountCondition; 70 sal_Int32 volatile m_nAccessCount; 71 72 sal_Bool volatile m_bDisposed; 73 sal_Bool volatile m_bInDispose; 74 75 // 76 sal_Bool m_bLongLastingCallsCancelable; 77 ::osl::Condition m_aNoLongLastingCallCountCondition; 78 sal_Int32 volatile m_nLongLastingCallCount; 79 }; 80 81 class CloseableLifeTimeManager : public LifeTimeManager 82 { 83 protected: 84 ::com::sun::star::util::XCloseable* m_pCloseable; 85 86 ::osl::Condition m_aEndTryClosingCondition; 87 sal_Bool volatile m_bClosed; 88 sal_Bool volatile m_bInTryClose; 89 //the ownership between model and controller is not clear at first 90 //each controller might consider him as owner of the model first 91 //at start the model is not considered as owner of itself 92 sal_Bool volatile m_bOwnership; 93 //with a XCloseable::close call and during XCloseListener::queryClosing 94 //the ownership can be regulated more explicit, 95 //if so the ownership is considered to be well known 96 sal_Bool volatile m_bOwnershipIsWellKnown; 97 98 public: 99 OOO_DLLPUBLIC_CHARTTOOLS CloseableLifeTimeManager( ::com::sun::star::util::XCloseable* pCloseable 100 , ::com::sun::star::lang::XComponent* pComponent 101 , sal_Bool bLongLastingCallsCancelable = sal_False ); 102 OOO_DLLPUBLIC_CHARTTOOLS virtual ~CloseableLifeTimeManager(); 103 104 OOO_DLLPUBLIC_CHARTTOOLS bool impl_isDisposedOrClosed( bool bAssert=true ); 105 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool g_close_startTryClose(sal_Bool bDeliverOwnership); 106 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool g_close_isNeedToCancelLongLastingCalls( sal_Bool bDeliverOwnership, ::com::sun::star::util::CloseVetoException& ex ); 107 OOO_DLLPUBLIC_CHARTTOOLS void g_close_endTryClose(sal_Bool bDeliverOwnership, sal_Bool bMyVeto ); 108 OOO_DLLPUBLIC_CHARTTOOLS void g_close_endTryClose_doClose(); 109 OOO_DLLPUBLIC_CHARTTOOLS sal_Bool g_addCloseListener( const ::com::sun::star::uno::Reference< 110 ::com::sun::star::util::XCloseListener > & xListener ); 111 112 protected: 113 virtual sal_Bool impl_canStartApiCall(); 114 virtual void impl_apiCallCountReachedNull(); 115 116 void impl_setOwnership( sal_Bool bDeliverOwnership, sal_Bool bMyVeto ); 117 sal_Bool impl_shouldCloseAtNextChance(); 118 void impl_doClose(); 119 impl_init()120 void impl_init() 121 { 122 m_bClosed = sal_False; 123 m_bInTryClose = sal_False; 124 m_bOwnership = sal_False; 125 m_bOwnershipIsWellKnown = sal_False; 126 m_aEndTryClosingCondition.set(); 127 } 128 }; 129 130 //----------------------------------------------------------------- 131 /* 132 Use this Guard in your apicalls to protect access on resources 133 which will be released in dispose. 134 It's guarantied, that the release of resources only starts if your 135 guarded call has finished. 136 ! It's only partly guaranteed that this resources will not change during the call. 137 See the example for details. 138 139 This class is to be used as described in the example. 140 141 If this guard is used in all api calls of an XCloseable object 142 it's guarantied, that the closeable will close itself after finishing the last call 143 if it should do so. 144 145 ::ApiCall 146 { 147 //hold no mutex!!! 148 LifeTimeGuard aLifeTimeGuard(m_aLifeTimeManager); 149 150 //mutex is acquired; call is not registered 151 152 if(!aLifeTimeGuard.startApiCall()) 153 return ; //behave as passive as possible, if disposed or closed 154 155 //mutex is acquired, call is registered 156 { 157 //you might access some private members here 158 //but than you need to protect access to these members always like this 159 //never call to the outside here 160 } 161 162 aLifeTimeGuard.clear(); //!!! 163 164 //Mutex is released, the running call is still registered 165 //this call will finish before the 'release-section' in dispose is allowed to start 166 167 { 168 //you might access some private members here guarded with your own mutex 169 //but release your mutex at the end of this block 170 } 171 172 //you can call to the outside (without holding the mutex) without becoming disposed 173 174 //End of method -> ~LifeTimeGuard 175 //-> call is unregistered 176 //-> this object might be disposed now 177 } 178 179 your XComponent::dispose method has to be implemented in the following way: 180 181 ::dispose() 182 { 183 //hold no mutex!!! 184 if( !m_aLifeTimeManager.dispose() ) 185 return; 186 187 //--release all resources and references 188 //... 189 } 190 191 */ 192 //----------------------------------------------------------------- 193 194 class OOO_DLLPUBLIC_CHARTTOOLS LifeTimeGuard 195 { 196 197 public: LifeTimeGuard(LifeTimeManager & rManager)198 LifeTimeGuard( LifeTimeManager& rManager ) 199 : m_guard( rManager.m_aAccessMutex ) 200 , m_rManager(rManager) 201 , m_bCallRegistered(sal_False) 202 , m_bLongLastingCallRegistered(sal_False) 203 { 204 205 } 206 sal_Bool startApiCall(sal_Bool bLongLastingCall=sal_False); 207 ~LifeTimeGuard(); clear()208 void clear() { m_guard.clear(); } 209 210 private: 211 osl::ClearableMutexGuard m_guard; 212 LifeTimeManager& m_rManager; 213 sal_Bool m_bCallRegistered; 214 sal_Bool m_bLongLastingCallRegistered; 215 216 private: 217 // these make no sense 218 LifeTimeGuard( ::osl::Mutex& rMutex ); 219 LifeTimeGuard( const LifeTimeGuard& ); 220 LifeTimeGuard& operator= ( const LifeTimeGuard& ); 221 }; 222 223 224 template<class T> 225 class NegativeGuard 226 { 227 protected: 228 T * m_pT; 229 public: 230 NegativeGuard(T * pT)231 NegativeGuard(T * pT) : m_pT(pT) 232 { 233 m_pT->release(); 234 } 235 NegativeGuard(T & t)236 NegativeGuard(T & t) : m_pT(&t) 237 { 238 m_pT->release(); 239 } 240 ~NegativeGuard()241 ~NegativeGuard() 242 { 243 m_pT->acquire(); 244 } 245 }; 246 247 }//end namespace apphelper 248 #endif 249