diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/endpt.c | 146 | ||||
| -rw-r--r-- | src/core/endpt.h | 5 | ||||
| -rw-r--r-- | src/core/init.c | 25 | ||||
| -rw-r--r-- | src/core/init.h | 2 |
4 files changed, 83 insertions, 95 deletions
diff --git a/src/core/endpt.c b/src/core/endpt.c index 1108a504..76cddbc3 100644 --- a/src/core/endpt.c +++ b/src/core/endpt.c @@ -15,6 +15,29 @@ // Functionality realited to end points. +static nni_objhash *nni_eps = NULL; +static void *nni_ep_ctor(uint32_t); +static void nni_ep_dtor(void *); + +int +nni_ep_sys_init(void) +{ + int rv; + + rv = nni_objhash_init(&nni_eps, nni_ep_ctor, nni_ep_dtor); + + return (rv); +} + + +void +nni_ep_sys_fini(void) +{ + nni_objhash_fini(nni_eps); + nni_eps = NULL; +} + + int nni_ep_hold(nni_ep **epp, uint32_t id) { @@ -24,13 +47,11 @@ nni_ep_hold(nni_ep **epp, uint32_t id) if ((rv = nni_init()) != 0) { return (rv); } - nni_mtx_lock(nni_idlock); - if ((rv = nni_idhash_find(nni_endpoints, id, (void **) &ep)) != 0) { - nni_mtx_unlock(nni_idlock); + + rv = nni_objhash_find(nni_eps, id, (void **) &ep); + if (rv != 0) { return (NNG_ECLOSED); } - ep->ep_holds++; - nni_mtx_unlock(nni_idlock); *epp = ep; return (0); } @@ -39,43 +60,52 @@ nni_ep_hold(nni_ep **epp, uint32_t id) void nni_ep_rele(nni_ep *ep) { - nni_mtx_lock(nni_idlock); - ep->ep_holds--; - if (ep->ep_holds == 0) { - nni_cv_wake(&ep->ep_holdcv); - } - nni_mtx_unlock(nni_idlock); + nni_objhash_unref(nni_eps, ep->ep_id); } -int -nni_ep_hold_close(nni_ep **epp, uint32_t id) +uint32_t +nni_ep_id(nni_ep *ep) +{ + return (ep->ep_id); +} + + +static void * +nni_ep_ctor(uint32_t id) { - int rv; nni_ep *ep; + int rv; - if ((rv = nni_init()) != 0) { - return (rv); - } - nni_mtx_lock(nni_idlock); - if ((rv = nni_idhash_find(nni_endpoints, id, (void **) &ep)) != 0) { - nni_mtx_unlock(nni_idlock); - return (NNG_ECLOSED); + if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { + return (NULL); } - ep->ep_id = 0; - nni_idhash_remove(nni_endpoints, id); - while (ep->ep_holds) { - nni_cv_wait(&ep->ep_holdcv); + ep->ep_close = 0; + ep->ep_bound = 0; + ep->ep_pipe = NULL; + ep->ep_id = id; + + NNI_LIST_NODE_INIT(&ep->ep_node); + +#if 0 + if ((rv = nni_cv_init(&ep->ep_cv, &sock->s_mx)) != 0) { + nni_cv_fini(&ep->ep_cv); + NNI_FREE_STRUCT(ep); + return (NULL); } - nni_mtx_unlock(nni_idlock); - return (0); +#endif + + return (ep); } -uint32_t -nni_ep_id(nni_ep *ep) +static void +nni_ep_dtor(void *ptr) { - return (ep->ep_id); + nni_ep *ep = ptr; + + nni_cv_fini(&ep->ep_cv); + NNI_FREE_STRUCT(ep); } @@ -85,6 +115,7 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr) nni_tran *tran; nni_ep *ep; int rv; + uint32_t id; if ((tran = nni_tran_find(addr)) == NULL) { return (NNG_ENOTSUP); @@ -93,19 +124,13 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr) return (NNG_EINVAL); } - if ((ep = NNI_ALLOC_STRUCT(ep)) == NULL) { - return (NNG_ENOMEM); + rv = nni_objhash_alloc(nni_eps, &id, (void **) &ep); + if (rv != 0) { + return (rv); } ep->ep_sock = sock; - ep->ep_close = 0; - ep->ep_bound = 0; - ep->ep_pipe = NULL; ep->ep_tran = tran; - ep->ep_holds = 0; - ep->ep_id = 0; - memset(&ep->ep_cv, 0, sizeof (ep->ep_cv)); - memset(&ep->ep_holdcv, 0, sizeof (ep->ep_holdcv)); - NNI_LIST_NODE_INIT(&ep->ep_node); + // Could safely use strcpy here, but this avoids discussion. (void) snprintf(ep->ep_addr, sizeof (ep->ep_addr), "%s", addr); @@ -114,10 +139,7 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr) // dereference on hot paths. ep->ep_ops = *tran->tran_ep; - if (((rv = nni_cv_init(&ep->ep_cv, &sock->s_mx)) != 0) || - ((rv = nni_cv_init(&ep->ep_holdcv, nni_idlock)) != 0)) { - nni_cv_fini(&ep->ep_cv); - nni_cv_fini(&ep->ep_holdcv); + if ((rv = nni_cv_init(&ep->ep_cv, &sock->s_mx)) != 0) { NNI_FREE_STRUCT(ep); return (rv); } @@ -125,35 +147,19 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr) nni_mtx_lock(&sock->s_mx); if (sock->s_closing) { nni_mtx_unlock(&sock->s_mx); - nni_cv_fini(&ep->ep_cv); - nni_cv_fini(&ep->ep_holdcv); - NNI_FREE_STRUCT(ep); + nni_objhash_unref(nni_eps, id); return (NNG_ECLOSED); } rv = ep->ep_ops.ep_init(&ep->ep_data, addr, sock); if (rv != 0) { nni_mtx_unlock(&sock->s_mx); - nni_cv_fini(&ep->ep_cv); - nni_cv_fini(&ep->ep_holdcv); - NNI_FREE_STRUCT(ep); + nni_objhash_unref(nni_eps, id); return (rv); } nni_list_append(&sock->s_eps, ep); nni_mtx_unlock(&sock->s_mx); - nni_mtx_lock(nni_idlock); - rv = nni_idhash_alloc(nni_endpoints, &ep->ep_id, ep); - nni_mtx_unlock(nni_idlock); - if (rv != 0) { - nni_mtx_lock(&sock->s_mx); - nni_list_remove(&sock->s_eps, ep); - ep->ep_ops.ep_fini(ep->ep_data); - nni_cv_fini(&ep->ep_cv); - nni_cv_fini(&ep->ep_holdcv); - NNI_FREE_STRUCT(ep); - } - *epp = ep; return (0); } @@ -165,19 +171,6 @@ nni_ep_close(nni_ep *ep) nni_pipe *pipe; nni_mtx *mx = &ep->ep_sock->s_mx; - nni_mtx_lock(nni_idlock); - if (ep->ep_id != 0) { - // We might have removed this already as a result of - // application initiated endpoint close request instead - // of socket close. - nni_idhash_remove(nni_endpoints, ep->ep_id); - ep->ep_id = 0; - } - while (ep->ep_holds) { - nni_cv_wait(&ep->ep_holdcv); - } - nni_mtx_unlock(nni_idlock); - nni_mtx_lock(mx); NNI_ASSERT(ep->ep_close == 0); ep->ep_close = 1; @@ -193,8 +186,7 @@ nni_ep_close(nni_ep *ep) nni_thr_fini(&ep->ep_thr); ep->ep_ops.ep_fini(ep->ep_data); - nni_cv_fini(&ep->ep_cv); - NNI_FREE_STRUCT(ep); + nni_objhash_unref(nni_eps, ep->ep_id); } diff --git a/src/core/endpt.h b/src/core/endpt.h index dbd9ec42..0dc0433d 100644 --- a/src/core/endpt.h +++ b/src/core/endpt.h @@ -31,8 +31,6 @@ struct nni_ep { int ep_close; // full shutdown int ep_bound; // true if we bound locally nni_cv ep_cv; - int ep_holds; // user references (by id) - nni_cv ep_holdcv; nni_pipe * ep_pipe; // Connected pipe (dialers only) }; @@ -40,8 +38,9 @@ struct nni_ep { #define NNI_EP_MODE_DIAL 1 #define NNI_EP_MODE_LISTEN 2 +extern int nni_ep_sys_init(void); +extern void nni_ep_sys_fini(void); extern int nni_ep_hold(nni_ep **, uint32_t); -extern int nni_ep_hold_close(nni_ep **, uint32_t); extern void nni_ep_rele(nni_ep *); extern uint32_t nni_ep_id(nni_ep *); extern int nni_ep_create(nni_ep **, nni_sock *, const char *); diff --git a/src/core/init.c b/src/core/init.c index 4d73e897..7b3b4433 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -11,14 +11,10 @@ #include <stdlib.h> #include <stdio.h> -nni_idhash *nni_endpoints; nni_idhash *nni_pipes; -nni_idhash *nni_sockets; nni_mtx *nni_idlock; -static nni_idhash nni_endpoints_x; static nni_idhash nni_pipes_x; -static nni_idhash nni_sockets_x; static nni_mtx nni_idlock_x; static int @@ -44,17 +40,24 @@ nni_init_helper(void) nni_taskq_sys_fini(); return (rv); } + if ((rv = nni_ep_sys_init()) != 0) { + nni_sock_sys_fini(); + nni_random_sys_fini(); + nni_timer_sys_fini(); + nni_taskq_sys_fini(); + return (rv); + } if ((rv = nni_mtx_init(&nni_idlock_x)) != 0) { + nni_ep_sys_fini(); nni_sock_sys_fini(); nni_random_sys_fini(); nni_timer_sys_fini(); nni_taskq_sys_fini(); return (rv); } - if (((rv = nni_idhash_init(&nni_endpoints_x)) != 0) || - ((rv = nni_idhash_init(&nni_pipes_x)) != 0) || - ((rv = nni_idhash_init(&nni_sockets_x)) != 0)) { + if ((rv = nni_idhash_init(&nni_pipes_x)) != 0) { nni_mtx_fini(&nni_idlock_x); + nni_ep_sys_fini(); nni_sock_sys_fini(); nni_random_sys_fini(); nni_timer_sys_fini(); @@ -63,13 +66,9 @@ nni_init_helper(void) } nni_idhash_set_limits(&nni_pipes_x, 1, 0x7fffffff, (nni_random() & 0x7ffffffe) + 1); - nni_idhash_set_limits(&nni_sockets_x, 1, 0x7fffffff, 1); - nni_idhash_set_limits(&nni_endpoints_x, 1, 0xffffffff, 1); nni_idlock = &nni_idlock_x; nni_pipes = &nni_pipes_x; - nni_endpoints = &nni_endpoints_x; - nni_sockets = &nni_sockets_x; nni_tran_sys_init(); return (0); @@ -86,11 +85,11 @@ nni_init(void) void nni_fini(void) { - nni_idhash_fini(&nni_endpoints_x); nni_idhash_fini(&nni_pipes_x); - nni_idhash_fini(&nni_sockets_x); nni_mtx_fini(&nni_idlock_x); nni_tran_sys_fini(); + nni_ep_sys_fini(); + nni_sock_sys_fini(); nni_random_sys_fini(); nni_timer_sys_fini(); nni_taskq_sys_fini(); diff --git a/src/core/init.h b/src/core/init.h index e48b6705..70663c65 100644 --- a/src/core/init.h +++ b/src/core/init.h @@ -24,8 +24,6 @@ void nni_fini(void); // Private hash tables matching IDs to values. Consumers need to use the // nni_idlock to protect access to these. extern nni_mtx *nni_idlock; -extern nni_idhash *nni_endpoints; extern nni_idhash *nni_pipes; -extern nni_idhash *nni_sockets; #endif // CORE_INIT_H |
