aboutsummaryrefslogtreecommitdiff
path: root/src/protocol/pair1
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-08-15 14:09:17 -0700
committerGarrett D'Amore <garrett@damore.org>2020-08-16 23:07:35 -0700
commit4f5e11c391c4a8f1b2731aee5ad47bc0c925042a (patch)
tree640aef66eb7e0030a2833bc9bba3246edb29d074 /src/protocol/pair1
parent750662d4aab305d8a3d48bfa6edfc4dac4018881 (diff)
downloadnng-4f5e11c391c4a8f1b2731aee5ad47bc0c925042a.tar.gz
nng-4f5e11c391c4a8f1b2731aee5ad47bc0c925042a.tar.bz2
nng-4f5e11c391c4a8f1b2731aee5ad47bc0c925042a.zip
fixes #1289 zerotier should have it's own copy of the id hashing code
fixes #1288 id allocation can overallocate fixes #1126 consider removing lock from idhash This substantially refactors the id hash code, giving a cleaner API, and eliminating a extra locking as well as some wasteful allocations. The ZeroTier code has it's own copy, that is 64-bit friendly, as the rest of the consumers need only a simpler 32-bit API.
Diffstat (limited to 'src/protocol/pair1')
-rw-r--r--src/protocol/pair1/pair.c15
-rw-r--r--src/protocol/pair1/pair1_poly.c14
2 files changed, 12 insertions, 17 deletions
diff --git a/src/protocol/pair1/pair.c b/src/protocol/pair1/pair.c
index 00959a4c..0492119a 100644
--- a/src/protocol/pair1/pair.c
+++ b/src/protocol/pair1/pair.c
@@ -34,7 +34,7 @@ struct pair1_sock {
bool raw;
nni_atomic_int ttl;
nni_mtx mtx;
- nni_idhash * pipes;
+ nni_id_map pipes;
nni_list plist;
bool started;
nni_stat_item stat_poly;
@@ -66,7 +66,7 @@ pair1_sock_fini(void *arg)
{
pair1_sock *s = arg;
- nni_idhash_fini(s->pipes);
+ nni_id_map_fini(&s->pipes);
nni_mtx_fini(&s->mtx);
}
@@ -75,9 +75,7 @@ pair1_sock_init_impl(void *arg, nni_sock *sock, bool raw)
{
pair1_sock *s = arg;
- if (nni_idhash_init(&s->pipes) != 0) {
- return (NNG_ENOMEM);
- }
+ nni_id_map_init(&s->pipes, 0, 0, false);
NNI_LIST_INIT(&s->plist, pair1_pipe, node);
// Raw mode uses this.
@@ -199,12 +197,12 @@ pair1_pipe_start(void *arg)
}
id = nni_pipe_id(p->pipe);
- if ((rv = nni_idhash_insert(s->pipes, id, p)) != 0) {
+ if ((rv = nni_id_set(&s->pipes, id, p)) != 0) {
nni_mtx_unlock(&s->mtx);
return (rv);
}
if (!nni_list_empty(&s->plist)) {
- nni_idhash_remove(s->pipes, id);
+ nni_id_remove(&s->pipes, id);
nni_mtx_unlock(&s->mtx);
BUMP_STAT(&s->stat_reject_already);
return (NNG_EBUSY);
@@ -234,7 +232,7 @@ pair1_pipe_close(void *arg)
nni_aio_close(&p->aio_get);
nni_mtx_lock(&s->mtx);
- nni_idhash_remove(s->pipes, nni_pipe_id(p->pipe));
+ nni_id_remove(&s->pipes, nni_pipe_id(p->pipe));
nni_list_node_remove(&p->node);
nni_mtx_unlock(&s->mtx);
}
@@ -400,7 +398,6 @@ pair1_sock_get_max_ttl(void *arg, void *buf, size_t *szp, nni_opt_type t)
return (nni_copyout_int(nni_atomic_get(&s->ttl), buf, szp, t));
}
-
#ifdef NNG_TEST_LIB
static int
pair1_set_test_inject_header(void *arg, const void *buf, size_t sz, nni_type t)
diff --git a/src/protocol/pair1/pair1_poly.c b/src/protocol/pair1/pair1_poly.c
index 950c60f7..fc1bbf6a 100644
--- a/src/protocol/pair1/pair1_poly.c
+++ b/src/protocol/pair1/pair1_poly.c
@@ -40,7 +40,7 @@ struct pair1poly_sock {
nni_sock * sock;
nni_atomic_int ttl;
nni_mtx mtx;
- nni_idhash * pipes;
+ nni_id_map pipes;
nni_list plist;
bool started;
nni_aio aio_get;
@@ -72,7 +72,7 @@ pair1poly_sock_fini(void *arg)
pair1poly_sock *s = arg;
nni_aio_fini(&s->aio_get);
- nni_idhash_fini(s->pipes);
+ nni_id_map_fini(&s->pipes);
nni_mtx_fini(&s->mtx);
}
@@ -81,9 +81,7 @@ pair1poly_sock_init(void *arg, nni_sock *sock)
{
pair1poly_sock *s = arg;
- if (nni_idhash_init(&s->pipes) != 0) {
- return (NNG_ENOMEM);
- }
+ nni_id_map_init(&s->pipes, 0, 0, false);
NNI_LIST_INIT(&s->plist, pair1poly_pipe, node);
// Raw mode uses this.
@@ -196,7 +194,7 @@ pair1poly_pipe_start(void *arg)
}
id = nni_pipe_id(p->pipe);
- if ((rv = nni_idhash_insert(s->pipes, id, p)) != 0) {
+ if ((rv = nni_id_set(&s->pipes, id, p)) != 0) {
nni_mtx_unlock(&s->mtx);
return (rv);
}
@@ -231,7 +229,7 @@ pair1poly_pipe_close(void *arg)
nni_aio_close(&p->aio_get);
nni_mtx_lock(&s->mtx);
- nni_idhash_remove(s->pipes, nni_pipe_id(p->pipe));
+ nni_id_remove(&s->pipes, nni_pipe_id(p->pipe));
nni_list_node_remove(&p->node);
nni_mtx_unlock(&s->mtx);
@@ -311,7 +309,7 @@ pair1poly_sock_get_cb(void *arg)
(!nni_list_empty(&s->plist))) {
p = nni_list_first(&s->plist);
} else {
- nni_idhash_find(s->pipes, id, (void **) &p);
+ p = nni_id_get(&s->pipes, id);
}
// Try a non-blocking send. If this fails we just discard the