1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: threadmanager.hxx,v $
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>
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
59 class ThreadManager
: public IThreadListenerOwner
63 explicit ThreadManager( ::com::sun::star::uno::Reference
< ::com::sun::star::util::XJobManager
>& rThreadJoiner
);
66 // --> IThreadListenerOwner
67 virtual boost::weak_ptr
< IFinishedThreadListener
> GetThreadListenerWeakRef();
68 virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID
);
73 IMPORTANT NOTE: Needs to be called directly after construction
79 /** add thread to the thread manager and taking ownership for the thread
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.
100 inline void SuspendStartingOfThreads()
102 osl::MutexGuard
aGuard(maMutex
);
104 mbStartingOfThreadsSuspended
= true;
107 /** continues the starting of threads after it has been suspended
111 void ResumeStartingOfThreads();
113 inline bool StartingOfThreadsSuspended()
115 osl::MutexGuard
aGuard(maMutex
);
117 return mbStartingOfThreadsSuspended
;
122 oslInterlockedCount nThreadID
;
123 ::rtl::Reference
< ObservableThread
> pThread
;
124 com::sun::star::uno::Reference
< com::sun::star::util::XCancellable
> aJob
;
135 static const std::deque
< tThreadData
>::size_type mnStartedSize
;
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
;
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
);