LibreOffice Module sd (master) 1
TimerBasedTaskExecution.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
22#include <tools/time.hxx>
23#include <sal/log.hxx>
24#include <memory>
25#include <utility>
26
27namespace sd::tools {
28
32{
33public:
35 {
36 delete pObject;
37 }
38};
39
40std::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
41 const std::shared_ptr<AsynchronousTask>& rpTask,
42 sal_uInt32 nMillisecondsBetweenSteps,
43 sal_uInt32 nMaxTimePerStep)
44{
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;
54}
55
57{
58 maTimer.Stop();
59 mpSelf.reset();
60}
61
62//static
64 const std::weak_ptr<TimerBasedTaskExecution>& rpExecution)
65{
66 if ( rpExecution.expired())
67 return;
68
69 try
70 {
71 std::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
72 pExecution->Release();
73 }
74 catch (const std::bad_weak_ptr&)
75 {
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.
80 }
81}
82
84 std::shared_ptr<AsynchronousTask> pTask,
85 sal_uInt32 nMillisecondsBetweenSteps,
86 sal_uInt32 nMaxTimePerStep)
87 : mpTask(std::move(pTask)),
88 maTimer("sd TimerBasedTaskExecution maTimer"),
89 mnMaxTimePerStep(nMaxTimePerStep)
90{
92 maTimer.SetTimeout(nMillisecondsBetweenSteps);
93 maTimer.Start();
94}
95
97{
98 maTimer.Stop();
99}
100
102{
103 if (mpTask == nullptr)
104 return;
105
106 if (mpTask->HasNextStep())
107 {
108 // Execute as many steps as fit into the time span of length
109 // mnMaxTimePerStep. Note that the last step may take longer
110 // than allowed.
111 sal_uInt32 nStartTime (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime());
112 SAL_INFO("sd.tools", __func__ << ": starting TimerBasedTaskExecution at " << nStartTime);
113 do
114 {
115 mpTask->RunNextStep();
116 sal_uInt32 nDuration (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime()-nStartTime);
117 SAL_INFO("sd.tools", __func__ << ": executed step in " << nDuration);
118 if (nDuration > mnMaxTimePerStep)
119 break;
120 }
121 while (mpTask->HasNextStep());
122 SAL_INFO("sd.tools", __func__ << ": TimerBasedTaskExecution sleeping");
123 maTimer.Start();
124 }
125 else
126 mpSelf.reset();
127}
128
129} // end of namespace ::sd::tools
130
131/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Stop()
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
Used by the shared_ptr instead of the private destructor.
void operator()(TimerBasedTaskExecution *pObject)
Execute an AsynchronousTask timer based, i.e.
static void ReleaseTask(const std::weak_ptr< TimerBasedTaskExecution > &rpTask)
Convenience method that calls Release() on the given task.
static std::shared_ptr< TimerBasedTaskExecution > Create(const std::shared_ptr< AsynchronousTask > &rpTask, sal_uInt32 nMillisecondsBetweenSteps, sal_uInt32 nMaxTimePerStep)
Create a new object of this class.
void Release()
Stop the execution of the task and release the shared pointer to itself so that it will eventually be...
std::shared_ptr< TimerBasedTaskExecution > mpSelf
This shared_ptr to this is used to destroy a TimerBasedTaskExecution object when its task has been ex...
TimerBasedTaskExecution(std::shared_ptr< AsynchronousTask > pTask, sal_uInt32 nMillisecondsBetweenSteps, sal_uInt32 nMaxTimePerStep)
EmbeddedObjectRef * pObject
#define SAL_INFO(area, stream)
IMPL_LINK_NOARG(EventMultiplexer::Implementation, SlideSorterSelectionChangeListener, LinkParamNone *, void)