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
20#pragma once
21
22#include <vcl/idle.hxx>
23#include <mutex>
24#include <osl/interlck.h>
25#include <rtl/ref.hxx>
26
27#include <deque>
29#include <observablethread.hxx>
30
31#include <memory>
32
33namespace com::sun::star::util { class XCancellable; }
34namespace com::sun::star::util { class XJobManager; }
35
37class ThreadListener;
38class Timer;
39
47class ThreadManager final
48{
49 public:
50
51 explicit ThreadManager( css::uno::Reference< css::util::XJobManager > const & rThreadJoiner );
53
54 std::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef() const;
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 std::unique_lock aGuard(maMutex);
83
85 }
86
90
92 {
93 std::unique_lock aGuard(maMutex);
94
96 }
97
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 snStartedSize;
113
114 std::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
130 {
131 oslInterlockedCount 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
147/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
interface class to listen on the finish of a thread
helper class to observe threads
class to manage threads
std::deque< tThreadData > maStartedThreads
std::shared_ptr< ThreadListener > mpThreadListener
void SuspendStartingOfThreads()
suspend the starting of threads
bool StartThread(const tThreadData &aThreadData)
oslInterlockedCount mnThreadIDCounter
std::deque< tThreadData > maWaitingForStartThreads
std::mutex maMutex
void ResumeStartingOfThreads()
continues the starting of threads after it has been suspended
bool StartingOfThreadsSuspended()
css::uno::WeakReference< css::util::XJobManager > mrThreadJoiner
bool StartWaitingThread()
static const std::deque< tThreadData >::size_type snStartedSize
class to manage threads
ThreadManager(css::uno::Reference< css::util::XJobManager > const &rThreadJoiner)
oslInterlockedCount AddThread(const ::rtl::Reference< ObservableThread > &rThread)
add thread to the thread manager and taking ownership for the thread
void Init()
initialization
bool mbStartingOfThreadsSuspended
std::weak_ptr< IFinishedThreadListener > GetThreadListenerWeakRef() const
DECL_LINK(TryToStartNewThread, Timer *, void)
void NotifyAboutFinishedThread(const oslInterlockedCount nThreadID)
void RemoveThread(const oslInterlockedCount nThreadID, const bool bThreadFinished=false)
oslInterlockedCount mnThreadID
ThreadPred(oslInterlockedCount nThreadID)
bool operator()(const tThreadData &rThreadData) const
css::uno::Reference< css::util::XCancellable > aJob
::rtl::Reference< ObservableThread > pThread
oslInterlockedCount nThreadID