Skip to content

Commit

Permalink
re-open resources on re-initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
bucko909 committed Dec 20, 2015
1 parent 02117cb commit eb0dd46
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions apps/resources/c_src/resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ free_res(ErlNifEnv* env, void* obj)
}

static int
load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
open_resource(ErlNifEnv* env)
{
const char* mod = "resources";
const char* name = "Example";
Expand All @@ -30,7 +30,14 @@ load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)

RES_TYPE = enif_open_resource_type(env, mod, name, free_res, flags, NULL);
if(RES_TYPE == NULL) return -1;

return 0;
}

static int
load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
{
if(open_resource(env) == -1) return -1;

atom_ok = enif_make_atom(env, "ok");

tracker = (Tracker*) enif_alloc(sizeof(Tracker));
Expand All @@ -40,6 +47,21 @@ load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
return 0;
}

// Erlang requires that we re-open resources on re-initialisation.
static int
reload(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
{
if(open_resource(env) == -1) return -1;
return 0;
}

static int
reload(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM load_info)
{
if(open_resource(env) == -1) return -1;
return 0;
}

static ERL_NIF_TERM
count(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
Expand Down Expand Up @@ -110,5 +132,5 @@ static ErlNifFunc nif_funcs[] = {
{"read", 1, read}
};

ERL_NIF_INIT(resources, nif_funcs, &load, NULL, NULL, NULL);
ERL_NIF_INIT(resources, nif_funcs, &load, &reload, &upgrade, NULL);

0 comments on commit eb0dd46

Please sign in to comment.