Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / docnode / threadmanager.cxx
blob20d71e746aea2fac6793e723945c631c995a84e0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "cancellablejob.hxx"
21 #include "threadmanager.hxx"
22 #include <threadlistener.hxx>
24 #include <osl/diagnose.h>
26 #include <algorithm>
28 #include <com/sun/star/util/XJobManager.hpp>
30 using namespace ::com::sun::star;
32 /** class to manage threads
34 #i73788#
36 const std::deque< ThreadManager::tThreadData >::size_type ThreadManager::snStartedSize = 10;
38 ThreadManager::ThreadManager( uno::Reference< util::XJobManager > const & rThreadJoiner )
39 : mrThreadJoiner( rThreadJoiner ),
40 mnThreadIDCounter( 0 ),
41 maStartNewThreadIdle("SW ThreadManager StartNewThreadIdle"),
42 mbStartingOfThreadsSuspended( false )
46 void ThreadManager::Init()
48 mpThreadListener = std::make_shared<ThreadListener>( *this );
50 maStartNewThreadIdle.SetPriority( TaskPriority::LOWEST );
51 maStartNewThreadIdle.SetInvokeHandler( LINK( this, ThreadManager, TryToStartNewThread ) );
54 ThreadManager::~ThreadManager()
56 maWaitingForStartThreads.clear();
57 maStartedThreads.clear();
60 std::weak_ptr< IFinishedThreadListener > ThreadManager::GetThreadListenerWeakRef() const
62 return mpThreadListener;
65 void ThreadManager::NotifyAboutFinishedThread( const oslInterlockedCount nThreadID )
67 RemoveThread( nThreadID, true );
70 oslInterlockedCount ThreadManager::AddThread(
71 const rtl::Reference< ObservableThread >& rThread )
74 std::unique_lock aGuard(maMutex);
76 // create new thread
77 tThreadData aThreadData;
78 oslInterlockedCount nNewThreadID( osl_atomic_increment( &mnThreadIDCounter ) );
80 aThreadData.nThreadID = nNewThreadID;
82 aThreadData.pThread = rThread;
83 aThreadData.aJob = new CancellableJob( aThreadData.pThread );
85 aThreadData.pThread->setPriority( osl_Thread_PriorityBelowNormal );
86 mpThreadListener->ListenToThread( aThreadData.nThreadID,
87 *(aThreadData.pThread) );
90 // add thread to manager
91 if ( maStartedThreads.size() < snStartedSize &&
92 !mbStartingOfThreadsSuspended )
94 // Try to start thread
95 if ( !StartThread( aThreadData ) )
97 // No success on starting thread
98 // If no more started threads exist, but still threads are waiting,
99 // setup Timer to start thread from waiting ones
100 if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
102 maStartNewThreadIdle.Start();
106 else
108 // Thread will be started later
109 maWaitingForStartThreads.push_back( aThreadData );
112 return nNewThreadID;
115 void ThreadManager::RemoveThread( const oslInterlockedCount nThreadID,
116 const bool bThreadFinished )
118 // --> SAFE ----
119 std::unique_lock aGuard(maMutex);
121 std::deque< tThreadData >::iterator aIter =
122 std::find_if( maStartedThreads.begin(), maStartedThreads.end(),
123 ThreadPred( nThreadID ) );
125 if ( aIter != maStartedThreads.end() )
127 tThreadData aTmpThreadData( *aIter );
129 maStartedThreads.erase( aIter );
131 if ( bThreadFinished )
133 // release thread as job from thread joiner instance
134 css::uno::Reference< css::util::XJobManager > rThreadJoiner( mrThreadJoiner );
135 if ( rThreadJoiner.is() )
137 rThreadJoiner->releaseJob( aTmpThreadData.aJob );
139 else
141 OSL_FAIL( "<ThreadManager::RemoveThread(..)> - ThreadJoiner already gone!" );
145 // Try to start thread from waiting ones
146 aGuard.unlock();
147 TryToStartNewThread( nullptr );
149 else
151 aIter = std::find_if( maWaitingForStartThreads.begin(),
152 maWaitingForStartThreads.end(), ThreadPred( nThreadID ) );
154 if ( aIter != maWaitingForStartThreads.end() )
156 maWaitingForStartThreads.erase( aIter );
159 // <-- SAFE ----
162 bool ThreadManager::StartWaitingThread()
164 if ( !maWaitingForStartThreads.empty() )
166 tThreadData aThreadData( maWaitingForStartThreads.front() );
167 maWaitingForStartThreads.pop_front();
168 return StartThread( aThreadData );
170 else
172 return false;
176 bool ThreadManager::StartThread( const tThreadData& rThreadData )
178 bool bThreadStarted( false );
180 if ( rThreadData.pThread->create() )
182 // start of thread successful.
183 bThreadStarted = true;
185 maStartedThreads.push_back( rThreadData );
187 // register thread as job at thread joiner instance
188 css::uno::Reference< css::util::XJobManager > rThreadJoiner( mrThreadJoiner );
189 if ( rThreadJoiner.is() )
191 rThreadJoiner->registerJob( rThreadData.aJob );
193 else
195 OSL_FAIL( "<ThreadManager::StartThread(..)> - ThreadJoiner already gone!" );
198 else
200 // thread couldn't be started.
201 maWaitingForStartThreads.push_front( rThreadData );
204 return bThreadStarted;
207 IMPL_LINK_NOARG(ThreadManager, TryToStartNewThread, Timer *, void)
209 std::unique_lock aGuard(maMutex);
211 if ( mbStartingOfThreadsSuspended )
212 return;
214 // Try to start thread from waiting ones
215 if ( !StartWaitingThread() )
217 // No success on starting thread
218 // If no more started threads exist, but still threads are waiting,
219 // setup Timer to start thread from waiting ones
220 if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
222 maStartNewThreadIdle.Start();
227 void ThreadManager::ResumeStartingOfThreads()
229 std::unique_lock aGuard(maMutex);
231 mbStartingOfThreadsSuspended = false;
233 while ( maStartedThreads.size() < snStartedSize &&
234 !maWaitingForStartThreads.empty() )
236 if ( !StartWaitingThread() )
238 // No success on starting thread
239 // If no more started threads exist, but still threads are waiting,
240 // setup Timer to start thread from waiting ones
241 if ( maStartedThreads.empty() && !maWaitingForStartThreads.empty() )
243 maStartNewThreadIdle.Start();
244 break;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */