The event filter and event watch functions are now thread-safe
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index c87dc99..9b6fff8 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -38,17 +38,15 @@
/* An arbitrary limit so we don't have unbounded growth */
#define SDL_MAX_QUEUED_EVENTS 65535
-/* Public data -- the event filter */
-SDL_EventFilter SDL_EventOK = NULL;
-void *SDL_EventOKParam;
-
typedef struct SDL_EventWatcher {
SDL_EventFilter callback;
void *userdata;
- struct SDL_EventWatcher *next;
} SDL_EventWatcher;
+static SDL_mutex *SDL_event_watchers_lock;
+static SDL_EventWatcher SDL_EventOK;
static SDL_EventWatcher *SDL_event_watchers = NULL;
+static int SDL_event_watchers_count = 0;
typedef struct {
Uint32 bits[8];
@@ -367,12 +365,17 @@ SDL_StopEventLoop(void)
SDL_disabled_events[i] = NULL;
}
- while (SDL_event_watchers) {
- SDL_EventWatcher *tmp = SDL_event_watchers;
- SDL_event_watchers = tmp->next;
- SDL_free(tmp);
+ if (SDL_event_watchers_lock) {
+ SDL_UnlockMutex(SDL_event_watchers_lock);
+ SDL_DestroyMutex(SDL_event_watchers_lock);
+ SDL_event_watchers_lock = NULL;
+ }
+ if (SDL_event_watchers_count > 0) {
+ SDL_free(SDL_event_watchers);
+ SDL_event_watchers = NULL;
+ SDL_event_watchers_count = 0;
}
- SDL_EventOK = NULL;
+ SDL_zero(SDL_EventOK);
if (SDL_EventQ.lock) {
SDL_UnlockMutex(SDL_EventQ.lock);
@@ -395,9 +398,16 @@ SDL_StartEventLoop(void)
#if !SDL_THREADS_DISABLED
if (!SDL_EventQ.lock) {
SDL_EventQ.lock = SDL_CreateMutex();
+ if (SDL_EventQ.lock == NULL) {
+ return -1;
+ }
}
- if (SDL_EventQ.lock == NULL) {
- return -1;
+
+ if (!SDL_event_watchers_lock) {
+ SDL_event_watchers_lock = SDL_CreateMutex();
+ if (SDL_event_watchers_lock == NULL) {
+ return -1;
+ }
}
#endif /* !SDL_THREADS_DISABLED */
@@ -606,7 +616,7 @@ SDL_FlushEvents(Uint32 minType, Uint32 maxType)
#endif
/* Lock the event queue */
- if (SDL_EventQ.lock && SDL_LockMutex(SDL_EventQ.lock) == 0) {
+ if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
SDL_EventEntry *entry, *next;
Uint32 type;
for (entry = SDL_EventQ.head; entry; entry = next) {
@@ -616,7 +626,9 @@ SDL_FlushEvents(Uint32 minType, Uint32 maxType)
SDL_CutEvent(entry);
}
}
- SDL_UnlockMutex(SDL_EventQ.lock);
+ if (SDL_EventQ.lock) {
+ SDL_UnlockMutex(SDL_EventQ.lock);
+ }
}
}
@@ -688,16 +700,41 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
int
SDL_PushEvent(SDL_Event * event)
{
- SDL_EventWatcher *curr;
-
event->common.timestamp = SDL_GetTicks();
- if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
- return 0;
- }
+ if (SDL_EventOK.callback || SDL_event_watchers_count != 0) {
+ SDL_EventWatcher event_ok;
+ SDL_EventWatcher *event_watchers = NULL;
+ int i, event_watchers_count = 0;
+
+ if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ event_ok = SDL_EventOK;
+
+ if (SDL_event_watchers_count > 0) {
+ event_watchers = SDL_stack_alloc(SDL_EventWatcher, SDL_event_watchers_count);
+ if (event_watchers) {
+ SDL_memcpy(event_watchers, SDL_event_watchers, SDL_event_watchers_count * sizeof(*event_watchers));
+ event_watchers_count = SDL_event_watchers_count;
+ }
+ }
+
+ if (SDL_event_watchers_lock) {
+ SDL_UnlockMutex(SDL_event_watchers_lock);
+ }
+ } else {
+ event_ok = SDL_EventOK;
+ }
+
+ if (event_ok.callback && !event_ok.callback(event_ok.userdata, event)) {
+ return 0;
+ }
- for (curr = SDL_event_watchers; curr; curr = curr->next) {
- curr->callback(curr->userdata, event);
+ if (event_watchers_count > 0) {
+ for (i = 0; i < event_watchers_count; ++i) {
+ event_watchers[i].callback(event_watchers[i].userdata, event);
+ }
+ SDL_stack_free(event_watchers);
+ }
}
if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
@@ -712,69 +749,83 @@ SDL_PushEvent(SDL_Event * event)
void
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
{
- /* Set filter and discard pending events */
- SDL_EventOK = NULL;
- SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
- SDL_EventOKParam = userdata;
- SDL_EventOK = filter;
+ if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ /* Set filter and discard pending events */
+ SDL_EventOK.callback = filter;
+ SDL_EventOK.userdata = userdata;
+ SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
+
+ if (SDL_event_watchers_lock) {
+ SDL_UnlockMutex(SDL_event_watchers_lock);
+ }
+ }
}
SDL_bool
SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
{
+ SDL_EventWatcher event_ok;
+
+ if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ event_ok = SDL_EventOK;
+
+ if (SDL_event_watchers_lock) {
+ SDL_UnlockMutex(SDL_event_watchers_lock);
+ }
+ } else {
+ SDL_zero(event_ok);
+ }
+
if (filter) {
- *filter = SDL_EventOK;
+ *filter = event_ok.callback;
}
if (userdata) {
- *userdata = SDL_EventOKParam;
+ *userdata = event_ok.userdata;
}
- return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
+ return event_ok.callback ? SDL_TRUE : SDL_FALSE;
}
-/* FIXME: This is not thread-safe yet */
void
SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
{
- SDL_EventWatcher *watcher, *tail;
-
- watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher));
- if (!watcher) {
- /* Uh oh... */
- return;
- }
-
- /* create the watcher */
- watcher->callback = filter;
- watcher->userdata = userdata;
- watcher->next = NULL;
+ if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ SDL_EventWatcher *event_watchers;
+
+ event_watchers = SDL_realloc(SDL_event_watchers, (SDL_event_watchers_count + 1) * sizeof(event_watchers));
+ if (event_watchers) {
+ SDL_EventWatcher *watcher;
+
+ SDL_event_watchers = event_watchers;
+ watcher = &SDL_event_watchers[SDL_event_watchers_count];
+ watcher->callback = filter;
+ watcher->userdata = userdata;
+ ++SDL_event_watchers_count;
+ }
- /* add the watcher to the end of the list */
- if (SDL_event_watchers) {
- for (tail = SDL_event_watchers; tail->next; tail = tail->next) {
- continue;
+ if (SDL_event_watchers_lock) {
+ SDL_UnlockMutex(SDL_event_watchers_lock);
}
- tail->next = watcher;
- } else {
- SDL_event_watchers = watcher;
}
}
-/* FIXME: This is not thread-safe yet */
void
SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
{
- SDL_EventWatcher *prev = NULL;
- SDL_EventWatcher *curr;
-
- for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) {
- if (curr->callback == filter && curr->userdata == userdata) {
- if (prev) {
- prev->next = curr->next;
- } else {
- SDL_event_watchers = curr->next;
+ if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
+ int i;
+
+ for (i = 0; i < SDL_event_watchers_count; ++i) {
+ if (SDL_event_watchers[i].callback == filter && SDL_event_watchers[i].userdata == userdata) {
+ --SDL_event_watchers_count;
+ if (i < SDL_event_watchers_count) {
+ SDL_memcpy(&SDL_event_watchers[i], &SDL_event_watchers[i+1], (SDL_event_watchers_count - i) * sizeof(SDL_event_watchers[i]));
+ }
+ break;
}
- SDL_free(curr);
- break;
+ }
+
+ if (SDL_event_watchers_lock) {
+ SDL_UnlockMutex(SDL_event_watchers_lock);
}
}
}
@@ -782,7 +833,7 @@ SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
void
SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
{
- if (SDL_EventQ.lock && SDL_LockMutex(SDL_EventQ.lock) == 0) {
+ if (!SDL_EventQ.lock || SDL_LockMutex(SDL_EventQ.lock) == 0) {
SDL_EventEntry *entry, *next;
for (entry = SDL_EventQ.head; entry; entry = next) {
next = entry->next;
@@ -790,7 +841,9 @@ SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
SDL_CutEvent(entry);
}
}
- SDL_UnlockMutex(SDL_EventQ.lock);
+ if (SDL_EventQ.lock) {
+ SDL_UnlockMutex(SDL_EventQ.lock);
+ }
}
}
diff --git a/src/events/SDL_events_c.h b/src/events/SDL_events_c.h
index 83b0e14..56e48dd 100644
--- a/src/events/SDL_events_c.h
+++ b/src/events/SDL_events_c.h
@@ -46,8 +46,4 @@ extern void SDL_QuitQuit(void);
extern void SDL_SendPendingQuit(void);
-/* The event filter function */
-extern SDL_EventFilter SDL_EventOK;
-extern void *SDL_EventOKParam;
-
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 4c4cae7..b49b28e 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -601,10 +601,7 @@ void SDL_PrivateJoystickAdded(int device_index)
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index;
- if ((SDL_EventOK == NULL) ||
- (*SDL_EventOK) (SDL_EventOKParam, &event)) {
- SDL_PushEvent(&event);
- }
+ SDL_PushEvent(&event);
}
#endif /* !SDL_EVENTS_DISABLED */
}
@@ -647,10 +644,7 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_instance;
- if ((SDL_EventOK == NULL) ||
- (*SDL_EventOK) (SDL_EventOKParam, &event)) {
- SDL_PushEvent(&event);
- }
+ SDL_PushEvent(&event);
}
UpdateEventsForDeviceRemoval();