Skip to content

Commit

Permalink
streamline nextmode
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Nov 10, 2023
1 parent 8f361e6 commit e3783b3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
31 changes: 22 additions & 9 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ SEXP rawToChar(unsigned char *buf, const size_t sz) {

static SEXP nano_inHook(SEXP x, SEXP fun) {

if (TYPEOF(x) != EXTPTRSXP) return R_NilValue;
if (TYPEOF(x) != EXTPTRSXP)
return R_NilValue;
SEXP refList, list, names, out;
R_xlen_t xlen;
refList = nano_refList;
Expand Down Expand Up @@ -215,7 +216,7 @@ void nano_serialize_next(nano_buf *buf, SEXP object) {
NANONEXT_SERIAL_VER,
nano_write_char,
nano_write_bytes,
CAR(nano_refHook) != R_NilValue ? nano_inHook : NULL,
nano_refHookIn != R_NilValue ? nano_inHook : NULL,
R_NilValue
);

Expand All @@ -225,7 +226,7 @@ void nano_serialize_next(nano_buf *buf, SEXP object) {

if (nano_refList != R_NilValue) {
SEXP call, out;
PROTECT(call = Rf_lcons(CAR(nano_refHook), Rf_cons(nano_refList, R_NilValue)));
PROTECT(call = Rf_lcons(nano_refHookIn, Rf_cons(nano_refList, R_NilValue)));
PROTECT(out = Rf_eval(call, R_GlobalEnv));
if (TYPEOF(out) != RAWSXP) {
R_ReleaseObject(nano_refList);
Expand Down Expand Up @@ -280,7 +281,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
if (sz > offset) {
PROTECT(raw = Rf_allocVector(RAWSXP, sz - offset));
memcpy(STDVEC_DATAPTR(raw), buf + offset, sz - offset);
PROTECT(call = Rf_lcons(CADR(nano_refHook), Rf_cons(raw, R_NilValue)));
PROTECT(call = Rf_lcons(nano_refHookOut, Rf_cons(raw, R_NilValue)));
nano_refList = Rf_eval(call, R_GlobalEnv);
if (TYPEOF(nano_refList) != VECSXP) {
nano_refList = R_NilValue;
Expand Down Expand Up @@ -1431,20 +1432,32 @@ SEXP rnng_next_mode(SEXP infun, SEXP outfun, SEXP mark) {
case CLOSXP:
case BUILTINSXP:
case SPECIALSXP:
case NILSXP:
SETCAR(nano_refHook, infun);
if (nano_refHookIn != R_NilValue)
R_ReleaseObject(nano_refHookIn);
R_PreserveObject(nano_refHookIn = infun);
break;
case NILSXP:
if (nano_refHookIn != R_NilValue) {
R_ReleaseObject(nano_refHookIn);
nano_refHookIn = R_NilValue;
}
}

switch(TYPEOF(outfun)) {
case CLOSXP:
case BUILTINSXP:
case SPECIALSXP:
case NILSXP:
SETCADR(nano_refHook, outfun);
if (nano_refHookOut != R_NilValue)
R_ReleaseObject(nano_refHookOut);
R_PreserveObject(nano_refHookOut = outfun);
break;
case NILSXP:
if (nano_refHookOut != R_NilValue) {
R_ReleaseObject(nano_refHookOut);
nano_refHookOut = R_NilValue;
}
}

return Rf_shallow_duplicate(nano_refHook);
return Rf_cons(nano_refHookIn, Rf_cons(nano_refHookOut, R_NilValue));

}
15 changes: 11 additions & 4 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ SEXP nano_error;
SEXP nano_ncurlAio;
SEXP nano_ncurlSession;
SEXP nano_recvAio;
SEXP nano_refHook;
SEXP nano_refHookIn;
SEXP nano_refHookOut;
SEXP nano_refList;
SEXP nano_sendAio;
SEXP nano_success;
Expand Down Expand Up @@ -106,26 +107,33 @@ static void PreserveObjects(void) {
SET_TAG(nano_ncurlSession, R_ClassSymbol);
R_PreserveObject(nano_recvAio = Rf_cons(Rf_mkString("recvAio"), R_NilValue));
SET_TAG(nano_recvAio, R_ClassSymbol);
R_PreserveObject(nano_refHook = Rf_list2(R_NilValue, R_NilValue));
R_PreserveObject(nano_sendAio = Rf_cons(Rf_mkString("sendAio"), R_NilValue));
SET_TAG(nano_sendAio, R_ClassSymbol);
R_PreserveObject(nano_success = Rf_ScalarInteger(0));
R_PreserveObject(nano_unresolved = Rf_shallow_duplicate(Rf_ScalarLogical(NA_LOGICAL)));
Rf_classgets(nano_unresolved, Rf_mkString("unresolvedValue"));
nano_refList = R_NilValue;
nano_refHookIn = R_NilValue;
nano_refHookOut = R_NilValue;
}

static void ReleaseObjects(void) {
R_ReleaseObject(nano_unresolved);
R_ReleaseObject(nano_success);
R_ReleaseObject(nano_sendAio);
R_ReleaseObject(nano_refHook);
R_ReleaseObject(nano_recvAio);
R_ReleaseObject(nano_ncurlSession);
R_ReleaseObject(nano_ncurlAio);
R_ReleaseObject(nano_error);
R_ReleaseObject(nano_aioNFuncs);
R_ReleaseObject(nano_aioFuncs);
R_ReleaseObject(nano_aioFormals);
if (nano_refList != R_NilValue)
R_ReleaseObject(nano_refList);
if (nano_refHookIn != R_NilValue)
R_ReleaseObject(nano_refHookIn);
if (nano_refHookOut != R_NilValue)
R_ReleaseObject(nano_refHookOut);
}

static const R_CMethodDef cMethods[] = {
Expand Down Expand Up @@ -218,7 +226,6 @@ static const R_ExternalMethodDef externalMethods[] = {
void attribute_visible R_init_nanonext(DllInfo* dll) {
RegisterSymbols();
PreserveObjects();
nano_refList = R_NilValue;
#if NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 6
nng_mtx_alloc(&shr_mtx);
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/nanonext.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ extern SEXP nano_error;
extern SEXP nano_ncurlAio;
extern SEXP nano_ncurlSession;
extern SEXP nano_recvAio;
extern SEXP nano_refHook;
extern SEXP nano_refHookIn;
extern SEXP nano_refHookOut;
extern SEXP nano_refList;
extern SEXP nano_sendAio;
extern SEXP nano_success;
Expand Down
4 changes: 2 additions & 2 deletions src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ SEXP rnng_wait_thread_create(SEXP aio) {
nng_mtx *mtx;
nng_cv *cv;

int xc, signalled = 1;
int xc, signalled;

if ((xc = nng_mtx_alloc(&mtx))) {
R_Free(ncv);
Expand All @@ -246,6 +246,7 @@ SEXP rnng_wait_thread_create(SEXP aio) {
R_RegisterCFinalizerEx(xptr, thread_aio_finalizer, TRUE);

while (1) {
signalled = 1;
nng_mtx_lock(mtx);
while (ncv->condition == 0) {
if (nng_cv_until(cv, 2000) == NNG_ETIMEDOUT) {
Expand All @@ -256,7 +257,6 @@ SEXP rnng_wait_thread_create(SEXP aio) {
nng_mtx_unlock(mtx);
if (signalled) break;
R_CheckUserInterrupt();
signalled = 1;
}

switch (aiop->type) {
Expand Down

0 comments on commit e3783b3

Please sign in to comment.