summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/endpt.c40
-rw-r--r--src/core/socket.c35
2 files changed, 30 insertions, 45 deletions
diff --git a/src/core/endpt.c b/src/core/endpt.c
index 58b30b9f..2366a8e8 100644
--- a/src/core/endpt.c
+++ b/src/core/endpt.c
@@ -37,41 +37,49 @@ nni_ep_create(nni_ep **epp, nni_sock *sock, const char *addr)
ep->ep_bound = 0;
ep->ep_pipe = NULL;
ep->ep_tran = tran;
-
- nni_mtx_lock(nni_idlock);
- rv = nni_idhash_alloc(nni_endpoints, &ep->ep_id, ep);
- nni_mtx_unlock(nni_idlock);
- if (rv != 0) {
- NNI_FREE_STRUCT(ep);
- return (rv);
- }
+ 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);
// Make a copy of the endpoint operations. This allows us to
// modify them (to override NULLs for example), and avoids an extra
// dereference on hot paths.
ep->ep_ops = *tran->tran_ep;
- NNI_LIST_NODE_INIT(&ep->ep_node);
- if ((rv = nni_cv_init(&ep->ep_cv, &ep->ep_sock->s_mx)) != 0) {
- nni_mtx_lock(nni_idlock);
- nni_idhash_remove(nni_endpoints, ep->ep_id);
- nni_mtx_unlock(nni_idlock);
+ if ((rv = nni_cv_init(&ep->ep_cv, &sock->s_mx)) != 0) {
NNI_FREE_STRUCT(ep);
- return (NNG_ENOMEM);
+ return (rv);
}
- // Could safely use strcpy here, but this avoids discussion.
- (void) snprintf(ep->ep_addr, sizeof (ep->ep_addr), "%s", addr);
+ nni_mtx_lock(&sock->s_mx);
+ if (sock->s_closing) {
+ nni_mtx_unlock(&sock->s_mx);
+ nni_cv_fini(&ep->ep_cv);
+ NNI_FREE_STRUCT(ep);
+ return (NNG_ECLOSED);
+ }
+ 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_unlock(&sock->s_mx);
+ nni_cv_fini(&ep->ep_cv);
+ NNI_FREE_STRUCT(ep);
+ return (rv);
+ }
rv = ep->ep_ops.ep_init(&ep->ep_data, addr, nni_sock_proto(sock));
if (rv != 0) {
nni_mtx_lock(nni_idlock);
nni_idhash_remove(nni_endpoints, ep->ep_id);
nni_mtx_unlock(nni_idlock);
+ nni_mtx_unlock(&sock->s_mx);
nni_cv_fini(&ep->ep_cv);
NNI_FREE_STRUCT(ep);
return (rv);
}
+ nni_list_append(&sock->s_eps, ep);
+ nni_mtx_unlock(&sock->s_mx);
*epp = ep;
return (0);
diff --git a/src/core/socket.c b/src/core/socket.c
index 0ae20a28..d9119a64 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -547,25 +547,14 @@ nni_sock_dial(nni_sock *sock, const char *addr, nni_ep **epp, int flags)
nni_ep *ep;
int rv;
- nni_mtx_lock(&sock->s_mx);
- if (sock->s_closing) {
- nni_mtx_unlock(&sock->s_mx);
- return (NNG_ECLOSED);
- }
if ((rv = nni_ep_create(&ep, sock, addr)) != 0) {
- nni_mtx_unlock(&sock->s_mx);
return (rv);
}
- nni_list_append(&sock->s_eps, ep);
- nni_mtx_unlock(&sock->s_mx);
- rv = nni_ep_dial(ep, flags);
- if (rv != 0) {
+ if ((rv = nni_ep_dial(ep, flags)) != 0) {
nni_ep_close(ep);
- } else {
- if (epp != NULL) {
- *epp = ep;
- }
+ } else if (epp != NULL) {
+ *epp = ep;
}
return (rv);
@@ -578,26 +567,14 @@ nni_sock_listen(nni_sock *sock, const char *addr, nni_ep **epp, int flags)
nni_ep *ep;
int rv;
- nni_mtx_lock(&sock->s_mx);
- if (sock->s_closing) {
- nni_mtx_unlock(&sock->s_mx);
- return (NNG_ECLOSED);
- }
-
if ((rv = nni_ep_create(&ep, sock, addr)) != 0) {
- nni_mtx_unlock(&sock->s_mx);
return (rv);
}
- nni_list_append(&sock->s_eps, ep);
- nni_mtx_unlock(&sock->s_mx);
- rv = nni_ep_listen(ep, flags);
- if (rv != 0) {
+ if ((rv = nni_ep_listen(ep, flags)) != 0) {
nni_ep_close(ep);
- } else {
- if (epp != NULL) {
- *epp = ep;
- }
+ } else if (epp != NULL) {
+ *epp = ep;
}
return (rv);