LibreOffice Module sw (master) 1
finalthreadmanager.cxx
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
21
22#include <osl/diagnose.h>
23#include <osl/thread.hxx>
25#include <swthreadjoiner.hxx>
26
27#include <com/sun/star/frame/Desktop.hpp>
28#include <com/sun/star/frame/TerminationVetoException.hpp>
29#include <rtl/ustring.hxx>
31#include <mutex>
32#include <thread>
33#include <utility>
34
39class CancelJobsThread : public osl::Thread
40{
41 public:
42 explicit CancelJobsThread( std::list< css::uno::Reference< css::util::XCancellable > >&& rJobs )
43 : maJobs( std::move(rJobs) ),
44 mbAllJobsCancelled( false ),
45 mbStopped( false )
46 {
47 }
48
49 void addJobs( std::list< css::uno::Reference< css::util::XCancellable > >& rJobs );
50 bool allJobsCancelled() const;
52
53 private:
54 bool existJobs() const;
55
56 css::uno::Reference< css::util::XCancellable > getNextJob();
57
58 bool stopped() const;
59 virtual void SAL_CALL run() override;
60 mutable std::mutex maMutex;
61
62 std::list< css::uno::Reference< css::util::XCancellable > > maJobs;
63
66};
67
68void CancelJobsThread::addJobs( std::list< css::uno::Reference< css::util::XCancellable > >& rJobs )
69{
70 std::scoped_lock aGuard(maMutex);
71
72 maJobs.insert( maJobs.end(), rJobs.begin(), rJobs.end() );
73 mbAllJobsCancelled = !maJobs.empty();
74}
75
77{
78 std::scoped_lock aGuard(maMutex);
79
80 return !maJobs.empty();
81}
82
84{
85 std::scoped_lock aGuard(maMutex);
86
87 return maJobs.empty() && mbAllJobsCancelled;
88}
89
91{
92 std::scoped_lock aGuard(maMutex);
93
94 mbStopped = true;
95}
96
97css::uno::Reference< css::util::XCancellable > CancelJobsThread::getNextJob()
98{
99 css::uno::Reference< css::util::XCancellable > xRet;
100
101 {
102 std::scoped_lock aGuard(maMutex);
103
104 if ( !maJobs.empty() )
105 {
106 xRet = maJobs.front();
107 maJobs.pop_front();
108 }
109 }
110
111 return xRet;
112}
113
115{
116 std::scoped_lock aGuard(maMutex);
117
118 return mbStopped;
119}
120
122{
123 osl_setThreadName("sw CancelJobsThread");
124
125 while ( !stopped() )
126 {
127 while ( existJobs() )
128 {
129 css::uno::Reference< css::util::XCancellable > aJob( getNextJob() );
130 if ( aJob.is() )
131 aJob->cancel();
132 }
133
134 mbAllJobsCancelled = true;
135
136 {
137 std::this_thread::sleep_for(std::chrono::seconds(1));
138 }
139 }
140}
141
146class TerminateOfficeThread : public osl::Thread
147{
148 public:
149 TerminateOfficeThread( CancelJobsThread const & rCancelJobsThread,
150 css::uno::Reference< css::uno::XComponentContext > xContext )
151 : mrCancelJobsThread( rCancelJobsThread ),
153 mxContext(std::move( xContext ))
154 {
155 }
156
158
159 private:
160 virtual void SAL_CALL run() override;
161 virtual void SAL_CALL onTerminated() override;
164
165 osl::Mutex maMutex;
166
169
170 css::uno::Reference< css::uno::XComponentContext > mxContext;
171};
172
174{
175 osl::MutexGuard aGuard(maMutex);
176
178}
179
181{
182 osl::MutexGuard aGuard(maMutex);
183
185}
186
188{
189 osl_setThreadName("sw TerminateOfficeThread");
190
191 while ( !OfficeTerminationStopped() )
192 {
193 osl::MutexGuard aGuard(maMutex);
194
196 break;
197 }
198
201}
202
204{
205 css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create(mxContext);
206
207 css::uno::Reference< css::container::XElementAccess > xList = xDesktop->getFrames();
208 if ( !xList.is() )
209 {
210 OSL_FAIL( "<TerminateOfficeThread::PerformOfficeTermination()> - no XElementAccess!" );
211 return;
212 }
213
214 if ( !xList->hasElements() )
215 {
217 xDesktop->terminate();
218 }
219}
220
222{
224 delete this;
225}
226
227FinalThreadManager::FinalThreadManager(css::uno::Reference< css::uno::XComponentContext > context)
228 : m_xContext(std::move(context)),
229 mpTerminateOfficeThread( nullptr ),
230 mbRegisteredAtDesktop( false )
231{
232
233}
234
236{
237 css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create(m_xContext);
238 xDesktop->addTerminateListener( css::uno::Reference< css::frame::XTerminateListener >( this ) );
239}
240
242{
244 {
245 mpPauseThreadStarting.reset();
246 }
247
248 if ( mpTerminateOfficeThread != nullptr )
249 {
250 mpTerminateOfficeThread->StopOfficeTermination(); // thread kills itself.
251 mpTerminateOfficeThread = nullptr;
252 }
253
254 if ( !maThreads.empty() )
255 {
256 OSL_FAIL( "<FinalThreadManager::~FinalThreadManager()> - still registered jobs are existing -> perform cancellation" );
258 }
259
260 if ( mpCancelJobsThread != nullptr )
261 {
262 if ( !mpCancelJobsThread->allJobsCancelled() )
263 OSL_FAIL( "<FinalThreadManager::~FinalThreadManager()> - cancellation of registered jobs not yet finished -> wait for its finish" );
264
265 mpCancelJobsThread->stopWhenAllJobsCancelled();
266 mpCancelJobsThread->join();
267 mpCancelJobsThread.reset();
268 }
269}
270
271// com.sun.star.uno.XServiceInfo:
273{
274 return "com.sun.star.util.comp.FinalThreadManager";
275}
276
277sal_Bool SAL_CALL FinalThreadManager::supportsService(OUString const & serviceName)
278{
279 return cppu::supportsService(this, serviceName);
280}
281
282css::uno::Sequence< OUString > SAL_CALL FinalThreadManager::getSupportedServiceNames()
283{
284 return { "com.sun.star.util.JobManager" };
285}
286
287// css::util::XJobManager:
288void SAL_CALL FinalThreadManager::registerJob(const css::uno::Reference< css::util::XCancellable > & Job)
289{
290 osl::MutexGuard aGuard(maMutex);
291
292 maThreads.push_back( Job );
293
295 {
298 }
299}
300
301void SAL_CALL FinalThreadManager::releaseJob(const css::uno::Reference< css::util::XCancellable > & Job)
302{
303 osl::MutexGuard aGuard(maMutex);
304
305 maThreads.remove( Job );
306}
307
309{
310 std::list< css::uno::Reference< css::util::XCancellable > > aThreads;
311 {
312 osl::MutexGuard aGuard(maMutex);
313
314 aThreads.insert( aThreads.end(), maThreads.begin(), maThreads.end() );
315 maThreads.clear();
316 }
317
318 if ( aThreads.empty() )
319 return;
320
321 osl::MutexGuard aGuard(maMutex);
322
323 if ( mpCancelJobsThread == nullptr )
324 {
325 mpCancelJobsThread.reset(new CancelJobsThread( std::list(aThreads) ));
326 if ( !mpCancelJobsThread->create() )
327 {
328 mpCancelJobsThread.reset();
329 for (auto const& elem : aThreads)
330 {
331 elem->cancel();
332 }
333 aThreads.clear();
334 }
335 }
336 else
337 mpCancelJobsThread->addJobs( aThreads );
338}
339
340// css::frame::XTerminateListener
341void SAL_CALL FinalThreadManager::queryTermination( const css::lang::EventObject& )
342{
343 osl::MutexGuard aGuard(maMutex);
344
346 // Sleep 1 second to give the thread for job cancellation some time.
347 // Probably, all started threads have already finished its work.
348 if ( mpCancelJobsThread != nullptr &&
349 !mpCancelJobsThread->allJobsCancelled() )
350 {
351 std::this_thread::sleep_for(std::chrono::seconds(1));
352 }
353
354 if ( mpCancelJobsThread != nullptr &&
355 !mpCancelJobsThread->allJobsCancelled() )
356 {
357 if ( mpTerminateOfficeThread != nullptr )
358 {
359 if ( mpTerminateOfficeThread->isRunning() )
360 mpTerminateOfficeThread->StopOfficeTermination(); // thread kills itself.
361 else
363
364 mpTerminateOfficeThread = nullptr;
365 }
367 m_xContext );
368 if ( !mpTerminateOfficeThread->create() )
369 {
371 mpTerminateOfficeThread = nullptr;
372 }
373
374 throw css::frame::TerminationVetoException();
375 }
376
378}
379
380void SAL_CALL FinalThreadManager::cancelTermination( const css::lang::EventObject& )
381{
382 mpPauseThreadStarting.reset();
383}
384
385void SAL_CALL FinalThreadManager::notifyTermination( const css::lang::EventObject& )
386{
387 if ( mpTerminateOfficeThread != nullptr )
388 {
389 if ( mpTerminateOfficeThread->isRunning() )
390 mpTerminateOfficeThread->StopOfficeTermination(); // thread kills itself.
391 else
393
394 mpTerminateOfficeThread = nullptr;
395 }
396
397 if ( !maThreads.empty() )
399
400 if ( mpCancelJobsThread != nullptr )
401 {
402 mpCancelJobsThread->stopWhenAllJobsCancelled();
403 mpCancelJobsThread->join();
404 mpCancelJobsThread.reset();
405 }
406
407 // get reference of this
408 css::uno::Reference< css::uno::XInterface > aOwnRef( static_cast< cppu::OWeakObject* >( this ));
409 // notify <SwThreadJoiner> to release its reference
411}
412
413// ::com::sun:star::lang::XEventListener (inherited via css::frame::XTerminateListener)
414void SAL_CALL FinalThreadManager::disposing( const css::lang::EventObject& )
415{
416 // nothing to do, because instance doesn't hold any references of observed objects
417}
418
419extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
421 css::uno::Sequence<css::uno::Any> const &)
422{
423 return cppu::acquire(new FinalThreadManager(context));
424}
425
426/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
thread to cancel a give list of cancellable jobs
CancelJobsThread(std::list< css::uno::Reference< css::util::XCancellable > > &&rJobs)
std::list< css::uno::Reference< css::util::XCancellable > > maJobs
css::uno::Reference< css::util::XCancellable > getNextJob()
bool allJobsCancelled() const
virtual void SAL_CALL run() override
void addJobs(std::list< css::uno::Reference< css::util::XCancellable > > &rJobs)
virtual void SAL_CALL notifyTermination(const css::lang::EventObject &Event) override
std::unique_ptr< SwPauseThreadStarting, o3tl::default_delete< SwPauseThreadStarting > > mpPauseThreadStarting
virtual OUString SAL_CALL getImplementationName() override
virtual ~FinalThreadManager() override
virtual void SAL_CALL releaseJob(const css::uno::Reference< css::util::XCancellable > &Job) override
TerminateOfficeThread * mpTerminateOfficeThread
std::list< css::uno::Reference< css::util::XCancellable > > maThreads
std::unique_ptr< CancelJobsThread > mpCancelJobsThread
virtual void SAL_CALL registerJob(const css::uno::Reference< css::util::XCancellable > &Job) override
virtual void SAL_CALL cancelAllJobs() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual void SAL_CALL queryTermination(const css::lang::EventObject &Event) override
FinalThreadManager(css::uno::Reference< css::uno::XComponentContext > context)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual void SAL_CALL disposing(const css::lang::EventObject &Source) override
virtual void SAL_CALL cancelTermination(const css::lang::EventObject &Event) override
Helper class to pause starting of threads during existence of an instance of this class.
thread to terminate office, when all jobs are cancelled.
virtual void SAL_CALL onTerminated() override
const CancelJobsThread & mrCancelJobsThread
virtual void SAL_CALL run() override
TerminateOfficeThread(CancelJobsThread const &rCancelJobsThread, css::uno::Reference< css::uno::XComponentContext > xContext)
css::uno::Reference< css::uno::XComponentContext > mxContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_util_comp_FinalThreadManager_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
void ReleaseThreadJoiner()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool