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 0 : ThreadManager::ThreadManager( uno::Reference< util::XJobManager >& rThreadJoiner )
32 : : maMutex(),
33 : mrThreadJoiner( rThreadJoiner ),
34 : mpThreadListener(),
35 : mnThreadIDCounter( 0 ),
36 : maWaitingForStartThreads(),
37 : maStartedThreads(),
38 : maStartNewThreadTimer(),
39 0 : mbStartingOfThreadsSuspended( false )
40 : {
41 0 : }
42 :
43 0 : void ThreadManager::Init()
44 : {
45 0 : mpThreadListener.reset( new ThreadListener( *this ) );
46 :
47 0 : maStartNewThreadTimer.SetTimeout( 2000 );
48 0 : maStartNewThreadTimer.SetTimeoutHdl( LINK( this, ThreadManager, TryToStartNewThread ) );
49 0 : }
50 :
51 0 : ThreadManager::~ThreadManager()
52 : {
53 0 : maWaitingForStartThreads.clear();
54 0 : maStartedThreads.clear();
55 0 : }
56 :
57 0 : boost::weak_ptr< IFinishedThreadListener > ThreadManager::GetThreadListenerWeakRef()
58 : {
59 0 : return mpThreadListener;
60 : }
61 :
62 0 : void ThreadManager::NotifyAboutFinishedThread( const oslInterlockedCount nThreadID )
63 : {
64 0 : RemoveThread( nThreadID, true );
65 0 : }
66 :
67 0 : oslInterlockedCount ThreadManager::AddThread(
68 : const rtl::Reference< ObservableThread >& rThread )
69 :
70 : {
71 0 : osl::MutexGuard aGuard(maMutex);
72 :
73 : // create new thread
74 0 : tThreadData aThreadData;
75 0 : oslInterlockedCount nNewThreadID( RetrieveNewThreadID() );
76 : {
77 0 : aThreadData.nThreadID = nNewThreadID;
78 :
79 0 : aThreadData.pThread = rThread;
80 0 : aThreadData.aJob = new CancellableJob( aThreadData.pThread );
81 :
82 0 : aThreadData.pThread->setPriority( osl_Thread_PriorityBelowNormal );
83 : mpThreadListener->ListenToThread( aThreadData.nThreadID,
84 0 : *(aThreadData.pThread) );
85 : }
86 :
87 : // add thread to manager
88 0 : if ( maStartedThreads.size() < mnStartedSize &&
89 0 : !StartingOfThreadsSuspended() )
90 : {
91 : // Try to start thread
92 0 : 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 0 : return nNewThreadID;
110 : }
111 :
112 0 : void ThreadManager::RemoveThread( const oslInterlockedCount nThreadID,
113 : const bool bThreadFinished )
114 : {
115 : // --> SAFE ----
116 0 : osl::MutexGuard aGuard(maMutex);
117 :
118 : std::deque< tThreadData >::iterator aIter =
119 : std::find_if( maStartedThreads.begin(), maStartedThreads.end(),
120 0 : ThreadPred( nThreadID ) );
121 :
122 0 : if ( aIter != maStartedThreads.end() )
123 : {
124 0 : tThreadData aTmpThreadData( (*aIter) );
125 :
126 0 : maStartedThreads.erase( aIter );
127 :
128 0 : if ( bThreadFinished )
129 : {
130 : // release thread as job from thread joiner instance
131 0 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager > rThreadJoiner( mrThreadJoiner );
132 0 : if ( rThreadJoiner.is() )
133 : {
134 0 : rThreadJoiner->releaseJob( aTmpThreadData.aJob );
135 : }
136 : else
137 : {
138 : OSL_FAIL( "<ThreadManager::RemoveThread(..)> - ThreadJoiner already gone!" );
139 0 : }
140 : }
141 :
142 : // Try to start thread from waiting ones
143 0 : TryToStartNewThread( 0 );
144 : }
145 : else
146 : {
147 : aIter = std::find_if( maWaitingForStartThreads.begin(),
148 0 : maWaitingForStartThreads.end(), ThreadPred( nThreadID ) );
149 :
150 0 : if ( aIter != maWaitingForStartThreads.end() )
151 : {
152 0 : maWaitingForStartThreads.erase( aIter );
153 : }
154 0 : }
155 : // <-- SAFE ----
156 0 : }
157 :
158 0 : bool ThreadManager::StartWaitingThread()
159 : {
160 0 : 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 0 : return false;
169 : }
170 : }
171 :
172 0 : bool ThreadManager::StartThread( const tThreadData& rThreadData )
173 : {
174 0 : bool bThreadStarted( false );
175 :
176 0 : if ( rThreadData.pThread->create() )
177 : {
178 : // start of thread successful.
179 0 : bThreadStarted = true;
180 :
181 0 : maStartedThreads.push_back( rThreadData );
182 :
183 : // register thread as job at thread joiner instance
184 0 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager > rThreadJoiner( mrThreadJoiner );
185 0 : if ( rThreadJoiner.is() )
186 : {
187 0 : rThreadJoiner->registerJob( rThreadData.aJob );
188 : }
189 : else
190 : {
191 : OSL_FAIL( "<ThreadManager::StartThread(..)> - ThreadJoiner already gone!" );
192 0 : }
193 : }
194 : else
195 : {
196 : // thread couldn't be started.
197 0 : maWaitingForStartThreads.push_front( rThreadData );
198 : }
199 :
200 0 : return bThreadStarted;
201 : }
202 :
203 0 : IMPL_LINK_NOARG(ThreadManager, TryToStartNewThread)
204 : {
205 0 : osl::MutexGuard aGuard(maMutex);
206 :
207 0 : if ( !StartingOfThreadsSuspended() )
208 : {
209 : // Try to start thread from waiting ones
210 0 : 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 0 : if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
216 : {
217 0 : maStartNewThreadTimer.Start();
218 : }
219 : }
220 : }
221 :
222 0 : return sal_True;
223 : }
224 :
225 0 : void ThreadManager::ResumeStartingOfThreads()
226 : {
227 0 : osl::MutexGuard aGuard(maMutex);
228 :
229 0 : mbStartingOfThreadsSuspended = false;
230 :
231 0 : while ( maStartedThreads.size() < mnStartedSize &&
232 0 : !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 0 : }
246 0 : }
247 :
248 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|