Blender V5.0
GHOST_TimerManager.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
12
13#include "GHOST_TimerManager.hh"
14
15#include <algorithm>
16
17#include "GHOST_TimerTask.hh"
18
20
25
27{
28 return uint32_t(timers_.size());
29}
30
32{
33 TTimerVector::const_iterator iter = std::find(timers_.begin(), timers_.end(), timer);
34 return iter != timers_.end();
35}
36
38{
39 GHOST_TSuccess success;
40 if (!getTimerFound(timer)) {
41 /* Add the timer task. */
42 timers_.push_back(timer);
43 success = GHOST_kSuccess;
44 }
45 else {
46 success = GHOST_kFailure;
47 }
48 return success;
49}
50
52{
53 GHOST_TSuccess success;
54 TTimerVector::iterator iter = std::find(timers_.begin(), timers_.end(), timer);
55 if (iter != timers_.end()) {
56 /* Remove the timer task. */
57 timers_.erase(iter);
58 delete timer;
59 success = GHOST_kSuccess;
60 }
61 else {
62 success = GHOST_kFailure;
63 }
64 return success;
65}
66
68{
70 TTimerVector::iterator iter;
71
72 for (iter = timers_.begin(); iter != timers_.end(); ++iter) {
73 const uint64_t next = (*iter)->getNext();
74 smallest = std::min(next, smallest);
75 }
76
77 return smallest;
78}
79
81{
82 TTimerVector::iterator iter;
83 bool anyProcessed = false;
84
85 for (iter = timers_.begin(); iter != timers_.end(); ++iter) {
86 if (fireTimer(time, *iter)) {
87 anyProcessed = true;
88 }
89 }
90
91 return anyProcessed;
92}
93
95{
96 uint64_t next = task->getNext();
97
98 /* Check if the timer should be fired. */
99 if (time > next) {
100 /* Fire the timer. */
101 GHOST_TimerProcPtr timer_proc = task->getTimerProc();
102 uint64_t start = task->getStart();
103 timer_proc(task, time - start);
104
105 /* Update the time at which we will fire it again. */
106 uint64_t interval = task->getInterval();
107 uint64_t numCalls = (next - start) / interval;
108 numCalls++;
109 next = start + numCalls * interval;
110 task->setNext(next);
111
112 return true;
113 }
114 return false;
115}
116
118{
119 while (timers_.empty() == false) {
120 delete timers_[0];
121 timers_.erase(timers_.begin());
122 }
123}
void(* GHOST_TimerProcPtr)(struct GHOST_TimerTaskHandle__ *task, uint64_t time)
GHOST_TSuccess
Definition GHOST_Types.h:57
@ GHOST_kFailure
Definition GHOST_Types.h:57
@ GHOST_kSuccess
Definition GHOST_Types.h:57
@ GHOST_kFireTimeNever
unsigned long long int uint64_t
bool fireTimers(uint64_t time)
GHOST_TSuccess removeTimer(GHOST_TimerTask *timer)
bool fireTimer(uint64_t time, GHOST_TimerTask *task)
bool getTimerFound(GHOST_TimerTask *timer)
GHOST_TSuccess addTimer(GHOST_TimerTask *timer)
uint64_t getNext() const
uint64_t getInterval() const
GHOST_TimerProcPtr getTimerProc() const override
uint64_t getStart() const
void setNext(uint64_t next)
static ulong * next
wmTimer * timer