|
2 | 2 | #define _ASIO_H |
3 | 3 |
|
4 | 4 | #define INTERRUPT_MODE UV_RUN_NOWAIT |
5 | | -#ifndef CERTIFICATE |
6 | | - #define CERTIFICATE "localhost" |
7 | | -#endif |
8 | 5 |
|
9 | 6 | #include "evt_tls.h" |
10 | 7 | #include "url_http.h" |
11 | 8 | #include "reflection.h" |
12 | 9 |
|
13 | 10 | #ifdef _WIN32 |
14 | | -#define INVALID_FD -EBADF |
15 | 11 | #define use_ipc false |
16 | 12 | #else |
17 | | -#define INVALID_FD -EBADF |
18 | 13 | #define use_ipc true |
19 | 14 | #endif |
| 15 | +#define INVALID_FD -EBADF |
20 | 16 |
|
21 | 17 | /* Cast ~libuv~ `obj` to `uv_stream_t` ptr. */ |
22 | 18 | #define streamer(obj) ((uv_stream_t *)obj) |
@@ -201,6 +197,7 @@ typedef void (*stream_cb)(uv_stream_t *); |
201 | 197 | typedef void (*packet_cb)(udp_packet_t *); |
202 | 198 | typedef void (*spawn_cb)(int64_t status, int signal); |
203 | 199 | typedef void (*spawn_handler_cb)(uv_stream_t *input, string output, uv_stream_t *duplex); |
| 200 | +typedef void (*queue_cb)(vectors_t result); |
204 | 201 |
|
205 | 202 | typedef struct { |
206 | 203 | asio_types type; |
@@ -481,6 +478,49 @@ C_API int udp_send(uv_udp_t *handle, string_t message, string_t addr); |
481 | 478 | C_API udp_packet_t *udp_recv(uv_udp_t *); |
482 | 479 | C_API int udp_send_packet(udp_packet_t *, string_t); |
483 | 480 |
|
| 481 | +/* |
| 482 | +This runs the function `fn` asynchronously (potentially in a separate thread which |
| 483 | +might be a part of a thread pool) and returns a `future` that will eventually hold |
| 484 | +the result of that function call. |
| 485 | +
|
| 486 | +Similar to: https://en.cppreference.com/w/cpp/thread/async.html |
| 487 | +https://en.cppreference.com/w/cpp/thread/packaged_task.html |
| 488 | +
|
| 489 | +MUST call either `queue_then()` or `queue_get()` to actually start execution in thread. |
| 490 | +*/ |
| 491 | +C_API future queue_work(thrd_func_t fn, size_t num_args, ...); |
| 492 | + |
| 493 | +/* |
| 494 | +This will complete an normal `uv_queue_work()` setup execution and allow thread to run |
| 495 | +`queue_work()` provided `fn`. |
| 496 | +
|
| 497 | +Will return `promise` only useful with `queue_get()`. |
| 498 | +
|
| 499 | +Similar to: https://en.cppreference.com/w/cpp/thread/promise.html */ |
| 500 | +C_API promise *queue_then(future, queue_cb callback); |
| 501 | + |
| 502 | +/* |
| 503 | +This waits aka `yield` until the `future` or `promise` is ready, then retrieves |
| 504 | +the value stored. Right after calling this function `queue_is_valid()` is `false`. |
| 505 | +
|
| 506 | +Similar to: https://en.cppreference.com/w/cpp/thread/future/get.html */ |
| 507 | +C_API template_t queue_get(void_t); |
| 508 | + |
| 509 | +/* |
| 510 | +Checks if the ~future/uv_work_t~ refers to a shared state aka `promise`, and `running`. |
| 511 | +
|
| 512 | +Similar to: https://en.cppreference.com/w/cpp/thread/future/valid.html |
| 513 | +*/ |
| 514 | +C_API bool queue_is_valid(future); |
| 515 | + |
| 516 | +/* |
| 517 | +Will `pause` and `yield` to another `coroutine` until `ALL` ~future/uv_work_t~ |
| 518 | +results/requests in `array` become available/done. Calls `queue_is_valid()` on each. |
| 519 | +
|
| 520 | +Similar to: https://en.cppreference.com/w/cpp/thread/future/wait.html */ |
| 521 | +C_API void queue_wait(arrays_t); |
| 522 | +C_API void queue_delete(future); |
| 523 | + |
484 | 524 | #define UV_TLS RAII_SCHEME_TLS |
485 | 525 | #define UV_CTX ASIO_ARGS + RAII_NAN |
486 | 526 |
|
@@ -517,7 +557,9 @@ C_API bool is_tty_out(void_t); |
517 | 557 | C_API bool is_tty_err(void_t); |
518 | 558 | C_API bool is_addrinfo(void_t); |
519 | 559 | C_API bool is_nameinfo(void_t); |
520 | | -C_API bool is_addressable(void_t); |
| 560 | + |
| 561 | +C_API bool is_promise(void_t); |
| 562 | +C_API bool is_future(void_t); |
521 | 563 |
|
522 | 564 | /* This library provides its own ~main~, |
523 | 565 | which call this function as an coroutine! */ |
|
0 commit comments