diff --git a/lib/crypto/c_src/algorithms.c b/lib/crypto/c_src/algorithms.c index be19286509d9..9686ed6464dd 100644 --- a/lib/crypto/c_src/algorithms.c +++ b/lib/crypto/c_src/algorithms.c @@ -51,7 +51,6 @@ void init_rsa_opts_types(ErlNifEnv* env); void init_algorithms_types(ErlNifEnv* env) { - mtx_init_curve_types = enif_mutex_create("init_curve_types"); #ifdef HAS_3_0_API #else init_hash_types(env); @@ -62,9 +61,21 @@ void init_algorithms_types(ErlNifEnv* env) /* ciphers and macs are initiated statically */ } -void cleanup_algorithms_types(ErlNifEnv* env) + +int create_curve_mutex(void) +{ + if (!mtx_init_curve_types) { + mtx_init_curve_types = enif_mutex_create("init_curve_types"); + } + return !!mtx_init_curve_types; +} + +void destroy_curve_mutex(void) { - enif_mutex_destroy(mtx_init_curve_types); + if (mtx_init_curve_types) { + enif_mutex_destroy(mtx_init_curve_types); + mtx_init_curve_types = NULL; + } } /*================================================================ diff --git a/lib/crypto/c_src/algorithms.h b/lib/crypto/c_src/algorithms.h index 25e1db129955..3c22a9fccc42 100644 --- a/lib/crypto/c_src/algorithms.h +++ b/lib/crypto/c_src/algorithms.h @@ -23,8 +23,9 @@ #include "common.h" +int create_curve_mutex(void); +void destroy_curve_mutex(void); void init_algorithms_types(ErlNifEnv* env); -void cleanup_algorithms_types(ErlNifEnv* env); ERL_NIF_TERM hash_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); ERL_NIF_TERM pubkey_algorithms(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); diff --git a/lib/crypto/c_src/crypto.c b/lib/crypto/c_src/crypto.c index 12abf8aca223..00fe2e44ec74 100644 --- a/lib/crypto/c_src/crypto.c +++ b/lib/crypto/c_src/crypto.c @@ -218,6 +218,8 @@ static int initialize(ErlNifEnv* env, ERL_NIF_TERM load_info) if (!create_engine_mutex(env)) { return __LINE__; } + if (!create_curve_mutex()) + return __LINE__; #ifdef HAS_3_0_API prov_cnt = 0; @@ -333,7 +335,7 @@ static int upgrade(ErlNifEnv* env, void** priv_data, void** old_priv_data, static void unload(ErlNifEnv* env, void* priv_data) { if (--library_refc == 0) { - cleanup_algorithms_types(env); + destroy_curve_mutex(); destroy_engine_mutex(env); }