aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_tcpconn.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-01-05 11:16:03 -0800
committerGarrett D'Amore <garrett@damore.org>2020-01-05 13:22:32 -0800
commit1eaf9e86a8f54d77d6f392829d1b859c94965329 (patch)
tree2efa5ea0befd760b9011989639f9572a58a55f03 /src/platform/posix/posix_tcpconn.c
parent36ff88911f8c4a0859457b0fc511333965163c82 (diff)
downloadnng-1eaf9e86a8f54d77d6f392829d1b859c94965329.tar.gz
nng-1eaf9e86a8f54d77d6f392829d1b859c94965329.tar.bz2
nng-1eaf9e86a8f54d77d6f392829d1b859c94965329.zip
fixes #1112 POSIX pollq finalizers could be simpler
We reap the connections when closing, to ensure that the clean up is done outside the pollq thread. This also reduces pressure on the pollq, we think. But more importantly it eliminates some complex code that was meant to avoid deadlocks, but ultimately created other use-after-free challenges. This work is an enabler for further simplifications in the aio/task logic. While here we converted some potentially racy locking of the dialers and reference counts to simpler lock-free reference counting.
Diffstat (limited to 'src/platform/posix/posix_tcpconn.c')
-rw-r--r--src/platform/posix/posix_tcpconn.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/platform/posix/posix_tcpconn.c b/src/platform/posix/posix_tcpconn.c
index 625fd7fd..2a209984 100644
--- a/src/platform/posix/posix_tcpconn.c
+++ b/src/platform/posix/posix_tcpconn.c
@@ -1,5 +1,5 @@
//
-// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
@@ -173,7 +173,9 @@ tcp_error(void *arg, int err)
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, err);
}
- nni_posix_pfd_close(c->pfd);
+ if (c->pfd != NULL) {
+ nni_posix_pfd_close(c->pfd);
+ }
nni_mtx_unlock(&c->mtx);
}
@@ -190,7 +192,9 @@ tcp_close(void *arg)
nni_aio_list_remove(aio);
nni_aio_finish_error(aio, NNG_ECLOSED);
}
- nni_posix_pfd_close(c->pfd);
+ if (c->pfd != NULL) {
+ nni_posix_pfd_close(c->pfd);
+ }
}
nni_mtx_unlock(&c->mtx);
}
@@ -202,10 +206,9 @@ tcp_fini(void *arg)
{
nni_tcp_conn *c = arg;
tcp_close(c);
- nni_posix_pfd_fini(c->pfd);
- nni_mtx_lock(&c->mtx); // not strictly needed, but shut up TSAN
- c->pfd = NULL;
- nni_mtx_unlock(&c->mtx);
+ if (c->pfd != NULL) {
+ nni_posix_pfd_fini(c->pfd);
+ }
nni_mtx_fini(&c->mtx);
if (c->dialer != NULL) {
@@ -474,16 +477,15 @@ tcp_setx(void *arg, const char *name, const void *buf, size_t sz, nni_type t)
}
int
-nni_posix_tcp_init(nni_tcp_conn **cp, nni_posix_pfd *pfd)
+nni_posix_tcp_alloc(nni_tcp_conn **cp, nni_tcp_dialer *d)
{
nni_tcp_conn *c;
-
if ((c = NNI_ALLOC_STRUCT(c)) == NULL) {
return (NNG_ENOMEM);
}
c->closed = false;
- c->pfd = pfd;
+ c->dialer = d;
nni_mtx_init(&c->mtx);
nni_aio_list_init(&c->readq);
@@ -501,6 +503,12 @@ nni_posix_tcp_init(nni_tcp_conn **cp, nni_posix_pfd *pfd)
}
void
+nni_posix_tcp_init(nni_tcp_conn *c, nni_posix_pfd *pfd)
+{
+ c->pfd = pfd;
+}
+
+void
nni_posix_tcp_start(nni_tcp_conn *c, int nodelay, int keepalive)
{
// Configure the initial socket options.