From ca41e1c52a2264a63dcf6604a49e29b1d4a221c6 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sun, 11 Jul 2021 13:03:54 -0700 Subject: fixes #1409 reader/writer lock desired This provides the initial implementation, and converts the transport lookup routines to use it. This is probably of limited performance benefit, but rwlock's may be useful in further future work. --- src/sp/transport.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/sp') diff --git a/src/sp/transport.c b/src/sp/transport.c index f8751f68..7f76af16 100644 --- a/src/sp/transport.c +++ b/src/sp/transport.c @@ -43,9 +43,9 @@ typedef struct nni_sp_transport { nni_list_node t_node; } nni_sp_transport; -static nni_list nni_sp_tran_list; -static nni_mtx nni_sp_tran_lk; -static int nni_sp_tran_inited; +static nni_list nni_sp_tran_list; +static nni_rwlock nni_sp_tran_lk; +static int nni_sp_tran_inited; int nni_sp_tran_register(const nni_sp_tran *tran) @@ -65,32 +65,32 @@ nni_sp_tran_register(const nni_sp_tran *tran) return (NNG_ENOTSUP); } - nni_mtx_lock(&nni_sp_tran_lk); + nni_rwlock_wrlock(&nni_sp_tran_lk); // Check to see if the transport is already registered... NNI_LIST_FOREACH (&nni_sp_tran_list, t) { if (strcmp(tran->tran_scheme, t->t_tran.tran_scheme) == 0) { if (tran->tran_init == t->t_tran.tran_init) { // duplicate. - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); return (0); } - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); return (NNG_ESTATE); } } if ((t = NNI_ALLOC_STRUCT(t)) == NULL) { - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); return (NNG_ENOMEM); } t->t_tran = *tran; if ((rv = t->t_tran.tran_init()) != 0) { - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); NNI_FREE_STRUCT(t); return (rv); } nni_list_append(&nni_sp_tran_list, t); - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); return (0); } @@ -100,14 +100,14 @@ nni_sp_tran_find(nni_url *url) // address is of the form "://blah..." nni_sp_transport *t; - nni_mtx_lock(&nni_sp_tran_lk); + nni_rwlock_rdlock(&nni_sp_tran_lk); NNI_LIST_FOREACH (&nni_sp_tran_list, t) { if (strcmp(url->u_scheme, t->t_tran.tran_scheme) == 0) { - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); return (&t->t_tran); } } - nni_mtx_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&nni_sp_tran_lk); return (NULL); } @@ -150,7 +150,7 @@ nni_sp_tran_sys_init(void) nni_sp_tran_inited = 1; NNI_LIST_INIT(&nni_sp_tran_list, nni_sp_transport, t_node); - nni_mtx_init(&nni_sp_tran_lk); + nni_rwlock_init(&nni_sp_tran_lk); for (i = 0; nni_sp_tran_ctors[i] != NULL; i++) { int rv; @@ -174,6 +174,6 @@ nni_sp_tran_sys_fini(void) t->t_tran.tran_fini(); NNI_FREE_STRUCT(t); } - nni_mtx_fini(&nni_sp_tran_lk); + nni_rwlock_fini(&nni_sp_tran_lk); nni_sp_tran_inited = 0; } -- cgit v1.2.3-70-g09d2