| 1 |
|
| 2 |
|
| 3 |
#define NANONEXT_PROTOCOLS |
| 4 |
#define NANONEXT_IO |
| 5 |
#include "nanonext.h" |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
static nng_mtx *nano_wait_mtx = NULL; |
| 10 |
static nng_cv *nano_wait_cv = NULL; |
| 11 |
static nng_thread *nano_wait_thr = NULL; |
| 12 |
static nng_aio *nano_shared_aio = NULL; |
| 13 |
static int nano_wait_condition = 0; |
| 14 |
|
| 15 |
void nano_thread_shutdown(void) { |
| 16 |
if (nano_wait_thr == NULL) |
| 17 |
return; |
| 18 |
if (nano_shared_aio != NULL) |
| 19 |
nng_aio_stop(nano_shared_aio); |
| 20 |
nng_mtx_lock(nano_wait_mtx); |
| 21 |
nano_wait_condition = -1; |
| 22 |
nng_cv_wake(nano_wait_cv); |
| 23 |
nng_mtx_unlock(nano_wait_mtx); |
| 24 |
nng_thread_destroy(nano_wait_thr); |
| 25 |
nng_cv_free(nano_wait_cv); |
| 26 |
nng_mtx_free(nano_wait_mtx); |
| 27 |
nano_wait_thr = NULL; |
| 28 |
} |
| 29 |
|
| 30 |
static void thread_finalizer(SEXP xptr) { |
| 31 |
|
| 32 |
if (NANO_PTR(xptr) == NULL) return; |
| 33 |
nng_thread *xp = (nng_thread *) NANO_PTR(xptr); |
| 34 |
nng_thread_destroy(xp); |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static void thread_aio_finalizer(SEXP xptr) { |
| 42 |
|
| 43 |
if (NANO_PTR(xptr) == NULL) return; |
| 44 |
nano_thread_aio *xp = (nano_thread_aio *) NANO_PTR(xptr); |
| 45 |
nano_cv *ncv = xp->cv; |
| 46 |
nng_mtx *mtx = ncv->mtx; |
| 47 |
nng_cv *cv = ncv->cv; |
| 48 |
nng_aio_stop(xp->aio); |
| 49 |
nng_thread_destroy(xp->thr); |
| 50 |
nng_cv_free(cv); |
| 51 |
nng_mtx_free(mtx); |
| 52 |
free(ncv); |
| 53 |
free(xp); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
static void rnng_wait_thread_single(void *args) { |
| 59 |
|
| 60 |
nano_thread_aio *taio = (nano_thread_aio *) args; |
| 61 |
nano_cv *ncv = taio->cv; |
| 62 |
nng_mtx *mtx = ncv->mtx; |
| 63 |
nng_cv *cv = ncv->cv; |
| 64 |
|
| 65 |
nng_aio_wait(taio->aio); |
| 66 |
|
| 67 |
nng_mtx_lock(mtx); |
| 68 |
ncv->condition = 1; |
| 69 |
nng_cv_wake(cv); |
| 70 |
nng_mtx_unlock(mtx); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
void single_wait_thread_create(SEXP x) { |
| 75 |
|
| 76 |
nano_aio *aiop = (nano_aio *) NANO_PTR(x); |
| 77 |
|
| 78 |
int xc, signalled; |
| 79 |
nano_thread_aio *taio = NULL; |
| 80 |
nano_cv *ncv = NULL; |
| 81 |
|
| 82 |
taio = malloc(sizeof(nano_thread_aio)); |
| 83 |
NANO_ENSURE_ALLOC(taio); |
| 84 |
ncv = calloc(1, sizeof(nano_cv)); |
| 85 |
NANO_ENSURE_ALLOC(ncv); |
| 86 |
taio->aio = aiop->aio; |
| 87 |
taio->cv = ncv; |
| 88 |
nng_mtx *mtx = NULL; |
| 89 |
nng_cv *cv = NULL; |
| 90 |
|
| 91 |
if ((xc = nng_mtx_alloc(&mtx)) || |
| 92 |
(xc = nng_cv_alloc(&cv, mtx))) |
| 93 |
goto fail; |
| 94 |
|
| 95 |
ncv->mtx = mtx; |
| 96 |
ncv->cv = cv; |
| 97 |
|
| 98 |
if ((xc = nng_thread_create(&taio->thr, rnng_wait_thread_single, taio))) |
| 99 |
goto fail; |
| 100 |
|
| 101 |
SEXP xptr; |
| 102 |
PROTECT(xptr = R_MakeExternalPtr(taio, R_NilValue, R_NilValue)); |
| 103 |
R_RegisterCFinalizerEx(xptr, thread_aio_finalizer, TRUE); |
| 104 |
R_MakeWeakRef(x, xptr, R_NilValue, TRUE); |
| 105 |
UNPROTECT(1); |
| 106 |
|
| 107 |
nng_time time = nng_clock(); |
| 108 |
|
| 109 |
while (1) { |
| 110 |
time = time + 400; |
| 111 |
signalled = 1; |
| 112 |
nng_mtx_lock(mtx); |
| 113 |
while (ncv->condition == 0) { |
| 114 |
if (nng_cv_until(cv, time) == NNG_ETIMEDOUT) { |
| 115 |
signalled = 0; |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
nng_mtx_unlock(mtx); |
| 120 |
if (signalled) break; |
| 121 |
R_CheckUserInterrupt(); |
| 122 |
} |
| 123 |
|
| 124 |
return; |
| 125 |
|
| 126 |
fail: |
| 127 |
nng_cv_free(cv); |
| 128 |
nng_mtx_free(mtx); |
| 129 |
failmem: |
| 130 |
free(ncv); |
| 131 |
free(taio); |
| 132 |
ERROR_OUT(xc); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static void thread_duo_finalizer(SEXP xptr) { |
| 139 |
|
| 140 |
if (NANO_PTR(xptr) == NULL) return; |
| 141 |
nano_thread_duo *xp = (nano_thread_duo *) NANO_PTR(xptr); |
| 142 |
nano_cv *ncv = xp->cv; |
| 143 |
if (ncv != NULL) { |
| 144 |
nng_mtx *mtx = ncv->mtx; |
| 145 |
nng_cv *cv = ncv->cv; |
| 146 |
nng_mtx_lock(mtx); |
| 147 |
ncv->condition = -1; |
| 148 |
nng_cv_wake(cv); |
| 149 |
nng_mtx_unlock(mtx); |
| 150 |
} |
| 151 |
nng_thread_destroy(xp->thr); |
| 152 |
free(xp); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
static void rnng_wait_thread(void *args) { |
| 157 |
|
| 158 |
while (1) { |
| 159 |
nng_mtx_lock(nano_wait_mtx); |
| 160 |
while (nano_wait_condition == 0) |
| 161 |
nng_cv_wait(nano_wait_cv); |
| 162 |
if (nano_wait_condition == -1) { |
| 163 |
nng_mtx_unlock(nano_wait_mtx); |
| 164 |
break; |
| 165 |
} |
| 166 |
nng_mtx_unlock(nano_wait_mtx); |
| 167 |
|
| 168 |
nng_aio_wait(nano_shared_aio); |
| 169 |
|
| 170 |
nng_mtx_lock(nano_wait_mtx); |
| 171 |
nano_shared_aio = NULL; |
| 172 |
nano_wait_condition = 0; |
| 173 |
nng_cv_wake(nano_wait_cv); |
| 174 |
nng_mtx_unlock(nano_wait_mtx); |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
SEXP rnng_wait_thread_create(SEXP x) { |
| 180 |
|
| 181 |
const SEXPTYPE typ = TYPEOF(x); |
| 182 |
if (typ == ENVSXP) { |
| 183 |
|
| 184 |
const SEXP coreaio = nano_findVarInFrame(x, nano_AioSymbol, NULL); |
| 185 |
if (NANO_PTR_CHECK(coreaio, nano_AioSymbol)) |
| 186 |
return x; |
| 187 |
|
| 188 |
nano_aio *aiop = (nano_aio *) NANO_PTR(coreaio); |
| 189 |
|
| 190 |
int xc, signalled; |
| 191 |
|
| 192 |
if (nano_wait_thr == NULL) { |
| 193 |
if ((xc = nng_mtx_alloc(&nano_wait_mtx)) || |
| 194 |
(xc = nng_cv_alloc(&nano_wait_cv, nano_wait_mtx)) || |
| 195 |
(xc = nng_thread_create(&nano_wait_thr, rnng_wait_thread, NULL))) |
| 196 |
goto fail; |
| 197 |
} |
| 198 |
|
| 199 |
int thread_required = 0; |
| 200 |
nng_mtx_lock(nano_wait_mtx); |
| 201 |
if (nano_wait_condition == 0) { |
| 202 |
nano_shared_aio = aiop->aio; |
| 203 |
nano_wait_condition = 1; |
| 204 |
nng_cv_wake(nano_wait_cv); |
| 205 |
} else { |
| 206 |
thread_required = nano_shared_aio != aiop->aio; |
| 207 |
} |
| 208 |
nng_mtx_unlock(nano_wait_mtx); |
| 209 |
|
| 210 |
if (thread_required) { |
| 211 |
|
| 212 |
PROTECT(coreaio); |
| 213 |
single_wait_thread_create(coreaio); |
| 214 |
UNPROTECT(1); |
| 215 |
|
| 216 |
} else { |
| 217 |
|
| 218 |
nng_time time = nng_clock(); |
| 219 |
|
| 220 |
while (1) { |
| 221 |
time = time + 400; |
| 222 |
signalled = 1; |
| 223 |
nng_mtx_lock(nano_wait_mtx); |
| 224 |
while (nano_wait_condition == 1) { |
| 225 |
if (nng_cv_until(nano_wait_cv, time) == NNG_ETIMEDOUT) { |
| 226 |
signalled = 0; |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
nng_mtx_unlock(nano_wait_mtx); |
| 231 |
if (signalled) break; |
| 232 |
R_CheckUserInterrupt(); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
switch (aiop->type) { |
| 238 |
case RECVAIO: |
| 239 |
case REQAIO: |
| 240 |
case IOV_RECVAIO: |
| 241 |
case RECVAIOS: |
| 242 |
case REQAIOS: |
| 243 |
case IOV_RECVAIOS: |
| 244 |
nano_aio_get_msg(x); |
| 245 |
break; |
| 246 |
case SENDAIO: |
| 247 |
case IOV_SENDAIO: |
| 248 |
nano_aio_result(x); |
| 249 |
break; |
| 250 |
case HTTP_AIO: |
| 251 |
nano_aio_http_status(x); |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
return x; |
| 256 |
|
| 257 |
fail: |
| 258 |
nng_cv_free(nano_wait_cv); |
| 259 |
nng_mtx_free(nano_wait_mtx); |
| 260 |
ERROR_OUT(xc); |
| 261 |
|
| 262 |
} else if (typ == VECSXP) { |
| 263 |
|
| 264 |
const R_xlen_t xlen = Rf_xlength(x); |
| 265 |
const SEXP *xp = VECTOR_PTR_RO(x); |
| 266 |
for (R_xlen_t i = 0; i < xlen; i++) { |
| 267 |
rnng_wait_thread_create(xp[i]); |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
return x; |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
static void rnng_signal_thread(void *args) { |
| 277 |
|
| 278 |
nano_thread_duo *duo = (nano_thread_duo *) args; |
| 279 |
nano_cv *ncv = duo->cv; |
| 280 |
nng_mtx *mtx = ncv->mtx; |
| 281 |
nng_cv *cv = ncv->cv; |
| 282 |
nano_cv *ncv2 = duo->cv2; |
| 283 |
nng_mtx *mtx2 = ncv2->mtx; |
| 284 |
nng_cv *cv2 = ncv2->cv; |
| 285 |
|
| 286 |
int incr, cond = 0; |
| 287 |
|
| 288 |
nng_mtx_lock(mtx); |
| 289 |
while (ncv->condition == cond) |
| 290 |
nng_cv_wait(cv); |
| 291 |
if (ncv->condition < 0) { |
| 292 |
ncv->condition = cond; |
| 293 |
nng_mtx_unlock(mtx); |
| 294 |
return; |
| 295 |
} |
| 296 |
incr = ncv->condition - cond; |
| 297 |
cond = cond + incr; |
| 298 |
nng_mtx_unlock(mtx); |
| 299 |
|
| 300 |
while (1) { |
| 301 |
|
| 302 |
nng_mtx_lock(mtx2); |
| 303 |
ncv2->condition = ncv2->condition + incr; |
| 304 |
nng_cv_wake(cv2); |
| 305 |
nng_mtx_unlock(mtx2); |
| 306 |
|
| 307 |
nng_mtx_lock(mtx); |
| 308 |
while (ncv->condition == cond) |
| 309 |
nng_cv_wait(cv); |
| 310 |
if (ncv->condition < 0) { |
| 311 |
ncv->condition = cond; |
| 312 |
nng_mtx_unlock(mtx); |
| 313 |
break; |
| 314 |
} |
| 315 |
incr = ncv->condition - cond; |
| 316 |
cond = cond + incr; |
| 317 |
nng_mtx_unlock(mtx); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
SEXP rnng_signal_thread_create(SEXP cv, SEXP cv2) { |
| 324 |
|
| 325 |
if (NANO_PTR_CHECK(cv, nano_CvSymbol)) |
| 326 |
Rf_error("`cv` is not a valid Condition Variable"); |
| 327 |
|
| 328 |
if (NANO_PTR_CHECK(cv2, nano_CvSymbol)) |
| 329 |
Rf_error("`cv2` is not a valid Condition Variable"); |
| 330 |
|
| 331 |
int xc; |
| 332 |
nano_thread_duo *duo = malloc(sizeof(nano_thread_duo)); |
| 333 |
NANO_ENSURE_ALLOC(duo); |
| 334 |
|
| 335 |
SEXP existing = Rf_getAttrib(cv, nano_ThreadSymbol); |
| 336 |
if (existing != R_NilValue) { |
| 337 |
thread_duo_finalizer(existing); |
| 338 |
R_ClearExternalPtr(existing); |
| 339 |
} |
| 340 |
|
| 341 |
nano_cv *ncv = (nano_cv *) NANO_PTR(cv); |
| 342 |
nano_cv *ncv2 = (nano_cv *) NANO_PTR(cv2); |
| 343 |
duo->cv = ncv; |
| 344 |
duo->cv2 = ncv2; |
| 345 |
|
| 346 |
nng_mtx *dmtx = ncv->mtx; |
| 347 |
nng_mtx_lock(dmtx); |
| 348 |
ncv->condition = 0; |
| 349 |
nng_mtx_unlock(dmtx); |
| 350 |
|
| 351 |
if ((xc = nng_thread_create(&duo->thr, rnng_signal_thread, duo))) |
| 352 |
goto fail; |
| 353 |
|
| 354 |
SEXP xptr = R_MakeExternalPtr(duo, R_NilValue, R_NilValue); |
| 355 |
Rf_setAttrib(cv, nano_ThreadSymbol, xptr); |
| 356 |
R_RegisterCFinalizerEx(xptr, thread_duo_finalizer, TRUE); |
| 357 |
|
| 358 |
return cv2; |
| 359 |
|
| 360 |
fail: |
| 361 |
free(duo); |
| 362 |
Rf_setAttrib(cv, nano_ThreadSymbol, R_NilValue); |
| 363 |
failmem: |
| 364 |
ERROR_OUT(xc); |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
char *nano_readline(void) { |
| 369 |
|
| 370 |
size_t sz = NANONEXT_INIT_BUFSIZE; |
| 371 |
size_t cur = 0; |
| 372 |
char *buf = malloc(sz); |
| 373 |
if (buf == NULL) { |
| 374 |
return NULL; |
| 375 |
} |
| 376 |
|
| 377 |
int c; |
| 378 |
while ((c = fgetc(stdin)) != EOF) { |
| 379 |
if (cur + 1 >= sz) { |
| 380 |
sz += sz; |
| 381 |
char *nbuf = realloc(buf, sz); |
| 382 |
if (nbuf == NULL) |
| 383 |
break; |
| 384 |
buf = nbuf; |
| 385 |
} |
| 386 |
|
| 387 |
buf[cur++] = (char) c; |
| 388 |
if (c == '\n') |
| 389 |
break; |
| 390 |
} |
| 391 |
|
| 392 |
if (cur == 0 && c == EOF) { |
| 393 |
free(buf); |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
|
| 397 |
buf[cur] = '\0'; |
| 398 |
return buf; |
| 399 |
|
| 400 |
} |
| 401 |
|
| 402 |
void nano_read_thread(void *arg) { |
| 403 |
|
| 404 |
int xc = 0; |
| 405 |
nng_socket sock = {0}; |
| 406 |
nng_dialer dp = {0}; |
| 407 |
if ((xc = nng_push0_open(&sock)) || |
| 408 |
(xc = nng_dialer_create(&dp, sock, "inproc://nanonext-reserved-reader")) || |
| 409 |
(xc = nng_dialer_start(dp, 0))) |
| 410 |
goto cleanup; |
| 411 |
|
| 412 |
char *buf = NULL; |
| 413 |
while (1) { |
| 414 |
buf = nano_readline(); |
| 415 |
if (buf == NULL) |
| 416 |
break; |
| 417 |
xc = nng_send(sock, buf, strlen(buf) + 1, 0); |
| 418 |
free(buf); |
| 419 |
if (xc) |
| 420 |
break; |
| 421 |
} |
| 422 |
|
| 423 |
cleanup: |
| 424 |
nng_close(sock); |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
SEXP rnng_read_stdin(SEXP interactive) { |
| 429 |
|
| 430 |
if (NANO_INTEGER(interactive)) |
| 431 |
Rf_error("can only be used in non-interactive sessions"); |
| 432 |
|
| 433 |
int xc; |
| 434 |
nng_socket *sock = NULL; |
| 435 |
nng_listener *lp = NULL; |
| 436 |
sock = malloc(sizeof(nng_socket)); |
| 437 |
NANO_ENSURE_ALLOC(sock); |
| 438 |
lp = malloc(sizeof(nng_listener)); |
| 439 |
NANO_ENSURE_ALLOC(lp); |
| 440 |
|
| 441 |
if ((xc = nng_pull0_open(sock)) || |
| 442 |
(xc = nng_listener_create(lp, *sock, "inproc://nanonext-reserved-reader")) || |
| 443 |
(xc = nng_listener_start(*lp, 0))) |
| 444 |
goto fail; |
| 445 |
|
| 446 |
nng_thread *thr; |
| 447 |
|
| 448 |
if ((xc = nng_thread_create(&thr, nano_read_thread, NULL))) |
| 449 |
ERROR_OUT(xc); |
| 450 |
|
| 451 |
SEXP socket, con, thread; |
| 452 |
PROTECT(thread = R_MakeExternalPtr(thr, R_NilValue, R_NilValue)); |
| 453 |
R_RegisterCFinalizerEx(thread, thread_finalizer, TRUE); |
| 454 |
PROTECT(con = R_MakeExternalPtr(lp, R_NilValue, thread)); |
| 455 |
R_RegisterCFinalizerEx(con, listener_finalizer, TRUE); |
| 456 |
PROTECT(socket = R_MakeExternalPtr(sock, nano_SocketSymbol, con)); |
| 457 |
R_RegisterCFinalizerEx(socket, socket_finalizer, TRUE); |
| 458 |
|
| 459 |
NANO_CLASS2(socket, "nanoSocket", "nano"); |
| 460 |
Rf_setAttrib(socket, nano_IdSymbol, Rf_ScalarInteger(nng_socket_id(*sock))); |
| 461 |
Rf_setAttrib(socket, nano_ProtocolSymbol, Rf_mkString("pull")); |
| 462 |
Rf_setAttrib(socket, nano_StateSymbol, Rf_mkString("opened")); |
| 463 |
|
| 464 |
UNPROTECT(3); |
| 465 |
return socket; |
| 466 |
|
| 467 |
fail: |
| 468 |
nng_close(*sock); |
| 469 |
failmem: |
| 470 |
free(lp); |
| 471 |
free(sock); |
| 472 |
ERROR_OUT(xc); |
| 473 |
|
| 474 |
} |