1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TimerBasedTaskExecution.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include "tools/TimerBasedTaskExecution.hxx"
35 #include "tools/AsynchronousTask.hxx"
36 #include <tools/time.hxx>
37 #include <osl/diagnose.h>
38 #include <boost/weak_ptr.hpp>
42 namespace sd
{ namespace tools
{
44 /** Used by the shared_ptr instead of the private destructor.
46 class TimerBasedTaskExecution::Deleter
49 void operator() (TimerBasedTaskExecution
* pObject
)
58 ::boost::shared_ptr
<TimerBasedTaskExecution
> TimerBasedTaskExecution::Create (
59 const ::boost::shared_ptr
<AsynchronousTask
>& rpTask
,
60 sal_uInt32 nMillisecondsBetweenSteps
,
61 sal_uInt32 nMaxTimePerStep
)
63 ::boost::shared_ptr
<TimerBasedTaskExecution
> pExecution(
64 new TimerBasedTaskExecution(rpTask
,nMillisecondsBetweenSteps
,nMaxTimePerStep
),
66 // Let the new object have a shared_ptr to itself, so that it can
67 // release itself when the AsynchronousTask has been executed
69 pExecution
->SetSelf(pExecution
);
76 void TimerBasedTaskExecution::Release (void)
86 void TimerBasedTaskExecution::ReleaseTask (
87 const ::boost::weak_ptr
<TimerBasedTaskExecution
>& rpExecution
)
89 if ( ! rpExecution
.expired())
93 ::boost::shared_ptr
<tools::TimerBasedTaskExecution
> pExecution (rpExecution
);
94 pExecution
->Release();
96 catch (::boost::bad_weak_ptr
)
98 // When a bad_weak_ptr has been thrown then the object pointed
99 // to by rpTask has been released right after we checked that it
100 // still existed. Too bad, but that means, that we have nothing
109 TimerBasedTaskExecution::TimerBasedTaskExecution (
110 const ::boost::shared_ptr
<AsynchronousTask
>& rpTask
,
111 sal_uInt32 nMillisecondsBetweenSteps
,
112 sal_uInt32 nMaxTimePerStep
)
116 mnMaxTimePerStep(nMaxTimePerStep
)
118 Link
aLink(LINK(this,TimerBasedTaskExecution
,TimerCallback
));
119 maTimer
.SetTimeoutHdl(aLink
);
120 maTimer
.SetTimeout(nMillisecondsBetweenSteps
);
127 TimerBasedTaskExecution::~TimerBasedTaskExecution (void)
135 void TimerBasedTaskExecution::SetSelf (
136 const ::boost::shared_ptr
<TimerBasedTaskExecution
>& rpSelf
)
138 if (mpTask
.get() != NULL
)
145 IMPL_LINK(TimerBasedTaskExecution
,TimerCallback
, Timer
*,EMPTYARG
)
147 if (mpTask
.get() != NULL
)
149 if (mpTask
->HasNextStep())
151 // Execute as many steps as fit into the time span of length
152 // mnMaxTimePerStep. Note that the last step may take longer
154 sal_uInt32
nStartTime (Time().GetMSFromTime());
156 OSL_TRACE("starting TimerBasedTaskExecution at %d", nStartTime
);
160 mpTask
->RunNextStep();
161 sal_uInt32
nDuration (Time().GetMSFromTime()-nStartTime
);
163 OSL_TRACE("executed step in %d", nDuration
);
165 if (nDuration
> mnMaxTimePerStep
)
168 while (mpTask
->HasNextStep());
170 OSL_TRACE("TimerBasedTaskExecution sleeping");
182 } } // end of namespace ::sd::tools