Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 : #include <threadmanager.hxx>
20 :
21 : #include <algorithm>
22 :
23 : using namespace ::com::sun::star;
24 :
25 : /** class to manage threads
26 :
27 : #i73788#
28 : */
29 : const std::deque< ThreadManager::tThreadData >::size_type ThreadManager::mnStartedSize = 10;
30 :
31 2 : ThreadManager::ThreadManager( uno::Reference< util::XJobManager >& rThreadJoiner )
32 : : maMutex(),
33 : mrThreadJoiner( rThreadJoiner ),
34 : mpThreadListener(),
35 : mnThreadIDCounter( 0 ),
36 : maWaitingForStartThreads(),
37 : maStartedThreads(),
38 : maStartNewThreadTimer(),
39 2 : mbStartingOfThreadsSuspended( false )
40 : {
41 2 : }
42 :
43 2 : void ThreadManager::Init()
44 : {
45 2 : mpThreadListener.reset( new ThreadListener( *this ) );
46 :
47 2 : maStartNewThreadTimer.SetTimeout( 2000 );
48 2 : maStartNewThreadTimer.SetTimeoutHdl( LINK( this, ThreadManager, TryToStartNewThread ) );
49 2 : }
50 :
51 6 : ThreadManager::~ThreadManager()
52 : {
53 2 : maWaitingForStartThreads.clear();
54 2 : maStartedThreads.clear();
55 4 : }
56 :
57 8 : boost::weak_ptr< IFinishedThreadListener > ThreadManager::GetThreadListenerWeakRef()
58 : {
59 8 : return mpThreadListener;
60 : }
61 :
62 8 : void ThreadManager::NotifyAboutFinishedThread( const oslInterlockedCount nThreadID )
63 : {
64 8 : RemoveThread( nThreadID, true );
65 8 : }
66 :
67 8 : oslInterlockedCount ThreadManager::AddThread(
68 : const rtl::Reference< ObservableThread >& rThread )
69 :
70 : {
71 8 : osl::MutexGuard aGuard(maMutex);
72 :
73 : // create new thread
74 16 : tThreadData aThreadData;
75 8 : oslInterlockedCount nNewThreadID( RetrieveNewThreadID() );
76 : {
77 8 : aThreadData.nThreadID = nNewThreadID;
78 :
79 8 : aThreadData.pThread = rThread;
80 8 : aThreadData.aJob = new CancellableJob( aThreadData.pThread );
81 :
82 8 : aThreadData.pThread->setPriority( osl_Thread_PriorityBelowNormal );
83 : mpThreadListener->ListenToThread( aThreadData.nThreadID,
84 8 : *(aThreadData.pThread) );
85 : }
86 :
87 : // add thread to manager
88 16 : if ( maStartedThreads.size() < mnStartedSize &&
89 8 : !StartingOfThreadsSuspended() )
90 : {
91 : // Try to start thread
92 8 : if ( !StartThread( aThreadData ) )
93 : {
94 : // No success on starting thread
95 : // If no more started threads exist, but still threads are waiting,
96 : // setup Timer to start thread from waiting ones
97 0 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
98 : {
99 0 : maStartNewThreadTimer.Start();
100 : }
101 : }
102 : }
103 : else
104 : {
105 : // Thread will be started later
106 0 : maWaitingForStartThreads.push_back( aThreadData );
107 : }
108 :
109 16 : return nNewThreadID;
110 : }
111 :
112 16 : void ThreadManager::RemoveThread( const oslInterlockedCount nThreadID,
113 : const bool bThreadFinished )
114 : {
115 : // --> SAFE ----
116 16 : osl::MutexGuard aGuard(maMutex);
117 :
118 : std::deque< tThreadData >::iterator aIter =
119 : std::find_if( maStartedThreads.begin(), maStartedThreads.end(),
120 16 : ThreadPred( nThreadID ) );
121 :
122 16 : if ( aIter != maStartedThreads.end() )
123 : {
124 8 : tThreadData aTmpThreadData( (*aIter) );
125 :
126 8 : maStartedThreads.erase( aIter );
127 :
128 8 : if ( bThreadFinished )
129 : {
130 : // release thread as job from thread joiner instance
131 8 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager > rThreadJoiner( mrThreadJoiner );
132 8 : if ( rThreadJoiner.is() )
133 : {
134 8 : rThreadJoiner->releaseJob( aTmpThreadData.aJob );
135 : }
136 : else
137 : {
138 : OSL_FAIL( "<ThreadManager::RemoveThread(..)> - ThreadJoiner already gone!" );
139 8 : }
140 : }
141 :
142 : // Try to start thread from waiting ones
143 8 : TryToStartNewThread( 0 );
144 : }
145 : else
146 : {
147 : aIter = std::find_if( maWaitingForStartThreads.begin(),
148 8 : maWaitingForStartThreads.end(), ThreadPred( nThreadID ) );
149 :
150 8 : if ( aIter != maWaitingForStartThreads.end() )
151 : {
152 0 : maWaitingForStartThreads.erase( aIter );
153 : }
154 16 : }
155 : // <-- SAFE ----
156 16 : }
157 :
158 8 : bool ThreadManager::StartWaitingThread()
159 : {
160 8 : if ( !maWaitingForStartThreads.empty() )
161 : {
162 0 : tThreadData aThreadData( maWaitingForStartThreads.front() );
163 0 : maWaitingForStartThreads.pop_front();
164 0 : return StartThread( aThreadData );
165 : }
166 : else
167 : {
168 8 : return false;
169 : }
170 : }
171 :
172 8 : bool ThreadManager::StartThread( const tThreadData& rThreadData )
173 : {
174 8 : bool bThreadStarted( false );
175 :
176 8 : if ( rThreadData.pThread->create() )
177 : {
178 : // start of thread successful.
179 8 : bThreadStarted = true;
180 :
181 8 : maStartedThreads.push_back( rThreadData );
182 :
183 : // register thread as job at thread joiner instance
184 8 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager > rThreadJoiner( mrThreadJoiner );
185 8 : if ( rThreadJoiner.is() )
186 : {
187 8 : rThreadJoiner->registerJob( rThreadData.aJob );
188 : }
189 : else
190 : {
191 : OSL_FAIL( "<ThreadManager::StartThread(..)> - ThreadJoiner already gone!" );
192 8 : }
193 : }
194 : else
195 : {
196 : // thread couldn't be started.
197 0 : maWaitingForStartThreads.push_front( rThreadData );
198 : }
199 :
200 8 : return bThreadStarted;
201 : }
202 :
203 8 : IMPL_LINK_NOARG(ThreadManager, TryToStartNewThread)
204 : {
205 8 : osl::MutexGuard aGuard(maMutex);
206 :
207 8 : if ( !StartingOfThreadsSuspended() )
208 : {
209 : // Try to start thread from waiting ones
210 8 : if ( !StartWaitingThread() )
211 : {
212 : // No success on starting thread
213 : // If no more started threads exist, but still threads are waiting,
214 : // setup Timer to start thread from waiting ones
215 8 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
216 : {
217 0 : maStartNewThreadTimer.Start();
218 : }
219 : }
220 : }
221 :
222 8 : return sal_True;
223 : }
224 :
225 233 : void ThreadManager::ResumeStartingOfThreads()
226 : {
227 233 : osl::MutexGuard aGuard(maMutex);
228 :
229 233 : mbStartingOfThreadsSuspended = false;
230 :
231 699 : while ( maStartedThreads.size() < mnStartedSize &&
232 233 : !maWaitingForStartThreads.empty() )
233 : {
234 0 : if ( !StartWaitingThread() )
235 : {
236 : // No success on starting thread
237 : // If no more started threads exist, but still threads are waiting,
238 : // setup Timer to start thread from waiting ones
239 0 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
240 : {
241 0 : maStartNewThreadTimer.Start();
242 0 : break;
243 : }
244 : }
245 233 : }
246 233 : }
247 :
248 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|