aboutsummaryrefslogtreecommitdiff
path: root/src/core/aio.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/aio.c')
-rw-r--r--src/core/aio.c65
1 files changed, 26 insertions, 39 deletions
diff --git a/src/core/aio.c b/src/core/aio.c
index c7d2b0b0..b69671b0 100644
--- a/src/core/aio.c
+++ b/src/core/aio.c
@@ -7,13 +7,13 @@
// found online at https://opensource.org/licenses/MIT.
//
-#include <string.h>
#include "core/nng_impl.h"
+#include <string.h>
-#define NNI_AIO_WAKE (1<<0)
-#define NNI_AIO_DONE (1<<1)
-#define NNI_AIO_FINI (1<<2)
-#define NNI_AIO_STOP (1<<3)
+#define NNI_AIO_WAKE (1 << 0)
+#define NNI_AIO_DONE (1 << 1)
+#define NNI_AIO_FINI (1 << 2)
+#define NNI_AIO_STOP (1 << 3)
int
nni_aio_init(nni_aio *aio, nni_cb cb, void *arg)
@@ -21,10 +21,10 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg)
int rv;
if (cb == NULL) {
- cb = (nni_cb) nni_aio_wake;
+ cb = (nni_cb) nni_aio_wake;
arg = aio;
}
- memset(aio, 0, sizeof (*aio));
+ memset(aio, 0, sizeof(*aio));
if ((rv = nni_mtx_init(&aio->a_lk)) != 0) {
return (rv);
}
@@ -32,16 +32,15 @@ nni_aio_init(nni_aio *aio, nni_cb cb, void *arg)
nni_mtx_fini(&aio->a_lk);
return (rv);
}
- aio->a_cb = cb;
- aio->a_cbarg = arg;
+ aio->a_cb = cb;
+ aio->a_cbarg = arg;
aio->a_expire = NNI_TIME_NEVER;
- aio->a_flags = 0;
+ aio->a_flags = 0;
nni_taskq_ent_init(&aio->a_tqe, cb, arg);
return (0);
}
-
void
nni_aio_fini(nni_aio *aio)
{
@@ -72,7 +71,6 @@ nni_aio_fini(nni_aio *aio)
}
}
-
int
nni_aio_result(nni_aio *aio)
{
@@ -80,21 +78,19 @@ nni_aio_result(nni_aio *aio)
nni_mtx_lock(&aio->a_lk);
rv = aio->a_result;
- if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) {
+ if (aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP)) {
rv = NNG_ECANCELED;
}
nni_mtx_unlock(&aio->a_lk);
return (rv);
}
-
size_t
nni_aio_count(nni_aio *aio)
{
return (aio->a_count);
}
-
void
nni_aio_wake(nni_aio *aio)
{
@@ -104,18 +100,16 @@ nni_aio_wake(nni_aio *aio)
nni_mtx_unlock(&aio->a_lk);
}
-
void
nni_aio_wait(nni_aio *aio)
{
nni_mtx_lock(&aio->a_lk);
- while ((aio->a_flags & (NNI_AIO_WAKE|NNI_AIO_FINI)) == 0) {
+ while ((aio->a_flags & (NNI_AIO_WAKE | NNI_AIO_FINI)) == 0) {
nni_cv_wait(&aio->a_cv);
}
nni_mtx_unlock(&aio->a_lk);
}
-
int
nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data)
{
@@ -123,28 +117,27 @@ nni_aio_start(nni_aio *aio, void (*cancel)(nni_aio *), void *data)
NNI_ASSERT(aio->a_prov_cancel == NULL);
nni_mtx_lock(&aio->a_lk);
- aio->a_flags &= ~(NNI_AIO_DONE|NNI_AIO_WAKE);
- if (aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP)) {
+ aio->a_flags &= ~(NNI_AIO_DONE | NNI_AIO_WAKE);
+ if (aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP)) {
// We should not reschedule anything at this point.
nni_mtx_unlock(&aio->a_lk);
return (NNG_ECANCELED);
}
- aio->a_result = 0;
- aio->a_count = 0;
+ aio->a_result = 0;
+ aio->a_count = 0;
aio->a_prov_cancel = cancel;
- aio->a_prov_data = data;
+ aio->a_prov_data = data;
nni_mtx_unlock(&aio->a_lk);
return (0);
}
-
void
nni_aio_stop(nni_aio *aio)
{
void (*cancelfn)(nni_aio *);
nni_mtx_lock(&aio->a_lk);
- aio->a_flags |= NNI_AIO_DONE|NNI_AIO_STOP;
+ aio->a_flags |= NNI_AIO_DONE | NNI_AIO_STOP;
cancelfn = aio->a_prov_cancel;
nni_mtx_unlock(&aio->a_lk);
@@ -154,7 +147,7 @@ nni_aio_stop(nni_aio *aio)
}
nni_mtx_lock(&aio->a_lk);
- aio->a_prov_data = NULL;
+ aio->a_prov_data = NULL;
aio->a_prov_cancel = NULL;
nni_cv_wake(&aio->a_cv);
nni_mtx_unlock(&aio->a_lk);
@@ -164,7 +157,6 @@ nni_aio_stop(nni_aio *aio)
nni_taskq_cancel(NULL, &aio->a_tqe);
}
-
void
nni_aio_cancel(nni_aio *aio)
{
@@ -179,7 +171,7 @@ nni_aio_cancel(nni_aio *aio)
}
aio->a_flags |= NNI_AIO_DONE;
aio->a_result = NNG_ECANCELED;
- cancelfn = aio->a_prov_cancel;
+ cancelfn = aio->a_prov_cancel;
nni_mtx_unlock(&aio->a_lk);
// This unregisters the AIO from the provider.
@@ -189,16 +181,15 @@ nni_aio_cancel(nni_aio *aio)
nni_mtx_lock(&aio->a_lk);
// These should have already been cleared by the cancel function.
- aio->a_prov_data = NULL;
+ aio->a_prov_data = NULL;
aio->a_prov_cancel = NULL;
- if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) {
+ if (!(aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP))) {
nni_taskq_dispatch(NULL, &aio->a_tqe);
}
nni_mtx_unlock(&aio->a_lk);
}
-
// I/O provider related functions.
void
@@ -211,25 +202,23 @@ nni_aio_finish(nni_aio *aio, int result, size_t count)
return;
}
aio->a_flags |= NNI_AIO_DONE;
- aio->a_result = result;
- aio->a_count = count;
+ aio->a_result = result;
+ aio->a_count = count;
aio->a_prov_cancel = NULL;
- aio->a_prov_data = NULL;
+ aio->a_prov_data = NULL;
- if (!(aio->a_flags & (NNI_AIO_FINI|NNI_AIO_STOP))) {
+ if (!(aio->a_flags & (NNI_AIO_FINI | NNI_AIO_STOP))) {
nni_taskq_dispatch(NULL, &aio->a_tqe);
}
nni_mtx_unlock(&aio->a_lk);
}
-
void
nni_aio_list_init(nni_list *list)
{
NNI_LIST_INIT(list, nni_aio, a_prov_node);
}
-
void
nni_aio_list_append(nni_list *list, nni_aio *aio)
{
@@ -237,14 +226,12 @@ nni_aio_list_append(nni_list *list, nni_aio *aio)
nni_list_append(list, aio);
}
-
void
nni_aio_list_remove(nni_aio *aio)
{
nni_list_node_remove(&aio->a_prov_node);
}
-
int
nni_aio_list_active(nni_aio *aio)
{