Line data Source code
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 :
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"
27 :
28 : namespace sd { namespace tools {
29 :
30 : /** Used by the shared_ptr instead of the private destructor.
31 : */
32 : class TimerBasedTaskExecution::Deleter
33 : {
34 : public:
35 0 : void operator() (TimerBasedTaskExecution* pObject)
36 : {
37 0 : delete pObject;
38 0 : }
39 : };
40 :
41 :
42 :
43 :
44 0 : ::boost::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
45 : const ::boost::shared_ptr<AsynchronousTask>& rpTask,
46 : sal_uInt32 nMillisecondsBetweenSteps,
47 : sal_uInt32 nMaxTimePerStep)
48 : {
49 : ::boost::shared_ptr<TimerBasedTaskExecution> pExecution(
50 0 : new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep),
51 0 : 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 0 : pExecution->SetSelf(pExecution);
56 0 : return pExecution;
57 : }
58 :
59 :
60 :
61 :
62 0 : void TimerBasedTaskExecution::Release (void)
63 : {
64 0 : maTimer.Stop();
65 0 : mpSelf.reset();
66 0 : }
67 :
68 :
69 :
70 :
71 : //static
72 0 : void TimerBasedTaskExecution::ReleaseTask (
73 : const ::boost::weak_ptr<TimerBasedTaskExecution>& rpExecution)
74 : {
75 0 : if ( ! rpExecution.expired())
76 : {
77 : try
78 : {
79 0 : ::boost::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
80 0 : pExecution->Release();
81 : }
82 0 : catch (const ::boost::bad_weak_ptr&)
83 : {
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.
88 : }
89 : }
90 0 : }
91 :
92 :
93 :
94 :
95 0 : 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 0 : mnMaxTimePerStep(nMaxTimePerStep)
103 : {
104 0 : Link aLink(LINK(this,TimerBasedTaskExecution,TimerCallback));
105 0 : maTimer.SetTimeoutHdl(aLink);
106 0 : maTimer.SetTimeout(nMillisecondsBetweenSteps);
107 0 : maTimer.Start();
108 0 : }
109 :
110 :
111 :
112 :
113 0 : TimerBasedTaskExecution::~TimerBasedTaskExecution (void)
114 : {
115 0 : maTimer.Stop();
116 0 : }
117 :
118 :
119 :
120 :
121 0 : void TimerBasedTaskExecution::SetSelf (
122 : const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf)
123 : {
124 0 : if (mpTask.get() != NULL)
125 0 : mpSelf = rpSelf;
126 0 : }
127 :
128 :
129 :
130 :
131 0 : IMPL_LINK_NOARG(TimerBasedTaskExecution, TimerCallback)
132 : {
133 0 : if (mpTask.get() != NULL)
134 : {
135 0 : if (mpTask->HasNextStep())
136 : {
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 0 : sal_uInt32 nStartTime (Time( Time::SYSTEM ).GetMSFromTime());
141 : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": starting TimerBasedTaskExecution at " << nStartTime);
142 0 : do
143 : {
144 0 : mpTask->RunNextStep();
145 0 : sal_uInt32 nDuration (Time( Time::SYSTEM ).GetMSFromTime()-nStartTime);
146 : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": executed step in " << nDuration);
147 0 : if (nDuration > mnMaxTimePerStep)
148 0 : break;
149 : }
150 0 : while (mpTask->HasNextStep());
151 : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": TimerBasedTaskExecution sleeping");
152 0 : maTimer.Start();
153 : }
154 : else
155 0 : mpSelf.reset();
156 : }
157 :
158 0 : return 0;
159 : }
160 :
161 :
162 : } } // end of namespace ::sd::tools
163 :
164 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|