From a9633313ec8e578c805cd53b37ba3360d83157bc Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 15 Aug 2017 21:59:55 -0700 Subject: 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. --- src/protocol/reqrep/rep.c | 46 +++++++++++++--------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) (limited to 'src/protocol/reqrep/rep.c') diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 4319cbf8..09f2b285 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -74,20 +74,18 @@ nni_rep_sock_init(void **repp, nni_sock *sock) if ((rep = NNI_ALLOC_STRUCT(rep)) == NULL) { return (NNG_ENOMEM); } + if ((rv = nni_idhash_init(&rep->pipes)) != 0) { + NNI_FREE_STRUCT(rep); + return (rv); + } + rep->ttl = 8; // Per RFC rep->sock = sock; rep->raw = 0; rep->btrace = NULL; rep->btrace_len = 0; - if ((rv = nni_idhash_init(&rep->pipes)) != 0) { - goto fail; - } - - rv = nni_aio_init(&rep->aio_getq, nni_rep_sock_getq_cb, rep); - if (rv != 0) { - goto fail; - } + nni_aio_init(&rep->aio_getq, nni_rep_sock_getq_cb, rep); rep->uwq = nni_sock_sendq(sock); rep->urq = nni_sock_recvq(sock); @@ -96,10 +94,6 @@ nni_rep_sock_init(void **repp, nni_sock *sock) nni_sock_senderr(sock, NNG_ESTATE); return (0); - -fail: - nni_rep_sock_fini(rep); - return (rv); } static void @@ -128,32 +122,18 @@ nni_rep_pipe_init(void **rpp, nni_pipe *pipe, void *rsock) return (NNG_ENOMEM); } if ((rv = nni_msgq_init(&rp->sendq, 2)) != 0) { - goto fail; - } - if ((rv = nni_aio_init(&rp->aio_getq, nni_rep_pipe_getq_cb, rp)) != - 0) { - goto fail; - } - if ((rv = nni_aio_init(&rp->aio_send, nni_rep_pipe_send_cb, rp)) != - 0) { - goto fail; - } - if ((rv = nni_aio_init(&rp->aio_recv, nni_rep_pipe_recv_cb, rp)) != - 0) { - goto fail; - } - if ((rv = nni_aio_init(&rp->aio_putq, nni_rep_pipe_putq_cb, rp)) != - 0) { - goto fail; + NNI_FREE_STRUCT(rp); + return (rv); } + nni_aio_init(&rp->aio_getq, nni_rep_pipe_getq_cb, rp); + nni_aio_init(&rp->aio_send, nni_rep_pipe_send_cb, rp); + nni_aio_init(&rp->aio_recv, nni_rep_pipe_recv_cb, rp); + nni_aio_init(&rp->aio_putq, nni_rep_pipe_putq_cb, rp); + rp->pipe = pipe; rp->rep = rsock; *rpp = rp; return (0); - -fail: - nni_rep_pipe_fini(rp); - return (rv); } static void -- cgit v1.2.3-70-g09d2