From 69190eb962f6a74be3d693f584320e42a79462d1 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Fri, 8 Jun 2018 17:08:03 -0700 Subject: fixes #511 Want to be able to have deferred destroy of tasks and aios Essentially, if we're destroying an aio, and we are doing so from the thread that is running the callback, then we should defer the destruction of the task until it returns. Note that calling nni_aio_wait() or anything else that calls it from the callback is still verboten and will result in a single party deadlock. --- src/core/aio.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/core/aio.c') diff --git a/src/core/aio.c b/src/core/aio.c index 99da356a..87755b1b 100644 --- a/src/core/aio.c +++ b/src/core/aio.c @@ -130,7 +130,21 @@ void nni_aio_fini(nni_aio *aio) { if (aio != NULL) { - nni_aio_stop(aio); + + nni_aio_cancelfn cancelfn; + + // This is like aio_close, but we don't want to dispatch + // the task. And unlike aio_stop, we don't want to wait + // for the task. (Because we implicitly do task_fini.) + nni_mtx_lock(&nni_aio_lk); + cancelfn = aio->a_prov_cancel; + aio->a_prov_cancel = NULL; + aio->a_stop = true; + nni_mtx_unlock(&nni_aio_lk); + + if (cancelfn != NULL) { + cancelfn(aio, NNG_ECLOSED); + } // Wait for the aio to be "done"; this ensures that we don't // destroy an aio from a "normal" completion callback while @@ -138,6 +152,10 @@ nni_aio_fini(nni_aio *aio) nni_mtx_lock(&nni_aio_lk); while (nni_aio_expire_aio == aio) { + if (nni_thr_is_self(&nni_aio_expire_thr)) { + nni_aio_expire_aio = NULL; + break; + } nni_cv_wait(&nni_aio_expire_cv); } nni_mtx_unlock(&nni_aio_lk); -- cgit v1.2.3-70-g09d2