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 : : #include <threadmanager.hxx>
29 : :
30 : : #include <algorithm>
31 : :
32 : : using namespace ::com::sun::star;
33 : :
34 : : /** class to manage threads
35 : :
36 : : #i73788#
37 : :
38 : : @author OD
39 : : */
40 : : const std::deque< ThreadManager::tThreadData >::size_type ThreadManager::mnStartedSize = 10;
41 : :
42 : 2 : ThreadManager::ThreadManager( uno::Reference< util::XJobManager >& rThreadJoiner )
43 : : : maMutex(),
44 : : mrThreadJoiner( rThreadJoiner ),
45 : : mpThreadListener(),
46 : : mnThreadIDCounter( 0 ),
47 : : maWaitingForStartThreads(),
48 : : maStartedThreads(),
49 : : maStartNewThreadTimer(),
50 [ + - ][ + - ]: 2 : mbStartingOfThreadsSuspended( false )
[ + - ][ + - ]
[ + - ][ + - ]
51 : : {
52 : 2 : }
53 : :
54 : 2 : void ThreadManager::Init()
55 : : {
56 [ + - ]: 2 : mpThreadListener.reset( new ThreadListener( *this ) );
57 : :
58 : 2 : maStartNewThreadTimer.SetTimeout( 2000 );
59 : 2 : maStartNewThreadTimer.SetTimeoutHdl( LINK( this, ThreadManager, TryToStartNewThread ) );
60 : 2 : }
61 : :
62 [ + - ][ + - ]: 2 : ThreadManager::~ThreadManager()
[ + - ][ + - ]
63 : : {
64 : 2 : maWaitingForStartThreads.clear();
65 : 2 : maStartedThreads.clear();
66 [ - + ]: 4 : }
67 : :
68 : 8 : boost::weak_ptr< IFinishedThreadListener > ThreadManager::GetThreadListenerWeakRef()
69 : : {
70 [ + - ]: 8 : return mpThreadListener;
71 : : }
72 : :
73 : 8 : void ThreadManager::NotifyAboutFinishedThread( const oslInterlockedCount nThreadID )
74 : : {
75 : 8 : RemoveThread( nThreadID, true );
76 : 8 : }
77 : :
78 : 8 : oslInterlockedCount ThreadManager::AddThread(
79 : : const rtl::Reference< ObservableThread >& rThread )
80 : :
81 : : {
82 [ + - ]: 8 : osl::MutexGuard aGuard(maMutex);
83 : :
84 : : // create new thread
85 [ + - ]: 8 : tThreadData aThreadData;
86 [ + - ]: 8 : oslInterlockedCount nNewThreadID( RetrieveNewThreadID() );
87 : : {
88 : 8 : aThreadData.nThreadID = nNewThreadID;
89 : :
90 [ + - ]: 8 : aThreadData.pThread = rThread;
91 [ + - ][ + - ]: 8 : aThreadData.aJob = new CancellableJob( aThreadData.pThread );
[ + - ]
92 : :
93 [ + - ]: 8 : aThreadData.pThread->setPriority( osl_Thread_PriorityBelowNormal );
94 : : mpThreadListener->ListenToThread( aThreadData.nThreadID,
95 [ + - ]: 8 : *(aThreadData.pThread) );
96 : : }
97 : :
98 : : // add thread to manager
99 [ + - ][ + - ]: 16 : if ( maStartedThreads.size() < mnStartedSize &&
[ + - ]
100 [ + - ]: 8 : !StartingOfThreadsSuspended() )
101 : : {
102 : : // Try to start thread
103 [ + - ][ - + ]: 8 : if ( !StartThread( aThreadData ) )
104 : : {
105 : : // No success on starting thread
106 : : // If no more started threads exist, but still threads are waiting,
107 : : // setup Timer to start thread from waiting ones
108 [ # # ][ # # ]: 0 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
[ # # ]
109 : : {
110 [ # # ]: 0 : maStartNewThreadTimer.Start();
111 : : }
112 : : }
113 : : }
114 : : else
115 : : {
116 : : // Thread will be started later
117 [ # # ]: 0 : maWaitingForStartThreads.push_back( aThreadData );
118 : : }
119 : :
120 [ + - ][ + - ]: 8 : return nNewThreadID;
121 : : }
122 : :
123 : 16 : void ThreadManager::RemoveThread( const oslInterlockedCount nThreadID,
124 : : const bool bThreadFinished )
125 : : {
126 : : // --> SAFE ----
127 [ + - ]: 16 : osl::MutexGuard aGuard(maMutex);
128 : :
129 : : std::deque< tThreadData >::iterator aIter =
130 : : std::find_if( maStartedThreads.begin(), maStartedThreads.end(),
131 [ + - ]: 16 : ThreadPred( nThreadID ) );
132 : :
133 [ + - ][ + + ]: 16 : if ( aIter != maStartedThreads.end() )
134 : : {
135 [ + - ]: 8 : tThreadData aTmpThreadData( (*aIter) );
136 : :
137 [ + - ]: 8 : maStartedThreads.erase( aIter );
138 : :
139 [ + - ]: 8 : if ( bThreadFinished )
140 : : {
141 : : // release thread as job from thread joiner instance
142 [ + - ]: 8 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager > rThreadJoiner( mrThreadJoiner );
143 [ + - ]: 8 : if ( rThreadJoiner.is() )
144 : : {
145 [ + - ][ + - ]: 8 : rThreadJoiner->releaseJob( aTmpThreadData.aJob );
146 : : }
147 : : else
148 : : {
149 : : OSL_FAIL( "<ThreadManager::RemoveThread(..)> - ThreadJoiner already gone!" );
150 : 8 : }
151 : : }
152 : :
153 : : // Try to start thread from waiting ones
154 [ + - ][ + - ]: 8 : TryToStartNewThread( 0 );
155 : : }
156 : : else
157 : : {
158 : : aIter = std::find_if( maWaitingForStartThreads.begin(),
159 [ + - ]: 8 : maWaitingForStartThreads.end(), ThreadPred( nThreadID ) );
160 : :
161 [ + - ][ - + ]: 8 : if ( aIter != maWaitingForStartThreads.end() )
162 : : {
163 [ # # ]: 0 : maWaitingForStartThreads.erase( aIter );
164 : : }
165 [ + - ]: 16 : }
166 : : // <-- SAFE ----
167 : 16 : }
168 : :
169 : 8 : bool ThreadManager::StartWaitingThread()
170 : : {
171 [ - + ]: 8 : if ( !maWaitingForStartThreads.empty() )
172 : : {
173 [ # # ]: 0 : tThreadData aThreadData( maWaitingForStartThreads.front() );
174 [ # # ]: 0 : maWaitingForStartThreads.pop_front();
175 [ # # ][ # # ]: 0 : return StartThread( aThreadData );
176 : : }
177 : : else
178 : : {
179 : 8 : return false;
180 : : }
181 : : }
182 : :
183 : 8 : bool ThreadManager::StartThread( const tThreadData& rThreadData )
184 : : {
185 : 8 : bool bThreadStarted( false );
186 : :
187 [ + - ]: 8 : if ( rThreadData.pThread->create() )
188 : : {
189 : : // start of thread successful.
190 : 8 : bThreadStarted = true;
191 : :
192 [ + - ]: 8 : maStartedThreads.push_back( rThreadData );
193 : :
194 : : // register thread as job at thread joiner instance
195 [ + - ]: 8 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager > rThreadJoiner( mrThreadJoiner );
196 [ + - ]: 8 : if ( rThreadJoiner.is() )
197 : : {
198 [ + - ][ + - ]: 8 : rThreadJoiner->registerJob( rThreadData.aJob );
199 : : }
200 : : else
201 : : {
202 : : OSL_FAIL( "<ThreadManager::StartThread(..)> - ThreadJoiner already gone!" );
203 : 8 : }
204 : : }
205 : : else
206 : : {
207 : : // thread couldn't be started.
208 : 0 : maWaitingForStartThreads.push_front( rThreadData );
209 : : }
210 : :
211 : 8 : return bThreadStarted;
212 : : }
213 : :
214 : 0 : IMPL_LINK_NOARG(ThreadManager, TryToStartNewThread)
215 : : {
216 [ + - ]: 8 : osl::MutexGuard aGuard(maMutex);
217 : :
218 [ + - ][ + - ]: 8 : if ( !StartingOfThreadsSuspended() )
219 : : {
220 : : // Try to start thread from waiting ones
221 [ + - ][ + - ]: 8 : if ( !StartWaitingThread() )
222 : : {
223 : : // No success on starting thread
224 : : // If no more started threads exist, but still threads are waiting,
225 : : // setup Timer to start thread from waiting ones
226 [ + - ][ - + ]: 8 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
[ - + ]
227 : : {
228 [ # # ]: 0 : maStartNewThreadTimer.Start();
229 : : }
230 : : }
231 : : }
232 : :
233 [ + - ]: 8 : return sal_True;
234 : : }
235 : :
236 : 234 : void ThreadManager::ResumeStartingOfThreads()
237 : : {
238 [ + - ]: 234 : osl::MutexGuard aGuard(maMutex);
239 : :
240 : 234 : mbStartingOfThreadsSuspended = false;
241 : :
242 [ + - - + ]: 468 : while ( maStartedThreads.size() < mnStartedSize &&
[ - + ]
243 : 234 : !maWaitingForStartThreads.empty() )
244 : : {
245 [ # # ][ # # ]: 0 : if ( !StartWaitingThread() )
246 : : {
247 : : // No success on starting thread
248 : : // If no more started threads exist, but still threads are waiting,
249 : : // setup Timer to start thread from waiting ones
250 [ # # ][ # # ]: 0 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
[ # # ]
251 : : {
252 [ # # ]: 0 : maStartNewThreadTimer.Start();
253 : 0 : break;
254 : : }
255 : : }
256 [ + - ]: 234 : }
257 : 234 : }
258 : :
259 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|