workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / include / vcl / TaskStopwatch.hxx
blobb27b3a316abee115aa47c2ebac073d7f686174bf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_VCL_TASK_STOPWATCH_HXX
11 #define INCLUDED_VCL_TASK_STOPWATCH_HXX
13 #include <tools/time.hxx>
14 #include <vcl/dllapi.h>
15 #include <vcl/inputtypes.hxx>
16 #include <vcl/svapp.hxx>
18 /**
19 * Helper class primary used to track time of long running iterating tasks.
21 * Normally it should be sufficient to instantiate the watch object before
22 * starting the iteration and query continueIter() at the end of each.
24 * Called Stopwatch, because there is already a Timer class in the Scheduler.
26 * TODO: merge into the general Scheduler, so this can also be used to track
27 * Task runtimes in a more general way.
28 * TODO: handle fast iterations, where continueIter is called multiple times
29 * per tick, by counting the iterations per tick and use that for approximation.
30 **/
31 class VCL_DLLPUBLIC TaskStopwatch
33 static constexpr VclInputFlags eDefaultInputStop = VCL_INPUT_ANY & ~VclInputFlags::TIMER;
34 static constexpr unsigned int nDefaultTimeSlice = 50;
35 static unsigned int m_nTimeSlice;
37 sal_uInt64 m_nStartTicks;
38 sal_uInt64 m_nIterStartTicks;
39 bool m_bConsiderLastIterTime;
40 VclInputFlags m_eInputStop;
42 bool nextIter()
44 sal_uInt64 nCurTicks = tools::Time::GetSystemTicks();
45 // handle system ticks wrap as exceeded time slice
46 if (nCurTicks < m_nStartTicks)
47 return false;
49 if (m_bConsiderLastIterTime)
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;
56 // time slice exceeded
57 else if (m_nTimeSlice < nCurTicks - m_nStartTicks)
58 return false;
60 m_nIterStartTicks = nCurTicks;
62 return !Application::AnyInput(m_eInputStop);
65 public:
66 /**
67 * Per default the watch considers the last iter time when asking for an
68 * other iteration, so considers Scheduler::acceptableTaskTime as a
69 * maximum value.
71 * If you already know your iter time vary in a large range, consider
72 * setting bConciderLastIterTime to false, so Scheduler::acceptableTaskTime
73 * will be used as a minimum time slot.
74 **/
75 TaskStopwatch(bool bConciderLastIterTime = true)
76 : m_nStartTicks(tools::Time::GetSystemTicks())
77 , m_nIterStartTicks(m_nStartTicks)
78 , m_bConsiderLastIterTime(bConciderLastIterTime)
79 , m_eInputStop(eDefaultInputStop)
83 /**
84 * Returns true, if another iteration will probably pass in the time slot
85 **/
86 bool continueIter() { return nextIter(); }
88 /**
89 * Reset the stopwatch
90 **/
91 void reset()
93 m_nStartTicks = tools::Time::GetSystemTicks();
94 m_nIterStartTicks = m_nStartTicks;
97 /**
98 * Sets the input events, which should also "exceed" the stopwatch.
100 * Per default this ignores the VclInputFlags::TIMER.
102 void setInputStop(VclInputFlags eInputStop = eDefaultInputStop) { m_eInputStop = eInputStop; }
103 VclInputFlags inputStop() const { return m_eInputStop; }
106 * Sets the time considered the acceptable maximum for a task to run
108 * This is an orientation for long time background jobs to yield to
109 * the scheduler, so Idle task don't starve each other too much.
111 static unsigned int timeSlice() { return m_nTimeSlice; }
112 static void setTimeSlice(unsigned int nTimeSlice) { m_nTimeSlice = nTimeSlice; }
115 #endif // INCLUDED_VCL_TASK_STOPWATCH_HXX
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */