diff options
author | Loïc Hoguin <[email protected]> | 2015-10-20 02:24:32 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2015-10-20 02:24:32 +0200 |
commit | 054be80416dbd7f853ffd680d1b90323acd2074f (patch) | |
tree | 42cc8067f40e41942b144f3ca36c9483964ed898 /c_src/esdl2.c | |
parent | 35fe8bdd42e1ebe0438e15a53c7affa415e00ce0 (diff) | |
download | esdl2-054be80416dbd7f853ffd680d1b90323acd2074f.tar.gz esdl2-054be80416dbd7f853ffd680d1b90323acd2074f.tar.bz2 esdl2-054be80416dbd7f853ffd680d1b90323acd2074f.zip |
Don't crash on code reloading
Diffstat (limited to 'c_src/esdl2.c')
-rw-r--r-- | c_src/esdl2.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/c_src/esdl2.c b/c_src/esdl2.c index 1613901..430847f 100644 --- a/c_src/esdl2.c +++ b/c_src/esdl2.c @@ -18,6 +18,8 @@ NIF_ATOMS(NIF_ATOM_DECL) NIF_RESOURCES(NIF_RES_DECL) +static int loads = 0; + int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) { NIF_ATOMS(NIF_ATOM_INIT) @@ -25,16 +27,30 @@ int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) *priv_data = nif_create_main_thread("esdl2"); + loads++; + + return 0; +} + +int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data, ERL_NIF_TERM load_info) +{ + *priv_data = *old_priv_data; + + loads++; + return 0; } void unload(ErlNifEnv* env, void* priv_data) { - nif_destroy_main_thread(priv_data); + if (loads == 1) + nif_destroy_main_thread(priv_data); + + loads--; } static ErlNifFunc nif_funcs[] = { NIF_FUNCTIONS(NIF_FUNCTION_ARRAY) }; -ERL_NIF_INIT(esdl2, nif_funcs, load, NULL, NULL, unload) +ERL_NIF_INIT(esdl2, nif_funcs, load, NULL, upgrade, unload) |