LCOV - code coverage report
Current view: top level - sw/source/core/inc - threadmanager.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 18 18 100.0 %
Date: 2014-04-11 Functions: 8 9 88.9 %
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             : #ifndef INCLUDED_SW_SOURCE_CORE_INC_THREADMANAGER_HXX
      20             : #define INCLUDED_SW_SOURCE_CORE_INC_THREADMANAGER_HXX
      21             : 
      22             : #include <ithreadlistenerowner.hxx>
      23             : #include <vcl/timer.hxx>
      24             : #include <osl/mutex.hxx>
      25             : #include <osl/interlck.h>
      26             : #include <rtl/ref.hxx>
      27             : 
      28             : #include <deque>
      29             : #include <list>
      30             : #include <cppuhelper/weak.hxx>
      31             : #include "com/sun/star/util/XJobManager.hpp"
      32             : #include <observablethread.hxx>
      33             : #include <cancellablejob.hxx>
      34             : #include <threadlistener.hxx>
      35             : 
      36             : #include <boost/shared_ptr.hpp>
      37             : #include <boost/weak_ptr.hpp>
      38             : 
      39             : /** class to manage threads
      40             : 
      41             :     OD 2007-01-29 #i73788#
      42             :     An instance of this class takes care of the starting of threads.
      43             :     It assures that not more than <mnStartedSize> threads
      44             :     are started.
      45             : */
      46             : class ThreadManager : public IThreadListenerOwner
      47             : {
      48             :     public:
      49             : 
      50             :         explicit ThreadManager( ::com::sun::star::uno::Reference< ::com::sun::star::util::XJobManager >& rThreadJoiner );
      51             :         virtual ~ThreadManager();
      52             : 
      53             :         // --> IThreadListenerOwner
      54             :         virtual boost::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef() SAL_OVERRIDE;
      55             :         virtual void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID ) SAL_OVERRIDE;
      56             : 
      57             :         /** initialization
      58             : 
      59             :             IMPORTANT NOTE: Needs to be called directly after construction
      60             :         */
      61             :         void Init();
      62             : 
      63             :         /** add thread to the thread manager and taking ownership for the thread
      64             : 
      65             :             @return unique ID for added thread
      66             :         */
      67             :         oslInterlockedCount AddThread(
      68             :                             const ::rtl::Reference< ObservableThread >& rThread );
      69             : 
      70             :         void RemoveThread( const oslInterlockedCount nThreadID,
      71             :                            const bool bThreadFinished = false );
      72             : 
      73             :         DECL_LINK( TryToStartNewThread, void* );
      74             : 
      75             :         /** suspend the starting of threads
      76             : 
      77             :             Suspending the starting of further threads is sensible during the
      78             :             destruction of a Writer document.
      79             :         */
      80         117 :         inline void SuspendStartingOfThreads()
      81             :         {
      82         117 :             osl::MutexGuard aGuard(maMutex);
      83             : 
      84         117 :             mbStartingOfThreadsSuspended = true;
      85         117 :         }
      86             : 
      87             :         /** continues the starting of threads after it has been suspended
      88             :         */
      89             :         void ResumeStartingOfThreads();
      90             : 
      91         127 :         inline bool StartingOfThreadsSuspended()
      92             :         {
      93         127 :             osl::MutexGuard aGuard(maMutex);
      94             : 
      95         127 :             return mbStartingOfThreadsSuspended;
      96             :         }
      97             : 
      98          20 :         struct tThreadData
      99             :         {
     100             :             oslInterlockedCount nThreadID;
     101             :             ::rtl::Reference< ObservableThread > pThread;
     102             :             com::sun::star::uno::Reference< com::sun::star::util::XCancellable > aJob;
     103             : 
     104           4 :             tThreadData()
     105             :                 : nThreadID( 0 ),
     106             :                   pThread( 0 ),
     107           4 :                   aJob()
     108           4 :             {}
     109             :         };
     110             : 
     111             :     private:
     112             : 
     113             :         static const std::deque< tThreadData >::size_type mnStartedSize;
     114             : 
     115             :         osl::Mutex maMutex;
     116             : 
     117             :         ::com::sun::star::uno::WeakReference< ::com::sun::star::util::XJobManager > mrThreadJoiner;
     118             : 
     119             :         boost::shared_ptr< ThreadListener > mpThreadListener;
     120             : 
     121             :         oslInterlockedCount mnThreadIDCounter;
     122             : 
     123             :         std::deque< tThreadData > maWaitingForStartThreads;
     124             :         std::deque< tThreadData > maStartedThreads;
     125             : 
     126             :         Timer maStartNewThreadTimer;
     127             : 
     128             :         bool mbStartingOfThreadsSuspended;
     129             : 
     130             :         struct ThreadPred
     131             :         {
     132             :             oslInterlockedCount mnThreadID;
     133          12 :             explicit ThreadPred( oslInterlockedCount nThreadID )
     134          12 :                 : mnThreadID( nThreadID )
     135          12 :             {}
     136             : 
     137           4 :             bool operator() ( const tThreadData& rThreadData ) const
     138             :             {
     139           4 :                 return rThreadData.nThreadID == mnThreadID;
     140             :             }
     141             :         };
     142             : 
     143           4 :         inline oslInterlockedCount RetrieveNewThreadID()
     144             :         {
     145           4 :             return osl_atomic_increment( &mnThreadIDCounter );
     146             :         }
     147             : 
     148             :         bool StartWaitingThread();
     149             : 
     150             :         bool StartThread( const tThreadData& aThreadData );
     151             : };
     152             : #endif
     153             : 
     154             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10