Blender  V3.3
BLI_timer.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. All rights reserved. */
3 
8 #include "BLI_timer.h"
9 #include "BLI_listbase.h"
10 
11 #include "MEM_guardedalloc.h"
12 #include "PIL_time.h"
13 
14 #define GET_TIME() PIL_check_seconds_timer()
15 
16 typedef struct TimedFunction {
17  struct TimedFunction *next, *prev;
20  void *user_data;
21  double next_time;
24  bool persistent;
26 
27 typedef struct TimerContainer {
30 
31 static TimerContainer GlobalTimer = {{0}};
32 
34  BLI_timer_func func,
35  void *user_data,
36  BLI_timer_data_free user_data_free,
37  double first_interval,
38  bool persistent)
39 {
40  TimedFunction *timed_func = MEM_callocN(sizeof(TimedFunction), __func__);
41  timed_func->func = func;
42  timed_func->user_data_free = user_data_free;
43  timed_func->user_data = user_data;
44  timed_func->next_time = GET_TIME() + first_interval;
45  timed_func->tag_removal = false;
46  timed_func->persistent = persistent;
47  timed_func->uuid = uuid;
48 
49  BLI_addtail(&GlobalTimer.funcs, timed_func);
50 }
51 
52 static void clear_user_data(TimedFunction *timed_func)
53 {
54  if (timed_func->user_data_free) {
55  timed_func->user_data_free(timed_func->uuid, timed_func->user_data);
56  timed_func->user_data_free = NULL;
57  }
58 }
59 
61 {
63  if (timed_func->uuid == uuid && !timed_func->tag_removal) {
64  timed_func->tag_removal = true;
65  clear_user_data(timed_func);
66  return true;
67  }
68  }
69  return false;
70 }
71 
73 {
75  if (timed_func->uuid == uuid && !timed_func->tag_removal) {
76  return true;
77  }
78  }
79  return false;
80 }
81 
83 {
84  double current_time = GET_TIME();
85 
87  if (timed_func->tag_removal) {
88  continue;
89  }
90  if (timed_func->next_time > current_time) {
91  continue;
92  }
93 
94  double ret = timed_func->func(timed_func->uuid, timed_func->user_data);
95 
96  if (ret < 0) {
97  timed_func->tag_removal = true;
98  }
99  else {
100  timed_func->next_time = current_time + ret;
101  }
102  }
103 }
104 
105 static void remove_tagged_functions(void)
106 {
107  for (TimedFunction *timed_func = GlobalTimer.funcs.first; timed_func;) {
108  TimedFunction *next = timed_func->next;
109  if (timed_func->tag_removal) {
110  clear_user_data(timed_func);
111  BLI_freelinkN(&GlobalTimer.funcs, timed_func);
112  }
113  timed_func = next;
114  }
115 }
116 
118 {
121 }
122 
124 {
125  LISTBASE_FOREACH (TimedFunction *, timed_func, &GlobalTimer.funcs) {
126  timed_func->tag_removal = true;
127  }
128 
130 }
131 
133 {
134  LISTBASE_FOREACH (TimedFunction *, timed_func, &GlobalTimer.funcs) {
135  if (!timed_func->persistent) {
136  timed_func->tag_removal = true;
137  }
138  }
139 }
140 
142 {
144 }
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:239
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
#define GET_TIME()
Definition: BLI_timer.c:14
static TimerContainer GlobalTimer
Definition: BLI_timer.c:31
static void clear_user_data(TimedFunction *timed_func)
Definition: BLI_timer.c:52
bool BLI_timer_is_registered(uintptr_t uuid)
Definition: BLI_timer.c:72
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.c:33
static void remove_tagged_functions(void)
Definition: BLI_timer.c:105
static void execute_functions_if_necessary(void)
Definition: BLI_timer.c:82
struct TimerContainer TimerContainer
struct TimedFunction TimedFunction
bool BLI_timer_unregister(uintptr_t uuid)
Definition: BLI_timer.c:60
void BLI_timer_execute()
Definition: BLI_timer.c:117
static void remove_non_persistent_functions(void)
Definition: BLI_timer.c:132
void BLI_timer_free()
Definition: BLI_timer.c:123
void BLI_timer_on_file_load(void)
Definition: BLI_timer.c:141
void(* BLI_timer_data_free)(uintptr_t uuid, void *user_data)
Definition: BLI_timer.h:22
double(* BLI_timer_func)(uintptr_t uuid, void *user_data)
Definition: BLI_timer.h:21
Read Guarded memory(de)allocation.
Platform independent time functions.
void * user_data
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static ulong * next
return ret
_W64 unsigned int uintptr_t
Definition: stdint.h:119
void * first
Definition: DNA_listBase.h:31
BLI_timer_data_free user_data_free
Definition: BLI_timer.c:19
bool tag_removal
Definition: BLI_timer.c:23
double next_time
Definition: BLI_timer.c:21
void * user_data
Definition: BLI_timer.c:20
uintptr_t uuid
Definition: BLI_timer.c:22
bool persistent
Definition: BLI_timer.c:24
struct TimedFunction * prev
Definition: BLI_timer.c:17
struct TimedFunction * next
Definition: BLI_timer.c:17
BLI_timer_func func
Definition: BLI_timer.c:18
ListBase funcs
Definition: BLI_timer.c:28