LCOV - code coverage report
Current view: top level - libreoffice/sw/source/core/docnode - threadmanager.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 85 0.0 %
Date: 2012-12-17 Functions: 0 13 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10