Skip to content

Commit

Permalink
ysfx: add a mutex to image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Jul 21, 2024
1 parent a7f9e2a commit b63a4da
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions sources/ysfx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions sources/ysfx_api_eel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions sources/ysfx_api_eel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)};
3 changes: 3 additions & 0 deletions sources/ysfx_api_gfx_lice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,20 +577,23 @@ 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;
}

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;
}

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;
Expand Down

0 comments on commit b63a4da

Please sign in to comment.