mirror of
https://github.com/Relintai/sdl2_frt.git
synced 2024-12-16 11:06:49 +01:00
The event filter and event watch functions are now thread-safe
This commit is contained in:
parent
37d89aa10f
commit
b647bd0692
@ -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,9 +398,16 @@ 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) {
|
||||||
|
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 */
|
#endif /* !SDL_THREADS_DISABLED */
|
||||||
|
|
||||||
@ -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,7 +626,9 @@ SDL_FlushEvents(Uint32 minType, Uint32 maxType)
|
|||||||
SDL_CutEvent(entry);
|
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
|
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) {
|
||||||
return 0;
|
SDL_EventWatcher event_ok;
|
||||||
}
|
SDL_EventWatcher *event_watchers = NULL;
|
||||||
|
int i, event_watchers_count = 0;
|
||||||
|
|
||||||
for (curr = SDL_event_watchers; curr; curr = curr->next) {
|
if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
|
||||||
curr->callback(curr->userdata, event);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
|
||||||
@ -712,69 +749,83 @@ SDL_PushEvent(SDL_Event * event)
|
|||||||
void
|
void
|
||||||
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
|
SDL_SetEventFilter(SDL_EventFilter filter, void *userdata)
|
||||||
{
|
{
|
||||||
/* Set filter and discard pending events */
|
if (!SDL_event_watchers_lock || SDL_LockMutex(SDL_event_watchers_lock) == 0) {
|
||||||
SDL_EventOK = NULL;
|
/* Set filter and discard pending events */
|
||||||
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
|
SDL_EventOK.callback = filter;
|
||||||
SDL_EventOKParam = userdata;
|
SDL_EventOK.userdata = userdata;
|
||||||
SDL_EventOK = filter;
|
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
|
||||||
|
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
|
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) {
|
if (filter) {
|
||||||
*filter = SDL_EventOK;
|
*filter = event_ok.callback;
|
||||||
}
|
}
|
||||||
if (userdata) {
|
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
|
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->callback = filter;
|
watcher = &SDL_event_watchers[SDL_event_watchers_count];
|
||||||
watcher->userdata = userdata;
|
watcher->callback = filter;
|
||||||
watcher->next = NULL;
|
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) {
|
if (SDL_event_watchers_lock) {
|
||||||
continue;
|
SDL_UnlockMutex(SDL_event_watchers_lock);
|
||||||
}
|
}
|
||||||
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;
|
}
|
||||||
|
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
|
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,7 +841,9 @@ SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
|
|||||||
SDL_CutEvent(entry);
|
SDL_CutEvent(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SDL_UnlockMutex(SDL_EventQ.lock);
|
if (SDL_EventQ.lock) {
|
||||||
|
SDL_UnlockMutex(SDL_EventQ.lock);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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: */
|
||||||
|
@ -601,10 +601,7 @@ 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_PushEvent(&event);
|
||||||
(*SDL_EventOK) (SDL_EventOKParam, &event)) {
|
|
||||||
SDL_PushEvent(&event);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif /* !SDL_EVENTS_DISABLED */
|
#endif /* !SDL_EVENTS_DISABLED */
|
||||||
}
|
}
|
||||||
@ -647,10 +644,7 @@ 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_PushEvent(&event);
|
||||||
(*SDL_EventOK) (SDL_EventOKParam, &event)) {
|
|
||||||
SDL_PushEvent(&event);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateEventsForDeviceRemoval();
|
UpdateEventsForDeviceRemoval();
|
||||||
|
Loading…
Reference in New Issue
Block a user