-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
timer.cpp
226 lines (178 loc) · 5.16 KB
/
timer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright (C) 2009 - 2018 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "gui/core/timer.hpp"
#include "events.hpp"
#include "gui/core/log.hpp"
#include <SDL2/SDL_timer.h>
#include <map>
#include <mutex>
namespace gui2
{
struct timer
{
timer() : sdl_id(0), interval(0), callback()
{
}
SDL_TimerID sdl_id;
uint32_t interval;
std::function<void(std::size_t id)> callback;
};
/** Ids for the timers. */
static std::size_t next_timer_id = 0;
/** The active timers. */
static std::map<std::size_t, timer>& get_timers()
{
static std::map<std::size_t, timer>* ptimers = new std::map<std::size_t, timer>();
return *ptimers;
}
/**
The id of the event being executed, 0 if none.
NOTE: it is possible that multiple timers are executed at the same time
if one of the timer starts an event loop for example if its handler
shows a dialog. In that case code that relies on this breaks. This
could probably fixed my making this a list/stack of ids.
*/
static std::size_t executing_id = 0;
std::mutex timers_mutex;
/** Did somebody try to remove the timer during its execution? */
static bool executing_id_removed = false;
/**
* Helper to make removing a timer in a callback safe.
*
* Upon creation it sets the executing id and clears the remove request flag.
*
* If an remove_timer() is called for the id being executed it requests a
* remove the timer and exits remove_timer().
*
* Upon destruction it tests whether there was a request to remove the id and
* does so. It also clears the executing id. It leaves the remove request flag
* since the execution function needs to know whether or not the event was
* removed.
*/
class executor
{
public:
executor(std::size_t id)
{
executing_id = id;
executing_id_removed = false;
}
~executor()
{
const std::size_t id = executing_id;
executing_id = 0;
if(executing_id_removed) {
remove_timer(id);
}
}
};
extern "C" {
static uint32_t timer_callback(uint32_t, void* id)
{
DBG_GUI_E << "Pushing timer event in queue.\n";
// iTunes still reports a couple of crashes here. Cannot see a problem yet.
Uint32 result;
{
std::lock_guard<std::mutex> lock(timers_mutex);
auto itor = get_timers().find(reinterpret_cast<std::size_t>(id));
if(itor == get_timers().end()) {
return 0;
}
result = itor->second.interval;
}
SDL_Event event;
event.type = TIMER_EVENT;
event.user.code = 0;
event.user.data1 = id;
event.user.data2 = nullptr;
SDL_PushEvent(&event);
return result;
}
} // extern "C"
std::size_t add_timer(const uint32_t interval,
const std::function<void(std::size_t id)>& callback,
const bool repeat)
{
static_assert(sizeof(std::size_t) == sizeof(void*), "Pointer and std::size_t are not the same size");
DBG_GUI_E << "Adding timer.\n";
timer timer;
{
std::lock_guard<std::mutex> lock(timers_mutex);
do {
++next_timer_id;
} while(next_timer_id == 0 || get_timers().count(next_timer_id) > 0);
timer.sdl_id = SDL_AddTimer(
interval, timer_callback, reinterpret_cast<void*>(next_timer_id));
}
if(timer.sdl_id == 0) {
WRN_GUI_E << "Failed to create an sdl timer." << std::endl;
return 0;
}
if(repeat) {
timer.interval = interval;
}
timer.callback = callback;
{
std::lock_guard<std::mutex> lock(timers_mutex);
get_timers().emplace(next_timer_id, timer);
}
DBG_GUI_E << "Added timer " << next_timer_id << ".\n";
return next_timer_id;
}
bool remove_timer(const std::size_t id)
{
DBG_GUI_E << "Removing timer " << id << ".\n";
std::lock_guard<std::mutex> lock(timers_mutex);
auto itor = get_timers().find(id);
if(itor == get_timers().end()) {
LOG_GUI_E << "Can't remove timer since it no longer exists.\n";
return false;
}
if(id == executing_id) {
executing_id_removed = true;
return true;
}
if(!SDL_RemoveTimer(itor->second.sdl_id)) {
/*
* This can happen if the caller of the timer didn't get the event yet
* but the timer has already been fired. This due to the fact that a
* timer pushes an event in the queue, which allows the following
* condition:
* - Timer fires
* - Push event in queue
* - Another event is processed and tries to remove the event.
*/
DBG_GUI_E << "The timer is already out of the SDL timer list.\n";
}
get_timers().erase(itor);
return true;
}
bool execute_timer(const std::size_t id)
{
DBG_GUI_E << "Executing timer " << id << ".\n";
std::function<void(size_t)> callback = nullptr;
{
std::lock_guard<std::mutex> lock(timers_mutex);
auto itor = get_timers().find(id);
if(itor == get_timers().end()) {
LOG_GUI_E << "Can't execute timer since it no longer exists.\n";
return false;
}
callback = itor->second.callback;
if(itor->second.interval == 0) {
get_timers().erase(itor);
}
}
callback(id);
return true;
}
} // namespace gui2