aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/pair/pair.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-06-24 14:11:35 -0700
committerGarrett D'Amore <garrett@damore.org>2017-06-24 14:11:35 -0700
commit0a51aa7bfc88d55b98fdde0d497b072e6911457d (patch)
tree722ef4713bf27a9aac9dce0a1fe9fa0edfe34a2d /src/protocol/pair/pair.c
parentd753c00d43e6dc642b2445e4821537a92b8b8d23 (diff)
downloadnng-0a51aa7bfc88d55b98fdde0d497b072e6911457d.tar.gz
nng-0a51aa7bfc88d55b98fdde0d497b072e6911457d.tar.bz2
nng-0a51aa7bfc88d55b98fdde0d497b072e6911457d.zip
Protocols keep their own reference counts.
Diffstat (limited to 'src/protocol/pair/pair.c')
-rw-r--r--src/protocol/pair/pair.c75
1 files changed, 51 insertions, 24 deletions
diff --git a/src/protocol/pair/pair.c b/src/protocol/pair/pair.c
index a2b97fe0..c2d4fa0d 100644
--- a/src/protocol/pair/pair.c
+++ b/src/protocol/pair/pair.c
@@ -32,6 +32,7 @@ struct nni_pair_sock {
nni_msgq * uwq;
nni_msgq * urq;
int raw;
+ nni_mtx mtx;
};
// An nni_pair_pipe is our per-pipe protocol private structure. We keep
@@ -47,16 +48,23 @@ struct nni_pair_pipe {
nni_aio aio_putq;
int busy;
int closed;
+ nni_mtx mtx;
+ int refcnt;
};
static int
nni_pair_sock_init(void **sp, nni_sock *nsock)
{
nni_pair_sock *psock;
+ int rv;
if ((psock = NNI_ALLOC_STRUCT(psock)) == NULL) {
return (NNG_ENOMEM);
}
+ if ((rv = nni_mtx_init(&psock->mtx)) != 0) {
+ NNI_FREE_STRUCT(psock);
+ return (rv);
+ }
psock->nsock = nsock;
psock->ppipe = NULL;
psock->raw = 0;
@@ -73,6 +81,8 @@ nni_pair_sock_fini(void *arg)
nni_pair_sock *psock = arg;
if (psock != NULL) {
+ nni_mtx_fini(&psock->mtx);
+
NNI_FREE_STRUCT(psock);
}
}
@@ -87,31 +97,33 @@ nni_pair_pipe_init(void **pp, nni_pipe *npipe, void *psock)
if ((ppipe = NNI_ALLOC_STRUCT(ppipe)) == NULL) {
return (NNG_ENOMEM);
}
-
+ if ((rv = nni_mtx_init(&ppipe->mtx)) != 0) {
+ goto fail;
+ }
rv = nni_aio_init(&ppipe->aio_send, nni_pair_send_cb, ppipe);
if (rv != 0) {
- nni_pair_pipe_fini(ppipe);
- return (rv);
+ goto fail;
}
rv = nni_aio_init(&ppipe->aio_recv, nni_pair_recv_cb, ppipe);
if (rv != 0) {
- nni_pair_pipe_fini(ppipe);
- return (rv);
+ goto fail;
}
rv = nni_aio_init(&ppipe->aio_getq, nni_pair_getq_cb, ppipe);
if (rv != 0) {
- nni_pair_pipe_fini(ppipe);
- return (rv);
+ goto fail;
}
rv = nni_aio_init(&ppipe->aio_putq, nni_pair_putq_cb, ppipe);
if (rv != 0) {
- nni_pair_pipe_fini(ppipe);
- return (rv);
+ goto fail;
}
ppipe->npipe = npipe;
ppipe->psock = psock;
*pp = ppipe;
return (0);
+
+fail:
+ nni_pair_pipe_fini(ppipe);
+ return (rv);
}
@@ -125,6 +137,7 @@ nni_pair_pipe_fini(void *arg)
nni_aio_fini(&ppipe->aio_recv);
nni_aio_fini(&ppipe->aio_putq);
nni_aio_fini(&ppipe->aio_getq);
+ nni_mtx_fini(&ppipe->mtx);
NNI_FREE_STRUCT(ppipe);
}
@@ -135,16 +148,21 @@ nni_pair_pipe_start(void *arg)
nni_pair_pipe *ppipe = arg;
nni_pair_sock *psock = ppipe->psock;
+ nni_mtx_lock(&psock->mtx);
if (psock->ppipe != NULL) {
+ nni_mtx_unlock(&psock->mtx);
return (NNG_EBUSY); // Already have a peer, denied.
}
psock->ppipe = ppipe;
+ nni_mtx_unlock(&psock->mtx);
+
+ nni_mtx_lock(&ppipe->mtx);
+ ppipe->refcnt = 2;
+ nni_mtx_unlock(&ppipe->mtx);
// Schedule a getq on the upper, and a read from the pipe.
// Each of these also sets up another hold on the pipe itself.
- nni_pipe_hold(ppipe->npipe);
nni_msgq_aio_get(psock->uwq, &ppipe->aio_getq);
- nni_pipe_hold(ppipe->npipe);
nni_pipe_aio_recv(ppipe->npipe, &ppipe->aio_recv);
return (0);
@@ -152,17 +170,31 @@ nni_pair_pipe_start(void *arg)
static void
-nni_pair_pipe_stop(void *arg)
+nni_pair_pipe_stop(nni_pair_pipe *ppipe)
{
- nni_pair_pipe *ppipe = arg;
nni_pair_sock *psock = ppipe->psock;
+ int refcnt;
- nni_msgq_aio_cancel(psock->uwq, &ppipe->aio_getq);
- nni_msgq_aio_cancel(psock->urq, &ppipe->aio_putq);
+ nni_mtx_lock(&psock->mtx);
if (psock->ppipe == ppipe) {
psock->ppipe = NULL;
}
+ nni_mtx_unlock(&psock->mtx);
+
+ // These operations are idempotent.
+ nni_msgq_aio_cancel(psock->uwq, &ppipe->aio_getq);
+ nni_msgq_aio_cancel(psock->urq, &ppipe->aio_putq);
+
+ nni_mtx_lock(&ppipe->mtx);
+ NNI_ASSERT(ppipe->refcnt > 0);
+ ppipe->refcnt--;
+ refcnt = ppipe->refcnt;
+ nni_mtx_unlock(&ppipe->mtx);
+
+ if (refcnt == 0) {
+ nni_pipe_remove(ppipe->npipe);
+ }
}
@@ -173,8 +205,7 @@ nni_pair_recv_cb(void *arg)
nni_pair_sock *psock = ppipe->psock;
if (nni_aio_result(&ppipe->aio_recv) != 0) {
- nni_pipe_close(ppipe->npipe);
- nni_pipe_rele(ppipe->npipe);
+ nni_pair_pipe_stop(ppipe);
return;
}
@@ -192,8 +223,7 @@ nni_pair_putq_cb(void *arg)
if (nni_aio_result(&ppipe->aio_putq) != 0) {
nni_msg_free(ppipe->aio_putq.a_msg);
ppipe->aio_putq.a_msg = NULL;
- nni_pipe_close(ppipe->npipe);
- nni_pipe_rele(ppipe->npipe);
+ nni_pair_pipe_stop(ppipe);
return;
}
nni_pipe_aio_recv(ppipe->npipe, &ppipe->aio_recv);
@@ -208,8 +238,7 @@ nni_pair_getq_cb(void *arg)
nni_msg *msg;
if (nni_aio_result(&ppipe->aio_getq) != 0) {
- nni_pipe_close(ppipe->npipe);
- nni_pipe_rele(ppipe->npipe);
+ nni_pair_pipe_stop(ppipe);
return;
}
@@ -228,8 +257,7 @@ nni_pair_send_cb(void *arg)
if (nni_aio_result(&ppipe->aio_send) != 0) {
nni_msg_free(ppipe->aio_send.a_msg);
ppipe->aio_send.a_msg = NULL;
- nni_pipe_close(ppipe->npipe);
- nni_pipe_rele(ppipe->npipe);
+ nni_pair_pipe_stop(ppipe);
return;
}
@@ -278,7 +306,6 @@ static nni_proto_pipe_ops nni_pair_pipe_ops = {
.pipe_init = nni_pair_pipe_init,
.pipe_fini = nni_pair_pipe_fini,
.pipe_start = nni_pair_pipe_start,
- .pipe_stop = nni_pair_pipe_stop,
};
static nni_proto_sock_ops nni_pair_sock_ops = {