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