Skip to content

Commit

Permalink
squash: GRegLock switch to pthread mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhuyan committed Sep 26, 2023
1 parent 36da2ae commit 9a7265d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
11 changes: 3 additions & 8 deletions c_src/quicer_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) \
{ \
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
22 changes: 12 additions & 10 deletions c_src/quicer_reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,26 +34,28 @@ 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);
}
}

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);
}

Expand All @@ -65,15 +67,15 @@ 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
return ATOM_OK;

exit:
destroy_r_ctx(r_ctx);
enif_mutex_unlock(GRegLock);
pthread_mutex_unlock(&GRegLock);
return res;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 9a7265d

Please sign in to comment.