diff options
| -rw-r--r-- | src/core/socket.c | 27 | ||||
| -rw-r--r-- | src/platform/posix/posix_synch.c | 1 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/sock.c | 31 |
4 files changed, 50 insertions, 10 deletions
diff --git a/src/core/socket.c b/src/core/socket.c index 1b3bcf56..99573a2f 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -57,14 +57,27 @@ nni_socket_create(nni_socket **sockp, uint16_t proto) NNI_LIST_INIT(&sock->s_pipes, nni_pipe, p_sock_node); NNI_LIST_INIT(&sock->s_eps, nni_endpt, ep_sock_node); + if (((rv = nni_msgqueue_create(&sock->s_uwq, 1)) != 0) || + ((rv = nni_msgqueue_create(&sock->s_urq, 1)) != 0)) { + goto fail; + } + if ((rv = sock->s_ops.proto_create(&sock->s_data, sock)) != 0) { - nni_cond_fini(&sock->s_cv); - nni_mutex_fini(&sock->s_mx); - nni_free(sock, sizeof (*sock)); - return (rv); + goto fail; } *sockp = sock; return (0); +fail: + if (sock->s_urq != NULL) { + nni_msgqueue_destroy(sock->s_urq); + } + if (sock->s_uwq != NULL) { + nni_msgqueue_destroy(sock->s_uwq); + } + nni_cond_fini(&sock->s_cv); + nni_mutex_fini(&sock->s_mx); + nni_free(sock, sizeof (*sock)); + return (rv); } @@ -76,7 +89,6 @@ nni_socket_close(nni_socket *sock) nni_endpt *ep; nni_time linger; - nni_mutex_enter(&sock->s_mx); // Mark us closing, so no more EPs or changes can occur. sock->s_closing = 1; @@ -117,7 +129,6 @@ nni_socket_close(nni_socket *sock) nni_msgqueue_close(sock->s_urq); // Go through and close all the pipes. - nni_mutex_enter(&sock->s_mx); NNI_LIST_FOREACH (&sock->s_pipes, pipe) { nni_pipe_close(pipe); } @@ -142,15 +153,13 @@ nni_socket_close(nni_socket *sock) nni_mutex_exit(&sock->s_mx); // At this point nothing else should be referencing us. - // The protocol needs to clean up its state. - sock->s_ops.proto_destroy(&sock->s_data); + sock->s_ops.proto_destroy(sock->s_data); // And we need to clean up *our* state. nni_cond_fini(&sock->s_cv); nni_mutex_fini(&sock->s_mx); nni_free(sock, sizeof (*sock)); - return (0); } diff --git a/src/platform/posix/posix_synch.c b/src/platform/posix/posix_synch.c index 3d91ee3d..d4c4d6ac 100644 --- a/src/platform/posix/posix_synch.c +++ b/src/platform/posix/posix_synch.c @@ -35,7 +35,6 @@ void nni_mutex_fini(nni_mutex *mp) { int rv; - if ((rv = pthread_mutex_destroy(&mp->mx)) != 0) { nni_panic("pthread_mutex_destroy failed: %s", strerror(rv)); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 46cae66a..8a2bbb5c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -53,3 +53,4 @@ else () endif () add_nng_test(list 5) +add_nng_test(sock 5)
\ No newline at end of file diff --git a/tests/sock.c b/tests/sock.c new file mode 100644 index 00000000..40949bfe --- /dev/null +++ b/tests/sock.c @@ -0,0 +1,31 @@ +// +// Copyright 2016 Garrett D'Amore <garrett@damore.org> +// +// This software is supplied under the terms of the MIT License, a +// copy of which should be located in the distribution where this +// file was obtained (LICENSE.txt). A copy of the license may also be +// found online at https://opensource.org/licenses/MIT. +// + +#include "convey.h" +#include "nng.h" + +TestMain("Socket Operations", { + Convey("We are able to open a PAIR socket", { + int rv; + nng_socket *sock = NULL; + + rv = nng_socket_create(&sock, NNG_PROTO_PAIR); + So(rv == 0); + So(sock != NULL); + + Convey("And we can close it", { + rv = nng_socket_close(sock); + So(rv == 0); + }) + + Convey("It's type is still proto", { + So(nng_socket_protocol(sock) == NNG_PROTO_PAIR); + }) + }) +})
\ No newline at end of file |
