From 9a7265d842aafd41db7c6b9eddb93b186a5d74d8 Mon Sep 17 00:00:00 2001 From: William Yang Date: Tue, 26 Sep 2023 17:58:38 +0200 Subject: [PATCH] squash: GRegLock switch to pthread mutex --- c_src/quicer_nif.c | 11 +++-------- c_src/quicer_reg.c | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/c_src/quicer_nif.c b/c_src/quicer_nif.c index b11482ce..293dced9 100644 --- a/c_src/quicer_nif.c +++ b/c_src/quicer_nif.c @@ -774,7 +774,7 @@ ERL_NIF_TERM ATOM_QUIC_DATAGRAM_SEND_CANCELED; ATOM(ATOM_UNDEFINED, undefined); extern QuicerRegistrationCTX *G_r_ctx; -extern ErlNifMutex *GRegLock; +extern pthread_mutex_t GRegLock; const QUIC_API_TABLE *MsQuic = NULL; // Mutex for MsQuic @@ -978,11 +978,6 @@ on_load(ErlNifEnv *env, MsQuicLock = enif_mutex_create("msquic_lock"); } - if (!GRegLock) - { - GRegLock = enif_mutex_create("global_reg_lock"); - } - // init atoms in use. #define ATOM(name, val) \ { \ @@ -1134,7 +1129,7 @@ closeLib(__unused_parm__ ErlNifEnv *env, { TP_NIF_3(do_close, MsQuic, 0); - enif_mutex_lock(GRegLock); + pthread_mutex_lock(&GRegLock); // end of the world if (G_r_ctx && !G_r_ctx->is_released) { @@ -1146,7 +1141,7 @@ closeLib(__unused_parm__ ErlNifEnv *env, destroy_r_ctx(G_r_ctx); G_r_ctx = NULL; } - enif_mutex_unlock(GRegLock); + pthread_mutex_unlock(&GRegLock); MsQuicClose(MsQuic); MsQuic = NULL; diff --git a/c_src/quicer_reg.c b/c_src/quicer_reg.c index 55b3531b..ee65a8d0 100644 --- a/c_src/quicer_reg.c +++ b/c_src/quicer_reg.c @@ -20,7 +20,7 @@ static BOOLEAN parse_reg_conf(ERL_NIF_TERM eprofile, QUIC_REGISTRATION_CONFIG *RegConfig); QuicerRegistrationCTX *G_r_ctx = NULL; -ErlNifMutex *GRegLock = NULL; +pthread_mutex_t GRegLock = PTHREAD_MUTEX_INITIALIZER; /* ** For global registration only @@ -34,18 +34,20 @@ registration(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) QUIC_STATUS status; ERL_NIF_TERM res = ATOM_OK; - if (!MsQuic || !GRegLock || G_r_ctx) + if (!MsQuic || G_r_ctx) { return ERROR_TUPLE_2(ATOM_BADARG); } - enif_mutex_lock(GRegLock); + //enif_mutex_lock(GRegLock); + pthread_mutex_lock(&GRegLock); + if (argc == 1) { eprofile = argv[0]; if (!parse_reg_conf(eprofile, &RegConfig)) { - enif_mutex_unlock(GRegLock); + pthread_mutex_unlock(&GRegLock); return ERROR_TUPLE_2(ATOM_BADARG); } } @@ -53,7 +55,7 @@ registration(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) QuicerRegistrationCTX *r_ctx = init_r_ctx(); if (!r_ctx) { - enif_mutex_unlock(GRegLock); + pthread_mutex_unlock(&GRegLock); return ERROR_TUPLE_2(ATOM_ERROR_NOT_ENOUGH_MEMORY); } @@ -65,7 +67,7 @@ registration(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) } G_r_ctx = r_ctx; - enif_mutex_unlock(GRegLock); + pthread_mutex_unlock(&GRegLock); // nif owns the global registration // thus not return to the erlang side @@ -73,7 +75,7 @@ registration(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) exit: destroy_r_ctx(r_ctx); - enif_mutex_unlock(GRegLock); + pthread_mutex_unlock(&GRegLock); return res; } @@ -86,19 +88,19 @@ deregistration(__unused_parm__ ErlNifEnv *env, __unused_parm__ const ERL_NIF_TERM argv[]) { int error_code = 0; - if (!MsQuic || !GRegLock) + if (!MsQuic) { return ERROR_TUPLE_2(ATOM_BADARG); } - enif_mutex_lock(GRegLock); + pthread_mutex_lock(&GRegLock); if (G_r_ctx && !G_r_ctx->is_released) { MsQuic->RegistrationShutdown(G_r_ctx->Registration, FALSE, error_code); destroy_r_ctx(G_r_ctx); G_r_ctx = NULL; } - enif_mutex_unlock(GRegLock); + pthread_mutex_unlock(&GRegLock); return ATOM_OK; }