aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-08-15 09:43:27 +0500
committerGarrett D'Amore <garrett@damore.org>2018-08-15 09:43:27 +0500
commit56354bd08cd6549cbaf348cae62f9db01786afc4 (patch)
tree48cc5171615d2d1a2ca71084fb7e0d6ee65977f9 /src
parenta1f485bba51c56305537f3308a777035ea7c514d (diff)
downloadnng-56354bd08cd6549cbaf348cae62f9db01786afc4.tar.gz
nng-56354bd08cd6549cbaf348cae62f9db01786afc4.tar.bz2
nng-56354bd08cd6549cbaf348cae62f9db01786afc4.zip
fixes #654 use aio for synchronous connect
Diffstat (limited to 'src')
-rw-r--r--src/core/dialer.c51
-rw-r--r--src/core/sockimpl.h4
2 files changed, 28 insertions, 27 deletions
diff --git a/src/core/dialer.c b/src/core/dialer.c
index 4b2f105d..45d5150e 100644
--- a/src/core/dialer.c
+++ b/src/core/dialer.c
@@ -65,7 +65,6 @@ nni_dialer_destroy(nni_dialer *d)
if (d->d_data != NULL) {
d->d_ops.d_fini(d->d_data);
}
- nni_cv_fini(&d->d_cv);
nni_mtx_fini(&d->d_mtx);
nni_url_free(d->d_url);
NNI_FREE_STRUCT(d);
@@ -110,7 +109,6 @@ nni_dialer_create(nni_dialer **dp, nni_sock *s, const char *urlstr)
NNI_LIST_INIT(&d->d_pipes, nni_pipe, p_ep_node);
nni_mtx_init(&d->d_mtx);
- nni_cv_init(&d->d_cv, &d->d_mtx);
if (((rv = nni_aio_init(&d->d_con_aio, dialer_connect_cb, d)) != 0) ||
((rv = nni_aio_init(&d->d_tmo_aio, dialer_timer_cb, d)) != 0) ||
@@ -234,11 +232,12 @@ dialer_connect_cb(void *arg)
{
nni_dialer *d = arg;
nni_aio * aio = d->d_con_aio;
+ nni_aio * uaio;
int rv;
- bool synch;
nni_mtx_lock(&d->d_mtx);
- synch = d->d_synch;
+ uaio = d->d_user_aio;
+ d->d_user_aio = NULL;
nni_mtx_unlock(&d->d_mtx);
switch ((rv = nni_aio_result(aio))) {
@@ -249,17 +248,15 @@ dialer_connect_cb(void *arg)
case NNG_ECANCELED: // No further action.
break;
default:
- if (!synch) {
+ if (uaio == NULL) {
nni_dialer_timer_start(d);
+ } else {
+ nni_atomic_flag_reset(&d->d_started);
}
break;
}
- if (synch) {
- nni_mtx_lock(&d->d_mtx);
- d->d_synch = false;
- d->d_lastrv = rv;
- nni_cv_wake(&d->d_cv);
- nni_mtx_unlock(&d->d_mtx);
+ if (uaio != NULL) {
+ nni_aio_finish(uaio, rv, 0);
}
}
@@ -268,43 +265,49 @@ dialer_connect_start(nni_dialer *d)
{
nni_aio *aio = d->d_con_aio;
- // Call with the Endpoint lock held.
d->d_ops.d_connect(d->d_data, aio);
}
int
nni_dialer_start(nni_dialer *d, int flags)
{
- int rv = 0;
+ int rv = 0;
+ nni_aio *aio;
if (nni_atomic_flag_test_and_set(&d->d_started)) {
return (NNG_ESTATE);
}
if ((flags & NNG_FLAG_NONBLOCK) != 0) {
+ aio = NULL;
+ } else {
+ if ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) {
+ nni_atomic_flag_reset(&d->d_started);
+ return (rv);
+ }
+ nni_aio_begin(aio);
+ }
+#if 0
+ if ((flags & NNG_FLAG_NONBLOCK) != 0) {
nni_mtx_lock(&d->d_mtx);
d->d_currtime = d->d_inirtime;
nni_mtx_unlock(&d->d_mtx);
dialer_connect_start(d);
return (0);
}
+#endif
nni_mtx_lock(&d->d_mtx);
- d->d_synch = true;
- nni_mtx_unlock(&d->d_mtx);
-
+ d->d_user_aio = aio;
dialer_connect_start(d);
-
- nni_mtx_lock(&d->d_mtx);
- while (d->d_synch) {
- nni_cv_wait(&d->d_cv);
- }
- rv = d->d_lastrv;
nni_mtx_unlock(&d->d_mtx);
- if (rv != 0) {
- nni_atomic_flag_reset(&d->d_started);
+ if (aio != NULL) {
+ nni_aio_wait(aio);
+ rv = nni_aio_result(aio);
+ nni_aio_fini(aio);
}
+
return (rv);
}
diff --git a/src/core/sockimpl.h b/src/core/sockimpl.h
index 29e83f7a..be454d8a 100644
--- a/src/core/sockimpl.h
+++ b/src/core/sockimpl.h
@@ -25,14 +25,12 @@ struct nni_dialer {
nni_url * d_url;
nni_pipe * d_pipe; // active pipe (for redialer)
int d_refcnt;
- int d_lastrv; // last result from synchronous
- bool d_synch; // synchronous connect in progress?
bool d_closed; // full shutdown
bool d_closing;
nni_atomic_flag d_started;
nni_mtx d_mtx;
- nni_cv d_cv;
nni_list d_pipes;
+ nni_aio * d_user_aio;
nni_aio * d_con_aio;
nni_aio * d_tmo_aio; // backoff timer
nni_duration d_maxrtime; // maximum time for reconnect