LibreOffice Module sw (master)  1
threadmanager.hxx
Go to the documentation of this file.
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_DOCNODE_THREADMANAGER_HXX
20 #define INCLUDED_SW_SOURCE_CORE_DOCNODE_THREADMANAGER_HXX
21 
22 #include <vcl/idle.hxx>
23 #include <osl/mutex.hxx>
24 #include <osl/interlck.h>
25 #include <rtl/ref.hxx>
26 
27 #include <deque>
28 #include <cppuhelper/weakref.hxx>
29 #include <observablethread.hxx>
30 
31 #include <memory>
32 
33 namespace com::sun::star::util { class XCancellable; }
34 namespace com::sun::star::util { class XJobManager; }
35 
37 class ThreadListener;
38 class Timer;
39 
47 class ThreadManager final
48 {
49  public:
50 
51  explicit ThreadManager( css::uno::Reference< css::util::XJobManager > const & rThreadJoiner );
53 
54  std::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef();
55  void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID );
56 
61  void Init();
62 
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, Timer*, void );
74 
81  {
82  osl::MutexGuard aGuard(maMutex);
83 
85  }
86 
90 
92  {
93  osl::MutexGuard aGuard(maMutex);
94 
96  }
97 
98  struct tThreadData
99  {
100  oslInterlockedCount nThreadID;
102  css::uno::Reference< css::util::XCancellable > aJob;
103 
105  : nThreadID( 0 ),
106  aJob()
107  {}
108  };
109 
110  private:
111 
112  static const std::deque< tThreadData >::size_type mnStartedSize;
113 
114  osl::Mutex maMutex;
115 
116  css::uno::WeakReference< css::util::XJobManager > mrThreadJoiner;
117 
118  std::shared_ptr< ThreadListener > mpThreadListener;
119 
120  oslInterlockedCount mnThreadIDCounter;
121 
122  std::deque< tThreadData > maWaitingForStartThreads;
123  std::deque< tThreadData > maStartedThreads;
124 
126 
128 
129  struct ThreadPred
130  {
131  oslInterlockedCount const mnThreadID;
132  explicit ThreadPred( oslInterlockedCount nThreadID )
133  : mnThreadID( nThreadID )
134  {}
135 
136  bool operator() ( const tThreadData& rThreadData ) const
137  {
138  return rThreadData.nThreadID == mnThreadID;
139  }
140  };
141 
142  bool StartWaitingThread();
143 
144  bool StartThread( const tThreadData& aThreadData );
145 };
146 #endif
147 
148 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Init()
initialization
ThreadManager(css::uno::Reference< css::util::XJobManager > const &rThreadJoiner)
css::uno::Reference< css::util::XCancellable > aJob
void RemoveThread(const oslInterlockedCount nThreadID, const bool bThreadFinished=false)
std::shared_ptr< ThreadListener > mpThreadListener
oslInterlockedCount mnThreadIDCounter
ThreadPred(oslInterlockedCount nThreadID)
bool StartWaitingThread()
void ResumeStartingOfThreads()
continues the starting of threads after it has been suspended
class to manage threads
oslInterlockedCount AddThread(const ::rtl::Reference< ObservableThread > &rThread)
add thread to the thread manager and taking ownership for the thread
bool StartThread(const tThreadData &aThreadData)
std::deque< tThreadData > maWaitingForStartThreads
bool StartingOfThreadsSuspended()
std::deque< tThreadData > maStartedThreads
bool mbStartingOfThreadsSuspended
osl::Mutex maMutex
void SuspendStartingOfThreads()
suspend the starting of threads
helper class to observe threads
interface class to listen on the finish of a thread
css::uno::WeakReference< css::util::XJobManager > mrThreadJoiner
std::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef()
bool operator()(const tThreadData &rThreadData) const
oslInterlockedCount nThreadID
oslInterlockedCount const mnThreadID
::rtl::Reference< ObservableThread > pThread
DECL_LINK(TryToStartNewThread, Timer *, void)
void NotifyAboutFinishedThread(const oslInterlockedCount nThreadID)
static const std::deque< tThreadData >::size_type mnStartedSize
class to manage threads