bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / ui / tools / TimerBasedTaskExecution.cxx
bloba80680e79f0b87866c470b8062497d5b29ae54bc
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 .
21 #include "tools/TimerBasedTaskExecution.hxx"
22 #include "tools/AsynchronousTask.hxx"
23 #include <tools/time.hxx>
24 #include <osl/diagnose.h>
25 #include <boost/weak_ptr.hpp>
26 #include "sal/log.hxx"
28 namespace sd { namespace tools {
30 /** Used by the shared_ptr instead of the private destructor.
32 class TimerBasedTaskExecution::Deleter
34 public:
35 void operator() (TimerBasedTaskExecution* pObject)
37 delete pObject;
44 ::boost::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
45 const ::boost::shared_ptr<AsynchronousTask>& rpTask,
46 sal_uInt32 nMillisecondsBetweenSteps,
47 sal_uInt32 nMaxTimePerStep)
49 ::boost::shared_ptr<TimerBasedTaskExecution> pExecution(
50 new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep),
51 Deleter());
52 // Let the new object have a shared_ptr to itself, so that it can
53 // release itself when the AsynchronousTask has been executed
54 // completely.
55 pExecution->SetSelf(pExecution);
56 return pExecution;
62 void TimerBasedTaskExecution::Release (void)
64 maTimer.Stop();
65 mpSelf.reset();
71 //static
72 void TimerBasedTaskExecution::ReleaseTask (
73 const ::boost::weak_ptr<TimerBasedTaskExecution>& rpExecution)
75 if ( ! rpExecution.expired())
77 try
79 ::boost::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
80 pExecution->Release();
82 catch (const ::boost::bad_weak_ptr&)
84 // When a bad_weak_ptr has been thrown then the object pointed
85 // to by rpTask has been released right after we checked that it
86 // still existed. Too bad, but that means, that we have nothing
87 // more do.
95 TimerBasedTaskExecution::TimerBasedTaskExecution (
96 const ::boost::shared_ptr<AsynchronousTask>& rpTask,
97 sal_uInt32 nMillisecondsBetweenSteps,
98 sal_uInt32 nMaxTimePerStep)
99 : mpTask(rpTask),
100 maTimer(),
101 mpSelf(),
102 mnMaxTimePerStep(nMaxTimePerStep)
104 Link aLink(LINK(this,TimerBasedTaskExecution,TimerCallback));
105 maTimer.SetTimeoutHdl(aLink);
106 maTimer.SetTimeout(nMillisecondsBetweenSteps);
107 maTimer.Start();
113 TimerBasedTaskExecution::~TimerBasedTaskExecution (void)
115 maTimer.Stop();
121 void TimerBasedTaskExecution::SetSelf (
122 const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf)
124 if (mpTask.get() != NULL)
125 mpSelf = rpSelf;
131 IMPL_LINK_NOARG(TimerBasedTaskExecution, TimerCallback)
133 if (mpTask.get() != NULL)
135 if (mpTask->HasNextStep())
137 // Execute as many steps as fit into the time span of length
138 // mnMaxTimePerStep. Note that the last step may take longer
139 // than allowed.
140 sal_uInt32 nStartTime (Time( Time::SYSTEM ).GetMSFromTime());
141 SAL_INFO("sd.tools", OSL_THIS_FUNC << ": starting TimerBasedTaskExecution at " << nStartTime);
144 mpTask->RunNextStep();
145 sal_uInt32 nDuration (Time( Time::SYSTEM ).GetMSFromTime()-nStartTime);
146 SAL_INFO("sd.tools", OSL_THIS_FUNC << ": executed step in " << nDuration);
147 if (nDuration > mnMaxTimePerStep)
148 break;
150 while (mpTask->HasNextStep());
151 SAL_INFO("sd.tools", OSL_THIS_FUNC << ": TimerBasedTaskExecution sleeping");
152 maTimer.Start();
154 else
155 mpSelf.reset();
158 return 0;
162 } } // end of namespace ::sd::tools
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */