Skip to content

Commit

Permalink
cocoa: Put a mutex around GL_SwapBuffers.
Browse files Browse the repository at this point in the history
Prevents deadlock when swapping two different GL contexts on two different
threads at the same time on macOS 10.14 ("Mojave").

Fixes Bugzilla #4278.
  • Loading branch information
icculus committed Oct 19, 2018
1 parent 84e7832 commit 1fb20f0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/video/cocoa/SDL_cocoaopengl.m
Expand Up @@ -410,8 +410,14 @@ - (void)setWindow:(SDL_Window *)newWindow
{ @autoreleasepool
{
SDLOpenGLContext* nscontext = (SDLOpenGLContext*)SDL_GL_GetCurrentContext();
SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;

/* on 10.14 ("Mojave") and later, this deadlocks if two contexts in two
threads try to swap at the same time, so put a mutex around it. */
SDL_LockMutex(videodata->swaplock);
[nscontext flushBuffer];
[nscontext updateIfNeeded];
SDL_UnlockMutex(videodata->swaplock);
return 0;
}}

Expand Down
2 changes: 1 addition & 1 deletion src/video/cocoa/SDL_cocoavideo.h
Expand Up @@ -107,7 +107,7 @@ typedef struct SDL_VideoData
Uint32 screensaver_activity;
BOOL screensaver_use_iopm;
IOPMAssertionID screensaver_assertion;

SDL_mutex *swaplock;
} SDL_VideoData;

/* Utility functions */
Expand Down
8 changes: 8 additions & 0 deletions src/video/cocoa/SDL_cocoavideo.m
Expand Up @@ -175,15 +175,23 @@
/* The IOPM assertion API can disable the screensaver as of 10.7. */
data->screensaver_use_iopm = floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6;

data->swaplock = SDL_CreateMutex();
if (!data->swaplock) {
return -1;
}

return 0;
}

void
Cocoa_VideoQuit(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Cocoa_QuitModes(_this);
Cocoa_QuitKeyboard(_this);
Cocoa_QuitMouse(_this);
SDL_DestroyMutex(data->swaplock);
data->swaplock = NULL;
}

/* This function assumes that it's called from within an autorelease pool */
Expand Down

0 comments on commit 1fb20f0

Please sign in to comment.