Blender V5.3
BLI_timer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_timer.hh"
10#include "BLI_listbase.hh"
11#include "BLI_time.hh"
12
13#include "MEM_guardedalloc.h"
14
15namespace blender {
16
17#define GET_TIME() BLI_time_now_seconds()
18
29
31 ListBaseT<TimedFunction> funcs;
32};
33
34static TimerContainer GlobalTimer = {{nullptr}};
35
37 BLI_timer_func func,
38 void *user_data,
39 BLI_timer_data_free user_data_free,
40 double first_interval,
41 bool persistent)
42{
43 TimedFunction *timed_func = MEM_new_zeroed<TimedFunction>(__func__);
44 timed_func->func = func;
45 timed_func->user_data_free = user_data_free;
46 timed_func->user_data = user_data;
47 timed_func->next_time = GET_TIME() + first_interval;
48 timed_func->tag_removal = false;
49 timed_func->persistent = persistent;
50 timed_func->uuid = uuid;
51
52 BLI_addtail(&GlobalTimer.funcs, timed_func);
53}
54
55static void clear_user_data(TimedFunction *timed_func)
56{
57 if (timed_func->user_data_free) {
58 timed_func->user_data_free(timed_func->uuid, timed_func->user_data);
59 timed_func->user_data_free = nullptr;
60 }
61}
62
64{
65 for (TimedFunction &timed_func : GlobalTimer.funcs) {
66 if (timed_func.uuid == uuid && !timed_func.tag_removal) {
67 timed_func.tag_removal = true;
68 clear_user_data(&timed_func);
69 return true;
70 }
71 }
72 return false;
73}
74
76{
77 for (TimedFunction &timed_func : GlobalTimer.funcs) {
78 if (timed_func.uuid == uuid && !timed_func.tag_removal) {
79 return true;
80 }
81 }
82 return false;
83}
84
86{
87 double current_time = GET_TIME();
88
89 for (TimedFunction &timed_func : GlobalTimer.funcs) {
90 if (timed_func.tag_removal) {
91 continue;
92 }
93 if (timed_func.next_time > current_time) {
94 continue;
95 }
96
97 double ret = timed_func.func(timed_func.uuid, timed_func.user_data);
98
99 if (ret < 0) {
100 timed_func.tag_removal = true;
101 }
102 else {
103 timed_func.next_time = current_time + ret;
104 }
105 }
106}
107
109{
110 for (TimedFunction *timed_func = GlobalTimer.funcs.first(); timed_func;) {
111 TimedFunction *next = timed_func->next;
112 if (timed_func->tag_removal) {
113 clear_user_data(timed_func);
114 BLI_freelinkN(&GlobalTimer.funcs, timed_func);
115 }
116 timed_func = next;
117 }
118}
119
125
127{
128 for (TimedFunction &timed_func : GlobalTimer.funcs) {
129 timed_func.tag_removal = true;
130 }
131
133}
134
136{
137 for (TimedFunction &timed_func : GlobalTimer.funcs) {
138 if (!timed_func.persistent) {
139 timed_func.tag_removal = true;
140 }
141 }
142}
143
148
149} // namespace blender
Platform independent time functions.
#define GET_TIME()
Definition BLI_timer.cc:17
Read Blender memory allocation and freeing.
BMesh Iterators.
double(*) BLI_timer_func(uintptr_t uuid, void *user_data)
Definition BLI_timer.hh:20
static void execute_functions_if_necessary()
Definition BLI_timer.cc:85
void BLI_timer_free()
Definition BLI_timer.cc:126
static void remove_tagged_functions()
Definition BLI_timer.cc:108
bool BLI_timer_is_registered(uintptr_t uuid)
Definition BLI_timer.cc:75
static void clear_user_data(TimedFunction *timed_func)
Definition BLI_timer.cc:55
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
static TimerContainer GlobalTimer
Definition BLI_timer.cc:34
static void remove_non_persistent_functions()
Definition BLI_timer.cc:135
void(*) BLI_timer_data_free(uintptr_t uuid, void *user_data)
Definition BLI_timer.hh:21
void BLI_timer_on_file_load()
Definition BLI_timer.cc:144
void BLI_timer_register(uintptr_t uuid, BLI_timer_func func, void *user_data, BLI_timer_data_free user_data_free, double first_interval, bool persistent)
Definition BLI_timer.cc:36
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
bool BLI_timer_unregister(uintptr_t uuid)
Definition BLI_timer.cc:63
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BLI_timer_execute()
Definition BLI_timer.cc:120
TimedFunction * next
Definition BLI_timer.cc:20
BLI_timer_data_free user_data_free
Definition BLI_timer.cc:22
BLI_timer_func func
Definition BLI_timer.cc:21
TimedFunction * prev
Definition BLI_timer.cc:20
ListBaseT< TimedFunction > funcs
Definition BLI_timer.cc:31