aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/aio.c19
-rw-r--r--src/core/aio.h2
-rw-r--r--src/nng.c6
-rw-r--r--src/nng.h5
4 files changed, 31 insertions, 1 deletions
diff --git a/src/core/aio.c b/src/core/aio.c
index dd5edf0e..e0206830 100644
--- a/src/core/aio.c
+++ b/src/core/aio.c
@@ -70,6 +70,7 @@ struct nng_aio {
unsigned a_expiring : 1; // expiration callback in progress
unsigned a_waiting : 1; // a thread is waiting for this to finish
unsigned a_synch : 1; // run completion synchronously
+ unsigned a_sleep : 1; // sleeping with no action
nni_task a_task;
// Read/write operations.
@@ -375,6 +376,7 @@ nni_aio_finish_impl(nni_aio *aio, int rv, size_t count, nni_msg *msg)
}
aio->a_expire = NNI_TIME_NEVER;
+ aio->a_sleep = 0;
// If we are expiring, then we rely on the expiration thread to
// complete this; we must not because the expiration thread is
@@ -471,6 +473,7 @@ nni_aio_expire_loop(void *arg)
nni_aio * aio;
nni_time now;
nni_aio_cancelfn cancelfn;
+ int rv;
NNI_ARG_UNUSED(arg);
@@ -506,14 +509,18 @@ nni_aio_expire_loop(void *arg)
// and the done flag won't be set either.
aio->a_expiring = 1;
cancelfn = aio->a_prov_cancel;
+ rv = aio->a_sleep ? 0 : NNG_ETIMEDOUT;
+ aio->a_sleep = 0;
// Cancel any outstanding activity. This is always non-NULL
// for a valid aio, and becomes NULL only when an AIO is
// already being canceled or finished.
if (cancelfn != NULL) {
nni_mtx_unlock(&nni_aio_lk);
- cancelfn(aio, NNG_ETIMEDOUT);
+ cancelfn(aio, aio->a_sleep ? 0 : NNG_ETIMEDOUT);
nni_mtx_lock(&nni_aio_lk);
+ } else {
+ aio->a_pend = 1;
}
NNI_ASSERT(aio->a_pend); // nni_aio_finish was run
@@ -612,6 +619,16 @@ nni_aio_iov_advance(nni_aio *aio, size_t n)
}
void
+nni_sleep_aio(nng_duration ms, nng_aio *aio)
+{
+ nni_aio_set_timeout(aio, ms);
+ aio->a_sleep = 1;
+ if (nni_aio_start(aio, NULL, NULL)) {
+ return;
+ }
+}
+
+void
nni_aio_sys_fini(void)
{
nni_mtx *mtx = &nni_aio_lk;
diff --git a/src/core/aio.h b/src/core/aio.h
index 141248b8..49ef5298 100644
--- a/src/core/aio.h
+++ b/src/core/aio.h
@@ -151,6 +151,8 @@ extern void nni_aio_get_iov(nni_aio *, unsigned *, nni_iov **);
extern void nni_aio_normalize_timeout(nni_aio *, nng_duration);
extern void nni_aio_bump_count(nni_aio *, size_t);
+extern void nni_sleep_aio(nni_duration, nni_aio *);
+
extern int nni_aio_sys_init(void);
extern void nni_aio_sys_fini(void);
#endif // CORE_AIO_H
diff --git a/src/nng.c b/src/nng.c
index 19b33220..4c2a58db 100644
--- a/src/nng.c
+++ b/src/nng.c
@@ -1039,6 +1039,12 @@ nng_aio_free(nng_aio *aio)
nni_aio_fini(aio);
}
+void
+nng_sleep_aio(nng_duration ms, nng_aio *aio)
+{
+ nni_sleep_aio(ms, aio);
+}
+
int
nng_aio_result(nng_aio *aio)
{
diff --git a/src/nng.h b/src/nng.h
index 7615785d..b7adee93 100644
--- a/src/nng.h
+++ b/src/nng.h
@@ -352,6 +352,11 @@ NNG_DECL int nng_aio_set_iov(nng_aio *, unsigned, const nng_iov *);
// given aio.
NNG_DECL void nng_aio_finish(nng_aio *, int);
+// nng_aio_sleep does a "sleeping" operation, basically does nothing
+// but wait for the specified number of milliseconds to expire, then
+// calls the callback. This returns 0, rather than NNG_ETIMEDOUT.
+NNG_DECL void nng_sleep_aio(nng_duration, nng_aio *);
+
// Message API.
NNG_DECL int nng_msg_alloc(nng_msg **, size_t);
NNG_DECL void nng_msg_free(nng_msg *);