summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-01-17 19:57:56 -0800
committerGarrett D'Amore <garrett@damore.org>2017-01-17 19:57:56 -0800
commit5633a467a009945a4f1eb06f7ffe9f02b833567f (patch)
treee0010755849424d1bc2b43940f537b5757052ebf /src
parenta00f1938497e629187ebc6035e03bb58d1017730 (diff)
downloadnng-5633a467a009945a4f1eb06f7ffe9f02b833567f.tar.gz
nng-5633a467a009945a4f1eb06f7ffe9f02b833567f.tar.bz2
nng-5633a467a009945a4f1eb06f7ffe9f02b833567f.zip
Pipe IDs are now tracked on global ID hashes.
Diffstat (limited to 'src')
-rw-r--r--src/core/defs.h5
-rw-r--r--src/core/init.c23
-rw-r--r--src/core/init.h7
-rw-r--r--src/core/pipe.c8
-rw-r--r--src/core/socket.c16
-rw-r--r--src/core/socket.h2
-rw-r--r--src/core/thread.h14
7 files changed, 50 insertions, 25 deletions
diff --git a/src/core/defs.h b/src/core/defs.h
index 25b131d4..50540102 100644
--- a/src/core/defs.h
+++ b/src/core/defs.h
@@ -39,6 +39,11 @@ typedef struct nni_proto_sock_ops nni_proto_sock_ops;
typedef struct nni_proto_pipe_ops nni_proto_pipe_ops;
typedef struct nni_proto nni_proto;
+typedef struct nni_mtx nni_mtx;
+typedef struct nni_cv nni_cv;
+typedef struct nni_idhash nni_idhash;
+typedef struct nni_thr nni_thr;
+typedef void (*nni_thr_func)(void *);
typedef int nni_signal; // Wakeup channel.
typedef uint64_t nni_time; // Abs. time (usec).
diff --git a/src/core/init.c b/src/core/init.c
index f10173c8..e0ce3bae 100644
--- a/src/core/init.c
+++ b/src/core/init.c
@@ -11,6 +11,12 @@
#include <stdlib.h>
#include <stdio.h>
+nni_idhash *nni_endpoints;
+nni_idhash *nni_pipes;
+nni_idhash *nni_sockets;
+nni_mtx *nni_idlock;
+static nni_mtx nni_idlock_x;
+
static int
nni_init_helper(void)
{
@@ -19,6 +25,19 @@ nni_init_helper(void)
if ((rv = nni_random_init()) != 0) {
return (rv);
}
+ if ((rv = nni_mtx_init(&nni_idlock_x)) != 0) {
+ return (rv);
+ }
+ if (((rv = nni_idhash_create(&nni_endpoints)) != 0) ||
+ ((rv = nni_idhash_create(&nni_pipes)) != 0) ||
+ ((rv = nni_idhash_create(&nni_sockets)) != 0)) {
+ nni_mtx_fini(&nni_idlock_x);
+ nni_random_fini();
+ return (rv);
+ }
+ nni_idhash_set_limits(nni_pipes, 1, 0x7fffffff,
+ nni_random() & 0x7fffffff);
+ nni_idlock = &nni_idlock_x;
nni_tran_init();
return (0);
}
@@ -34,6 +53,10 @@ nni_init(void)
void
nni_fini(void)
{
+ nni_idhash_destroy(nni_endpoints);
+ nni_idhash_destroy(nni_pipes);
+ nni_idhash_destroy(nni_sockets);
+ nni_mtx_fini(&nni_idlock_x);
nni_tran_fini();
nni_random_fini();
nni_plat_fini();
diff --git a/src/core/init.h b/src/core/init.h
index 856faa3c..bae78fc3 100644
--- a/src/core/init.h
+++ b/src/core/init.h
@@ -21,4 +21,11 @@ extern int nni_init(void);
// that all resources used by the library are released back to the system.
extern void nni_fini(void);
+// Private hash tables matching IDs to values. Consumers need to use the
+// nni_idlock to protect access to these.
+nni_idhash *nni_endpoints;
+nni_idhash *nni_pipes;
+nni_idhash *nni_sockets;
+nni_mtx *nni_idlock;
+
#endif // CORE_INIT_H
diff --git a/src/core/pipe.c b/src/core/pipe.c
index 6cc93d98..a1a423e7 100644
--- a/src/core/pipe.c
+++ b/src/core/pipe.c
@@ -184,7 +184,9 @@ nni_pipe_start(nni_pipe *pipe)
return (NNG_EPROTO);
}
- rv = nni_idhash_alloc(sock->s_pipes_by_id, &pipe->p_id, pipe);
+ nni_mtx_lock(nni_idlock);
+ rv = nni_idhash_alloc(nni_pipes, &pipe->p_id, pipe);
+ nni_mtx_unlock(nni_idlock);
if (rv != 0) {
nni_pipe_bail(pipe);
nni_mtx_unlock(&sock->s_mx);
@@ -192,7 +194,9 @@ nni_pipe_start(nni_pipe *pipe)
}
if ((rv = sock->s_pipe_ops.pipe_add(pipe->p_proto_data)) != 0) {
- nni_idhash_remove(sock->s_pipes_by_id, pipe->p_id);
+ nni_mtx_lock(nni_idlock);
+ nni_idhash_remove(nni_pipes, pipe->p_id);
+ nni_mtx_unlock(nni_idlock);
pipe->p_id = 0;
nni_pipe_bail(pipe);
nni_mtx_unlock(&sock->s_mx);
diff --git a/src/core/socket.c b/src/core/socket.c
index e19485a5..a7dfd07f 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -48,8 +48,9 @@ nni_reaper(void *arg)
if ((pipe = nni_list_first(&sock->s_reaps)) != NULL) {
nni_list_remove(&sock->s_reaps, pipe);
if (pipe->p_id != 0) {
- nni_idhash_remove(sock->s_pipes_by_id,
- pipe->p_id);
+ nni_mtx_lock(nni_idlock);
+ nni_idhash_remove(nni_pipes, pipe->p_id);
+ nni_mtx_unlock(nni_idlock);
}
if (((ep = pipe->p_ep) != NULL) &&
@@ -236,15 +237,6 @@ nni_sock_open(nni_sock **sockp, uint16_t pnum)
goto fail;
}
- if ((rv = nni_idhash_create(&sock->s_pipes_by_id)) != 0) {
- goto fail;
- }
- // Pipe IDs are always positive values with the upper bit clear.
- // Start the IDs at a random place to minimize chances of PIPE ID
- // reuse improperly.
- nni_idhash_set_limits(sock->s_pipes_by_id, 1, 0x7FFFFFFF,
- nni_random() & 0x7FFFFFFF);
-
rv = nni_ev_init(&sock->s_recv_ev, NNG_EV_CAN_RECV, sock);
if (rv != 0) {
goto fail;
@@ -303,7 +295,6 @@ fail:
nni_ev_fini(&sock->s_recv_ev);
nni_msgq_fini(sock->s_urq);
nni_msgq_fini(sock->s_uwq);
- nni_idhash_destroy(sock->s_pipes_by_id);
nni_cv_fini(&sock->s_notify_cv);
nni_cv_fini(&sock->s_cv);
nni_mtx_fini(&sock->s_notify_mx);
@@ -442,7 +433,6 @@ nni_sock_close(nni_sock *sock)
nni_msgq_fini(sock->s_uwq);
nni_ev_fini(&sock->s_send_ev);
nni_ev_fini(&sock->s_recv_ev);
- nni_idhash_destroy(sock->s_pipes_by_id);
nni_cv_fini(&sock->s_notify_cv);
nni_cv_fini(&sock->s_cv);
nni_mtx_fini(&sock->s_notify_mx);
diff --git a/src/core/socket.h b/src/core/socket.h
index 2860ad46..424b1321 100644
--- a/src/core/socket.h
+++ b/src/core/socket.h
@@ -42,8 +42,6 @@ struct nng_socket {
nni_cv s_notify_cv; // wakes notify thread
nni_mtx s_notify_mx; // protects s_notify list
- nni_idhash * s_pipes_by_id; // pipes by id
-
nni_list s_reaps; // pipes to reap
nni_thr s_reaper;
nni_thr s_notifier;
diff --git a/src/core/thread.h b/src/core/thread.h
index 44f5c9e4..48da4bf0 100644
--- a/src/core/thread.h
+++ b/src/core/thread.h
@@ -12,17 +12,15 @@
#include "core/nng_impl.h"
-typedef struct {
+struct nni_mtx {
nni_plat_mtx mtx;
-} nni_mtx;
+};
-typedef struct {
+struct nni_cv {
nni_plat_cv cv;
-} nni_cv;
+};
-typedef void (*nni_thr_func)(void *);
-
-typedef struct {
+struct nni_thr {
nni_plat_thr thr;
nni_plat_mtx mtx;
nni_plat_cv cv;
@@ -32,7 +30,7 @@ typedef struct {
int stop;
int done;
int init;
-} nni_thr;
+};
// nni_mtx_init initializes the mutex. (Win32 programmers take note;
// our mutexes are actually CriticalSections on Win32.)