LibreOffice Module vcl (master)  1
TaskStopwatch.hxx
Go to the documentation of this file.
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 
10 #ifndef INCLUDED_VCL_TASK_STOPWATCH_HXX
11 #define INCLUDED_VCL_TASK_STOPWATCH_HXX
12 
13 #include <tools/time.hxx>
14 #include <vcl/dllapi.h>
15 #include <vcl/inputtypes.hxx>
16 #include <vcl/svapp.hxx>
17 
32 {
33  static constexpr VclInputFlags eDefaultInputStop = VCL_INPUT_ANY & ~VclInputFlags::TIMER;
34  static constexpr unsigned int nDefaultTimeSlice = 50;
35  static unsigned int m_nTimeSlice;
36 
37  sal_uInt64 m_nStartTicks;
38  sal_uInt64 m_nIterStartTicks;
41 
42  bool nextIter(bool bQueryOnly)
43  {
44  sal_uInt64 nCurTicks = tools::Time::GetSystemTicks();
45  // handle system ticks wrap as exceeded time slice
46  if (nCurTicks < m_nStartTicks)
47  return false;
48 
49  if (!bQueryOnly && m_bConsiderLastIterTime)
50  {
51  // based on the last iter runtime, we don't expect to finish in time
52  // m_nTimeSlice < (nCurTicks - m_nStartTicks) + (nCurTicks - m_nIterStartTicks)
53  if (m_nTimeSlice < 2 * nCurTicks - m_nIterStartTicks - m_nStartTicks)
54  return false;
55  }
56  // time slice exceeded
57  else if (m_nTimeSlice < nCurTicks - m_nStartTicks)
58  return false;
59 
60  if (!bQueryOnly)
61  m_nIterStartTicks = nCurTicks;
62 
63  return !Application::AnyInput(m_eInputStop);
64  }
65 
66 public:
76  TaskStopwatch(bool bConciderLastIterTime = true)
77  : m_nStartTicks(tools::Time::GetSystemTicks())
78  , m_nIterStartTicks(m_nStartTicks)
79  , m_bConsiderLastIterTime(bConciderLastIterTime)
80  , m_eInputStop(eDefaultInputStop)
81  {
82  }
83 
87  bool exceededRuntime() { return !nextIter(true); }
88 
92  bool continueIter() { return nextIter(false); }
93 
97  void reset()
98  {
99  m_nStartTicks = tools::Time::GetSystemTicks();
100  m_nIterStartTicks = m_nStartTicks;
101  }
102 
108  void setInputStop(VclInputFlags eInputStop = eDefaultInputStop) { m_eInputStop = eInputStop; }
109  VclInputFlags inputStop() const { return m_eInputStop; }
110 
117  static unsigned int timeSlice() { return m_nTimeSlice; }
118  static void setTimeSlice(unsigned int nTimeSlice) { m_nTimeSlice = nTimeSlice; }
119 };
120 
121 #endif // INCLUDED_VCL_TASK_STOPWATCH_HXX
122 
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool continueIter()
Returns true, if another iteration will probably pass in the time slot.
TaskStopwatch(bool bConciderLastIterTime=true)
Per default the watch considers the last iter time when asking for an other iteration, so considers Scheduler::acceptableTaskTime as a maximum value.
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
void reset()
Reset the stopwatch.
VclInputFlags m_eInputStop
VclInputFlags inputStop() const
sal_uInt64 m_nIterStartTicks
VclInputFlags
Definition: inputtypes.hxx:25
bool m_bConsiderLastIterTime
static unsigned int m_nTimeSlice
static bool AnyInput(VclInputFlags nType=VCL_INPUT_ANY)
Determine if there are any pending input events.
Definition: svapp.cxx:567
static void setTimeSlice(unsigned int nTimeSlice)
sal_uInt64 m_nStartTicks
#define VCL_INPUT_ANY
Definition: inputtypes.hxx:39
bool nextIter(bool bQueryOnly)
bool exceededRuntime()
Returns true, if the time slot is already exceeded.
static unsigned int timeSlice()
Sets the time considered the acceptable maximum for a task to run.
Helper class primary used to track time of long running iterating tasks.
static sal_uInt64 GetSystemTicks()
void setInputStop(VclInputFlags eInputStop=eDefaultInputStop)
Sets the input events, which should also "exceed" the stopwatch.