From d1a9c84a6b375cb25a8b7475957130e364b41753 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Mon, 2 Jul 2018 22:36:08 -0700 Subject: fixes #572 Several locking errors found fixes #573 atomic flags could help This introduces a new atomic flag, and reduces some of the global locking. The lock refactoring work is not yet complete, but this is a positive step forward, and should help with certain things. While here we also fixed a compile warning due to incorrect types. --- src/core/pipe.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'src/core/pipe.c') diff --git a/src/core/pipe.c b/src/core/pipe.c index a42cdeff..02f8ea50 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -29,7 +29,7 @@ struct nni_pipe { nni_listener * p_listener; nni_dialer * p_dialer; bool p_closed; - bool p_stop; + nni_atomic_flag p_stop; bool p_cbs; int p_refcnt; nni_mtx p_mtx; @@ -102,16 +102,11 @@ nni_pipe_sys_fini(void) void nni_pipe_destroy(nni_pipe *p) { - bool cbs; if (p == NULL) { return; } - nni_mtx_lock(&p->p_mtx); - cbs = p->p_cbs; - nni_mtx_unlock(&p->p_mtx); - - if (cbs) { + if (p->p_cbs) { nni_sock_run_pipe_cb(p->p_sock, NNG_PIPE_EV_REM_POST, p->p_id); } @@ -247,13 +242,9 @@ void nni_pipe_stop(nni_pipe *p) { // Guard against recursive calls. - nni_mtx_lock(&p->p_mtx); - if (p->p_stop) { - nni_mtx_unlock(&p->p_mtx); + if (nni_atomic_flag_test_and_set(&p->p_stop)) { return; } - p->p_stop = true; - nni_mtx_unlock(&p->p_mtx); nni_pipe_close(p); @@ -283,9 +274,7 @@ nni_pipe_start_cb(void *arg) return; } - nni_mtx_lock(&p->p_mtx); p->p_cbs = true; // We're running all cbs going forward - nni_mtx_unlock(&p->p_mtx); nni_sock_run_pipe_cb(s, NNG_PIPE_EV_ADD_PRE, id); if (nni_pipe_closed(p)) { @@ -324,10 +313,10 @@ nni_pipe_create2(nni_pipe **pp, nni_sock *sock, nni_tran *tran, void *tdata) p->p_proto_data = NULL; p->p_sock = sock; p->p_closed = false; - p->p_stop = false; p->p_cbs = false; p->p_refcnt = 0; + nni_atomic_flag_reset(&p->p_stop); NNI_LIST_NODE_INIT(&p->p_reap_node); NNI_LIST_NODE_INIT(&p->p_sock_node); NNI_LIST_NODE_INIT(&p->p_ep_node); -- cgit v1.2.3-70-g09d2