From c62f79fc3266ba7c21ed4ada84d16aab771f0d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 8 Apr 2014 11:24:52 +0200 Subject: Make sdl_surface:img_load/1 run in the main thread While this and other surface functions don't need to, depending on the backend or flags the surface may not be in system memory and therefore may not be thread safe. --- c_src/sdl_surface.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'c_src') diff --git a/c_src/sdl_surface.c b/c_src/sdl_surface.c index 6f43f3e..97c755a 100644 --- a/c_src/sdl_surface.c +++ b/c_src/sdl_surface.c @@ -20,15 +20,17 @@ void dtor_Surface(ErlNifEnv* env, void* obj) SDL_FreeSurface(NIF_RES_GET(Surface, obj)); } -NIF_FUNCTION(img_load) +// img_load + +NIF_CALL_HANDLER(thread_img_load) { - char filename[FILENAME_MAX]; SDL_Surface* surface; ERL_NIF_TERM term; - BADARG_IF(!enif_get_string(env, argv[0], filename, FILENAME_MAX, ERL_NIF_LATIN1)); + surface = IMG_Load(args[0]); + + enif_free(args[0]); - surface = IMG_Load(filename); if (!surface) return sdl_error_tuple(env); @@ -39,3 +41,19 @@ NIF_FUNCTION(img_load) term ); } + +NIF_FUNCTION(img_load) +{ + unsigned int len; + char *filename; + + BADARG_IF(!enif_get_list_length(env, argv[0], &len)); + filename = (char*)enif_alloc(len + 1); + + if (!enif_get_string(env, argv[0], filename, len + 1, ERL_NIF_LATIN1)) { + enif_free(filename); + return enif_make_badarg(env); + } + + return nif_thread_call(env, thread_img_load, 1, filename); +} -- cgit v1.2.3