Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "tools/TimerBasedTaskExecution.hxx"
31 : : #include "tools/AsynchronousTask.hxx"
32 : : #include <tools/time.hxx>
33 : : #include <osl/diagnose.h>
34 : : #include <boost/weak_ptr.hpp>
35 : : #include "sal/log.hxx"
36 : :
37 : : namespace sd { namespace tools {
38 : :
39 : : /** Used by the shared_ptr instead of the private destructor.
40 : : */
41 : : class TimerBasedTaskExecution::Deleter
42 : : {
43 : : public:
44 : 0 : void operator() (TimerBasedTaskExecution* pObject)
45 : : {
46 [ # # ]: 0 : delete pObject;
47 : 0 : }
48 : : };
49 : :
50 : :
51 : :
52 : :
53 : 0 : ::boost::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create (
54 : : const ::boost::shared_ptr<AsynchronousTask>& rpTask,
55 : : sal_uInt32 nMillisecondsBetweenSteps,
56 : : sal_uInt32 nMaxTimePerStep)
57 : : {
58 : : ::boost::shared_ptr<TimerBasedTaskExecution> pExecution(
59 [ # # ]: 0 : new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep),
60 [ # # ][ # # ]: 0 : Deleter());
61 : : // Let the new object have a shared_ptr to itself, so that it can
62 : : // release itself when the AsynchronousTask has been executed
63 : : // completely.
64 [ # # ]: 0 : pExecution->SetSelf(pExecution);
65 : 0 : return pExecution;
66 : : }
67 : :
68 : :
69 : :
70 : :
71 : 0 : void TimerBasedTaskExecution::Release (void)
72 : : {
73 : 0 : maTimer.Stop();
74 : 0 : mpSelf.reset();
75 : 0 : }
76 : :
77 : :
78 : :
79 : :
80 : : //static
81 : 0 : void TimerBasedTaskExecution::ReleaseTask (
82 : : const ::boost::weak_ptr<TimerBasedTaskExecution>& rpExecution)
83 : : {
84 [ # # ]: 0 : if ( ! rpExecution.expired())
85 : : {
86 : : try
87 : : {
88 [ # # ]: 0 : ::boost::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution);
89 [ # # ][ # # ]: 0 : pExecution->Release();
[ # # ]
90 : : }
91 : 0 : catch (const ::boost::bad_weak_ptr&)
92 : : {
93 : : // When a bad_weak_ptr has been thrown then the object pointed
94 : : // to by rpTask has been released right after we checked that it
95 : : // still existed. Too bad, but that means, that we have nothing
96 : : // more do.
97 : : }
98 : : }
99 : 0 : }
100 : :
101 : :
102 : :
103 : :
104 : 0 : TimerBasedTaskExecution::TimerBasedTaskExecution (
105 : : const ::boost::shared_ptr<AsynchronousTask>& rpTask,
106 : : sal_uInt32 nMillisecondsBetweenSteps,
107 : : sal_uInt32 nMaxTimePerStep)
108 : : : mpTask(rpTask),
109 : : maTimer(),
110 : : mpSelf(),
111 [ # # ][ # # ]: 0 : mnMaxTimePerStep(nMaxTimePerStep)
112 : : {
113 [ # # ]: 0 : Link aLink(LINK(this,TimerBasedTaskExecution,TimerCallback));
114 : 0 : maTimer.SetTimeoutHdl(aLink);
115 [ # # ]: 0 : maTimer.SetTimeout(nMillisecondsBetweenSteps);
116 [ # # ]: 0 : maTimer.Start();
117 : 0 : }
118 : :
119 : :
120 : :
121 : :
122 [ # # ][ # # ]: 0 : TimerBasedTaskExecution::~TimerBasedTaskExecution (void)
123 : : {
124 [ # # ]: 0 : maTimer.Stop();
125 : 0 : }
126 : :
127 : :
128 : :
129 : :
130 : 0 : void TimerBasedTaskExecution::SetSelf (
131 : : const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf)
132 : : {
133 [ # # ]: 0 : if (mpTask.get() != NULL)
134 : 0 : mpSelf = rpSelf;
135 : 0 : }
136 : :
137 : :
138 : :
139 : :
140 : 0 : IMPL_LINK_NOARG(TimerBasedTaskExecution, TimerCallback)
141 : : {
142 [ # # ]: 0 : if (mpTask.get() != NULL)
143 : : {
144 [ # # ]: 0 : if (mpTask->HasNextStep())
145 : : {
146 : : // Execute as many steps as fit into the time span of length
147 : : // mnMaxTimePerStep. Note that the last step may take longer
148 : : // than allowed.
149 [ # # ][ # # ]: 0 : sal_uInt32 nStartTime (Time( Time::SYSTEM ).GetMSFromTime());
150 : : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": starting TimerBasedTaskExecution at " << nStartTime);
151 [ # # ][ # # ]: 0 : do
152 : : {
153 [ # # ]: 0 : mpTask->RunNextStep();
154 [ # # ][ # # ]: 0 : sal_uInt32 nDuration (Time( Time::SYSTEM ).GetMSFromTime()-nStartTime);
155 : : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": executed step in " << nDuration);
156 [ # # ]: 0 : if (nDuration > mnMaxTimePerStep)
157 : : break;
158 : : }
159 : 0 : while (mpTask->HasNextStep());
160 : : SAL_INFO("sd.tools", OSL_THIS_FUNC << ": TimerBasedTaskExecution sleeping");
161 [ # # ]: 0 : maTimer.Start();
162 : : }
163 : : else
164 : 0 : mpSelf.reset();
165 : : }
166 : :
167 : 0 : return 0;
168 : : }
169 : :
170 : :
171 : : } } // end of namespace ::sd::tools
172 : :
173 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|