From b63a4da42de8ebbdc4557ffec89db140931d0b8e Mon Sep 17 00:00:00 2001 From: Joep Vanlier Date: Sun, 21 Jul 2024 19:02:53 +0200 Subject: [PATCH] ysfx: add a mutex to image loading --- sources/ysfx.hpp | 1 + sources/ysfx_api_eel.cpp | 10 ++++++++++ sources/ysfx_api_eel.hpp | 10 ++++++++++ sources/ysfx_api_gfx_lice.hpp | 3 +++ 4 files changed, 24 insertions(+) diff --git a/sources/ysfx.hpp b/sources/ysfx.hpp index afda6a4..ce6ab26 100644 --- a/sources/ysfx.hpp +++ b/sources/ysfx.hpp @@ -57,6 +57,7 @@ struct ysfx_s { eel_string_context_state_u string_ctx; ysfx::mutex string_mutex; ysfx::mutex atomic_mutex; + ysfx::mutex image_mutex; NSEEL_VMCTX_u vm; // some default values, these are not standard, just arbitrary diff --git a/sources/ysfx_api_eel.cpp b/sources/ysfx_api_eel.cpp index 40dadf8..592f1f4 100644 --- a/sources/ysfx_api_eel.cpp +++ b/sources/ysfx_api_eel.cpp @@ -129,6 +129,16 @@ void ysfx_string_unlock(ysfx_t *fx) fx->string_mutex.unlock(); } +void ysfx_image_lock(ysfx_t *fx) +{ + fx->image_mutex.lock(); +} + +void ysfx_image_unlock(ysfx_t *fx) +{ + fx->image_mutex.unlock(); +} + const char *ysfx_string_access_unlocked(ysfx_t *fx, ysfx_real id, WDL_FastString **fs, bool for_write) { return fx->string_ctx->GetStringForIndex(id, fs, for_write); diff --git a/sources/ysfx_api_eel.hpp b/sources/ysfx_api_eel.hpp index 1fa5df0..6b982f9 100644 --- a/sources/ysfx_api_eel.hpp +++ b/sources/ysfx_api_eel.hpp @@ -45,6 +45,8 @@ bool ysfx_string_get(ysfx_t *fx, ysfx_real id, std::string &txt); bool ysfx_string_set(ysfx_t *fx, ysfx_real id, const std::string &txt); void ysfx_string_lock(ysfx_t *fx); void ysfx_string_unlock(ysfx_t *fx); +void ysfx_image_lock(ysfx_t *fx); +void ysfx_image_unlock(ysfx_t *fx); const char *ysfx_string_access_unlocked(ysfx_t *fx, ysfx_real id, WDL_FastString **fs, bool for_write); struct ysfx_string_scoped_lock { @@ -54,7 +56,15 @@ struct ysfx_string_scoped_lock { ysfx_t *m_fx = nullptr; }; +struct ysfx_image_scoped_lock { + ysfx_image_scoped_lock(ysfx_t *fx) : m_fx(fx) { ysfx_image_lock(fx); } + ~ysfx_image_scoped_lock() { ysfx_image_unlock(m_fx); } +private: + ysfx_t *m_fx = nullptr; +}; + #define EEL_STRING_GET_CONTEXT_POINTER(opaque) (((ysfx_t *)(opaque))->string_ctx.get()) #define EEL_STRING_GET_FOR_INDEX(x, wr) (ysfx_string_access_unlocked((ysfx_t *)(opaque), x, wr, false)) #define EEL_STRING_GET_FOR_WRITE(x, wr) (ysfx_string_access_unlocked((ysfx_t *)(opaque), x, wr, true)) #define EEL_STRING_MUTEXLOCK_SCOPE ysfx_string_scoped_lock lock{(ysfx_t *)(opaque)}; +#define EEL_IMG_MUTEXLOCK_SCOPE ysfx_image_scoped_lock lock{(ysfx_t *)(opaque)}; diff --git a/sources/ysfx_api_gfx_lice.hpp b/sources/ysfx_api_gfx_lice.hpp index a98994b..9bcbdc8 100644 --- a/sources/ysfx_api_gfx_lice.hpp +++ b/sources/ysfx_api_gfx_lice.hpp @@ -577,6 +577,7 @@ static EEL_F * NSEEL_CGEN_CALL ysfx_api_gfx_blurto(void *opaque, EEL_F *x, EEL_F static EEL_F * NSEEL_CGEN_CALL ysfx_api_gfx_getimgdim(void *opaque, EEL_F *img, EEL_F *w, EEL_F *h) { + EEL_IMG_MUTEXLOCK_SCOPE eel_lice_state *ctx=EEL_LICE_GET_CONTEXT(opaque); if (ctx) ctx->gfx_getimgdim(*img,w,h); return img; @@ -584,6 +585,7 @@ static EEL_F * NSEEL_CGEN_CALL ysfx_api_gfx_getimgdim(void *opaque, EEL_F *img, static EEL_F NSEEL_CGEN_CALL ysfx_api_gfx_loadimg(void *opaque, EEL_F *img, EEL_F *fr) { + EEL_IMG_MUTEXLOCK_SCOPE eel_lice_state *ctx=EEL_LICE_GET_CONTEXT(opaque); if (ctx) return ctx->gfx_loadimg(opaque,(int)*img,*fr); return 0.0; @@ -591,6 +593,7 @@ static EEL_F NSEEL_CGEN_CALL ysfx_api_gfx_loadimg(void *opaque, EEL_F *img, EEL_ static EEL_F NSEEL_CGEN_CALL ysfx_api_gfx_setimgdim(void *opaque, EEL_F *img, EEL_F *w, EEL_F *h) { + EEL_IMG_MUTEXLOCK_SCOPE eel_lice_state *ctx=EEL_LICE_GET_CONTEXT(opaque); if (ctx) return ctx->gfx_setimgdim((int)*img,w,h); return 0.0;