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 : #include "tools/TimerBasedTaskExecution.hxx"
21 : #include "tools/AsynchronousTask.hxx"
22 : #include <tools/time.hxx>
23 : #include <osl/diagnose.h>
24 : #include <boost/weak_ptr.hpp>
25 : #include "sal/log.hxx"
26 :
27 : namespace sd { namespace tools {
28 :
29 : /** Used by the shared_ptr instead of the private destructor.
30 : */
31 : class TimerBasedTaskExecution::Deleter
32 : {
33 : public:
34 0 : void operator() (TimerBasedTaskExecution* pObject)
35 : {
36 0 : delete pObject;
37 0 : }
38 : };
39 :
40 0 : ::boost::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
41 : const ::boost::shared_ptr<AsynchronousTask>& rpTask,
42 : sal_uInt32 nMillisecondsBetweenSteps,
43 : sal_uInt32 nMaxTimePerStep)
44 : {
45 : ::boost::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 : pExecution->SetSelf(pExecution);
52 0 : return pExecution;
53 : }
54 :
55 0 : void TimerBasedTaskExecution::Release (void)
56 : {
57 0 : maTimer.Stop();
58 0 : mpSelf.reset();
59 0 : }
60 :
61 : //static
62 0 : void TimerBasedTaskExecution::ReleaseTask (
63 : const ::boost::weak_ptr<TimerBasedTaskExecution>& rpExecution)
64 : {
65 0 : if ( ! rpExecution.expired())
66 : {
67 : try
68 : {
69 0 : ::boost::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
70 0 : pExecution->Release();
71 : }
72 0 : catch (const ::boost::bad_weak_ptr&)
73 : {
74 : // When a bad_weak_ptr has been thrown then the object pointed
75 : // to by rpTask has been released right after we checked that it
76 : // still existed. Too bad, but that means, that we have nothing
77 : // more do.
78 : }
79 : }
80 0 : }
81 :
82 0 : TimerBasedTaskExecution::TimerBasedTaskExecution (
83 : const ::boost::shared_ptr<AsynchronousTask>& rpTask,
84 : sal_uInt32 nMillisecondsBetweenSteps,
85 : sal_uInt32 nMaxTimePerStep)
86 : : mpTask(rpTask),
87 : maTimer(),
88 : mpSelf(),
89 0 : mnMaxTimePerStep(nMaxTimePerStep)
90 : {
91 0 : Link aLink(LINK(this,TimerBasedTaskExecution,TimerCallback));
92 0 : maTimer.SetTimeoutHdl(aLink);
93 0 : maTimer.SetTimeout(nMillisecondsBetweenSteps);
94 0 : maTimer.Start();
95 0 : }
96 :
97 0 : TimerBasedTaskExecution::~TimerBasedTaskExecution (void)
98 : {
99 0 : maTimer.Stop();
100 0 : }
101 :
102 0 : void TimerBasedTaskExecution::SetSelf (
103 : const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf)
104 : {
105 0 : if (mpTask.get() != NULL)
106 0 : mpSelf = rpSelf;
107 0 : }
108 :
109 0 : IMPL_LINK_NOARG(TimerBasedTaskExecution, TimerCallback)
110 : {
111 0 : if (mpTask.get() != NULL)
112 : {
113 0 : if (mpTask->HasNextStep())
114 : {
115 : // Execute as many steps as fit into the time span of length
116 : // mnMaxTimePerStep. Note that the last step may take longer
117 : // than allowed.
118 0 : sal_uInt32 nStartTime (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime());
119 : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": starting TimerBasedTaskExecution at " << nStartTime);
120 0 : do
121 : {
122 0 : mpTask->RunNextStep();
123 0 : sal_uInt32 nDuration (::tools::Time( ::tools::Time::SYSTEM ).GetMSFromTime()-nStartTime);
124 : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": executed step in " << nDuration);
125 0 : if (nDuration > mnMaxTimePerStep)
126 0 : break;
127 : }
128 0 : while (mpTask->HasNextStep());
129 : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": TimerBasedTaskExecution sleeping");
130 0 : maTimer.Start();
131 : }
132 : else
133 0 : mpSelf.reset();
134 : }
135 :
136 0 : return 0;
137 : }
138 :
139 : } } // end of namespace ::sd::tools
140 :
141 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|