Skip to content

Commit

Permalink
ctx: Simplify handling for closed contexts.
Browse files Browse the repository at this point in the history
Once a context has started the process of close, further attempts
to close it will return NNG_ECLOSED.  What was I thinking to ever
do anything else?
  • Loading branch information
gdamore committed Dec 7, 2024
1 parent 4f940ce commit 3dfa962
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ nni_sock_set_pipe_cb(nni_sock *s, int ev, nng_pipe_cb cb, void *arg)
}

int
nni_ctx_find(nni_ctx **cp, uint32_t id, bool closing)
nni_ctx_find(nni_ctx **cp, uint32_t id)
{
int rv = 0;
nni_ctx *ctx;
Expand All @@ -1043,7 +1043,7 @@ nni_ctx_find(nni_ctx **cp, uint32_t id, bool closing)
// we can close it, then we still allow. In the case
// the only valid operation will be to close the
// socket.)
if (ctx->c_closed || ((!closing) && ctx->c_sock->s_closed)) {
if (ctx->c_closed || ctx->c_sock->s_closed) {
rv = NNG_ECLOSED;
} else {
ctx->c_ref++;
Expand Down
7 changes: 2 additions & 5 deletions src/core/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ extern void nni_sock_set_pipe_cb(nni_sock *sock, int, nng_pipe_cb, void *);
// sockets (will also return NNG_ENOTSUP).
extern int nni_ctx_open(nni_ctx **, nni_sock *);

// nni_ctx_find finds a context given its id. The last argument should
// be true if the context is acquired merely to close it, false otherwise.
// (If the socket for the context is being closed, then this will return
// NNG_ECLOSED unless the final argument is true.)
extern int nni_ctx_find(nni_ctx **, uint32_t, bool);
// nni_ctx_find finds a context given its id.
extern int nni_ctx_find(nni_ctx **, uint32_t);

extern void *nni_ctx_proto_data(nni_ctx *);

Expand Down
14 changes: 7 additions & 7 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ nng_ctx_close(nng_ctx c)
int rv;
nni_ctx *ctx;

if ((rv = nni_ctx_find(&ctx, c.id, true)) != 0) {
if ((rv = nni_ctx_find(&ctx, c.id)) != 0) {
return (rv);
}
// no release, close releases implicitly.
Expand All @@ -293,7 +293,7 @@ nng_ctx_recvmsg(nng_ctx cid, nng_msg **msgp, int flags)
nni_aio aio;
nni_ctx *ctx;

if ((rv = nni_ctx_find(&ctx, cid.id, false)) != 0) {
if ((rv = nni_ctx_find(&ctx, cid.id)) != 0) {
return (rv);
}

Expand Down Expand Up @@ -326,7 +326,7 @@ nng_ctx_recv(nng_ctx cid, nng_aio *aio)
int rv;
nni_ctx *ctx;

if ((rv = nni_ctx_find(&ctx, cid.id, false)) != 0) {
if ((rv = nni_ctx_find(&ctx, cid.id)) != 0) {
if (nni_aio_begin(aio) == 0) {
nni_aio_finish_error(aio, rv);
}
Expand All @@ -348,7 +348,7 @@ nng_ctx_send(nng_ctx cid, nng_aio *aio)
}
return;
}
if ((rv = nni_ctx_find(&ctx, cid.id, false)) != 0) {
if ((rv = nni_ctx_find(&ctx, cid.id)) != 0) {
if (nni_aio_begin(aio) == 0) {
nni_aio_finish_error(aio, rv);
}
Expand All @@ -368,7 +368,7 @@ nng_ctx_sendmsg(nng_ctx cid, nng_msg *msg, int flags)
if (msg == NULL) {
return (NNG_EINVAL);
}
if ((rv = nni_ctx_find(&ctx, cid.id, false)) != 0) {
if ((rv = nni_ctx_find(&ctx, cid.id)) != 0) {
return (rv);
}

Expand Down Expand Up @@ -403,7 +403,7 @@ ctx_get(nng_ctx id, const char *n, void *v, size_t *szp, nni_type t)
nni_ctx *ctx;
int rv;

if ((rv = nni_ctx_find(&ctx, id.id, false)) != 0) {
if ((rv = nni_ctx_find(&ctx, id.id)) != 0) {
return (rv);
}
rv = nni_ctx_getopt(ctx, n, v, szp, t);
Expand Down Expand Up @@ -447,7 +447,7 @@ ctx_set(nng_ctx id, const char *n, const void *v, size_t sz, nni_type t)
nni_ctx *ctx;
int rv;

if ((rv = nni_ctx_find(&ctx, id.id, false)) != 0) {
if ((rv = nni_ctx_find(&ctx, id.id)) != 0) {
return (rv);
}
rv = nni_ctx_setopt(ctx, n, v, sz, t);
Expand Down
4 changes: 2 additions & 2 deletions src/sp/protocol/pubsub0/sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ nng_sub0_ctx_subscribe(nng_ctx id, const void *buf, size_t sz)
nni_ctx *c;
sub0_ctx *ctx;

if ((rv = nni_ctx_find(&c, id.id, false)) != 0) {
if ((rv = nni_ctx_find(&c, id.id)) != 0) {
return (rv);
}
// validate the socket type
Expand All @@ -774,7 +774,7 @@ nng_sub0_ctx_unsubscribe(nng_ctx id, const void *buf, size_t sz)
nni_ctx *c;
sub0_ctx *ctx;

if ((rv = nni_ctx_find(&c, id.id, false)) != 0) {
if ((rv = nni_ctx_find(&c, id.id)) != 0) {
return (rv);
}
// validate the socket type
Expand Down

0 comments on commit 3dfa962

Please sign in to comment.