Skip to content

Commit

Permalink
Make SDL_RegisterEvents() thread-safe
Browse files Browse the repository at this point in the history
Fixes #12457

(cherry picked from commit 82552e5)
  • Loading branch information
slouken committed Mar 4, 2025
1 parent ac09af2 commit de11dd3
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/events/SDL_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ typedef struct
} SDL_DisabledEventBlock;

static SDL_DisabledEventBlock *SDL_disabled_events[256];
static Uint32 SDL_userevents = SDL_EVENT_USER;
static SDL_AtomicInt SDL_userevents;

typedef struct SDL_TemporaryMemory
{
Expand Down Expand Up @@ -1893,9 +1893,11 @@ Uint32 SDL_RegisterEvents(int numevents)
{
Uint32 event_base = 0;

if ((numevents > 0) && (SDL_userevents + numevents <= SDL_EVENT_LAST)) {
event_base = SDL_userevents;
SDL_userevents += numevents;
if (numevents > 0) {
int value = SDL_AddAtomicInt(&SDL_userevents, numevents);
if (value >= 0 && value <= (SDL_EVENT_LAST - SDL_EVENT_USER)) {
event_base = (Uint32)(SDL_EVENT_USER + value);
}
}
return event_base;
}
Expand Down

0 comments on commit de11dd3

Please sign in to comment.