Skip to content

Commit 722bf46

Browse files
committed
fixes #1574 Non-blocking version of nng_aio_wait / nng_aio_result
This introduces a new API, nng_aio_busy(), that can be used to query the status of the aio without blocking. Some minor documentation fixes are included.
1 parent d989789 commit 722bf46

File tree

11 files changed

+124
-14
lines changed

11 files changed

+124
-14
lines changed

docs/man/libnng.3.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ The following functions are used in the asynchronous model:
164164
|xref:nng_aio_abort.3.adoc[nng_aio_abort()]|abort asynchronous I/O operation
165165
|xref:nng_aio_alloc.3.adoc[nng_aio_alloc()]|allocate asynchronous I/O handle
166166
|xref:nng_aio_begin.3.adoc[nng_aio_begin()]|begin asynchronous I/O operation
167+
|xref:nng_aio_busy.3.adoc[nng_aio_busy()]|test if asynchronous I/O is busy
167168
|xref:nng_aio_cancel.3.adoc[nng_aio_cancel()]|cancel asynchronous I/O operation
168169
|xref:nng_aio_count.3.adoc[nng_aio_count()]|return number of bytes transferred
169170
|xref:nng_aio_defer.3.adoc[nng_aio_defer()]|defer asynchronous I/O operation

docs/man/nng_aio_alloc.3.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= nng_aio_alloc(3)
22
//
3-
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
3+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
44
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
55
//
66
// This document is supplied under the terms of the MIT License, a
@@ -37,7 +37,7 @@ This also means you should avoid operations such as allocating new objects,
3737
which also means opening or closing sockets, dialers, and so forth.
3838

3939
TIP: If more complex or blocking work needs to be performed by _callb_, a separate
40-
thread can be used, along with a xref:nng_cv_alloc.3.adoc[condition variable]
40+
thread can be used, along with a xref:nng_cv_alloc.3supp.adoc[condition variable]
4141
which can be signaled by the callback.
4242

4343
Asynchronous I/O operations all take an xref:nng_aio.5.adoc[`nng_aio`]

docs/man/nng_aio_busy.3.adoc

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
= nng_aio_busy(3)
2+
//
3+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
4+
//
5+
// This document is supplied under the terms of the MIT License, a
6+
// copy of which should be located in the distribution where this
7+
// file was obtained (LICENSE.txt). A copy of the license may also be
8+
// found online at https://opensource.org/licenses/MIT.
9+
//
10+
11+
== NAME
12+
13+
nng_aio_busy - test if asynchronous I/O is busy
14+
15+
== SYNOPSIS
16+
17+
[source, c]
18+
----
19+
#include <nng/nng.h>
20+
21+
bool nng_aio_busy(nng_aio *aio);
22+
----
23+
24+
== DESCRIPTION
25+
26+
The `nng_aio_busy()` function returns true if the
27+
_aio_ is currently busy performing an asynchronous I/O
28+
operation or is executing a completion callback.
29+
30+
If no operation has been started, or the operation has
31+
been completed or canceled, and any callback has been
32+
executed, then it returns false.
33+
34+
This is the same test used internally by
35+
xref:nng_aio_wait.3.adoc[`nng_aio_wait()`]
36+
37+
NOTE: Care should be taken to ensure that the _aio_ object is not
38+
freed when using this function. The caller is responsible for
39+
coordinating any use of this with any reuse of the _aio_.
40+
41+
== RETURN VALUES
42+
43+
True if the _aio_ is busy, false otherwise.
44+
45+
== ERRORS
46+
47+
None.
48+
49+
== SEE ALSO
50+
51+
[.text-left]
52+
xref:nng_aio_abort.3.adoc[nng_aio_abort(3)],
53+
xref:nng_aio_alloc.3.adoc[nng_aio_alloc(3)],
54+
xref:nng_aio_wait.3.adoc[nng_aio_wait(3)],
55+
xref:nng_aio.5.adoc[nng_aio(5)],
56+
xref:nng.7.adoc[nng(7)]

docs/man/nng_aio_wait.3.adoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= nng_aio_wait(3)
22
//
3-
// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
3+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
44
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
55
//
66
// This document is supplied under the terms of the MIT License, a
@@ -29,7 +29,7 @@ to complete.
2929
If the operation has not been started, or has already
3030
completed, then it returns immediately.
3131

32-
If the a callback was set with _aio_ when it was allocated, then this
32+
If a callback was set with _aio_ when it was allocated, then this
3333
function will not be called until the callback has completed.
3434

3535
== RETURN VALUES
@@ -45,5 +45,6 @@ None.
4545
[.text-left]
4646
xref:nng_aio_abort.3.adoc[nng_aio_abort(3)],
4747
xref:nng_aio_alloc.3.adoc[nng_aio_alloc(3)],
48+
xref:nng_aio_busy.3.adoc[nng_aio_busy(3)],
4849
xref:nng_aio.5.adoc[nng_aio(5)],
4950
xref:nng.7.adoc[nng(7)]

include/nng/nng.h

+6
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ NNG_DECL void nng_aio_abort(nng_aio *, int);
544544
// callback or deadlock may occur.
545545
NNG_DECL void nng_aio_wait(nng_aio *);
546546

547+
// nng_aio_busy returns true if the aio is still busy processing the
548+
// operation, or executing associated completion functions. Note that
549+
// if the completion function schedules a new operation using the aio,
550+
// then this function will continue to return true.
551+
NNG_DECL bool nng_aio_busy(nng_aio *);
552+
547553
// nng_aio_set_msg sets the message structure to use for asynchronous
548554
// message send operations.
549555
NNG_DECL void nng_aio_set_msg(nng_aio *, nng_msg *);

src/core/aio.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
2+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
33
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
44
//
55
// This software is supplied under the terms of the MIT License, a
@@ -314,6 +314,12 @@ nni_aio_wait(nni_aio *aio)
314314
nni_task_wait(&aio->a_task);
315315
}
316316

317+
bool
318+
nni_aio_busy(nni_aio *aio)
319+
{
320+
return (nni_task_busy(&aio->a_task));
321+
}
322+
317323
int
318324
nni_aio_begin(nni_aio *aio)
319325
{

src/core/aio.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
2+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
33
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
44
//
55
// This software is supplied under the terms of the MIT License, a
@@ -93,6 +93,10 @@ extern int nni_aio_result(nni_aio *);
9393
// completed.
9494
extern size_t nni_aio_count(nni_aio *);
9595

96+
// nni_aio_busy returns true if the aio is still busy processing work.
97+
// This is a non-blocking form of the check used by nni_aio_wait().
98+
extern bool nni_aio_busy(nni_aio *);
99+
96100
// nni_aio_wait blocks the caller until the operation is complete.
97101
// The operation must have already been started. This routine will
98102
// block until the AIO, as well as any callback, has completed execution.

src/core/aio_test.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
2+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
33
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
44
//
55
// This software is supplied under the terms of the MIT License, a
@@ -364,6 +364,18 @@ test_sleep_cancel(void)
364364
nng_mtx_free(sl.mx);
365365
}
366366

367+
void
368+
test_aio_busy(void)
369+
{
370+
nng_aio *aio;
371+
NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
372+
nng_sleep_aio(100, aio);
373+
NUTS_ASSERT(nng_aio_busy(aio));
374+
nng_aio_wait(aio);
375+
NUTS_ASSERT(!nng_aio_busy(aio));
376+
nng_aio_free(aio);
377+
}
378+
367379
NUTS_TESTS = {
368380
{ "sleep", test_sleep },
369381
{ "sleep timeout", test_sleep_timeout },
@@ -377,5 +389,6 @@ NUTS_TESTS = {
377389
{ "aio reap", test_aio_reap },
378390
{ "sleep loop", test_sleep_loop },
379391
{ "sleep cancel", test_sleep_cancel },
392+
{ "aio busy", test_aio_busy },
380393
{ NULL, NULL },
381394
};

src/core/taskq.c

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
2+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
33
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
44
//
55
// This software is supplied under the terms of the MIT License, a
@@ -31,12 +31,12 @@ static void
3131
nni_taskq_thread(void *self)
3232
{
3333
nni_taskq_thr *thr = self;
34-
nni_taskq * tq = thr->tqt_tq;
35-
nni_task * task;
34+
nni_taskq *tq = thr->tqt_tq;
35+
nni_task *task;
3636

37-
nni_thr_set_name(NULL, "nng:task");
37+
nni_thr_set_name(NULL, "nng:task");
3838

39-
nni_mtx_lock(&tq->tq_mtx);
39+
nni_mtx_lock(&tq->tq_mtx);
4040
for (;;) {
4141
if ((task = nni_list_first(&tq->tq_tasks)) != NULL) {
4242

@@ -207,6 +207,16 @@ nni_task_wait(nni_task *task)
207207
nni_mtx_unlock(&task->task_mtx);
208208
}
209209

210+
bool
211+
nni_task_busy(nni_task *task)
212+
{
213+
bool busy;
214+
nni_mtx_lock(&task->task_mtx);
215+
busy = task->task_busy;
216+
nni_mtx_unlock(&task->task_mtx);
217+
return (busy);
218+
}
219+
210220
void
211221
nni_task_init(nni_task *task, nni_taskq *tq, nni_cb cb, void *arg)
212222
{

src/core/taskq.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
2+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
33
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
44
//
55
// This software is supplied under the terms of the MIT License, a
@@ -43,6 +43,13 @@ extern void nni_task_prep(nni_task *);
4343
// returns an error.
4444
extern void nni_task_abort(nni_task *);
4545

46+
// nni_task_busy checks to see if a task is still busy.
47+
// This is uses the same check that nni_task_wait uses.
48+
extern bool nni_task_busy(nni_task *);
49+
50+
// nni_task_wait waits for the task to complete. If additional
51+
// work is scheduled on the task then it will not return until that
52+
// work (or any other work subsequently scheduled) is complete.
4653
extern void nni_task_wait(nni_task *);
4754
extern void nni_task_init(nni_task *, nni_taskq *, nni_cb, void *);
4855

src/nng.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
2+
// Copyright 2022 Staysail Systems, Inc. <info@staysail.tech>
33
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
44
//
55
// This software is supplied under the terms of the MIT License, a
@@ -1883,6 +1883,12 @@ nng_aio_wait(nng_aio *aio)
18831883
nni_aio_wait(aio);
18841884
}
18851885

1886+
bool
1887+
nng_aio_busy(nng_aio *aio)
1888+
{
1889+
return (nni_aio_busy(aio));
1890+
}
1891+
18861892
void
18871893
nng_aio_abort(nng_aio *aio, int err_code)
18881894
{

0 commit comments

Comments
 (0)