/src/libreoffice/sd/source/ui/tools/TimerBasedTaskExecution.cxx
Line | Count | Source |
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 <tools/TimerBasedTaskExecution.hxx> |
21 | | #include <tools/AsynchronousTask.hxx> |
22 | | #include <tools/time.hxx> |
23 | | #include <sal/log.hxx> |
24 | | #include <memory> |
25 | | #include <utility> |
26 | | |
27 | | namespace sdtools { |
28 | | |
29 | | /** Used by the shared_ptr instead of the private destructor. |
30 | | */ |
31 | | class TimerBasedTaskExecution::Deleter |
32 | | { |
33 | | public: |
34 | | void operator() (TimerBasedTaskExecution* pObject) |
35 | 0 | { |
36 | 0 | delete pObject; |
37 | 0 | } |
38 | | }; |
39 | | |
40 | | std::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create ( |
41 | | const std::shared_ptr<AsynchronousTask>& rpTask, |
42 | | sal_uInt32 nMillisecondsBetweenSteps, |
43 | | sal_uInt32 nMaxTimePerStep) |
44 | 0 | { |
45 | 0 | std::shared_ptr<TimerBasedTaskExecution> pExecution( |
46 | 0 | new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep), |
47 | 0 | 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 | 0 | if (pExecution->mpTask != nullptr) |
52 | 0 | pExecution->mpSelf = pExecution; |
53 | 0 | return pExecution; |
54 | 0 | } |
55 | | |
56 | | void TimerBasedTaskExecution::Release() |
57 | 0 | { |
58 | 0 | maTimer.Stop(); |
59 | 0 | mpSelf.reset(); |
60 | 0 | } |
61 | | |
62 | | //static |
63 | | void TimerBasedTaskExecution::ReleaseTask ( |
64 | | const std::weak_ptr<TimerBasedTaskExecution>& rpExecution) |
65 | 0 | { |
66 | 0 | if ( rpExecution.expired()) |
67 | 0 | return; |
68 | | |
69 | 0 | try |
70 | 0 | { |
71 | 0 | std::shared_ptr<sdtools::TimerBasedTaskExecution> pExecution (rpExecution); |
72 | 0 | pExecution->Release(); |
73 | 0 | } |
74 | 0 | catch (const std::bad_weak_ptr&) |
75 | 0 | { |
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 | 0 | } |
81 | 0 | } |
82 | | |
83 | | TimerBasedTaskExecution::TimerBasedTaskExecution ( |
84 | | std::shared_ptr<AsynchronousTask> pTask, |
85 | | sal_uInt32 nMillisecondsBetweenSteps, |
86 | | sal_uInt32 nMaxTimePerStep) |
87 | 0 | : mpTask(std::move(pTask)), |
88 | 0 | maTimer("sd TimerBasedTaskExecution maTimer"), |
89 | 0 | mnMaxTimePerStep(nMaxTimePerStep) |
90 | 0 | { |
91 | 0 | maTimer.SetInvokeHandler( LINK(this,TimerBasedTaskExecution,TimerCallback) ); |
92 | 0 | maTimer.SetTimeout(nMillisecondsBetweenSteps); |
93 | 0 | maTimer.Start(); |
94 | 0 | } |
95 | | |
96 | | TimerBasedTaskExecution::~TimerBasedTaskExecution() |
97 | 0 | { |
98 | 0 | maTimer.Stop(); |
99 | 0 | } |
100 | | |
101 | | IMPL_LINK_NOARG(TimerBasedTaskExecution, TimerCallback, Timer *, void) |
102 | 0 | { |
103 | 0 | if (mpTask == nullptr) |
104 | 0 | return; |
105 | | |
106 | 0 | if (mpTask->HasNextStep()) |
107 | 0 | { |
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 | 0 | sal_uInt32 nStartTime (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime()); |
112 | 0 | SAL_INFO("sd.tools", __func__ << ": starting TimerBasedTaskExecution at " << nStartTime); |
113 | 0 | do |
114 | 0 | { |
115 | 0 | mpTask->RunNextStep(); |
116 | 0 | sal_uInt32 nDuration (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime()-nStartTime); |
117 | 0 | SAL_INFO("sd.tools", __func__ << ": executed step in " << nDuration); |
118 | 0 | if (nDuration > mnMaxTimePerStep) |
119 | 0 | break; |
120 | 0 | } |
121 | 0 | while (mpTask->HasNextStep()); |
122 | 0 | SAL_INFO("sd.tools", __func__ << ": TimerBasedTaskExecution sleeping"); |
123 | 0 | maTimer.Start(); |
124 | 0 | } |
125 | 0 | else |
126 | 0 | mpSelf.reset(); |
127 | 0 | } |
128 | | |
129 | | } // end of namespace ::sdtools |
130 | | |
131 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |