The event filter and event watch functions are now thread-safe

This commit is contained in:
Sam Lantinga 2017-10-10 17:41:41 -07:00
parent 37d89aa10f
commit b647bd0692
3 changed files with 116 additions and 73 deletions

View File

@ -38,17 +38,15 @@
/* An arbitrary limit so we don't have unbounded growth */ /* An arbitrary limit so we don't have unbounded growth */
#define SDL_MAX_QUEUED_EVENTS 65535 #define SDL_MAX_QUEUED_EVENTS 65535
/* Public data -- the event filter */
SDL_EventFilter SDL_EventOK = NULL;
void *SDL_EventOKParam;
typedef struct SDL_EventWatcher { typedef struct SDL_EventWatcher {
SDL_EventFilter callback; SDL_EventFilter callback;
void *userdata; void *userdata;
struct SDL_EventWatcher *next;
} SDL_EventWatcher; } SDL_EventWatcher;
static SDL_mutex *SDL_event_watchers_lock;
static SDL_EventWatcher SDL_EventOK;
static SDL_EventWatcher *SDL_event_watchers = NULL; static SDL_EventWatcher *SDL_event_watchers = NULL;
static int SDL_event_watchers_count = 0;
typedef struct { typedef struct {
Uint32 bits[8]; Uint32 bits[8];
@ -367,12 +365,17 @@ SDL_StopEventLoop(void)
SDL_disabled_events[i] = NULL; SDL_disabled_events[i] = NULL;
} }
while (SDL_event_watchers) { if (SDL_event_watchers_lock) {
SDL_EventWatcher *tmp = SDL_event_watchers; SDL_UnlockMutex(SDL_event_watchers_lock);
SDL_event_watchers = tmp->next; SDL_DestroyMutex(SDL_event_watchers_lock);
SDL_free(tmp); SDL_event_watchers_lock = NULL;
} }
SDL_EventOK = NULL; if (SDL_event_watchers_count > 0) {
SDL_free(SDL_event_watchers);
SDL_event_watchers = NULL;
SDL_event_watchers_count = 0;
}
SDL_zero(SDL_EventOK);
if (SDL_EventQ.lock) { if (SDL_EventQ.lock) {
SDL_UnlockMutex(SDL_EventQ.lock); SDL_UnlockMutex(SDL_EventQ.lock);
@ -395,10 +398,17 @@ SDL_StartEventLoop(void)
#if !SDL_THREADS_DISABLED #if !SDL_THREADS_DISABLED
if (!SDL_EventQ.lock) { if (!SDL_EventQ.lock) {
SDL_EventQ.lock = SDL_CreateMutex(); SDL_EventQ.lock = SDL_CreateMutex();
}
if (SDL_EventQ.lock == NULL) { if (SDL_EventQ.lock == NULL) {
return -1; 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 */ #endif /* !SDL_THREADS_DISABLED */
/* Process most event types */ /* Process most event types */
@ -606,7 +616,7 @@ SDL_FlushEvents(Uint32 minType, Uint32 maxType)
#endif #endif
/* Lock the event queue */ /* 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; SDL_EventEntry *entry, *next;
Uint32 type; Uint32 type;
for (entry = SDL_EventQ.head; entry; entry = next) { for (entry = SDL_EventQ.head; entry; entry = next) {
@ -616,9 +626,11 @@ SDL_FlushEvents(Uint32 minType, Uint32 maxType)
SDL_CutEvent(entry); SDL_CutEvent(entry);
} }
} }
if (SDL_EventQ.lock) {
SDL_UnlockMutex(SDL_EventQ.lock); SDL_UnlockMutex(SDL_EventQ.lock);
} }
} }
}
/* Run the system dependent event loops */ /* Run the system dependent event loops */
void void
@ -688,16 +700,41 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
int int
SDL_PushEvent(SDL_Event * event) SDL_PushEvent(SDL_Event * event)
{ {
SDL_EventWatcher *curr;
event->common.timestamp = SDL_GetTicks(); event->common.timestamp = SDL_GetTicks();
if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) { 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; return 0;
} }
for (curr = SDL_event_watchers; curr; curr = curr->next) { if (event_watchers_count > 0) {
curr->callback(curr->userdata, event); 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) { if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
@ -712,77 +749,91 @@ SDL_PushEvent(SDL_Event * event)
void void
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata) SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
{ {
if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
/* Set filter and discard pending events */ /* Set filter and discard pending events */
SDL_EventOK = NULL; SDL_EventOK.callback = filter;
SDL_EventOK.userdata = userdata;
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
SDL_EventOKParam = userdata;
SDL_EventOK = filter; if (SDL_event_watchers_lock) {
SDL_UnlockMutex(SDL_event_watchers_lock);
}
}
} }
SDL_bool SDL_bool
SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata) SDL_GetEventFilter(SDL_EventFilter * filter, void **userdata)
{ {
if (filter) { SDL_EventWatcher event_ok;
*filter = SDL_EventOK;
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);
} }
if (userdata) { } else {
*userdata = SDL_EventOKParam; SDL_zero(event_ok);
} }
return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
if (filter) {
*filter = event_ok.callback;
}
if (userdata) {
*userdata = event_ok.userdata;
}
return event_ok.callback ? SDL_TRUE : SDL_FALSE;
} }
/* FIXME: This is not thread-safe yet */
void void
SDL_AddEventWatch(SDL_EventFilter filter, void *userdata) SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
{ {
SDL_EventWatcher *watcher, *tail; if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
SDL_EventWatcher *event_watchers;
watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher)); event_watchers = SDL_realloc(SDL_event_watchers, (SDL_event_watchers_count + 1) * sizeof(event_watchers));
if (!watcher) { if (event_watchers) {
/* Uh oh... */ SDL_EventWatcher *watcher;
return;
}
/* create the watcher */ SDL_event_watchers = event_watchers;
watcher = &SDL_event_watchers[SDL_event_watchers_count];
watcher->callback = filter; watcher->callback = filter;
watcher->userdata = userdata; watcher->userdata = userdata;
watcher->next = NULL; ++SDL_event_watchers_count;
}
/* add the watcher to the end of the list */
if (SDL_event_watchers) { if (SDL_event_watchers_lock) {
for (tail = SDL_event_watchers; tail->next; tail = tail->next) { SDL_UnlockMutex(SDL_event_watchers_lock);
continue;
} }
tail->next = watcher;
} else {
SDL_event_watchers = watcher;
} }
} }
/* FIXME: This is not thread-safe yet */
void void
SDL_DelEventWatch(SDL_EventFilter filter, void *userdata) SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
{ {
SDL_EventWatcher *prev = NULL; if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
SDL_EventWatcher *curr; int i;
for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) { for (i = 0; i < SDL_event_watchers_count; ++i) {
if (curr->callback == filter && curr->userdata == userdata) { if (SDL_event_watchers[i].callback == filter && SDL_event_watchers[i].userdata == userdata) {
if (prev) { --SDL_event_watchers_count;
prev->next = curr->next; if (i < SDL_event_watchers_count) {
} else { SDL_memcpy(&SDL_event_watchers[i], &SDL_event_watchers[i+1], (SDL_event_watchers_count - i) * sizeof(SDL_event_watchers[i]));
SDL_event_watchers = curr->next;
} }
SDL_free(curr);
break; break;
} }
} }
if (SDL_event_watchers_lock) {
SDL_UnlockMutex(SDL_event_watchers_lock);
}
}
} }
void void
SDL_FilterEvents(SDL_EventFilter filter, void *userdata) 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; SDL_EventEntry *entry, *next;
for (entry = SDL_EventQ.head; entry; entry = next) { for (entry = SDL_EventQ.head; entry; entry = next) {
next = entry->next; next = entry->next;
@ -790,9 +841,11 @@ SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
SDL_CutEvent(entry); SDL_CutEvent(entry);
} }
} }
if (SDL_EventQ.lock) {
SDL_UnlockMutex(SDL_EventQ.lock); SDL_UnlockMutex(SDL_EventQ.lock);
} }
} }
}
Uint8 Uint8
SDL_EventState(Uint32 type, int state) SDL_EventState(Uint32 type, int state)

View File

@ -46,8 +46,4 @@ extern void SDL_QuitQuit(void);
extern void SDL_SendPendingQuit(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: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -601,11 +601,8 @@ void SDL_PrivateJoystickAdded(int device_index)
if (SDL_GetEventState(event.type) == SDL_ENABLE) { if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_index; event.jdevice.which = device_index;
if ((SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
}
#endif /* !SDL_EVENTS_DISABLED */ #endif /* !SDL_EVENTS_DISABLED */
} }
@ -647,11 +644,8 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
if (SDL_GetEventState(event.type) == SDL_ENABLE) { if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = device_instance; event.jdevice.which = device_instance;
if ((SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event)) {
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }
}
UpdateEventsForDeviceRemoval(); UpdateEventsForDeviceRemoval();
#endif /* !SDL_EVENTS_DISABLED */ #endif /* !SDL_EVENTS_DISABLED */