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  css::uno::Sequence< OUString > s { "com.sun.star.util.JobManager" };
288  return s;
289 }
290 
291 // css::util::XJobManager:
292 void SAL_CALL FinalThreadManager::registerJob(const css::uno::Reference< css::util::XCancellable > & Job)
293 {
294  osl::MutexGuard aGuard(maMutex);
295 
296  maThreads.push_back( Job );
297 
298  if ( !mbRegisteredAtDesktop )
299  {
301  mbRegisteredAtDesktop = true;
302  }
303 }
304 
305 void SAL_CALL FinalThreadManager::releaseJob(const css::uno::Reference< css::util::XCancellable > & Job)
306 {
307  osl::MutexGuard aGuard(maMutex);
308 
309  maThreads.remove( Job );
310 }
311 
313 {
314  std::list< css::uno::Reference< css::util::XCancellable > > aThreads;
315  {
316  osl::MutexGuard aGuard(maMutex);
317 
318  aThreads.insert( aThreads.end(), maThreads.begin(), maThreads.end() );
319  maThreads.clear();
320  }
321 
322  if ( !aThreads.empty() )
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 
344 // css::frame::XTerminateListener
345 void SAL_CALL FinalThreadManager::queryTermination( const css::lang::EventObject& )
346 {
347  osl::MutexGuard aGuard(maMutex);
348 
349  cancelAllJobs();
350  // Sleep 1 second to give the thread for job cancellation some time.
351  // Probably, all started threads have already finished its work.
352  if ( mpCancelJobsThread != nullptr &&
353  !mpCancelJobsThread->allJobsCancelled() )
354  {
355  osl::Thread::wait(std::chrono::seconds(1));
356  }
357 
358  if ( mpCancelJobsThread != nullptr &&
359  !mpCancelJobsThread->allJobsCancelled() )
360  {
361  if ( mpTerminateOfficeThread != nullptr )
362  {
363  if ( mpTerminateOfficeThread->isRunning() )
364  mpTerminateOfficeThread->StopOfficeTermination(); // thread kills itself.
365  else
367 
368  mpTerminateOfficeThread = nullptr;
369  }
371  m_xContext );
372  if ( !mpTerminateOfficeThread->create() )
373  {
375  mpTerminateOfficeThread = nullptr;
376  }
377 
378  throw css::frame::TerminationVetoException();
379  }
380 
382 }
383 
384 void SAL_CALL FinalThreadManager::cancelTermination( const css::lang::EventObject& )
385 {
386  mpPauseThreadStarting.reset();
387 }
388 
389 void SAL_CALL FinalThreadManager::notifyTermination( const css::lang::EventObject& )
390 {
391  if ( mpTerminateOfficeThread != nullptr )
392  {
393  if ( mpTerminateOfficeThread->isRunning() )
394  mpTerminateOfficeThread->StopOfficeTermination(); // thread kills itself.
395  else
397 
398  mpTerminateOfficeThread = nullptr;
399  }
400 
401  if ( !maThreads.empty() )
402  cancelAllJobs();
403 
404  if ( mpCancelJobsThread != nullptr )
405  {
406  mpCancelJobsThread->stopWhenAllJobsCancelled();
407  mpCancelJobsThread->join();
408  mpCancelJobsThread.reset();
409  }
410 
411  // get reference of this
412  css::uno::Reference< css::uno::XInterface > aOwnRef( static_cast< cppu::OWeakObject* >( this ));
413  // notify <SwThreadJoiner> to release its reference
415 }
416 
417 // ::com::sun:star::lang::XEventListener (inherited via css::frame::XTerminateListener)
418 void SAL_CALL FinalThreadManager::disposing( const css::lang::EventObject& )
419 {
420  // nothing to do, because instance doesn't hold any references of observed objects
421 }
422 
423 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
425  css::uno::Sequence<css::uno::Any> const &)
426 {
427  return cppu::acquire(new FinalThreadManager(context));
428 }
429 
430 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::list< css::uno::Reference< css::util::XCancellable > > maThreads
::osl::Mutex maMutex
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()
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
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
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 &)
const uno::Reference< uno::XComponentContext > m_xContext