Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #ifndef _THREADMANAGER_HXX
29 : : #define _THREADMANAGER_HXX
30 : :
31 : : #include <ithreadlistenerowner.hxx>
32 : : #include <vcl/timer.hxx>
33 : : #include <osl/mutex.hxx>
34 : : #include <osl/interlck.h>
35 : : #include <rtl/ref.hxx>
36 : :
37 : : #include <deque>
38 : : #include <list>
39 : : #include <cppuhelper/weak.hxx>
40 : : #include "com/sun/star/util/XJobManager.hpp"
41 : : #include <observablethread.hxx>
42 : : #include <cancellablejob.hxx>
43 : : #include <threadlistener.hxx>
44 : :
45 : : #include <boost/shared_ptr.hpp>
46 : : #include <boost/weak_ptr.hpp>
47 : :
48 : : /** class to manage threads
49 : :
50 : : OD 2007-01-29 #i73788#
51 : : An instance of this class takes care of the starting of threads.
52 : : It assures that not more than <mnStartedSize> threads
53 : : are started.
54 : :
55 : : @author OD
56 : : */
57 : : class ThreadManager : public IThreadListenerOwner
58 : : {
59 : : public:
60 : :
61 : : explicit ThreadManager( ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager >& rThreadJoiner );
62 : : ~ThreadManager();
63 : :
64 : : // --> IThreadListenerOwner
65 : : virtual boost::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef();
66 : : virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID );
67 : :
68 : : /** initialization
69 : :
70 : : IMPORTANT NOTE: Needs to be called directly after construction
71 : :
72 : : @author OD
73 : : */
74 : : void Init();
75 : :
76 : : /** add thread to the thread manager and taking ownership for the thread
77 : :
78 : : @author OD
79 : :
80 : : @return unique ID for added thread
81 : : */
82 : : oslInterlockedCount AddThread(
83 : : const ::rtl::Reference< ObservableThread >& rThread );
84 : :
85 : : void RemoveThread( const oslInterlockedCount nThreadID,
86 : : const bool bThreadFinished = false );
87 : :
88 : : DECL_LINK( TryToStartNewThread, void* );
89 : :
90 : : /** suspend the starting of threads
91 : :
92 : : Suspending the starting of further threads is sensible during the
93 : : destruction of a Writer document.
94 : :
95 : : @author OD
96 : : */
97 : 234 : inline void SuspendStartingOfThreads()
98 : : {
99 [ + - ]: 234 : osl::MutexGuard aGuard(maMutex);
100 : :
101 [ + - ]: 234 : mbStartingOfThreadsSuspended = true;
102 : 234 : }
103 : :
104 : : /** continues the starting of threads after it has been suspended
105 : :
106 : : @author OD
107 : : */
108 : : void ResumeStartingOfThreads();
109 : :
110 : 254 : inline bool StartingOfThreadsSuspended()
111 : : {
112 [ + - ]: 254 : osl::MutexGuard aGuard(maMutex);
113 : :
114 [ + - ]: 254 : return mbStartingOfThreadsSuspended;
115 : : }
116 : :
117 : 40 : struct tThreadData
118 : : {
119 : : oslInterlockedCount nThreadID;
120 : : ::rtl::Reference< ObservableThread > pThread;
121 : : com::sun::star::uno::Reference< com::sun::star::util::XCancellable > aJob;
122 : :
123 : 8 : tThreadData()
124 : : : nThreadID( 0 ),
125 : : pThread( 0 ),
126 : 8 : aJob()
127 : 8 : {}
128 : : };
129 : :
130 : : private:
131 : :
132 : : static const std::deque< tThreadData >::size_type mnStartedSize;
133 : :
134 : : osl::Mutex maMutex;
135 : :
136 : : ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XJobManager > mrThreadJoiner;
137 : :
138 : : boost::shared_ptr< ThreadListener > mpThreadListener;
139 : :
140 : : oslInterlockedCount mnThreadIDCounter;
141 : :
142 : : std::deque< tThreadData > maWaitingForStartThreads;
143 : : std::deque< tThreadData > maStartedThreads;
144 : :
145 : : Timer maStartNewThreadTimer;
146 : :
147 : : bool mbStartingOfThreadsSuspended;
148 : :
149 : : struct ThreadPred
150 : : {
151 : : oslInterlockedCount mnThreadID;
152 : 24 : explicit ThreadPred( oslInterlockedCount nThreadID )
153 : 24 : : mnThreadID( nThreadID )
154 : 24 : {}
155 : :
156 : 8 : bool operator() ( const tThreadData& rThreadData ) const
157 : : {
158 : 8 : return rThreadData.nThreadID == mnThreadID;
159 : : }
160 : : };
161 : :
162 : :
163 : 8 : inline oslInterlockedCount RetrieveNewThreadID()
164 : : {
165 : 8 : return osl_incrementInterlockedCount( &mnThreadIDCounter );
166 : : }
167 : :
168 : : bool StartWaitingThread();
169 : :
170 : : bool StartThread( const tThreadData& aThreadData );
171 : : };
172 : : #endif
173 : :
174 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|