Skip to content

Commit

Permalink
SDL3 mutex support
Browse files Browse the repository at this point in the history
  • Loading branch information
Starbuck5 committed Feb 10, 2024
1 parent 3017133 commit d320b6e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
28 changes: 28 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@
#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
SDL_SoftStretch(src, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST)

/* Emulating SDL2 SDL_LockMutex API. In SDL3, it returns void. */
static inline int
PG_LockMutex(SDL_mutex *mutex)
{
SDL_LockMutex(mutex);
return 0;
}

/* Emulating SDL2 SDL_UnlockMutex API. In SDL3, it returns void. */
static inline int
PG_UnlockMutex(SDL_mutex *mutex)
{
SDL_UnlockMutex(mutex);
return 0;
}

#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
Expand Down Expand Up @@ -109,6 +125,18 @@
#define PG_SoftStretchNearest(src, srcrect, dst, dstrect) \
SDL_SoftStretch(src, srcrect, dst, dstrect)

static inline int
PG_LockMutex(SDL_mutex *mutex)
{
return SDL_LockMutex(mutex);
}

static inline int
PG_UnlockMutex(SDL_mutex *mutex)
{
return SDL_UnlockMutex(mutex);
}

#if SDL_VERSION_ATLEAST(2, 0, 14)
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
#else
Expand Down
4 changes: 2 additions & 2 deletions src_c/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static char released_keys[SDL_NUM_SCANCODES] = {0};

#define PG_LOCK_EVFILTER_MUTEX \
if (pg_evfilter_mutex) { \
if (SDL_LockMutex(pg_evfilter_mutex) < 0) { \
if (PG_LockMutex(pg_evfilter_mutex) < 0) { \
/* TODO: better error handling with future error-event API */ \
/* since this error is very rare, we can completely give up if \
* this happens for now */ \
Expand All @@ -116,7 +116,7 @@ static char released_keys[SDL_NUM_SCANCODES] = {0};

#define PG_UNLOCK_EVFILTER_MUTEX \
if (pg_evfilter_mutex) { \
if (SDL_UnlockMutex(pg_evfilter_mutex) < 0) { \
if (PG_UnlockMutex(pg_evfilter_mutex) < 0) { \
/* TODO: handle errors with future error-event API */ \
/* since this error is very rare, we can completely give up if \
* this happens for now */ \
Expand Down
8 changes: 4 additions & 4 deletions src_c/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static SDL_mutex *pg_timer_mutex = NULL;
* easily */
#define PG_LOCK_TIMER_MUTEX \
if (pg_timer_mutex) { \
if (SDL_LockMutex(pg_timer_mutex) < 0) { \
if (PG_LockMutex(pg_timer_mutex) < 0) { \
/* TODO: better error handling with future error-event API */ \
/* since this error is very rare, we can completely give up if \
* this happens for now */ \
Expand All @@ -100,7 +100,7 @@ static SDL_mutex *pg_timer_mutex = NULL;

#define PG_UNLOCK_TIMER_MUTEX \
if (pg_timer_mutex) { \
if (SDL_UnlockMutex(pg_timer_mutex) < 0) { \
if (PG_UnlockMutex(pg_timer_mutex) < 0) { \
/* TODO: handle errors with future error-event API */ \
/* since this error is very rare, we can completely give up if \
* this happens for now */ \
Expand Down Expand Up @@ -441,7 +441,7 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs)
Py_BEGIN_ALLOW_THREADS;

#ifndef __EMSCRIPTEN__
if (SDL_LockMutex(pg_timer_mutex) < 0) {
if (PG_LockMutex(pg_timer_mutex) < 0) {
ecode = PG_TIMER_SDL_ERROR;
goto end_no_mutex;
}
Expand Down Expand Up @@ -475,7 +475,7 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs)

end:
#ifndef __EMSCRIPTEN__
if (SDL_UnlockMutex(pg_timer_mutex)) {
if (PG_UnlockMutex(pg_timer_mutex)) {
ecode = PG_TIMER_SDL_ERROR;
}

Expand Down

0 comments on commit d320b6e

Please sign in to comment.