From 26ee0590d68a6e028653e75228ee4f488c6fc0b0 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Fri, 14 Apr 2023 08:46:02 +0200 Subject: [PATCH] [client,sdl] use CriticalSectionLock use c++ RAII to lock critical sections --- client/SDL/sdl_freerdp.cpp | 22 +++++++--------------- client/SDL/sdl_utils.cpp | 6 ++---- client/SDL/sdl_utils.hpp | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/client/SDL/sdl_freerdp.cpp b/client/SDL/sdl_freerdp.cpp index b077a5923..1975d93c2 100644 --- a/client/SDL/sdl_freerdp.cpp +++ b/client/SDL/sdl_freerdp.cpp @@ -406,9 +406,9 @@ static BOOL sdl_end_paint(rdpContext* context) auto sdl = reinterpret_cast(context); WINPR_ASSERT(sdl); - EnterCriticalSection(&sdl->critical); + CriticalSectionLock lock(sdl->critical); const BOOL rc = sdl_push_user_event(SDL_USEREVENT_UPDATE, context); - LeaveCriticalSection(&sdl->critical); + return rc; } @@ -638,17 +638,10 @@ fail: static BOOL sdl_wait_create_windows(sdlContext* sdl) { - BOOL res = FALSE; - EnterCriticalSection(&sdl->critical); - res = ResetEvent(sdl->windows_created); - if (!res) - goto unlock; - res = sdl_push_user_event(SDL_USEREVENT_CREATE_WINDOWS, sdl); - if (!res) - goto unlock; -unlock: - LeaveCriticalSection(&sdl->critical); - if (!res) + CriticalSectionLock lock(sdl->critical); + if (!ResetEvent(sdl->windows_created)) + return FALSE; + if (!sdl_push_user_event(SDL_USEREVENT_CREATE_WINDOWS, sdl)) return FALSE; HANDLE handles[] = { sdl->initialized, freerdp_abort_event(&sdl->common.context) }; @@ -692,7 +685,7 @@ static int sdl_run(sdlContext* sdl) SDL_Log("got event %s [0x%08" PRIx32 "]", sdl_event_type_str(windowEvent.type), windowEvent.type); #endif - EnterCriticalSection(&sdl->critical); + CriticalSectionLock lock(sdl->critical); switch (windowEvent.type) { case SDL_QUIT: @@ -838,7 +831,6 @@ static int sdl_run(sdlContext* sdl) default: break; } - LeaveCriticalSection(&sdl->critical); } } diff --git a/client/SDL/sdl_utils.cpp b/client/SDL/sdl_utils.cpp index 3b6e3dfc1..37797e308 100644 --- a/client/SDL/sdl_utils.cpp +++ b/client/SDL/sdl_utils.cpp @@ -178,7 +178,7 @@ BOOL update_fullscreen(sdlContext* sdl, BOOL enter) { WINPR_ASSERT(sdl); - EnterCriticalSection(&sdl->critical); + CriticalSectionLock lock(sdl->critical); for (uint32_t x = 0; x < sdl->windowCount; x++) { sdl_window_t* window = &sdl->windows[x]; @@ -186,7 +186,6 @@ BOOL update_fullscreen(sdlContext* sdl, BOOL enter) return FALSE; } sdl->fullscreen = enter; - LeaveCriticalSection(&sdl->critical); return TRUE; } @@ -194,7 +193,7 @@ BOOL update_resizeable(sdlContext* sdl, BOOL enable) { WINPR_ASSERT(sdl); - EnterCriticalSection(&sdl->critical); + CriticalSectionLock lock(sdl->critical); const rdpSettings* settings = sdl->common.context.settings; const BOOL dyn = freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate); @@ -209,6 +208,5 @@ BOOL update_resizeable(sdlContext* sdl, BOOL enable) } sdl->resizeable = use; - LeaveCriticalSection(&sdl->critical); return TRUE; } diff --git a/client/SDL/sdl_utils.hpp b/client/SDL/sdl_utils.hpp index f64fce2a3..ce20f1107 100644 --- a/client/SDL/sdl_utils.hpp +++ b/client/SDL/sdl_utils.hpp @@ -26,6 +26,22 @@ #include "sdl_freerdp.hpp" +class CriticalSectionLock +{ + public: + CriticalSectionLock(CRITICAL_SECTION& section) : _section(section) + { + EnterCriticalSection(&_section); + } + ~CriticalSectionLock() + { + LeaveCriticalSection(&_section); + } + + private: + CRITICAL_SECTION _section; +}; + enum { SDL_USEREVENT_UPDATE = SDL_USEREVENT + 1,