1*5b190011SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*5b190011SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*5b190011SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*5b190011SAndrew Rist * distributed with this work for additional information 6*5b190011SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*5b190011SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*5b190011SAndrew Rist * "License"); you may not use this file except in compliance 9*5b190011SAndrew Rist * with the License. You may obtain a copy of the License at 10*5b190011SAndrew Rist * 11*5b190011SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*5b190011SAndrew Rist * 13*5b190011SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*5b190011SAndrew Rist * software distributed under the License is distributed on an 15*5b190011SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*5b190011SAndrew Rist * KIND, either express or implied. See the License for the 17*5b190011SAndrew Rist * specific language governing permissions and limitations 18*5b190011SAndrew Rist * under the License. 19*5b190011SAndrew Rist * 20*5b190011SAndrew Rist *************************************************************/ 21*5b190011SAndrew Rist 22*5b190011SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sd.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "tools/TimerBasedTaskExecution.hxx" 28cdf0e10cSrcweir #include "tools/AsynchronousTask.hxx" 29cdf0e10cSrcweir #include <tools/time.hxx> 30cdf0e10cSrcweir #include <osl/diagnose.h> 31cdf0e10cSrcweir #include <boost/weak_ptr.hpp> 32cdf0e10cSrcweir 33cdf0e10cSrcweir #undef VERBOSE 34cdf0e10cSrcweir 35cdf0e10cSrcweir namespace sd { namespace tools { 36cdf0e10cSrcweir 37cdf0e10cSrcweir /** Used by the shared_ptr instead of the private destructor. 38cdf0e10cSrcweir */ 39cdf0e10cSrcweir class TimerBasedTaskExecution::Deleter 40cdf0e10cSrcweir { 41cdf0e10cSrcweir public: 42cdf0e10cSrcweir void operator() (TimerBasedTaskExecution* pObject) 43cdf0e10cSrcweir { 44cdf0e10cSrcweir delete pObject; 45cdf0e10cSrcweir } 46cdf0e10cSrcweir }; 47cdf0e10cSrcweir 48cdf0e10cSrcweir 49cdf0e10cSrcweir 50cdf0e10cSrcweir 51cdf0e10cSrcweir ::boost::shared_ptr<TimerBasedTaskExecution> TimerBasedTaskExecution::Create ( 52cdf0e10cSrcweir const ::boost::shared_ptr<AsynchronousTask>& rpTask, 53cdf0e10cSrcweir sal_uInt32 nMillisecondsBetweenSteps, 54cdf0e10cSrcweir sal_uInt32 nMaxTimePerStep) 55cdf0e10cSrcweir { 56cdf0e10cSrcweir ::boost::shared_ptr<TimerBasedTaskExecution> pExecution( 57cdf0e10cSrcweir new TimerBasedTaskExecution(rpTask,nMillisecondsBetweenSteps,nMaxTimePerStep), 58cdf0e10cSrcweir Deleter()); 59cdf0e10cSrcweir // Let the new object have a shared_ptr to itself, so that it can 60cdf0e10cSrcweir // release itself when the AsynchronousTask has been executed 61cdf0e10cSrcweir // completely. 62cdf0e10cSrcweir pExecution->SetSelf(pExecution); 63cdf0e10cSrcweir return pExecution; 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir 67cdf0e10cSrcweir 68cdf0e10cSrcweir 69cdf0e10cSrcweir void TimerBasedTaskExecution::Release (void) 70cdf0e10cSrcweir { 71cdf0e10cSrcweir maTimer.Stop(); 72cdf0e10cSrcweir mpSelf.reset(); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir 76cdf0e10cSrcweir 77cdf0e10cSrcweir 78cdf0e10cSrcweir //static 79cdf0e10cSrcweir void TimerBasedTaskExecution::ReleaseTask ( 80cdf0e10cSrcweir const ::boost::weak_ptr<TimerBasedTaskExecution>& rpExecution) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir if ( ! rpExecution.expired()) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir try 85cdf0e10cSrcweir { 86cdf0e10cSrcweir ::boost::shared_ptr<tools::TimerBasedTaskExecution> pExecution (rpExecution); 87cdf0e10cSrcweir pExecution->Release(); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir catch (::boost::bad_weak_ptr) 90cdf0e10cSrcweir { 91cdf0e10cSrcweir // When a bad_weak_ptr has been thrown then the object pointed 92cdf0e10cSrcweir // to by rpTask has been released right after we checked that it 93cdf0e10cSrcweir // still existed. Too bad, but that means, that we have nothing 94cdf0e10cSrcweir // more do. 95cdf0e10cSrcweir } 96cdf0e10cSrcweir } 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir 100cdf0e10cSrcweir 101cdf0e10cSrcweir 102cdf0e10cSrcweir TimerBasedTaskExecution::TimerBasedTaskExecution ( 103cdf0e10cSrcweir const ::boost::shared_ptr<AsynchronousTask>& rpTask, 104cdf0e10cSrcweir sal_uInt32 nMillisecondsBetweenSteps, 105cdf0e10cSrcweir sal_uInt32 nMaxTimePerStep) 106cdf0e10cSrcweir : mpTask(rpTask), 107cdf0e10cSrcweir maTimer(), 108cdf0e10cSrcweir mpSelf(), 109cdf0e10cSrcweir mnMaxTimePerStep(nMaxTimePerStep) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir Link aLink(LINK(this,TimerBasedTaskExecution,TimerCallback)); 112cdf0e10cSrcweir maTimer.SetTimeoutHdl(aLink); 113cdf0e10cSrcweir maTimer.SetTimeout(nMillisecondsBetweenSteps); 114cdf0e10cSrcweir maTimer.Start(); 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir 118cdf0e10cSrcweir 119cdf0e10cSrcweir 120cdf0e10cSrcweir TimerBasedTaskExecution::~TimerBasedTaskExecution (void) 121cdf0e10cSrcweir { 122cdf0e10cSrcweir maTimer.Stop(); 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir 126cdf0e10cSrcweir 127cdf0e10cSrcweir 128cdf0e10cSrcweir void TimerBasedTaskExecution::SetSelf ( 129cdf0e10cSrcweir const ::boost::shared_ptr<TimerBasedTaskExecution>& rpSelf) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir if (mpTask.get() != NULL) 132cdf0e10cSrcweir mpSelf = rpSelf; 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir 136cdf0e10cSrcweir 137cdf0e10cSrcweir 138cdf0e10cSrcweir IMPL_LINK(TimerBasedTaskExecution,TimerCallback, Timer*,EMPTYARG) 139cdf0e10cSrcweir { 140cdf0e10cSrcweir if (mpTask.get() != NULL) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir if (mpTask->HasNextStep()) 143cdf0e10cSrcweir { 144cdf0e10cSrcweir // Execute as many steps as fit into the time span of length 145cdf0e10cSrcweir // mnMaxTimePerStep. Note that the last step may take longer 146cdf0e10cSrcweir // than allowed. 147cdf0e10cSrcweir sal_uInt32 nStartTime (Time().GetMSFromTime()); 148cdf0e10cSrcweir #ifdef VERBOSE 149cdf0e10cSrcweir OSL_TRACE("starting TimerBasedTaskExecution at %d", nStartTime); 150cdf0e10cSrcweir #endif 151cdf0e10cSrcweir do 152cdf0e10cSrcweir { 153cdf0e10cSrcweir mpTask->RunNextStep(); 154cdf0e10cSrcweir sal_uInt32 nDuration (Time().GetMSFromTime()-nStartTime); 155cdf0e10cSrcweir #ifdef VERBOSE 156cdf0e10cSrcweir OSL_TRACE("executed step in %d", nDuration); 157cdf0e10cSrcweir #endif 158cdf0e10cSrcweir if (nDuration > mnMaxTimePerStep) 159cdf0e10cSrcweir break; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir while (mpTask->HasNextStep()); 162cdf0e10cSrcweir #ifdef VERBOSE 163cdf0e10cSrcweir OSL_TRACE("TimerBasedTaskExecution sleeping"); 164cdf0e10cSrcweir #endif 165cdf0e10cSrcweir maTimer.Start(); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir else 168cdf0e10cSrcweir mpSelf.reset(); 169cdf0e10cSrcweir } 170cdf0e10cSrcweir 171cdf0e10cSrcweir return 0; 172cdf0e10cSrcweir } 173cdf0e10cSrcweir 174cdf0e10cSrcweir 175cdf0e10cSrcweir } } // end of namespace ::sd::tools 176cdf0e10cSrcweir 177