bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / tools / TimerBasedTaskExecution.cxx
blobfb9f42cb96c1019a1b0c74608cf18a6accabe948
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 .
20 #include <tools/TimerBasedTaskExecution.hxx>
21 #include <tools/AsynchronousTask.hxx>
22 #include <tools/time.hxx>
23 #include <osl/diagnose.h>
24 #include <sal/log.hxx>
25 #include <memory>
27 namespace sd { namespace tools {
29 /** Used by the shared_ptr instead of the private destructor.
31 class TimerBasedTaskExecution::Deleter
33 public:
34 void operator() (TimerBasedTaskExecution* pObject)
36 delete pObject;
40 std::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
41 const std::shared_ptr<AsynchronousTask>& rpTask,
42 sal_uInt32 nMillisecondsBetweenSteps,
43 sal_uInt32 nMaxTimePerStep)
45 std::shared_ptr<TimerBasedTaskExecution> pExecution(
46 new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep),
47 Deleter());
48 // Let the new object have a shared_ptr to itself, so that it can
49 // release itself when the AsynchronousTask has been executed
50 // completely.
51 if (pExecution->mpTask != nullptr)
52 pExecution->mpSelf = pExecution;
53 return pExecution;
56 void TimerBasedTaskExecution::Release()
58 maTimer.Stop();
59 mpSelf.reset();
62 //static
63 void TimerBasedTaskExecution::ReleaseTask (
64 const std::weak_ptr<TimerBasedTaskExecution>& rpExecution)
66 if ( rpExecution.expired())
67 return;
69 try
71 std::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
72 pExecution->Release();
74 catch (const std::bad_weak_ptr&)
76 // When a bad_weak_ptr has been thrown then the object pointed
77 // to by rpTask has been released right after we checked that it
78 // still existed. Too bad, but that means, that we have nothing
79 // more do.
83 TimerBasedTaskExecution::TimerBasedTaskExecution (
84 const std::shared_ptr<AsynchronousTask>& rpTask,
85 sal_uInt32 nMillisecondsBetweenSteps,
86 sal_uInt32 nMaxTimePerStep)
87 : mpTask(rpTask),
88 maTimer(),
89 mpSelf(),
90 mnMaxTimePerStep(nMaxTimePerStep)
92 maTimer.SetInvokeHandler( LINK(this,TimerBasedTaskExecution,TimerCallback) );
93 maTimer.SetTimeout(nMillisecondsBetweenSteps);
94 maTimer.Start();
97 TimerBasedTaskExecution::~TimerBasedTaskExecution()
99 maTimer.Stop();
102 IMPL_LINK_NOARG(TimerBasedTaskExecution, TimerCallback, Timer *, void)
104 if (mpTask == nullptr)
105 return;
107 if (mpTask->HasNextStep())
109 // Execute as many steps as fit into the time span of length
110 // mnMaxTimePerStep. Note that the last step may take longer
111 // than allowed.
112 sal_uInt32 nStartTime (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime());
113 SAL_INFO("sd.tools", OSL_THIS_FUNC << ": starting TimerBasedTaskExecution at " << nStartTime);
116 mpTask->RunNextStep();
117 sal_uInt32 nDuration (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime()-nStartTime);
118 SAL_INFO("sd.tools", OSL_THIS_FUNC << ": executed step in " << nDuration);
119 if (nDuration > mnMaxTimePerStep)
120 break;
122 while (mpTask->HasNextStep());
123 SAL_INFO("sd.tools", OSL_THIS_FUNC << ": TimerBasedTaskExecution sleeping");
124 maTimer.Start();
126 else
127 mpSelf.reset();
130 } } // end of namespace ::sd::tools
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */