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