1*1d2dbeb0SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*1d2dbeb0SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*1d2dbeb0SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*1d2dbeb0SAndrew Rist * distributed with this work for additional information 6*1d2dbeb0SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*1d2dbeb0SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*1d2dbeb0SAndrew Rist * "License"); you may not use this file except in compliance 9*1d2dbeb0SAndrew Rist * with the License. You may obtain a copy of the License at 10*1d2dbeb0SAndrew Rist * 11*1d2dbeb0SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*1d2dbeb0SAndrew Rist * 13*1d2dbeb0SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*1d2dbeb0SAndrew Rist * software distributed under the License is distributed on an 15*1d2dbeb0SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*1d2dbeb0SAndrew Rist * KIND, either express or implied. See the License for the 17*1d2dbeb0SAndrew Rist * specific language governing permissions and limitations 18*1d2dbeb0SAndrew Rist * under the License. 19*1d2dbeb0SAndrew Rist * 20*1d2dbeb0SAndrew Rist *************************************************************/ 21*1d2dbeb0SAndrew Rist 22*1d2dbeb0SAndrew Rist 23cdf0e10cSrcweir #ifndef _THREADMANAGER_HXX 24cdf0e10cSrcweir #define _THREADMANAGER_HXX 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <ithreadlistenerowner.hxx> 27cdf0e10cSrcweir #include <vcl/timer.hxx> 28cdf0e10cSrcweir #include <osl/mutex.hxx> 29cdf0e10cSrcweir #include <osl/interlck.h> 30cdf0e10cSrcweir #include <rtl/ref.hxx> 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <deque> 33cdf0e10cSrcweir #include <list> 34cdf0e10cSrcweir #include <cppuhelper/weak.hxx> 35cdf0e10cSrcweir #include "com/sun/star/util/XJobManager.hpp" 36cdf0e10cSrcweir #include <observablethread.hxx> 37cdf0e10cSrcweir #include <cancellablejob.hxx> 38cdf0e10cSrcweir #include <threadlistener.hxx> 39cdf0e10cSrcweir 40cdf0e10cSrcweir #include <boost/shared_ptr.hpp> 41cdf0e10cSrcweir #include <boost/weak_ptr.hpp> 42cdf0e10cSrcweir 43cdf0e10cSrcweir /** class to manage threads 44cdf0e10cSrcweir 45cdf0e10cSrcweir OD 2007-01-29 #i73788# 46cdf0e10cSrcweir An instance of this class takes care of the starting of threads. 47cdf0e10cSrcweir It assures that not more than <mnStartedSize> threads 48cdf0e10cSrcweir are started. 49cdf0e10cSrcweir 50cdf0e10cSrcweir @author OD 51cdf0e10cSrcweir */ 52cdf0e10cSrcweir class ThreadManager : public IThreadListenerOwner 53cdf0e10cSrcweir { 54cdf0e10cSrcweir public: 55cdf0e10cSrcweir 56cdf0e10cSrcweir explicit ThreadManager( ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager >& rThreadJoiner ); 57cdf0e10cSrcweir ~ThreadManager(); 58cdf0e10cSrcweir 59cdf0e10cSrcweir // --> IThreadListenerOwner 60cdf0e10cSrcweir virtual boost::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef(); 61cdf0e10cSrcweir virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID ); 62cdf0e10cSrcweir // <-- 63cdf0e10cSrcweir 64cdf0e10cSrcweir /** initialization 65cdf0e10cSrcweir 66cdf0e10cSrcweir IMPORTANT NOTE: Needs to be called directly after construction 67cdf0e10cSrcweir 68cdf0e10cSrcweir @author OD 69cdf0e10cSrcweir */ 70cdf0e10cSrcweir void Init(); 71cdf0e10cSrcweir 72cdf0e10cSrcweir /** add thread to the thread manager and taking ownership for the thread 73cdf0e10cSrcweir 74cdf0e10cSrcweir @author OD 75cdf0e10cSrcweir 76cdf0e10cSrcweir @return unique ID for added thread 77cdf0e10cSrcweir */ 78cdf0e10cSrcweir oslInterlockedCount AddThread( 79cdf0e10cSrcweir const ::rtl::Reference< ObservableThread >& rThread ); 80cdf0e10cSrcweir 81cdf0e10cSrcweir void RemoveThread( const oslInterlockedCount nThreadID, 82cdf0e10cSrcweir const bool bThreadFinished = false ); 83cdf0e10cSrcweir 84cdf0e10cSrcweir DECL_LINK( TryToStartNewThread, Timer* ); 85cdf0e10cSrcweir 86cdf0e10cSrcweir /** suspend the starting of threads 87cdf0e10cSrcweir 88cdf0e10cSrcweir Suspending the starting of further threads is sensible during the 89cdf0e10cSrcweir destruction of a Writer document. 90cdf0e10cSrcweir 91cdf0e10cSrcweir @author OD 92cdf0e10cSrcweir */ SuspendStartingOfThreads()93cdf0e10cSrcweir inline void SuspendStartingOfThreads() 94cdf0e10cSrcweir { 95cdf0e10cSrcweir osl::MutexGuard aGuard(maMutex); 96cdf0e10cSrcweir 97cdf0e10cSrcweir mbStartingOfThreadsSuspended = true; 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir /** continues the starting of threads after it has been suspended 101cdf0e10cSrcweir 102cdf0e10cSrcweir @author OD 103cdf0e10cSrcweir */ 104cdf0e10cSrcweir void ResumeStartingOfThreads(); 105cdf0e10cSrcweir StartingOfThreadsSuspended()106cdf0e10cSrcweir inline bool StartingOfThreadsSuspended() 107cdf0e10cSrcweir { 108cdf0e10cSrcweir osl::MutexGuard aGuard(maMutex); 109cdf0e10cSrcweir 110cdf0e10cSrcweir return mbStartingOfThreadsSuspended; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir struct tThreadData 114cdf0e10cSrcweir { 115cdf0e10cSrcweir oslInterlockedCount nThreadID; 116cdf0e10cSrcweir ::rtl::Reference< ObservableThread > pThread; 117cdf0e10cSrcweir com::sun::star::uno::Reference< com::sun::star::util::XCancellable > aJob; 118cdf0e10cSrcweir tThreadDataThreadManager::tThreadData119cdf0e10cSrcweir tThreadData() 120cdf0e10cSrcweir : nThreadID( 0 ), 121cdf0e10cSrcweir pThread( 0 ), 122cdf0e10cSrcweir aJob() 123cdf0e10cSrcweir {} 124cdf0e10cSrcweir }; 125cdf0e10cSrcweir 126cdf0e10cSrcweir private: 127cdf0e10cSrcweir 128cdf0e10cSrcweir static const std::deque< tThreadData >::size_type mnStartedSize; 129cdf0e10cSrcweir 130cdf0e10cSrcweir osl::Mutex maMutex; 131cdf0e10cSrcweir 132cdf0e10cSrcweir ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XJobManager > mrThreadJoiner; 133cdf0e10cSrcweir 134cdf0e10cSrcweir boost::shared_ptr< ThreadListener > mpThreadListener; 135cdf0e10cSrcweir 136cdf0e10cSrcweir oslInterlockedCount mnThreadIDCounter; 137cdf0e10cSrcweir 138cdf0e10cSrcweir std::deque< tThreadData > maWaitingForStartThreads; 139cdf0e10cSrcweir std::deque< tThreadData > maStartedThreads; 140cdf0e10cSrcweir 141cdf0e10cSrcweir Timer maStartNewThreadTimer; 142cdf0e10cSrcweir 143cdf0e10cSrcweir bool mbStartingOfThreadsSuspended; 144cdf0e10cSrcweir 145cdf0e10cSrcweir struct ThreadPred 146cdf0e10cSrcweir { 147cdf0e10cSrcweir oslInterlockedCount mnThreadID; ThreadPredThreadManager::ThreadPred148cdf0e10cSrcweir explicit ThreadPred( oslInterlockedCount nThreadID ) 149cdf0e10cSrcweir : mnThreadID( nThreadID ) 150cdf0e10cSrcweir {} 151cdf0e10cSrcweir operator ()ThreadManager::ThreadPred152cdf0e10cSrcweir bool operator() ( const tThreadData& rThreadData ) const 153cdf0e10cSrcweir { 154cdf0e10cSrcweir return rThreadData.nThreadID == mnThreadID; 155cdf0e10cSrcweir } 156cdf0e10cSrcweir }; 157cdf0e10cSrcweir 158cdf0e10cSrcweir RetrieveNewThreadID()159cdf0e10cSrcweir inline oslInterlockedCount RetrieveNewThreadID() 160cdf0e10cSrcweir { 161cdf0e10cSrcweir return osl_incrementInterlockedCount( &mnThreadIDCounter ); 162cdf0e10cSrcweir } 163cdf0e10cSrcweir 164cdf0e10cSrcweir bool StartWaitingThread(); 165cdf0e10cSrcweir 166cdf0e10cSrcweir bool StartThread( const tThreadData& aThreadData ); 167cdf0e10cSrcweir }; 168cdf0e10cSrcweir #endif 169