Skip to content

Commit

Permalink
more efficient aio structure; remove legacy nng compat; requires nng …
Browse files Browse the repository at this point in the history
…>= 1.6.0
  • Loading branch information
shikokuchuo committed Apr 18, 2024
1 parent 3233638 commit b8ed236
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 152 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ License: GPL (>= 3)
BugReports: /~https://github.com/shikokuchuo/nanonext/issues
URL: https://shikokuchuo.net/nanonext/, /~https://github.com/shikokuchuo/nanonext/
Encoding: UTF-8
SystemRequirements: 'libnng' >= 1.5 and 'libmbedtls' >= 2.5, or 'cmake' to
SystemRequirements: 'libnng' >= 1.6 and 'libmbedtls' >= 2.5, or 'cmake' to
compile NNG and/or Mbed TLS included in package sources
Depends:
R (>= 3.5)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Integrates with the `later` package to provide the foundation for truly event-driven (non-polling) promises (thanks @jcheng5 for the initial prototype in #28), where side-effects are enacted asynchronously upon aio completion.
* Adds `request2()` for creating a request that may be turned into an event-driven promise.
* Updates minimum 'libnng' version requirement to v1.6.0 (if a suitable system-installed version is not found, the bundled version is compiled from source).

# nanonext 0.13.6

Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ vignette("nanonext", package = "nanonext")

#### Linux / Mac / Solaris

Installation from source requires 'libnng' >= v1.5.0 and 'libmbedtls' >= 2.5.0 (suitable installations are automatically detected), or else 'cmake' to compile 'libnng' v1.7.3 and 'libmbedtls' v3.6.0 LTS included within the package sources.
Installation from source requires 'libnng' >= v1.6.0 and 'libmbedtls' >= 2.5.0 (suitable installations are automatically detected), or else 'cmake' to compile 'libnng' v1.7.3 and 'libmbedtls' v3.6.0 LTS included within the package sources.

**It is recommended for optimal performance and stability to let the package automatically compile bundled versions of 'libmbedtls' and 'libnng' during installation.** To ensure the libraries are compiled from source even if system installations are present, set the `NANONEXT_LIBS` environment variable prior to installation e.g. by `Sys.setenv(NANONEXT_LIBS = 1)`.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ vignette("nanonext", package = "nanonext")

#### Linux / Mac / Solaris

Installation from source requires ‘libnng’ \>= v1.5.0 and ‘libmbedtls’
Installation from source requires ‘libnng’ \>= v1.6.0 and ‘libmbedtls’
\>= 2.5.0 (suitable installations are automatically detected), or else
‘cmake’ to compile ‘libnng’ v1.7.3 and ‘libmbedtls’ v3.6.0 LTS included
within the package sources.
Expand Down
4 changes: 2 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fi

echo "#include <nng/nng.h>
int main() {
#if NNG_MAJOR_VERSION < 1 || NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 5
#if NNG_MAJOR_VERSION < 1 || NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 6
*(void *) 0 = 0;
#endif
}" | ${CC} ${NNG_CFLAGS} -xc - -o /dev/null > /dev/null 2>&1
Expand All @@ -140,7 +140,7 @@ fi

if [ $? -ne 0 ]
then
echo "No existing 'libnng' >= 1.5 found"
echo "No existing 'libnng' >= 1.6 found"
echo "Detecting 'cmake'..."
which cmake
if [ $? -ne 0 ]
Expand Down
83 changes: 6 additions & 77 deletions src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,7 @@ static void saio_complete(void *arg) {
const int res = nng_aio_result(saio->aio);
if (res)
nng_msg_free(nng_aio_get_msg(saio->aio));

#ifdef NANONEXT_LEGACY_NNG
nng_mtx_lock(shr_mtx);
saio->result = res - !res;
nng_mtx_unlock(shr_mtx);
#else
saio->result = res - !res;
#endif

}

Expand All @@ -185,14 +178,7 @@ static void isaio_complete(void *arg) {
const int res = nng_aio_result(iaio->aio);
if (iaio->data != NULL)
R_Free(iaio->data);

#ifdef NANONEXT_LEGACY_NNG
nng_mtx_lock(shr_mtx);
iaio->result = res - !res;
nng_mtx_unlock(shr_mtx);
#else
iaio->result = res - !res;
#endif

}

Expand All @@ -203,13 +189,7 @@ static void raio_complete(void *arg) {
if (res == 0)
raio->data = nng_aio_get_msg(raio->aio);

#ifdef NANONEXT_LEGACY_NNG
nng_mtx_lock(shr_mtx);
raio->result = res - !res;
nng_mtx_unlock(shr_mtx);
#else
raio->result = res - !res;
#endif

}

Expand Down Expand Up @@ -271,17 +251,11 @@ static void raio_complete_cb(void *arg) {
const int res = nng_aio_result(raio->aio);
if (res == 0)
raio->data = nng_aio_get_msg(raio->aio);

#ifdef NANONEXT_LEGACY_NNG
nng_mtx_lock(shr_mtx);
raio->result = res - !res;
nng_mtx_unlock(shr_mtx);
#else
raio->result = res - !res;
#endif

if (CADR(ATTRIB(raio->cb)) != R_NilValue)
later2(raio_invoke_cb, raio->cb, 0);
nano_aio *saio = (nano_aio *) raio->next;
if (CADR(ATTRIB((SEXP) saio->data)) != R_NilValue)
later2(raio_invoke_cb, saio->data, 0);

}

Expand All @@ -303,23 +277,16 @@ static void request_complete_cb(void *arg) {
nng_cv_wake(cv);
nng_mtx_unlock(mtx);

if (CADR(ATTRIB(raio->cb)) != R_NilValue)
later2(raio_invoke_cb, raio->cb, 0);
if (CADR(ATTRIB((SEXP) saio->data)) != R_NilValue)
later2(raio_invoke_cb, saio->data, 0);

}

static void iraio_complete(void *arg) {

nano_aio *iaio = (nano_aio *) arg;
const int res = nng_aio_result(iaio->aio);

#ifdef NANONEXT_LEGACY_NNG
nng_mtx_lock(shr_mtx);
iaio->result = res - !res;
nng_mtx_unlock(shr_mtx);
#else
iaio->result = res - !res;
#endif

}

Expand Down Expand Up @@ -367,9 +334,6 @@ static void request_finalizer(SEXP xptr) {
if (R_ExternalPtrAddr(xptr) == NULL) return;
nano_aio *xp = (nano_aio *) R_ExternalPtrAddr(xptr);
nano_aio *saio = (nano_aio *) xp->next;
#ifdef NANONEXT_LEGACY_NNG
nng_ctx_close(*(nng_ctx *) saio->data);
#endif
nng_aio_free(saio->aio);
nng_aio_free(xp->aio);
if (xp->data != NULL)
Expand Down Expand Up @@ -447,15 +411,7 @@ SEXP rnng_aio_result(SEXP env) {

nano_aio *saio = (nano_aio *) R_ExternalPtrAddr(aio);

#ifdef NANONEXT_LEGACY_NNG
int res;
nng_mtx_lock(shr_mtx);
res = saio->result;
nng_mtx_unlock(shr_mtx);
if (res == 0)
#else
if (nng_aio_busy(saio->aio))
#endif
return nano_unresolved;

if (saio->result > 0)
Expand All @@ -479,15 +435,7 @@ SEXP rnng_aio_get_msg(SEXP env) {

nano_aio *raio = (nano_aio *) R_ExternalPtrAddr(aio);

#ifdef NANONEXT_LEGACY_NNG
int res;
nng_mtx_lock(shr_mtx);
res = raio->result;
nng_mtx_unlock(shr_mtx);
if (res == 0)
#else
if (nng_aio_busy(raio->aio))
#endif
return nano_unresolved;

if (raio->result > 0)
Expand Down Expand Up @@ -652,15 +600,7 @@ SEXP rnng_unresolved2(SEXP aio) {

nano_aio *aiop = (nano_aio *) R_ExternalPtrAddr(coreaio);

#ifdef NANONEXT_LEGACY_NNG
int res;
nng_mtx_lock(shr_mtx);
res = aiop->result;
nng_mtx_unlock(shr_mtx);
return Rf_ScalarLogical(!res);
#else
return Rf_ScalarLogical(nng_aio_busy(aiop->aio));
#endif

}

Expand Down Expand Up @@ -1004,15 +944,7 @@ SEXP rnng_aio_http(SEXP env, SEXP response, SEXP type) {

nano_aio *haio = (nano_aio *) R_ExternalPtrAddr(aio);

#ifdef NANONEXT_LEGACY_NNG
int res;
nng_mtx_lock(shr_mtx);
res = haio->result;
nng_mtx_unlock(shr_mtx);
if (res == 0)
#else
if (nng_aio_busy(haio->aio))
#endif
return nano_unresolved;

if (haio->result > 0)
Expand Down Expand Up @@ -1292,9 +1224,6 @@ SEXP rnng_request_impl(const SEXP con, const SEXP data, const SEXP sendmode,
}

saio = R_Calloc(1, nano_aio);
#ifdef NANONEXT_LEGACY_NNG
saio->data = ctx;
#endif
saio->next = ncv;

if ((xc = nng_msg_alloc(&msg, 0)))
Expand All @@ -1316,7 +1245,7 @@ SEXP rnng_request_impl(const SEXP con, const SEXP data, const SEXP sendmode,
raio->next = saio;
if (promises) {
R_PreserveObject(env);
raio->cb = env;
saio->data = env;
}

if ((xc = nng_aio_alloc(&raio->aio,
Expand Down
52 changes: 0 additions & 52 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,30 +982,6 @@ SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block) {
nng_ctx *ctxp = (nng_ctx *) R_ExternalPtrAddr(con);
nng_msg *msgp;

#ifdef NANONEXT_LEGACY_NNG

nng_aio *aiop;

if ((xc = nng_msg_alloc(&msgp, 0)))
goto exitlevel1;

if ((xc = nng_msg_append(msgp, buf.buf, buf.cur)) ||
(xc = nng_aio_alloc(&aiop, NULL, NULL))) {
nng_msg_free(msgp);
goto exitlevel1;
}

nng_aio_set_msg(aiop, msgp);
nng_aio_set_timeout(aiop, flags < 0 ? 0 : flags > 0 ? flags : (*NANO_INTEGER(block) == 1) * NNG_DURATION_DEFAULT);
nng_ctx_send(*ctxp, aiop);
NANO_FREE(buf);
nng_aio_wait(aiop);
if ((xc = nng_aio_result(aiop)))
nng_msg_free(nng_aio_get_msg(aiop));
nng_aio_free(aiop);

#else

if (flags <= 0) {

if ((xc = nng_msg_alloc(&msgp, 0)))
Expand Down Expand Up @@ -1043,8 +1019,6 @@ SEXP rnng_send(SEXP con, SEXP data, SEXP mode, SEXP block) {

}

#endif

} else if (ptrtag == nano_StreamSymbol) {

nano_encode(&buf, data);
Expand Down Expand Up @@ -1136,30 +1110,6 @@ SEXP rnng_recv(SEXP con, SEXP mode, SEXP block, SEXP bytes) {
nng_ctx *ctxp = (nng_ctx *) R_ExternalPtrAddr(con);
nng_msg *msgp;

#ifdef NANONEXT_LEGACY_NNG

nng_aio *aiop;

if ((xc = nng_aio_alloc(&aiop, NULL, NULL)))
goto exitlevel1;
nng_aio_set_timeout(aiop, flags < 0 ? 0 : flags > 0 ? flags : (*NANO_INTEGER(block) == 1) * NNG_DURATION_DEFAULT);
nng_ctx_recv(*ctxp, aiop);

nng_aio_wait(aiop);
if ((xc = nng_aio_result(aiop))) {
nng_aio_free(aiop);
goto exitlevel1;
}

msgp = nng_aio_get_msg(aiop);
nng_aio_free(aiop);
buf = nng_msg_body(msgp);
sz = nng_msg_len(msgp);
res = nano_decode(buf, sz, mod);
nng_msg_free(msgp);

#else

if (flags <= 0) {

xc = nng_ctx_recvmsg(*ctxp, &msgp, (flags < 0 || *NANO_INTEGER(block) != 1) * NNG_FLAG_NONBLOCK);
Expand Down Expand Up @@ -1195,8 +1145,6 @@ SEXP rnng_recv(SEXP con, SEXP mode, SEXP block, SEXP bytes) {

}

#endif

} else if (ptrtag == nano_StreamSymbol) {

mod = nano_matchargs(mode);
Expand Down
10 changes: 0 additions & 10 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ SEXP nano_refHook;
SEXP nano_success;
SEXP nano_unresolved;

#ifdef NANONEXT_LEGACY_NNG
nng_mtx *shr_mtx;
#endif

static void RegisterSymbols(void) {
nano_AioSymbol = Rf_install("aio");
nano_ContextSymbol = Rf_install("context");
Expand Down Expand Up @@ -193,17 +189,11 @@ static const R_ExternalMethodDef externalMethods[] = {
void attribute_visible R_init_nanonext(DllInfo* dll) {
RegisterSymbols();
PreserveObjects();
#ifdef NANONEXT_LEGACY_NNG
nng_mtx_alloc(&shr_mtx);
#endif
R_registerRoutines(dll, NULL, callMethods, NULL, externalMethods);
R_useDynamicSymbols(dll, FALSE);
R_forceSymbols(dll, TRUE);
}

void attribute_visible R_unload_nanonext(DllInfo *info) {
ReleaseObjects();
#ifdef NANONEXT_LEGACY_NNG
nng_mtx_free(shr_mtx);
#endif
}
8 changes: 0 additions & 8 deletions src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

#include <nng/nng.h>

#if NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 6
#define NANONEXT_LEGACY_NNG
#endif

#ifdef NANONEXT_PROTOCOLS
#include <nng/protocol/bus0/bus.h>
#include <nng/protocol/pair0/pair.h>
Expand All @@ -45,9 +41,6 @@
#ifdef NANONEXT_SUPPLEMENTALS
#include <nng/supplemental/tls/tls.h>
#include <nng/supplemental/util/platform.h>
#ifdef NANONEXT_LEGACY_NNG
extern nng_mtx *shr_mtx;
#endif

typedef struct nano_listener_s {
nng_listener list;
Expand Down Expand Up @@ -88,7 +81,6 @@ typedef struct nano_aio_s {
int result;
void *data;
void *next;
void *cb;
} nano_aio;

typedef struct nano_cv_s {
Expand Down

0 comments on commit b8ed236

Please sign in to comment.