Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / docnode / threadmanager.hxx
blobace0bc03164b5e95bc34064b8c94b8a05e20e7e8
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 #pragma once
22 #include <vcl/idle.hxx>
23 #include <mutex>
24 #include <osl/interlck.h>
25 #include <rtl/ref.hxx>
27 #include <deque>
28 #include <cppuhelper/weakref.hxx>
29 #include <observablethread.hxx>
31 #include <memory>
33 namespace com::sun::star::util { class XCancellable; }
34 namespace com::sun::star::util { class XJobManager; }
36 class IFinishedThreadListener;
37 class ThreadListener;
38 class Timer;
40 /** class to manage threads
42 OD 2007-01-29 #i73788#
43 An instance of this class takes care of the starting of threads.
44 It assures that not more than <mnStartedSize> threads
45 are started.
47 class ThreadManager final
49 public:
51 explicit ThreadManager( css::uno::Reference< css::util::XJobManager > const & rThreadJoiner );
52 ~ThreadManager();
54 std::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef() const;
55 void NotifyAboutFinishedThread( const oslInterlockedCount nThreadID );
57 /** initialization
59 IMPORTANT NOTE: Needs to be called directly after construction
61 void Init();
63 /** add thread to the thread manager and taking ownership for the thread
65 @return unique ID for added thread
67 oslInterlockedCount AddThread(
68 const ::rtl::Reference< ObservableThread >& rThread );
70 void RemoveThread( const oslInterlockedCount nThreadID,
71 const bool bThreadFinished = false );
73 DECL_LINK( TryToStartNewThread, Timer*, void );
75 /** suspend the starting of threads
77 Suspending the starting of further threads is sensible during the
78 destruction of a Writer document.
80 void SuspendStartingOfThreads()
82 std::unique_lock aGuard(maMutex);
84 mbStartingOfThreadsSuspended = true;
87 /** continues the starting of threads after it has been suspended
89 void ResumeStartingOfThreads();
91 bool StartingOfThreadsSuspended()
93 std::unique_lock aGuard(maMutex);
95 return mbStartingOfThreadsSuspended;
98 struct tThreadData
100 oslInterlockedCount nThreadID;
101 ::rtl::Reference< ObservableThread > pThread;
102 css::uno::Reference< css::util::XCancellable > aJob;
104 tThreadData()
105 : nThreadID( 0 ),
106 aJob()
110 private:
112 static const std::deque< tThreadData >::size_type snStartedSize;
114 std::mutex maMutex;
116 css::uno::WeakReference< css::util::XJobManager > mrThreadJoiner;
118 std::shared_ptr< ThreadListener > mpThreadListener;
120 oslInterlockedCount mnThreadIDCounter;
122 std::deque< tThreadData > maWaitingForStartThreads;
123 std::deque< tThreadData > maStartedThreads;
125 Idle maStartNewThreadIdle;
127 bool mbStartingOfThreadsSuspended;
129 struct ThreadPred
131 oslInterlockedCount mnThreadID;
132 explicit ThreadPred( oslInterlockedCount nThreadID )
133 : mnThreadID( nThreadID )
136 bool operator() ( const tThreadData& rThreadData ) const
138 return rThreadData.nThreadID == mnThreadID;
142 bool StartWaitingThread();
144 bool StartThread( const tThreadData& aThreadData );
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */