Skip to content

Commit c01ab7e

Browse files
committed
refactor saio struct
1 parent 65672dd commit c01ab7e

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/aio.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ SEXP rnng_aio_get_msg(SEXP env) {
209209
case RECVAIOS:
210210
case REQAIOS:
211211
case IOV_RECVAIOS: ;
212-
nano_cv *ncv = raio->type == REQAIOS ? (nano_cv *) ((nano_rsaio *) raio->next)->next :
213-
(nano_cv *) raio->next;
214-
nng_mtx *mtx = ncv->mtx;
212+
nng_mtx *mtx = ((nano_cv *) raio->next)->mtx;
215213
nng_mtx_lock(mtx);
216214
res = raio->result;
217215
nng_mtx_unlock(mtx);

src/nanonext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ typedef struct nano_aio_s {
184184
nano_aio_typ type;
185185
} nano_aio;
186186

187-
typedef struct nano_rsaio_s {
187+
typedef struct nano_saio_s {
188188
nng_aio *aio;
189-
void *next;
190-
} nano_rsaio;
189+
void *cb;
190+
} nano_saio;
191191

192192
typedef struct nano_cv_s {
193193
int condition;

src/sync.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ static void request_complete(void *arg) {
2929
raio->data = nng_aio_get_msg(raio->aio);
3030
raio->result = res - !res;
3131

32-
if (raio->cb != NULL)
33-
later2(raio_invoke_cb, raio->cb);
32+
nano_saio *saio = (nano_saio *) raio->cb;
33+
if (saio->cb != NULL)
34+
later2(raio_invoke_cb, saio->cb);
3435

3536
}
3637

@@ -46,15 +47,16 @@ static void request_complete_dropcon(void *arg) {
4647

4748
raio->result = res - !res;
4849

49-
if (raio->cb != NULL)
50-
later2(raio_invoke_cb, raio->cb);
50+
nano_saio *saio = (nano_saio *) raio->cb;
51+
if (saio->cb != NULL)
52+
later2(raio_invoke_cb, saio->cb);
5153

5254
}
5355

5456
static void request_complete_signal(void *arg) {
5557

5658
nano_aio *raio = (nano_aio *) arg;
57-
nano_cv *ncv = (nano_cv *) ((nano_rsaio *) raio->next)->next;
59+
nano_cv *ncv = (nano_cv *) raio->next;
5860
nng_cv *cv = ncv->cv;
5961
nng_mtx *mtx = ncv->mtx;
6062

@@ -68,14 +70,15 @@ static void request_complete_signal(void *arg) {
6870
nng_cv_wake(cv);
6971
nng_mtx_unlock(mtx);
7072

71-
if (raio->cb != NULL)
72-
later2(raio_invoke_cb, raio->cb);
73+
nano_saio *saio = (nano_saio *) raio->cb;
74+
if (saio->cb != NULL)
75+
later2(raio_invoke_cb, saio->cb);
7376

7477
}
7578

7679
static void sendaio_complete(void *arg) {
7780

78-
nng_aio *aio = ((nano_rsaio *) arg)->aio;
81+
nng_aio *aio = ((nano_saio *) arg)->aio;
7982
if (nng_aio_result(aio))
8083
nng_msg_free(nng_aio_get_msg(aio));
8184

@@ -180,14 +183,14 @@ static void request_finalizer(SEXP xptr) {
180183

181184
if (NANO_PTR(xptr) == NULL) return;
182185
nano_aio *xp = (nano_aio *) NANO_PTR(xptr);
183-
nano_rsaio *saio = (nano_rsaio *) xp->next;
186+
nano_saio *saio = (nano_saio *) xp->cb;
184187
nng_aio_free(saio->aio);
185188
nng_aio_free(xp->aio);
186189
if (xp->data != NULL)
187190
nng_msg_free((nng_msg *) xp->data);
188191
// release linked list node if cb has already run
189-
if (xp->cb != NULL && TAG((SEXP) xp->cb) == R_NilValue)
190-
nano_ReleaseObject((SEXP) xp->cb);
192+
if (saio->cb != NULL && TAG((SEXP) saio->cb) == R_NilValue)
193+
nano_ReleaseObject((SEXP) saio->cb);
191194
R_Free(saio);
192195
R_Free(xp);
193196

@@ -438,14 +441,14 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
438441

439442
SEXP aio, env, fun;
440443
nano_buf buf;
441-
nano_rsaio *saio;
444+
nano_saio *saio;
442445
nano_aio *raio;
443446
nng_msg *msg;
444447
int xc;
445448

446449
nano_encodes(sendmode) == 2 ? nano_encode(&buf, data) : nano_serialize(&buf, data, NANO_PROT(con));
447-
saio = R_Calloc(1, nano_rsaio);
448-
saio->next = ncv;
450+
saio = R_Calloc(1, nano_saio);
451+
saio->cb = NULL;
449452

450453
if ((xc = nng_msg_alloc(&msg, 0)))
451454
goto exitlevel1;
@@ -462,8 +465,8 @@ SEXP rnng_request(SEXP con, SEXP data, SEXP sendmode, SEXP recvmode, SEXP timeou
462465
raio = R_Calloc(1, nano_aio);
463466
raio->type = signal ? REQAIOS : REQAIO;
464467
raio->mode = mod;
465-
raio->cb = NULL;
466-
raio->next = saio;
468+
raio->cb = saio;
469+
raio->next = ncv;
467470

468471
if ((xc = nng_aio_alloc(&raio->aio, signal ? request_complete_signal : drop ? request_complete_dropcon : request_complete, raio)))
469472
goto exitlevel2;
@@ -518,6 +521,10 @@ SEXP rnng_create_promise(SEXP x, SEXP ctx) {
518521
switch (raio->type) {
519522
case REQAIO:
520523
case REQAIOS:
524+
NANO_SET_ENCLOS(x, ctx);
525+
nano_saio *saio = (nano_saio *) raio->cb;
526+
saio->cb = nano_PreserveObject(x);
527+
break;
521528
case RECVAIO:
522529
case RECVAIOS:
523530
case IOV_RECVAIO:

0 commit comments

Comments
 (0)