Update ooo320-m1
[ooovba.git] / sw / source / core / inc / threadmanager.hxx
blobb2ba34258a9ed6351374d8ce6ece68150228dc9c
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: threadmanager.hxx,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
30 #ifndef _THREADMANAGER_HXX
31 #define _THREADMANAGER_HXX
33 #include <ithreadlistenerowner.hxx>
34 #include <vcl/timer.hxx>
35 #include <osl/mutex.hxx>
36 #include <osl/interlck.h>
37 #include <rtl/ref.hxx>
39 #include <deque>
40 #include <list>
41 #include <cppuhelper/weak.hxx>
42 #include "com/sun/star/util/XJobManager.hpp"
43 #include <observablethread.hxx>
44 #include <cancellablejob.hxx>
45 #include <threadlistener.hxx>
47 #include <boost/shared_ptr.hpp>
48 #include <boost/weak_ptr.hpp>
50 /** class to manage threads
52 OD 2007-01-29 #i73788#
53 An instance of this class takes care of the starting of threads.
54 It assures that not more than <mnStartedSize> threads
55 are started.
57 @author OD
59 class ThreadManager : public IThreadListenerOwner
61 public:
63 explicit ThreadManager( ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager >& rThreadJoiner );
64 ~ThreadManager();
66 // --> IThreadListenerOwner
67 virtual boost::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef();
68 virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID );
69 // <--
71 /** initialization
73 IMPORTANT NOTE: Needs to be called directly after construction
75 @author OD
77 void Init();
79 /** add thread to the thread manager and taking ownership for the thread
81 @author OD
83 @return unique ID for added thread
85 oslInterlockedCount AddThread(
86 const ::rtl::Reference< ObservableThread >& rThread );
88 void RemoveThread( const oslInterlockedCount nThreadID,
89 const bool bThreadFinished = false );
91 DECL_LINK( TryToStartNewThread, Timer* );
93 /** suspend the starting of threads
95 Suspending the starting of further threads is sensible during the
96 destruction of a Writer document.
98 @author OD
100 inline void SuspendStartingOfThreads()
102 osl::MutexGuard aGuard(maMutex);
104 mbStartingOfThreadsSuspended = true;
107 /** continues the starting of threads after it has been suspended
109 @author OD
111 void ResumeStartingOfThreads();
113 inline bool StartingOfThreadsSuspended()
115 osl::MutexGuard aGuard(maMutex);
117 return mbStartingOfThreadsSuspended;
120 struct tThreadData
122 oslInterlockedCount nThreadID;
123 ::rtl::Reference< ObservableThread > pThread;
124 com::sun::star::uno::Reference< com::sun::star::util::XCancellable > aJob;
126 tThreadData()
127 : nThreadID( 0 ),
128 pThread( 0 ),
129 aJob()
133 private:
135 static const std::deque< tThreadData >::size_type mnStartedSize;
137 osl::Mutex maMutex;
139 ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XJobManager > mrThreadJoiner;
141 boost::shared_ptr< ThreadListener > mpThreadListener;
143 oslInterlockedCount mnThreadIDCounter;
145 std::deque< tThreadData > maWaitingForStartThreads;
146 std::deque< tThreadData > maStartedThreads;
148 Timer maStartNewThreadTimer;
150 bool mbStartingOfThreadsSuspended;
152 struct ThreadPred
154 oslInterlockedCount mnThreadID;
155 explicit ThreadPred( oslInterlockedCount nThreadID )
156 : mnThreadID( nThreadID )
159 bool operator() ( const tThreadData& rThreadData ) const
161 return rThreadData.nThreadID == mnThreadID;
166 inline oslInterlockedCount RetrieveNewThreadID()
168 return osl_incrementInterlockedCount( &mnThreadIDCounter );
171 bool StartWaitingThread();
173 bool StartThread( const tThreadData& aThreadData );
175 #endif