aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-04-08 11:24:52 +0200
committerLoïc Hoguin <[email protected]>2014-04-08 11:24:52 +0200
commitc62f79fc3266ba7c21ed4ada84d16aab771f0d02 (patch)
tree34f63bdd0985ccf2bcb1122316bb1c27402d687c
parent424cf7c72829a39f10f1d5e6975118e4348c0379 (diff)
downloadesdl2-c62f79fc3266ba7c21ed4ada84d16aab771f0d02.tar.gz
esdl2-c62f79fc3266ba7c21ed4ada84d16aab771f0d02.tar.bz2
esdl2-c62f79fc3266ba7c21ed4ada84d16aab771f0d02.zip
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.
-rw-r--r--c_src/sdl_surface.c26
-rw-r--r--src/sdl_surface.erl3
2 files changed, 24 insertions, 5 deletions
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);
+}
diff --git a/src/sdl_surface.erl b/src/sdl_surface.erl
index f36407f..15a4dec 100644
--- a/src/sdl_surface.erl
+++ b/src/sdl_surface.erl
@@ -17,4 +17,5 @@
-export([load/1]).
load(Filename) ->
- esdl2:img_load(Filename).
+ esdl2:img_load(Filename),
+ receive {'_nif_thread_ret_', Ret} -> Ret end.