aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/survey
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-08-15 21:59:55 -0700
committerGarrett D'Amore <garrett@damore.org>2017-08-16 18:31:42 -0700
commita9633313ec8e578c805cd53b37ba3360d83157bc (patch)
tree14d32c4031ea1c8508a75469407ca77e353fa315 /src/protocol/survey
parente7e2a6c14f0317eb77711951c6f1a650d4013dfe (diff)
downloadnng-a9633313ec8e578c805cd53b37ba3360d83157bc.tar.gz
nng-a9633313ec8e578c805cd53b37ba3360d83157bc.tar.bz2
nng-a9633313ec8e578c805cd53b37ba3360d83157bc.zip
Provide versions of mutex, condvar, and aio init that never fail.
If the underlying platform fails (FreeBSD is the only one I'm aware of that does this!), we use a global lock or condition variable instead. This means that our lock initializers never ever fail. Probably we could eliminate most of this for Linux and Darwin, since on those platforms, mutex and condvar initialization reasonably never fails. Initial benchmarks show little difference either way -- so we can revisit (optimize) later. This removes a lot of otherwise untested code in error cases and so forth, improving coverage and resilience in the face of allocation failures. Platforms other than POSIX should follow a similar pattern if they need this. (VxWorks, I'm thinking of you.) Most sane platforms won't have an issue here, since normally these initializations do not need to allocate memory. (Reportedly, even FreeBSD has plans to "fix" this in libthr2.) While here, some bugs were fixed in initialization & teardown. The fallback code is properly tested with dedicated test cases.
Diffstat (limited to 'src/protocol/survey')
-rw-r--r--src/protocol/survey/respond.c48
-rw-r--r--src/protocol/survey/survey.c43
2 files changed, 23 insertions, 68 deletions
diff --git a/src/protocol/survey/respond.c b/src/protocol/survey/respond.c
index 32513134..a097f551 100644
--- a/src/protocol/survey/respond.c
+++ b/src/protocol/survey/respond.c
@@ -77,6 +77,11 @@ nni_resp_sock_init(void **pp, nni_sock *nsock)
if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) {
return (NNG_ENOMEM);
}
+ if ((rv = nni_idhash_init(&psock->pipes)) != 0) {
+ NNI_FREE_STRUCT(psock);
+ return (rv);
+ }
+
psock->ttl = 8; // Per RFC
psock->nsock = nsock;
psock->raw = 0;
@@ -85,24 +90,12 @@ nni_resp_sock_init(void **pp, nni_sock *nsock)
psock->urq = nni_sock_recvq(nsock);
psock->uwq = nni_sock_sendq(nsock);
- if ((rv = nni_mtx_init(&psock->mtx)) != 0) {
- goto fail;
- }
- if ((rv = nni_idhash_init(&psock->pipes)) != 0) {
- goto fail;
- }
- rv = nni_aio_init(&psock->aio_getq, nni_resp_sock_getq_cb, psock);
- if (rv != 0) {
- goto fail;
- }
+ nni_mtx_init(&psock->mtx);
+ nni_aio_init(&psock->aio_getq, nni_resp_sock_getq_cb, psock);
*pp = psock;
nni_sock_senderr(nsock, NNG_ESTATE);
return (0);
-
-fail:
- nni_resp_sock_fini(psock);
- return (rv);
}
static void
@@ -131,33 +124,18 @@ nni_resp_pipe_init(void **pp, nni_pipe *npipe, void *psock)
return (NNG_ENOMEM);
}
if ((rv = nni_msgq_init(&ppipe->sendq, 2)) != 0) {
- goto fail;
- }
- rv = nni_aio_init(&ppipe->aio_putq, nni_resp_putq_cb, ppipe);
- if (rv != 0) {
- goto fail;
- }
- rv = nni_aio_init(&ppipe->aio_recv, nni_resp_recv_cb, ppipe);
- if (rv != 0) {
- goto fail;
- }
- rv = nni_aio_init(&ppipe->aio_getq, nni_resp_getq_cb, ppipe);
- if (rv != 0) {
- goto fail;
- }
- rv = nni_aio_init(&ppipe->aio_send, nni_resp_send_cb, ppipe);
- if (rv != 0) {
- goto fail;
+ NNI_FREE_STRUCT(ppipe);
+ return (rv);
}
+ nni_aio_init(&ppipe->aio_putq, nni_resp_putq_cb, ppipe);
+ nni_aio_init(&ppipe->aio_recv, nni_resp_recv_cb, ppipe);
+ nni_aio_init(&ppipe->aio_getq, nni_resp_getq_cb, ppipe);
+ nni_aio_init(&ppipe->aio_send, nni_resp_send_cb, ppipe);
ppipe->npipe = npipe;
ppipe->psock = psock;
*pp = ppipe;
return (0);
-
-fail:
- nni_resp_pipe_fini(ppipe);
- return (rv);
}
static void
diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c
index cb90c13f..2a32f289 100644
--- a/src/protocol/survey/survey.c
+++ b/src/protocol/survey/survey.c
@@ -70,19 +70,13 @@ static int
nni_surv_sock_init(void **sp, nni_sock *nsock)
{
nni_surv_sock *psock;
- int rv;
if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) {
return (NNG_ENOMEM);
}
- if ((rv = nni_mtx_init(&psock->mtx)) != 0) {
- goto fail;
- }
- rv = nni_aio_init(&psock->aio_getq, nni_surv_sock_getq_cb, psock);
- if (rv != 0) {
- goto fail;
- }
NNI_LIST_INIT(&psock->pipes, nni_surv_pipe, node);
+ nni_mtx_init(&psock->mtx);
+ nni_aio_init(&psock->aio_getq, nni_surv_sock_getq_cb, psock);
nni_timer_init(&psock->timer, nni_surv_timeout, psock);
psock->nextid = nni_random();
@@ -96,10 +90,6 @@ nni_surv_sock_init(void **sp, nni_sock *nsock)
*sp = psock;
nni_sock_recverr(nsock, NNG_ESTATE);
return (0);
-
-fail:
- nni_surv_sock_fini(psock);
- return (rv);
}
static void
@@ -143,32 +133,19 @@ nni_surv_pipe_init(void **pp, nni_pipe *npipe, void *psock)
}
// This depth could be tunable.
if ((rv = nni_msgq_init(&ppipe->sendq, 16)) != 0) {
- goto failed;
- }
- rv = nni_aio_init(&ppipe->aio_getq, nni_surv_getq_cb, ppipe);
- if (rv != 0) {
- goto failed;
- }
- rv = nni_aio_init(&ppipe->aio_putq, nni_surv_putq_cb, ppipe);
- if (rv != 0) {
- goto failed;
- }
- rv = nni_aio_init(&ppipe->aio_send, nni_surv_send_cb, ppipe);
- if (rv != 0) {
- goto failed;
- }
- rv = nni_aio_init(&ppipe->aio_recv, nni_surv_recv_cb, ppipe);
- if (rv != 0) {
- goto failed;
+ NNI_FREE_STRUCT(ppipe);
+ return (rv);
}
+
+ nni_aio_init(&ppipe->aio_getq, nni_surv_getq_cb, ppipe);
+ nni_aio_init(&ppipe->aio_putq, nni_surv_putq_cb, ppipe);
+ nni_aio_init(&ppipe->aio_send, nni_surv_send_cb, ppipe);
+ nni_aio_init(&ppipe->aio_recv, nni_surv_recv_cb, ppipe);
+
ppipe->npipe = npipe;
ppipe->psock = psock;
*pp = ppipe;
return (0);
-
-failed:
- nni_surv_pipe_fini(ppipe);
- return (rv);
}
static int