2021-10-20 23:05:32 +00:00
|
|
|
/*
|
2023-01-07 21:03:35 +00:00
|
|
|
* Copyright (c) 2023 Calvin Rose
|
2021-10-20 23:05:32 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
2020-05-28 15:39:40 +00:00
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef JANET_AMALG
|
|
|
|
#include "features.h"
|
|
|
|
#include <janet.h>
|
|
|
|
#include "util.h"
|
|
|
|
#include "gc.h"
|
|
|
|
#include "state.h"
|
2020-07-20 00:41:12 +00:00
|
|
|
#include "fiber.h"
|
2020-05-28 15:39:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JANET_EV
|
|
|
|
|
2020-11-15 16:21:37 +00:00
|
|
|
#include <math.h>
|
2020-10-04 17:46:15 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2020-11-14 17:48:23 +00:00
|
|
|
#include <winsock2.h>
|
2020-10-04 17:46:15 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2021-01-03 17:04:21 +00:00
|
|
|
#include <pthread.h>
|
2020-05-28 21:51:11 +00:00
|
|
|
#include <limits.h>
|
2020-05-28 15:39:40 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
2020-09-07 20:22:18 +00:00
|
|
|
#include <netinet/in.h>
|
2020-11-14 17:48:23 +00:00
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/socket.h>
|
2020-11-29 21:36:21 +00:00
|
|
|
#include <sys/wait.h>
|
2020-09-07 18:13:28 +00:00
|
|
|
#ifdef JANET_EV_EPOLL
|
|
|
|
#include <sys/epoll.h>
|
2020-05-28 15:39:40 +00:00
|
|
|
#include <sys/timerfd.h>
|
2020-09-07 18:13:28 +00:00
|
|
|
#endif
|
2021-09-03 19:29:13 +00:00
|
|
|
#ifdef JANET_EV_KQUEUE
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
2023-10-06 05:37:19 +00:00
|
|
|
#ifdef JANET_EV_POLL
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2020-10-04 17:46:15 +00:00
|
|
|
#endif
|
|
|
|
|
2021-01-07 05:19:22 +00:00
|
|
|
typedef struct {
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetVM *thread;
|
2021-01-07 05:19:22 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
uint32_t sched_id;
|
|
|
|
enum {
|
2021-08-15 16:48:13 +00:00
|
|
|
JANET_CP_MODE_READ,
|
|
|
|
JANET_CP_MODE_WRITE,
|
2021-01-07 05:19:22 +00:00
|
|
|
JANET_CP_MODE_CHOICE_READ,
|
2021-08-15 18:14:33 +00:00
|
|
|
JANET_CP_MODE_CHOICE_WRITE,
|
|
|
|
JANET_CP_MODE_CLOSE
|
2021-01-07 05:19:22 +00:00
|
|
|
} mode;
|
|
|
|
} JanetChannelPending;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
JanetQueue items;
|
|
|
|
JanetQueue read_pending;
|
|
|
|
JanetQueue write_pending;
|
|
|
|
int32_t limit;
|
2021-07-31 00:26:42 +00:00
|
|
|
int closed;
|
2021-08-20 02:16:20 +00:00
|
|
|
int is_threaded;
|
2022-06-17 22:13:58 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
CRITICAL_SECTION lock;
|
|
|
|
#else
|
|
|
|
pthread_mutex_t lock;
|
|
|
|
#endif
|
2021-01-07 05:19:22 +00:00
|
|
|
} JanetChannel;
|
|
|
|
|
2021-07-20 23:24:56 +00:00
|
|
|
typedef struct {
|
|
|
|
JanetFiber *fiber;
|
|
|
|
Janet value;
|
|
|
|
JanetSignal sig;
|
2021-09-28 20:41:41 +00:00
|
|
|
uint32_t expected_sched_id; /* If the fiber has been rescheduled this loop, don't run first scheduling. */
|
2021-07-20 23:24:56 +00:00
|
|
|
} JanetTask;
|
|
|
|
|
|
|
|
/* Wrap return value by pairing it with the callback used to handle it
|
|
|
|
* in the main thread */
|
|
|
|
typedef struct {
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
JanetThreadedCallback cb;
|
|
|
|
} JanetSelfPipeEvent;
|
|
|
|
|
|
|
|
/* Structure used to initialize threads in the thread pool
|
|
|
|
* (same head structure as self pipe event)*/
|
|
|
|
typedef struct {
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
JanetThreadedCallback cb;
|
|
|
|
JanetThreadedSubroutine subr;
|
|
|
|
JanetHandle write_pipe;
|
|
|
|
} JanetEVThreadInit;
|
|
|
|
|
2020-10-04 20:18:31 +00:00
|
|
|
#define JANET_MAX_Q_CAPACITY 0x7FFFFFF
|
2020-08-01 19:20:58 +00:00
|
|
|
|
|
|
|
static void janet_q_init(JanetQueue *q) {
|
|
|
|
q->data = NULL;
|
|
|
|
q->head = 0;
|
|
|
|
q->tail = 0;
|
|
|
|
q->capacity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void janet_q_deinit(JanetQueue *q) {
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(q->data);
|
2020-08-01 19:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int32_t janet_q_count(JanetQueue *q) {
|
|
|
|
return (q->head > q->tail)
|
|
|
|
? (q->tail + q->capacity - q->head)
|
|
|
|
: (q->tail - q->head);
|
|
|
|
}
|
|
|
|
|
2023-08-24 01:14:38 +00:00
|
|
|
static int janet_q_maybe_resize(JanetQueue *q, size_t itemsize) {
|
2020-08-01 19:20:58 +00:00
|
|
|
int32_t count = janet_q_count(q);
|
|
|
|
/* Resize if needed */
|
|
|
|
if (count + 1 >= q->capacity) {
|
|
|
|
if (count + 1 >= JANET_MAX_Q_CAPACITY) return 1;
|
|
|
|
int32_t newcap = (count + 2) * 2;
|
|
|
|
if (newcap > JANET_MAX_Q_CAPACITY) newcap = JANET_MAX_Q_CAPACITY;
|
2021-03-23 10:00:48 +00:00
|
|
|
q->data = janet_realloc(q->data, itemsize * newcap);
|
2020-08-01 19:20:58 +00:00
|
|
|
if (NULL == q->data) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (q->head > q->tail) {
|
|
|
|
/* Two segments, fix 2nd seg. */
|
|
|
|
int32_t newhead = q->head + (newcap - q->capacity);
|
2020-08-09 05:20:27 +00:00
|
|
|
size_t seg1 = (size_t)(q->capacity - q->head);
|
|
|
|
if (seg1 > 0) {
|
2020-10-04 17:46:15 +00:00
|
|
|
memmove((char *) q->data + (newhead * itemsize),
|
|
|
|
(char *) q->data + (q->head * itemsize),
|
2020-08-09 05:20:27 +00:00
|
|
|
seg1 * itemsize);
|
|
|
|
}
|
2020-08-01 19:20:58 +00:00
|
|
|
q->head = newhead;
|
|
|
|
}
|
|
|
|
q->capacity = newcap;
|
|
|
|
}
|
2023-08-24 01:14:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int janet_q_push(JanetQueue *q, void *item, size_t itemsize) {
|
|
|
|
if (janet_q_maybe_resize(q, itemsize)) return 1;
|
2020-10-04 17:46:15 +00:00
|
|
|
memcpy((char *) q->data + itemsize * q->tail, item, itemsize);
|
2020-08-09 05:20:27 +00:00
|
|
|
q->tail = q->tail + 1 < q->capacity ? q->tail + 1 : 0;
|
2020-08-01 19:20:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-24 01:14:38 +00:00
|
|
|
static int janet_q_push_head(JanetQueue *q, void *item, size_t itemsize) {
|
|
|
|
if (janet_q_maybe_resize(q, itemsize)) return 1;
|
|
|
|
int32_t newhead = q->head - 1;
|
|
|
|
if (newhead < 0) {
|
|
|
|
newhead += q->capacity;
|
|
|
|
}
|
|
|
|
memcpy((char *) q->data + itemsize * newhead, item, itemsize);
|
|
|
|
q->head = newhead;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-01 19:20:58 +00:00
|
|
|
static int janet_q_pop(JanetQueue *q, void *out, size_t itemsize) {
|
|
|
|
if (q->head == q->tail) return 1;
|
2020-10-04 17:46:15 +00:00
|
|
|
memcpy(out, (char *) q->data + itemsize * q->head, itemsize);
|
2020-08-09 05:20:27 +00:00
|
|
|
q->head = q->head + 1 < q->capacity ? q->head + 1 : 0;
|
2020-08-01 19:20:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:51:11 +00:00
|
|
|
/* Get current timestamp (millisecond precision) */
|
|
|
|
static JanetTimestamp ts_now(void);
|
|
|
|
|
|
|
|
/* Get current timestamp + an interval (millisecond precision) */
|
|
|
|
static JanetTimestamp ts_delta(JanetTimestamp ts, double delta) {
|
2023-02-05 15:29:39 +00:00
|
|
|
if (isinf(delta)) {
|
|
|
|
return delta < 0 ? ts : INT64_MAX;
|
|
|
|
}
|
2020-05-28 21:51:11 +00:00
|
|
|
ts += (int64_t)round(delta * 1000);
|
|
|
|
return ts;
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
/* Look at the next timeout value without removing it. */
|
2020-07-05 22:26:17 +00:00
|
|
|
static int peek_timeout(JanetTimeout *out) {
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.tq_count == 0) return 0;
|
|
|
|
*out = janet_vm.tq[0];
|
2020-07-05 22:26:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the next timeout from the priority queue */
|
|
|
|
static void pop_timeout(size_t index) {
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.tq_count <= index) return;
|
|
|
|
janet_vm.tq[index] = janet_vm.tq[--janet_vm.tq_count];
|
2020-07-05 22:26:17 +00:00
|
|
|
for (;;) {
|
|
|
|
size_t left = (index << 1) + 1;
|
|
|
|
size_t right = left + 1;
|
|
|
|
size_t smallest = index;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (left < janet_vm.tq_count &&
|
|
|
|
(janet_vm.tq[left].when < janet_vm.tq[smallest].when))
|
2020-07-05 22:26:17 +00:00
|
|
|
smallest = left;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (right < janet_vm.tq_count &&
|
|
|
|
(janet_vm.tq[right].when < janet_vm.tq[smallest].when))
|
2020-07-05 22:26:17 +00:00
|
|
|
smallest = right;
|
|
|
|
if (smallest == index) return;
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetTimeout temp = janet_vm.tq[index];
|
|
|
|
janet_vm.tq[index] = janet_vm.tq[smallest];
|
|
|
|
janet_vm.tq[smallest] = temp;
|
2020-07-05 22:26:17 +00:00
|
|
|
index = smallest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Add a timeout to the timeout min heap */
|
2020-05-28 21:51:11 +00:00
|
|
|
static void add_timeout(JanetTimeout to) {
|
2021-07-17 01:59:03 +00:00
|
|
|
size_t oldcount = janet_vm.tq_count;
|
2020-05-28 15:39:40 +00:00
|
|
|
size_t newcount = oldcount + 1;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (newcount > janet_vm.tq_capacity) {
|
2020-05-28 15:39:40 +00:00
|
|
|
size_t newcap = 2 * newcount;
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetTimeout *tq = janet_realloc(janet_vm.tq, newcap * sizeof(JanetTimeout));
|
2020-05-28 15:39:40 +00:00
|
|
|
if (NULL == tq) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.tq = tq;
|
|
|
|
janet_vm.tq_capacity = newcap;
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
/* Append */
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.tq_count = (int32_t) newcount;
|
|
|
|
janet_vm.tq[oldcount] = to;
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Heapify */
|
|
|
|
size_t index = oldcount;
|
|
|
|
while (index > 0) {
|
|
|
|
size_t parent = (index - 1) >> 1;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.tq[parent].when <= janet_vm.tq[index].when) break;
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Swap */
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetTimeout tmp = janet_vm.tq[index];
|
|
|
|
janet_vm.tq[index] = janet_vm.tq[parent];
|
|
|
|
janet_vm.tq[parent] = tmp;
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Next */
|
|
|
|
index = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
void janet_async_end(JanetFiber *fiber) {
|
|
|
|
if (fiber->ev_callback) {
|
2023-10-08 23:25:46 +00:00
|
|
|
fiber->ev_callback(fiber, JANET_ASYNC_EVENT_DEINIT);
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_gcunroot(janet_wrap_abstract(fiber->ev_stream));
|
|
|
|
fiber->ev_callback = NULL;
|
2023-11-10 19:34:17 +00:00
|
|
|
if (!(fiber->flags & JANET_FIBER_EV_FLAG_IN_FLIGHT)) {
|
|
|
|
if (fiber->ev_state) {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_free(fiber->ev_state);
|
2023-11-10 19:34:17 +00:00
|
|
|
fiber->ev_state = NULL;
|
2023-10-07 15:01:35 +00:00
|
|
|
}
|
2023-11-10 19:34:17 +00:00
|
|
|
janet_ev_dec_refcount();
|
2023-09-25 07:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
void janet_async_in_flight(JanetFiber *fiber) {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
fiber->flags |= JANET_FIBER_EV_FLAG_IN_FLIGHT;
|
|
|
|
#else
|
|
|
|
(void) fiber;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_async_start(JanetStream *stream, JanetAsyncMode mode, JanetEVCallback callback, void *state) {
|
|
|
|
JanetFiber *fiber = janet_vm.root_fiber;
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_assert(!fiber->ev_callback, "double async on fiber");
|
2024-04-16 02:32:17 +00:00
|
|
|
if (mode & JANET_ASYNC_LISTEN_READ) {
|
|
|
|
stream->read_fiber = fiber;
|
|
|
|
} else {
|
|
|
|
stream->read_fiber = NULL;
|
|
|
|
}
|
|
|
|
if (mode & JANET_ASYNC_LISTEN_WRITE) {
|
|
|
|
stream->write_fiber = fiber;
|
|
|
|
} else {
|
|
|
|
stream->write_fiber = NULL;
|
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
fiber->ev_callback = callback;
|
|
|
|
fiber->ev_stream = stream;
|
|
|
|
janet_ev_inc_refcount();
|
|
|
|
janet_gcroot(janet_wrap_abstract(stream));
|
2023-10-08 23:25:46 +00:00
|
|
|
fiber->ev_state = state;
|
|
|
|
callback(fiber, JANET_ASYNC_EVENT_INIT);
|
|
|
|
janet_await();
|
2023-09-25 07:43:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
void janet_fiber_did_resume(JanetFiber *fiber) {
|
|
|
|
janet_async_end(fiber);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 19:28:35 +00:00
|
|
|
static void janet_stream_checktoclose(JanetStream *stream) {
|
2023-10-06 05:37:19 +00:00
|
|
|
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->read_fiber && !stream->write_fiber) {
|
2023-09-24 19:28:35 +00:00
|
|
|
janet_stream_close(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
/* Forward declaration */
|
|
|
|
static void janet_register_stream(JanetStream *stream);
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
static const JanetMethod ev_default_stream_methods[] = {
|
|
|
|
{"close", janet_cfun_stream_close},
|
|
|
|
{"read", janet_cfun_stream_read},
|
|
|
|
{"chunk", janet_cfun_stream_chunk},
|
|
|
|
{"write", janet_cfun_stream_write},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Create a stream*/
|
|
|
|
JanetStream *janet_stream(JanetHandle handle, uint32_t flags, const JanetMethod *methods) {
|
|
|
|
JanetStream *stream = janet_abstract(&janet_stream_type, sizeof(JanetStream));
|
|
|
|
stream->handle = handle;
|
|
|
|
stream->flags = flags;
|
2023-10-06 05:37:19 +00:00
|
|
|
stream->read_fiber = NULL;
|
|
|
|
stream->write_fiber = NULL;
|
2020-11-14 20:28:10 +00:00
|
|
|
if (methods == NULL) methods = ev_default_stream_methods;
|
|
|
|
stream->methods = methods;
|
2023-10-06 05:37:19 +00:00
|
|
|
stream->index = 0;
|
|
|
|
janet_register_stream(stream);
|
2020-11-14 20:28:10 +00:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:43:36 +00:00
|
|
|
static void janet_stream_close_impl(JanetStream *stream) {
|
2023-09-24 17:08:40 +00:00
|
|
|
stream->flags |= JANET_STREAM_CLOSED;
|
2020-11-14 20:28:10 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2023-09-24 22:32:34 +00:00
|
|
|
if (stream->handle != INVALID_HANDLE_VALUE) {
|
2020-11-14 20:28:10 +00:00
|
|
|
#ifdef JANET_NET
|
2023-09-24 22:32:34 +00:00
|
|
|
if (stream->flags & JANET_STREAM_SOCKET) {
|
|
|
|
closesocket((SOCKET) stream->handle);
|
|
|
|
} else
|
2020-11-14 20:28:10 +00:00
|
|
|
#endif
|
2023-09-24 22:32:34 +00:00
|
|
|
{
|
|
|
|
CloseHandle(stream->handle);
|
|
|
|
}
|
|
|
|
stream->handle = INVALID_HANDLE_VALUE;
|
2020-11-14 20:28:10 +00:00
|
|
|
}
|
|
|
|
#else
|
2023-09-24 22:32:34 +00:00
|
|
|
if (stream->handle != -1) {
|
|
|
|
close(stream->handle);
|
|
|
|
stream->handle = -1;
|
2023-10-06 05:37:19 +00:00
|
|
|
#ifdef JANET_EV_POLL
|
|
|
|
uint32_t i = stream->index;
|
|
|
|
size_t j = janet_vm.stream_count - 1;
|
|
|
|
JanetStream *last = janet_vm.streams[j];
|
|
|
|
struct pollfd lastfd = janet_vm.fds[j + 1];
|
|
|
|
janet_vm.fds[i + 1] = lastfd;
|
|
|
|
janet_vm.streams[i] = last;
|
|
|
|
last->index = stream->index;
|
|
|
|
janet_vm.stream_count--;
|
|
|
|
#endif
|
2023-09-24 22:32:34 +00:00
|
|
|
}
|
2020-11-14 20:28:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-09-25 07:43:36 +00:00
|
|
|
void janet_stream_close(JanetStream *stream) {
|
2023-10-06 05:37:19 +00:00
|
|
|
JanetFiber *rf = stream->read_fiber;
|
|
|
|
JanetFiber *wf = stream->write_fiber;
|
|
|
|
if (rf && rf->ev_callback) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_CLOSE);
|
|
|
|
stream->read_fiber = NULL;
|
2023-09-25 07:43:36 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
if (wf && wf->ev_callback) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_CLOSE);
|
|
|
|
stream->write_fiber = NULL;
|
2023-09-25 07:43:36 +00:00
|
|
|
}
|
|
|
|
janet_stream_close_impl(stream);
|
|
|
|
}
|
|
|
|
|
2021-07-21 00:42:35 +00:00
|
|
|
/* Called to clean up a stream */
|
|
|
|
static int janet_stream_gc(void *p, size_t s) {
|
|
|
|
(void) s;
|
|
|
|
JanetStream *stream = (JanetStream *)p;
|
2023-09-25 07:43:36 +00:00
|
|
|
janet_stream_close_impl(stream);
|
2021-07-21 00:42:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
/* Mark a stream for GC */
|
|
|
|
static int janet_stream_mark(void *p, size_t s) {
|
|
|
|
(void) s;
|
|
|
|
JanetStream *stream = (JanetStream *) p;
|
2023-10-06 05:37:19 +00:00
|
|
|
JanetFiber *rf = stream->read_fiber;
|
|
|
|
JanetFiber *wf = stream->write_fiber;
|
|
|
|
if (rf) {
|
|
|
|
janet_mark(janet_wrap_fiber(rf));
|
2023-09-25 07:43:36 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
if (wf) {
|
|
|
|
janet_mark(janet_wrap_fiber(wf));
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
2020-11-14 20:28:10 +00:00
|
|
|
return 0;
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
static int janet_stream_getter(void *p, Janet key, Janet *out) {
|
|
|
|
JanetStream *stream = (JanetStream *)p;
|
|
|
|
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
|
|
|
|
const JanetMethod *stream_methods = stream->methods;
|
|
|
|
return janet_getmethod(janet_unwrap_keyword(key), stream_methods, out);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:33:05 +00:00
|
|
|
static void janet_stream_marshal(void *p, JanetMarshalContext *ctx) {
|
|
|
|
JanetStream *s = p;
|
|
|
|
if (!(ctx->flags & JANET_MARSHAL_UNSAFE)) {
|
|
|
|
janet_panic("can only marshal stream with unsafe flag");
|
|
|
|
}
|
|
|
|
janet_marshal_abstract(ctx, p);
|
|
|
|
janet_marshal_int(ctx, (int32_t) s->flags);
|
2023-06-24 17:13:51 +00:00
|
|
|
janet_marshal_ptr(ctx, s->methods);
|
2020-12-13 15:33:05 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
/* TODO - ref counting to avoid situation where a handle is closed or GCed
|
|
|
|
* while in transit, and it's value gets reused. DuplicateHandle does not work
|
2023-07-02 18:04:42 +00:00
|
|
|
* for network sockets, and in general for winsock it is better to not duplicate
|
2020-12-13 15:33:05 +00:00
|
|
|
* unless there is a need to. */
|
2021-01-24 22:48:46 +00:00
|
|
|
HANDLE duph = INVALID_HANDLE_VALUE;
|
|
|
|
if (s->flags & JANET_STREAM_SOCKET) {
|
|
|
|
duph = s->handle;
|
|
|
|
} else {
|
|
|
|
DuplicateHandle(
|
2021-01-30 02:11:38 +00:00
|
|
|
GetCurrentProcess(),
|
|
|
|
s->handle,
|
|
|
|
GetCurrentProcess(),
|
|
|
|
&duph,
|
|
|
|
0,
|
|
|
|
FALSE,
|
|
|
|
DUPLICATE_SAME_ACCESS);
|
2021-01-24 22:48:46 +00:00
|
|
|
}
|
|
|
|
janet_marshal_int64(ctx, (int64_t)(duph));
|
2020-12-13 15:33:05 +00:00
|
|
|
#else
|
|
|
|
/* Marshal after dup becuse it is easier than maintaining our own ref counting. */
|
|
|
|
int duph = dup(s->handle);
|
|
|
|
if (duph < 0) janet_panicf("failed to duplicate stream handle: %V", janet_ev_lasterr());
|
|
|
|
janet_marshal_int(ctx, (int32_t)(duph));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *janet_stream_unmarshal(JanetMarshalContext *ctx) {
|
|
|
|
if (!(ctx->flags & JANET_MARSHAL_UNSAFE)) {
|
|
|
|
janet_panic("can only unmarshal stream with unsafe flag");
|
|
|
|
}
|
|
|
|
JanetStream *p = janet_unmarshal_abstract(ctx, sizeof(JanetStream));
|
|
|
|
/* Can't share listening state and such across threads */
|
2023-10-06 05:37:19 +00:00
|
|
|
p->read_fiber = NULL;
|
|
|
|
p->write_fiber = NULL;
|
2020-12-13 15:33:05 +00:00
|
|
|
p->flags = (uint32_t) janet_unmarshal_int(ctx);
|
2023-06-24 17:13:51 +00:00
|
|
|
p->methods = janet_unmarshal_ptr(ctx);
|
2020-12-13 15:33:05 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
p->handle = (JanetHandle) janet_unmarshal_int64(ctx);
|
|
|
|
#else
|
|
|
|
p->handle = (JanetHandle) janet_unmarshal_int(ctx);
|
|
|
|
#endif
|
2024-02-19 19:19:23 +00:00
|
|
|
#ifdef JANET_EV_POLL
|
2024-02-19 19:16:45 +00:00
|
|
|
janet_register_stream(p);
|
2024-02-19 19:19:23 +00:00
|
|
|
#endif
|
2020-12-13 15:33:05 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-01-12 05:14:07 +00:00
|
|
|
static Janet janet_stream_next(void *p, Janet key) {
|
|
|
|
JanetStream *stream = (JanetStream *)p;
|
|
|
|
return janet_nextmethod(stream->methods, key);
|
|
|
|
}
|
2020-12-13 15:33:05 +00:00
|
|
|
|
2024-04-16 02:32:17 +00:00
|
|
|
static void janet_stream_tostring(void *p, JanetBuffer *buffer) {
|
|
|
|
JanetStream *stream = p;
|
|
|
|
/* Let user print the file descriptor for debugging */
|
|
|
|
janet_formatb(buffer, "<core/stream handle=%d>", stream->handle);
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:28:10 +00:00
|
|
|
const JanetAbstractType janet_stream_type = {
|
|
|
|
"core/stream",
|
|
|
|
janet_stream_gc,
|
|
|
|
janet_stream_mark,
|
|
|
|
janet_stream_getter,
|
2020-12-13 15:33:05 +00:00
|
|
|
NULL,
|
|
|
|
janet_stream_marshal,
|
|
|
|
janet_stream_unmarshal,
|
2024-04-16 02:32:17 +00:00
|
|
|
janet_stream_tostring,
|
2021-01-12 05:14:07 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
janet_stream_next,
|
|
|
|
JANET_ATEND_NEXT
|
2020-11-14 20:28:10 +00:00
|
|
|
};
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Register a fiber to resume with value */
|
2023-08-24 01:14:38 +00:00
|
|
|
static void janet_schedule_general(JanetFiber *fiber, Janet value, JanetSignal sig, int soon) {
|
2021-12-10 00:44:55 +00:00
|
|
|
if (fiber->gc.flags & JANET_FIBER_EV_FLAG_CANCELED) return;
|
2023-04-01 23:57:13 +00:00
|
|
|
if (!(fiber->gc.flags & JANET_FIBER_FLAG_ROOT)) {
|
|
|
|
Janet task_element = janet_wrap_fiber(fiber);
|
|
|
|
janet_table_put(&janet_vm.active_tasks, task_element, janet_wrap_true());
|
|
|
|
}
|
2021-09-28 20:41:41 +00:00
|
|
|
JanetTask t = { fiber, value, sig, ++fiber->sched_id };
|
2023-04-01 23:57:13 +00:00
|
|
|
fiber->gc.flags |= JANET_FIBER_FLAG_ROOT;
|
2021-12-10 00:44:55 +00:00
|
|
|
if (sig == JANET_SIGNAL_ERROR) fiber->gc.flags |= JANET_FIBER_EV_FLAG_CANCELED;
|
2023-08-24 01:14:38 +00:00
|
|
|
if (soon) {
|
|
|
|
janet_q_push_head(&janet_vm.spawn, &t, sizeof(t));
|
|
|
|
} else {
|
|
|
|
janet_q_push(&janet_vm.spawn, &t, sizeof(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_schedule_signal(JanetFiber *fiber, Janet value, JanetSignal sig) {
|
|
|
|
janet_schedule_general(fiber, value, sig, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_schedule_soon(JanetFiber *fiber, Janet value, JanetSignal sig) {
|
|
|
|
janet_schedule_general(fiber, value, sig, 1);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 22:29:57 +00:00
|
|
|
void janet_cancel(JanetFiber *fiber, Janet value) {
|
|
|
|
janet_schedule_signal(fiber, value, JANET_SIGNAL_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_schedule(JanetFiber *fiber, Janet value) {
|
|
|
|
janet_schedule_signal(fiber, value, JANET_SIGNAL_OK);
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Mark all pending tasks */
|
|
|
|
void janet_ev_mark(void) {
|
2020-11-16 15:30:04 +00:00
|
|
|
|
|
|
|
/* Pending tasks */
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetTask *tasks = janet_vm.spawn.data;
|
|
|
|
if (janet_vm.spawn.head <= janet_vm.spawn.tail) {
|
|
|
|
for (int32_t i = janet_vm.spawn.head; i < janet_vm.spawn.tail; i++) {
|
2020-08-01 19:20:58 +00:00
|
|
|
janet_mark(janet_wrap_fiber(tasks[i].fiber));
|
|
|
|
janet_mark(tasks[i].value);
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-17 01:59:03 +00:00
|
|
|
for (int32_t i = janet_vm.spawn.head; i < janet_vm.spawn.capacity; i++) {
|
2020-08-01 19:20:58 +00:00
|
|
|
janet_mark(janet_wrap_fiber(tasks[i].fiber));
|
|
|
|
janet_mark(tasks[i].value);
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
for (int32_t i = 0; i < janet_vm.spawn.tail; i++) {
|
2020-08-01 19:20:58 +00:00
|
|
|
janet_mark(janet_wrap_fiber(tasks[i].fiber));
|
|
|
|
janet_mark(tasks[i].value);
|
|
|
|
}
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
2020-11-16 15:30:04 +00:00
|
|
|
|
|
|
|
/* Pending timeouts */
|
2021-07-17 01:59:03 +00:00
|
|
|
for (size_t i = 0; i < janet_vm.tq_count; i++) {
|
|
|
|
janet_mark(janet_wrap_fiber(janet_vm.tq[i].fiber));
|
|
|
|
if (janet_vm.tq[i].curr_fiber != NULL) {
|
|
|
|
janet_mark(janet_wrap_fiber(janet_vm.tq[i].curr_fiber));
|
2020-12-05 16:32:34 +00:00
|
|
|
}
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 00:33:40 +00:00
|
|
|
static int janet_channel_push(JanetChannel *channel, Janet x, int mode);
|
2021-08-15 16:48:13 +00:00
|
|
|
static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice);
|
2021-01-07 05:19:22 +00:00
|
|
|
|
2021-08-31 03:04:15 +00:00
|
|
|
static Janet make_supervisor_event(const char *name, JanetFiber *fiber, int threaded) {
|
2022-06-25 23:15:58 +00:00
|
|
|
Janet tup[3];
|
2021-01-13 03:35:28 +00:00
|
|
|
tup[0] = janet_ckeywordv(name);
|
2021-08-31 19:50:27 +00:00
|
|
|
tup[1] = threaded ? fiber->last_value : janet_wrap_fiber(fiber) ;
|
2022-06-25 23:15:58 +00:00
|
|
|
if (fiber->env != NULL) {
|
|
|
|
tup[2] = janet_table_get(fiber->env, janet_ckeywordv("task-id"));
|
|
|
|
} else {
|
|
|
|
tup[2] = janet_wrap_nil();
|
|
|
|
}
|
|
|
|
return janet_wrap_tuple(janet_tuple_n(tup, 3));
|
2021-01-13 03:35:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Common init code */
|
|
|
|
void janet_ev_init_common(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_q_init(&janet_vm.spawn);
|
|
|
|
janet_vm.tq = NULL;
|
|
|
|
janet_vm.tq_count = 0;
|
|
|
|
janet_vm.tq_capacity = 0;
|
2021-08-20 01:56:48 +00:00
|
|
|
janet_table_init_raw(&janet_vm.threaded_abstracts, 0);
|
2023-04-01 23:57:13 +00:00
|
|
|
janet_table_init_raw(&janet_vm.active_tasks, 0);
|
2023-08-19 18:13:22 +00:00
|
|
|
janet_table_init_raw(&janet_vm.signal_handlers, 0);
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_rng_seed(&janet_vm.ev_rng, 0);
|
2022-11-11 17:01:59 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
|
|
|
pthread_attr_init(&janet_vm.new_thread_attr);
|
|
|
|
pthread_attr_setdetachstate(&janet_vm.new_thread_attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
#endif
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Common deinit code */
|
|
|
|
void janet_ev_deinit_common(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_q_deinit(&janet_vm.spawn);
|
|
|
|
janet_free(janet_vm.tq);
|
2021-08-20 01:56:48 +00:00
|
|
|
janet_table_deinit(&janet_vm.threaded_abstracts);
|
2023-04-01 23:57:13 +00:00
|
|
|
janet_table_deinit(&janet_vm.active_tasks);
|
2023-08-19 18:13:22 +00:00
|
|
|
janet_table_deinit(&janet_vm.signal_handlers);
|
2022-11-11 17:01:59 +00:00
|
|
|
#ifndef JANET_WINDOWS
|
|
|
|
pthread_attr_destroy(&janet_vm.new_thread_attr);
|
|
|
|
#endif
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 23:57:13 +00:00
|
|
|
/* Shorthand to yield to event loop */
|
2020-05-28 15:39:40 +00:00
|
|
|
void janet_await(void) {
|
2023-04-01 23:57:13 +00:00
|
|
|
/* Store the fiber in a gobal table */
|
2020-05-28 15:39:40 +00:00
|
|
|
janet_signalv(JANET_SIGNAL_EVENT, janet_wrap_nil());
|
|
|
|
}
|
|
|
|
|
2020-07-05 22:26:17 +00:00
|
|
|
/* Set timeout for the current root fiber */
|
|
|
|
void janet_addtimeout(double sec) {
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetFiber *fiber = janet_vm.root_fiber;
|
2020-07-05 22:26:17 +00:00
|
|
|
JanetTimeout to;
|
|
|
|
to.when = ts_delta(ts_now(), sec);
|
|
|
|
to.fiber = fiber;
|
2020-12-05 16:32:34 +00:00
|
|
|
to.curr_fiber = NULL;
|
2020-10-11 14:07:11 +00:00
|
|
|
to.sched_id = fiber->sched_id;
|
2020-08-16 22:29:57 +00:00
|
|
|
to.is_error = 1;
|
2020-07-05 22:26:17 +00:00
|
|
|
add_timeout(to);
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:36:21 +00:00
|
|
|
void janet_ev_inc_refcount(void) {
|
2023-10-01 15:27:51 +00:00
|
|
|
janet_atomic_inc(&janet_vm.listener_count);
|
2020-11-29 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_dec_refcount(void) {
|
2023-10-01 15:27:51 +00:00
|
|
|
janet_atomic_dec(&janet_vm.listener_count);
|
2020-11-29 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 04:45:25 +00:00
|
|
|
/* Channels */
|
|
|
|
|
2020-08-01 18:13:58 +00:00
|
|
|
#define JANET_MAX_CHANNEL_CAPACITY 0xFFFFFF
|
|
|
|
|
2021-08-15 16:48:13 +00:00
|
|
|
static inline int janet_chan_is_threaded(JanetChannel *chan) {
|
2021-08-20 02:16:20 +00:00
|
|
|
return chan->is_threaded;
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int janet_chan_pack(JanetChannel *chan, Janet *x) {
|
2021-08-20 02:16:20 +00:00
|
|
|
if (!janet_chan_is_threaded(chan)) return 0;
|
2021-08-15 16:48:13 +00:00
|
|
|
switch (janet_type(*x)) {
|
2021-08-15 21:12:40 +00:00
|
|
|
default: {
|
|
|
|
JanetBuffer *buf = janet_malloc(sizeof(JanetBuffer));
|
|
|
|
if (NULL == buf) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
janet_buffer_init(buf, 10);
|
|
|
|
janet_marshal(buf, *x, NULL, JANET_MARSHAL_UNSAFE);
|
|
|
|
*x = janet_wrap_buffer(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
case JANET_NIL:
|
|
|
|
case JANET_NUMBER:
|
|
|
|
case JANET_POINTER:
|
|
|
|
case JANET_BOOLEAN:
|
|
|
|
case JANET_CFUNCTION:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 21:41:19 +00:00
|
|
|
static int janet_chan_unpack(JanetChannel *chan, Janet *x, int is_cleanup) {
|
2021-08-15 16:48:13 +00:00
|
|
|
if (!janet_chan_is_threaded(chan)) return 0;
|
|
|
|
switch (janet_type(*x)) {
|
|
|
|
default:
|
|
|
|
return 1;
|
2021-08-15 21:12:40 +00:00
|
|
|
case JANET_BUFFER: {
|
|
|
|
JanetBuffer *buf = janet_unwrap_buffer(*x);
|
2021-12-04 16:28:00 +00:00
|
|
|
int flags = is_cleanup ? (JANET_MARSHAL_UNSAFE | JANET_MARSHAL_DECREF) : JANET_MARSHAL_UNSAFE;
|
2021-08-20 21:41:19 +00:00
|
|
|
*x = janet_unmarshal(buf->data, buf->count, flags, NULL, NULL);
|
2021-08-15 21:12:40 +00:00
|
|
|
janet_buffer_deinit(buf);
|
|
|
|
janet_free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
case JANET_NIL:
|
|
|
|
case JANET_NUMBER:
|
|
|
|
case JANET_POINTER:
|
|
|
|
case JANET_BOOLEAN:
|
|
|
|
case JANET_CFUNCTION:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void janet_chan_init(JanetChannel *chan, int32_t limit, int threaded) {
|
2020-08-01 18:13:58 +00:00
|
|
|
chan->limit = limit;
|
2021-07-31 00:26:42 +00:00
|
|
|
chan->closed = 0;
|
2021-08-20 02:16:20 +00:00
|
|
|
chan->is_threaded = threaded;
|
2020-08-01 19:20:58 +00:00
|
|
|
janet_q_init(&chan->items);
|
|
|
|
janet_q_init(&chan->read_pending);
|
|
|
|
janet_q_init(&chan->write_pending);
|
2022-06-17 22:13:58 +00:00
|
|
|
janet_os_mutex_init((JanetOSMutex *) &chan->lock);
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:48:13 +00:00
|
|
|
static void janet_chan_lock(JanetChannel *chan) {
|
|
|
|
if (!janet_chan_is_threaded(chan)) return;
|
2022-06-17 22:13:58 +00:00
|
|
|
janet_os_mutex_lock((JanetOSMutex *) &chan->lock);
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void janet_chan_unlock(JanetChannel *chan) {
|
|
|
|
if (!janet_chan_is_threaded(chan)) return;
|
2022-06-17 22:13:58 +00:00
|
|
|
janet_os_mutex_unlock((JanetOSMutex *) &chan->lock);
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 16:52:11 +00:00
|
|
|
static void janet_chan_deinit(JanetChannel *chan) {
|
|
|
|
if (janet_chan_is_threaded(chan)) {
|
|
|
|
Janet item;
|
|
|
|
janet_chan_lock(chan);
|
|
|
|
janet_q_deinit(&chan->read_pending);
|
|
|
|
janet_q_deinit(&chan->write_pending);
|
|
|
|
while (!janet_q_pop(&chan->items, &item, sizeof(item))) {
|
|
|
|
janet_chan_unpack(chan, &item, 1);
|
|
|
|
}
|
|
|
|
janet_q_deinit(&chan->items);
|
|
|
|
janet_chan_unlock(chan);
|
|
|
|
} else {
|
|
|
|
janet_q_deinit(&chan->read_pending);
|
|
|
|
janet_q_deinit(&chan->write_pending);
|
|
|
|
janet_q_deinit(&chan->items);
|
|
|
|
}
|
|
|
|
janet_os_mutex_deinit((JanetOSMutex *) &chan->lock);
|
|
|
|
}
|
|
|
|
|
2020-08-01 18:13:58 +00:00
|
|
|
/*
|
|
|
|
* Janet Channel abstract type
|
|
|
|
*/
|
|
|
|
|
2021-08-15 16:48:13 +00:00
|
|
|
static Janet janet_wrap_channel(JanetChannel *channel) {
|
2021-08-20 02:16:20 +00:00
|
|
|
return janet_wrap_abstract(channel);
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 18:13:58 +00:00
|
|
|
static int janet_chanat_gc(void *p, size_t s) {
|
2021-08-20 02:16:20 +00:00
|
|
|
(void) s;
|
|
|
|
JanetChannel *channel = p;
|
|
|
|
janet_chan_deinit(channel);
|
2020-08-01 18:13:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-01 19:20:58 +00:00
|
|
|
static void janet_chanat_mark_fq(JanetQueue *fq) {
|
2020-08-09 05:20:27 +00:00
|
|
|
JanetChannelPending *pending = fq->data;
|
2020-08-01 18:13:58 +00:00
|
|
|
if (fq->head <= fq->tail) {
|
|
|
|
for (int32_t i = fq->head; i < fq->tail; i++)
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_mark(janet_wrap_fiber(pending[i].fiber));
|
2020-08-01 18:13:58 +00:00
|
|
|
} else {
|
|
|
|
for (int32_t i = fq->head; i < fq->capacity; i++)
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_mark(janet_wrap_fiber(pending[i].fiber));
|
2020-08-01 18:13:58 +00:00
|
|
|
for (int32_t i = 0; i < fq->tail; i++)
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_mark(janet_wrap_fiber(pending[i].fiber));
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int janet_chanat_mark(void *p, size_t s) {
|
2021-08-20 02:16:20 +00:00
|
|
|
(void) s;
|
2020-08-01 18:13:58 +00:00
|
|
|
JanetChannel *chan = p;
|
|
|
|
janet_chanat_mark_fq(&chan->read_pending);
|
|
|
|
janet_chanat_mark_fq(&chan->write_pending);
|
2020-08-01 19:20:58 +00:00
|
|
|
JanetQueue *items = &chan->items;
|
|
|
|
Janet *data = chan->items.data;
|
|
|
|
if (items->head <= items->tail) {
|
|
|
|
for (int32_t i = items->head; i < items->tail; i++)
|
|
|
|
janet_mark(data[i]);
|
2020-08-01 18:13:58 +00:00
|
|
|
} else {
|
2020-08-01 19:20:58 +00:00
|
|
|
for (int32_t i = items->head; i < items->capacity; i++)
|
|
|
|
janet_mark(data[i]);
|
|
|
|
for (int32_t i = 0; i < items->tail; i++)
|
|
|
|
janet_mark(data[i]);
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
static Janet make_write_result(JanetChannel *channel) {
|
|
|
|
Janet *tup = janet_tuple_begin(2);
|
|
|
|
tup[0] = janet_ckeywordv("give");
|
2021-08-15 16:48:13 +00:00
|
|
|
tup[1] = janet_wrap_channel(channel);
|
2020-10-11 14:07:11 +00:00
|
|
|
return janet_wrap_tuple(janet_tuple_end(tup));
|
|
|
|
}
|
2020-08-01 18:13:58 +00:00
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
static Janet make_read_result(JanetChannel *channel, Janet x) {
|
|
|
|
Janet *tup = janet_tuple_begin(3);
|
|
|
|
tup[0] = janet_ckeywordv("take");
|
2021-08-15 16:48:13 +00:00
|
|
|
tup[1] = janet_wrap_channel(channel);
|
2020-10-11 14:07:11 +00:00
|
|
|
tup[2] = x;
|
|
|
|
return janet_wrap_tuple(janet_tuple_end(tup));
|
|
|
|
}
|
|
|
|
|
2021-07-31 00:26:42 +00:00
|
|
|
static Janet make_close_result(JanetChannel *channel) {
|
|
|
|
Janet *tup = janet_tuple_begin(2);
|
|
|
|
tup[0] = janet_ckeywordv("close");
|
2021-08-15 16:48:13 +00:00
|
|
|
tup[1] = janet_wrap_channel(channel);
|
2021-07-31 00:26:42 +00:00
|
|
|
return janet_wrap_tuple(janet_tuple_end(tup));
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:48:13 +00:00
|
|
|
/* Callback to use for scheduling a fiber from another thread. */
|
|
|
|
static void janet_thread_chan_cb(JanetEVGenericMessage msg) {
|
|
|
|
uint32_t sched_id = (uint32_t) msg.argi;
|
|
|
|
JanetFiber *fiber = msg.fiber;
|
|
|
|
int mode = msg.tag;
|
|
|
|
JanetChannel *channel = (JanetChannel *) msg.argp;
|
|
|
|
Janet x = msg.argj;
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_chan_lock(channel);
|
2021-08-15 16:48:13 +00:00
|
|
|
if (fiber->sched_id == sched_id) {
|
|
|
|
if (mode == JANET_CP_MODE_CHOICE_READ) {
|
2021-08-20 21:41:19 +00:00
|
|
|
janet_assert(!janet_chan_unpack(channel, &x, 0), "packing error");
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_schedule(fiber, make_read_result(channel, x));
|
|
|
|
} else if (mode == JANET_CP_MODE_CHOICE_WRITE) {
|
|
|
|
janet_schedule(fiber, make_write_result(channel));
|
|
|
|
} else if (mode == JANET_CP_MODE_READ) {
|
2021-08-20 21:41:19 +00:00
|
|
|
janet_assert(!janet_chan_unpack(channel, &x, 0), "packing error");
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_schedule(fiber, x);
|
2021-08-15 18:14:33 +00:00
|
|
|
} else if (mode == JANET_CP_MODE_WRITE) {
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_channel(channel));
|
2021-08-15 18:14:33 +00:00
|
|
|
} else { /* (mode == JANET_CP_MODE_CLOSE) */
|
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
2021-08-15 18:14:33 +00:00
|
|
|
} else if (mode != JANET_CP_MODE_CLOSE) {
|
2021-08-15 16:48:13 +00:00
|
|
|
/* Fiber has already been cancelled or resumed. */
|
|
|
|
/* Resend event to another waiting thread, depending on mode */
|
|
|
|
int is_read = (mode == JANET_CP_MODE_CHOICE_READ) || (mode == JANET_CP_MODE_READ);
|
|
|
|
if (is_read) {
|
|
|
|
JanetChannelPending reader;
|
|
|
|
if (!janet_q_pop(&channel->read_pending, &reader, sizeof(reader))) {
|
|
|
|
JanetVM *vm = reader.thread;
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
msg.tag = reader.mode;
|
|
|
|
msg.fiber = reader.fiber;
|
|
|
|
msg.argi = (int32_t) reader.sched_id;
|
|
|
|
msg.argp = channel;
|
|
|
|
msg.argj = x;
|
|
|
|
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JanetChannelPending writer;
|
|
|
|
if (!janet_q_pop(&channel->write_pending, &writer, sizeof(writer))) {
|
|
|
|
JanetVM *vm = writer.thread;
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
msg.tag = writer.mode;
|
|
|
|
msg.fiber = writer.fiber;
|
|
|
|
msg.argi = (int32_t) writer.sched_id;
|
|
|
|
msg.argp = channel;
|
|
|
|
msg.argj = janet_wrap_nil();
|
|
|
|
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_chan_unlock(channel);
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
/* Push a value to a channel, and return 1 if channel should block, zero otherwise.
|
2021-08-15 16:48:13 +00:00
|
|
|
* If the push would block, will add to the write_pending queue in the channel.
|
|
|
|
* Handles both threaded and unthreaded channels. */
|
2023-05-07 16:52:11 +00:00
|
|
|
static int janet_channel_push_with_lock(JanetChannel *channel, Janet x, int mode) {
|
2020-08-09 05:20:27 +00:00
|
|
|
JanetChannelPending reader;
|
|
|
|
int is_empty;
|
2021-08-15 16:48:13 +00:00
|
|
|
if (janet_chan_pack(channel, &x)) {
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_chan_unlock(channel);
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_panicf("failed to pack value for channel: %v", x);
|
|
|
|
}
|
|
|
|
if (channel->closed) {
|
|
|
|
janet_chan_unlock(channel);
|
|
|
|
janet_panic("cannot write to closed channel");
|
|
|
|
}
|
|
|
|
int is_threaded = janet_chan_is_threaded(channel);
|
2021-08-15 18:14:33 +00:00
|
|
|
if (is_threaded) {
|
|
|
|
/* don't dereference fiber from another thread */
|
2020-08-09 05:20:27 +00:00
|
|
|
is_empty = janet_q_pop(&channel->read_pending, &reader, sizeof(reader));
|
2021-08-15 18:14:33 +00:00
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
is_empty = janet_q_pop(&channel->read_pending, &reader, sizeof(reader));
|
|
|
|
} while (!is_empty && (reader.sched_id != reader.fiber->sched_id));
|
|
|
|
}
|
2020-08-09 05:20:27 +00:00
|
|
|
if (is_empty) {
|
2020-08-01 18:13:58 +00:00
|
|
|
/* No pending reader */
|
2020-10-11 14:07:11 +00:00
|
|
|
if (janet_q_push(&channel->items, &x, sizeof(Janet))) {
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_unlock(channel);
|
2020-10-11 14:07:11 +00:00
|
|
|
janet_panicf("channel overflow: %v", x);
|
2020-08-01 19:20:58 +00:00
|
|
|
} else if (janet_q_count(&channel->items) > channel->limit) {
|
2021-01-07 05:19:22 +00:00
|
|
|
/* No root fiber, we are in completion on a root fiber. Don't block. */
|
2021-08-15 16:48:13 +00:00
|
|
|
if (mode == 2) {
|
|
|
|
janet_chan_unlock(channel);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-01 18:13:58 +00:00
|
|
|
/* Pushed successfully, but should block. */
|
2020-08-09 05:20:27 +00:00
|
|
|
JanetChannelPending pending;
|
2021-08-15 16:48:13 +00:00
|
|
|
pending.thread = &janet_vm;
|
2021-07-17 01:59:03 +00:00
|
|
|
pending.fiber = janet_vm.root_fiber,
|
|
|
|
pending.sched_id = janet_vm.root_fiber->sched_id,
|
2021-08-15 16:48:13 +00:00
|
|
|
pending.mode = mode ? JANET_CP_MODE_CHOICE_WRITE : JANET_CP_MODE_WRITE;
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_q_push(&channel->write_pending, &pending, sizeof(pending));
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_unlock(channel);
|
|
|
|
if (is_threaded) {
|
|
|
|
janet_gcroot(janet_wrap_fiber(pending.fiber));
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
return 1;
|
2020-08-09 05:20:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Pending reader */
|
2021-08-15 16:48:13 +00:00
|
|
|
if (is_threaded) {
|
|
|
|
JanetVM *vm = reader.thread;
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
msg.tag = reader.mode;
|
|
|
|
msg.fiber = reader.fiber;
|
|
|
|
msg.argi = (int32_t) reader.sched_id;
|
|
|
|
msg.argp = channel;
|
|
|
|
msg.argj = x;
|
|
|
|
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
2020-08-09 05:20:27 +00:00
|
|
|
} else {
|
2021-08-15 16:48:13 +00:00
|
|
|
if (reader.mode == JANET_CP_MODE_CHOICE_READ) {
|
|
|
|
janet_schedule(reader.fiber, make_read_result(channel, x));
|
|
|
|
} else {
|
|
|
|
janet_schedule(reader.fiber, x);
|
|
|
|
}
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_unlock(channel);
|
2020-10-11 14:07:11 +00:00
|
|
|
return 0;
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2023-05-07 16:52:11 +00:00
|
|
|
static int janet_channel_push(JanetChannel *channel, Janet x, int mode) {
|
|
|
|
janet_chan_lock(channel);
|
|
|
|
return janet_channel_push_with_lock(channel, x, mode);
|
|
|
|
}
|
|
|
|
|
2021-07-17 20:13:28 +00:00
|
|
|
/* Pop from a channel - returns 1 if item was obtained, 0 otherwise. The item
|
2020-10-11 14:07:11 +00:00
|
|
|
* is returned by reference. If the pop would block, will add to the read_pending
|
|
|
|
* queue in the channel. */
|
2023-05-07 16:52:11 +00:00
|
|
|
static int janet_channel_pop_with_lock(JanetChannel *channel, Janet *item, int is_choice) {
|
2020-08-09 05:20:27 +00:00
|
|
|
JanetChannelPending writer;
|
2021-08-15 16:48:13 +00:00
|
|
|
if (channel->closed) {
|
|
|
|
janet_chan_unlock(channel);
|
|
|
|
*item = janet_wrap_nil();
|
|
|
|
return 1;
|
|
|
|
}
|
2021-08-15 18:14:33 +00:00
|
|
|
int is_threaded = janet_chan_is_threaded(channel);
|
2020-10-11 14:07:11 +00:00
|
|
|
if (janet_q_pop(&channel->items, item, sizeof(Janet))) {
|
2020-08-01 18:13:58 +00:00
|
|
|
/* Queue empty */
|
2020-08-09 05:20:27 +00:00
|
|
|
JanetChannelPending pending;
|
2021-08-15 16:48:13 +00:00
|
|
|
pending.thread = &janet_vm;
|
2021-07-17 01:59:03 +00:00
|
|
|
pending.fiber = janet_vm.root_fiber,
|
|
|
|
pending.sched_id = janet_vm.root_fiber->sched_id;
|
2021-08-15 16:48:13 +00:00
|
|
|
pending.mode = is_choice ? JANET_CP_MODE_CHOICE_READ : JANET_CP_MODE_READ;
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_q_push(&channel->read_pending, &pending, sizeof(pending));
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_unlock(channel);
|
|
|
|
if (is_threaded) {
|
|
|
|
janet_gcroot(janet_wrap_fiber(pending.fiber));
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
return 0;
|
2020-08-09 05:20:27 +00:00
|
|
|
}
|
2021-08-20 21:41:19 +00:00
|
|
|
janet_assert(!janet_chan_unpack(channel, item, 0), "bad channel packing");
|
2020-08-09 05:20:27 +00:00
|
|
|
if (!janet_q_pop(&channel->write_pending, &writer, sizeof(writer))) {
|
2021-08-15 16:48:13 +00:00
|
|
|
/* Pending writer */
|
|
|
|
if (is_threaded) {
|
|
|
|
JanetVM *vm = writer.thread;
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
msg.tag = writer.mode;
|
|
|
|
msg.fiber = writer.fiber;
|
|
|
|
msg.argi = (int32_t) writer.sched_id;
|
|
|
|
msg.argp = channel;
|
|
|
|
msg.argj = janet_wrap_nil();
|
|
|
|
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
2020-10-11 14:07:11 +00:00
|
|
|
} else {
|
2021-08-15 16:48:13 +00:00
|
|
|
if (writer.mode == JANET_CP_MODE_CHOICE_WRITE) {
|
|
|
|
janet_schedule(writer.fiber, make_write_result(channel));
|
|
|
|
} else {
|
|
|
|
janet_schedule(writer.fiber, janet_wrap_abstract(channel));
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_unlock(channel);
|
2020-10-11 14:07:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-07 16:52:11 +00:00
|
|
|
static int janet_channel_pop(JanetChannel *channel, Janet *item, int is_choice) {
|
|
|
|
janet_chan_lock(channel);
|
|
|
|
return janet_channel_pop_with_lock(channel, item, is_choice);
|
|
|
|
}
|
|
|
|
|
2021-08-17 02:14:06 +00:00
|
|
|
JanetChannel *janet_channel_unwrap(void *abstract) {
|
2021-08-20 02:16:20 +00:00
|
|
|
return abstract;
|
2021-08-17 02:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JanetChannel *janet_getchannel(const Janet *argv, int32_t n) {
|
|
|
|
return janet_channel_unwrap(janet_getabstract(argv, n, &janet_channel_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
JanetChannel *janet_optchannel(const Janet *argv, int32_t argc, int32_t n, JanetChannel *dflt) {
|
|
|
|
if (argc > n && !janet_checktype(argv[n], JANET_NIL)) {
|
|
|
|
return janet_getchannel(argv, n);
|
2021-08-15 16:48:13 +00:00
|
|
|
} else {
|
2021-08-17 02:14:06 +00:00
|
|
|
return dflt;
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
/* Channel Methods */
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_push,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/give channel value)",
|
2021-07-31 00:26:42 +00:00
|
|
|
"Write a value to a channel, suspending the current fiber if the channel is full. "
|
|
|
|
"Returns the channel if the write succeeded, nil otherwise.") {
|
2020-10-11 14:07:11 +00:00
|
|
|
janet_fixarity(argc, 2);
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *channel = janet_getchannel(argv, 0);
|
2020-10-11 14:07:11 +00:00
|
|
|
if (janet_channel_push(channel, argv[1], 0)) {
|
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_pop,
|
|
|
|
"(ev/take channel)",
|
|
|
|
"Read from a channel, suspending the current fiber if no value is available.") {
|
2020-10-11 14:07:11 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *channel = janet_getchannel(argv, 0);
|
2020-10-11 14:07:11 +00:00
|
|
|
Janet item;
|
|
|
|
if (janet_channel_pop(channel, &item, 0)) {
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_schedule(janet_vm.root_fiber, item);
|
2020-08-09 05:20:27 +00:00
|
|
|
}
|
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
|
2023-05-07 16:52:11 +00:00
|
|
|
static void chan_unlock_args(const Janet *argv, int32_t n) {
|
|
|
|
for (int32_t i = 0; i < n; i++) {
|
|
|
|
int32_t len;
|
|
|
|
const Janet *data;
|
|
|
|
JanetChannel *chan;
|
|
|
|
if (janet_indexed_view(argv[i], &data, &len) && len == 2) {
|
|
|
|
chan = janet_getchannel(data, 0);
|
|
|
|
} else {
|
|
|
|
chan = janet_getchannel(argv, i);
|
|
|
|
}
|
|
|
|
janet_chan_unlock(chan);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_choice,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/select & clauses)",
|
2023-05-07 13:22:33 +00:00
|
|
|
"Block until the first of several channel operations occur. Returns a "
|
2023-05-11 23:03:38 +00:00
|
|
|
"tuple of the form [:give chan], [:take chan x], or [:close chan], "
|
|
|
|
"where a :give tuple is the result of a write and a :take tuple is the "
|
|
|
|
"result of a read. Each clause must be either a channel (for a channel "
|
|
|
|
"take operation) or a tuple [channel x] (for a channel give operation). "
|
|
|
|
"Operations are tried in order such that earlier clauses take "
|
|
|
|
"precedence over later clauses. Both give and take operations can "
|
|
|
|
"return a [:close chan] tuple, which indicates that the specified "
|
|
|
|
"channel was closed while waiting, or that the channel was already "
|
|
|
|
"closed.") {
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
2020-10-11 14:07:11 +00:00
|
|
|
int32_t len;
|
|
|
|
const Janet *data;
|
|
|
|
|
|
|
|
/* Check channels for immediate reads and writes */
|
2020-08-09 05:20:27 +00:00
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
2020-10-11 14:07:11 +00:00
|
|
|
if (janet_indexed_view(argv[i], &data, &len) && len == 2) {
|
|
|
|
/* Write */
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *chan = janet_getchannel(data, 0);
|
|
|
|
janet_chan_lock(chan);
|
|
|
|
if (chan->closed) {
|
2021-12-10 00:59:59 +00:00
|
|
|
janet_chan_unlock(chan);
|
2023-05-07 16:52:11 +00:00
|
|
|
chan_unlock_args(argv, i);
|
2021-08-15 16:48:13 +00:00
|
|
|
return make_close_result(chan);
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
if (janet_q_count(&chan->items) < chan->limit) {
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_channel_push_with_lock(chan, data[1], 1);
|
|
|
|
chan_unlock_args(argv, i);
|
2020-10-11 14:07:11 +00:00
|
|
|
return make_write_result(chan);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Read */
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *chan = janet_getchannel(argv, i);
|
2021-12-10 00:59:59 +00:00
|
|
|
janet_chan_lock(chan);
|
2021-08-15 16:48:13 +00:00
|
|
|
if (chan->closed) {
|
2021-12-10 00:59:59 +00:00
|
|
|
janet_chan_unlock(chan);
|
2023-05-07 16:52:11 +00:00
|
|
|
chan_unlock_args(argv, i);
|
2021-08-15 16:48:13 +00:00
|
|
|
return make_close_result(chan);
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
if (chan->items.head != chan->items.tail) {
|
|
|
|
Janet item;
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_channel_pop_with_lock(chan, &item, 1);
|
|
|
|
chan_unlock_args(argv, i);
|
2020-10-11 14:07:11 +00:00
|
|
|
return make_read_result(chan, item);
|
|
|
|
}
|
|
|
|
}
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
|
|
|
|
/* Wait for all readers or writers */
|
2020-08-09 05:20:27 +00:00
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
2020-10-11 14:07:11 +00:00
|
|
|
if (janet_indexed_view(argv[i], &data, &len) && len == 2) {
|
|
|
|
/* Write */
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *chan = janet_getchannel(data, 0);
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_channel_push_with_lock(chan, data[1], 1);
|
2020-10-11 14:07:11 +00:00
|
|
|
} else {
|
|
|
|
/* Read */
|
|
|
|
Janet item;
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *chan = janet_getchannel(argv, i);
|
2023-05-07 16:52:11 +00:00
|
|
|
janet_channel_pop_with_lock(chan, &item, 1);
|
2020-10-11 14:07:11 +00:00
|
|
|
}
|
2020-08-09 05:20:27 +00:00
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
|
2020-08-09 05:20:27 +00:00
|
|
|
janet_await();
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_full,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/full channel)",
|
|
|
|
"Check if a channel is full or not.") {
|
2020-08-01 18:13:58 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *channel = janet_getchannel(argv, 0);
|
|
|
|
janet_chan_lock(channel);
|
|
|
|
Janet ret = janet_wrap_boolean(janet_q_count(&channel->items) >= channel->limit);
|
|
|
|
janet_chan_unlock(channel);
|
|
|
|
return ret;
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_capacity,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/capacity channel)",
|
|
|
|
"Get the number of items a channel will store before blocking writers.") {
|
2020-08-01 18:13:58 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *channel = janet_getchannel(argv, 0);
|
|
|
|
janet_chan_lock(channel);
|
|
|
|
Janet ret = janet_wrap_integer(channel->limit);
|
|
|
|
janet_chan_unlock(channel);
|
|
|
|
return ret;
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_count,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/count channel)",
|
|
|
|
"Get the number of items currently waiting in a channel.") {
|
2020-08-01 18:13:58 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *channel = janet_getchannel(argv, 0);
|
|
|
|
janet_chan_lock(channel);
|
|
|
|
Janet ret = janet_wrap_integer(janet_q_count(&channel->items));
|
|
|
|
janet_chan_unlock(channel);
|
|
|
|
return ret;
|
2020-08-01 18:13:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
/* Fisher yates shuffle of arguments to get fairness */
|
|
|
|
static void fisher_yates_args(int32_t argc, Janet *argv) {
|
2020-08-09 05:20:27 +00:00
|
|
|
for (int32_t i = argc; i > 1; i--) {
|
2021-07-17 01:59:03 +00:00
|
|
|
int32_t swap_index = janet_rng_u32(&janet_vm.ev_rng) % i;
|
2020-08-09 05:20:27 +00:00
|
|
|
Janet temp = argv[swap_index];
|
|
|
|
argv[swap_index] = argv[i - 1];
|
|
|
|
argv[i - 1] = temp;
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_rchoice,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/rselect & clauses)",
|
|
|
|
"Similar to ev/select, but will try clauses in a random order for fairness.") {
|
2020-10-11 14:07:11 +00:00
|
|
|
fisher_yates_args(argc, argv);
|
|
|
|
return cfun_channel_choice(argc, argv);
|
2020-08-09 05:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_new,
|
|
|
|
"(ev/chan &opt capacity)",
|
|
|
|
"Create a new channel. capacity is the number of values to queue before "
|
|
|
|
"blocking writers, defaults to 0 if not provided. Returns a new channel.") {
|
2020-08-01 18:13:58 +00:00
|
|
|
janet_arity(argc, 0, 1);
|
2020-08-03 12:57:02 +00:00
|
|
|
int32_t limit = janet_optnat(argv, argc, 0, 0);
|
2021-08-15 18:14:33 +00:00
|
|
|
JanetChannel *channel = janet_abstract(&janet_channel_type, sizeof(JanetChannel));
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_init(channel, limit, 0);
|
2020-08-01 18:13:58 +00:00
|
|
|
return janet_wrap_abstract(channel);
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:48:13 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_new_threaded,
|
|
|
|
"(ev/thread-chan &opt limit)",
|
|
|
|
"Create a threaded channel. A threaded channel is a channel that can be shared between threads and "
|
|
|
|
"used to communicate between any number of operating system threads.") {
|
|
|
|
janet_arity(argc, 0, 1);
|
|
|
|
int32_t limit = janet_optnat(argv, argc, 0, 0);
|
2021-08-20 02:16:20 +00:00
|
|
|
JanetChannel *tchan = janet_abstract_threaded(&janet_channel_type, sizeof(JanetChannel));
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_init(tchan, limit, 1);
|
2021-08-20 02:16:20 +00:00
|
|
|
return janet_wrap_abstract(tchan);
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 00:26:42 +00:00
|
|
|
JANET_CORE_FN(cfun_channel_close,
|
|
|
|
"(ev/chan-close chan)",
|
|
|
|
"Close a channel. A closed channel will cause all pending reads and writes to return nil. "
|
|
|
|
"Returns the channel.") {
|
|
|
|
janet_fixarity(argc, 1);
|
2021-08-15 16:48:13 +00:00
|
|
|
JanetChannel *channel = janet_getchannel(argv, 0);
|
|
|
|
janet_chan_lock(channel);
|
2021-07-31 00:26:42 +00:00
|
|
|
if (!channel->closed) {
|
|
|
|
channel->closed = 1;
|
|
|
|
JanetChannelPending writer;
|
|
|
|
while (!janet_q_pop(&channel->write_pending, &writer, sizeof(writer))) {
|
2021-08-15 16:48:13 +00:00
|
|
|
if (writer.thread != &janet_vm) {
|
2021-08-15 18:14:33 +00:00
|
|
|
JanetVM *vm = writer.thread;
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
msg.fiber = writer.fiber;
|
|
|
|
msg.argp = channel;
|
|
|
|
msg.tag = JANET_CP_MODE_CLOSE;
|
|
|
|
msg.argi = (int32_t) writer.sched_id;
|
|
|
|
msg.argj = janet_wrap_nil();
|
|
|
|
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
2021-07-31 00:26:42 +00:00
|
|
|
} else {
|
2024-02-19 14:37:49 +00:00
|
|
|
if (janet_fiber_can_resume(writer.fiber)) {
|
|
|
|
if (writer.mode == JANET_CP_MODE_CHOICE_WRITE) {
|
|
|
|
janet_schedule(writer.fiber, make_close_result(channel));
|
|
|
|
} else {
|
|
|
|
janet_schedule(writer.fiber, janet_wrap_nil());
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
2021-07-31 00:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JanetChannelPending reader;
|
|
|
|
while (!janet_q_pop(&channel->read_pending, &reader, sizeof(reader))) {
|
2021-08-15 16:48:13 +00:00
|
|
|
if (reader.thread != &janet_vm) {
|
2021-08-15 18:14:33 +00:00
|
|
|
JanetVM *vm = reader.thread;
|
|
|
|
JanetEVGenericMessage msg;
|
|
|
|
msg.fiber = reader.fiber;
|
|
|
|
msg.argp = channel;
|
|
|
|
msg.tag = JANET_CP_MODE_CLOSE;
|
|
|
|
msg.argi = (int32_t) reader.sched_id;
|
|
|
|
msg.argj = janet_wrap_nil();
|
|
|
|
janet_ev_post_event(vm, janet_thread_chan_cb, msg);
|
2021-07-31 00:26:42 +00:00
|
|
|
} else {
|
2024-02-19 14:37:49 +00:00
|
|
|
if (janet_fiber_can_resume(reader.fiber)) {
|
|
|
|
if (reader.mode == JANET_CP_MODE_CHOICE_READ) {
|
|
|
|
janet_schedule(reader.fiber, make_close_result(channel));
|
|
|
|
} else {
|
|
|
|
janet_schedule(reader.fiber, janet_wrap_nil());
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
}
|
2021-07-31 00:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 16:48:13 +00:00
|
|
|
janet_chan_unlock(channel);
|
|
|
|
return argv[0];
|
2021-07-31 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 05:14:07 +00:00
|
|
|
static const JanetMethod ev_chanat_methods[] = {
|
|
|
|
{"select", cfun_channel_choice},
|
|
|
|
{"rselect", cfun_channel_rchoice},
|
|
|
|
{"count", cfun_channel_count},
|
|
|
|
{"take", cfun_channel_pop},
|
|
|
|
{"give", cfun_channel_push},
|
|
|
|
{"capacity", cfun_channel_capacity},
|
|
|
|
{"full", cfun_channel_full},
|
2021-07-31 00:26:42 +00:00
|
|
|
{"close", cfun_channel_close},
|
2021-01-12 05:14:07 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int janet_chanat_get(void *p, Janet key, Janet *out) {
|
|
|
|
(void) p;
|
|
|
|
if (!janet_checktype(key, JANET_KEYWORD)) return 0;
|
|
|
|
return janet_getmethod(janet_unwrap_keyword(key), ev_chanat_methods, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet janet_chanat_next(void *p, Janet key) {
|
|
|
|
(void) p;
|
|
|
|
return janet_nextmethod(ev_chanat_methods, key);
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:32:54 +00:00
|
|
|
static void janet_chanat_marshal(void *p, JanetMarshalContext *ctx) {
|
|
|
|
JanetChannel *channel = (JanetChannel *)p;
|
2023-08-16 19:26:52 +00:00
|
|
|
janet_marshal_byte(ctx, channel->is_threaded);
|
2023-07-02 17:58:55 +00:00
|
|
|
janet_marshal_abstract(ctx, channel);
|
2022-11-10 22:32:54 +00:00
|
|
|
janet_marshal_byte(ctx, channel->closed);
|
|
|
|
janet_marshal_int(ctx, channel->limit);
|
|
|
|
int32_t count = janet_q_count(&channel->items);
|
|
|
|
janet_marshal_int(ctx, count);
|
|
|
|
JanetQueue *items = &channel->items;
|
|
|
|
Janet *data = channel->items.data;
|
|
|
|
if (items->head <= items->tail) {
|
|
|
|
for (int32_t i = items->head; i < items->tail; i++)
|
|
|
|
janet_marshal_janet(ctx, data[i]);
|
|
|
|
} else {
|
|
|
|
for (int32_t i = items->head; i < items->capacity; i++)
|
|
|
|
janet_marshal_janet(ctx, data[i]);
|
|
|
|
for (int32_t i = 0; i < items->tail; i++)
|
|
|
|
janet_marshal_janet(ctx, data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *janet_chanat_unmarshal(JanetMarshalContext *ctx) {
|
2023-08-16 19:26:52 +00:00
|
|
|
uint8_t is_threaded = janet_unmarshal_byte(ctx);
|
|
|
|
JanetChannel *abst;
|
|
|
|
if (is_threaded) {
|
|
|
|
abst = janet_unmarshal_abstract_threaded(ctx, sizeof(JanetChannel));
|
|
|
|
} else {
|
|
|
|
abst = janet_unmarshal_abstract(ctx, sizeof(JanetChannel));
|
|
|
|
}
|
2022-11-10 22:32:54 +00:00
|
|
|
uint8_t is_closed = janet_unmarshal_byte(ctx);
|
|
|
|
int32_t limit = janet_unmarshal_int(ctx);
|
|
|
|
int32_t count = janet_unmarshal_int(ctx);
|
|
|
|
if (count < 0) janet_panic("invalid negative channel count");
|
|
|
|
janet_chan_init(abst, limit, 0);
|
|
|
|
abst->closed = !!is_closed;
|
|
|
|
for (int32_t i = 0; i < count; i++) {
|
|
|
|
Janet item = janet_unmarshal_janet(ctx);
|
|
|
|
janet_q_push(&abst->items, &item, sizeof(item));
|
|
|
|
}
|
|
|
|
return abst;
|
|
|
|
}
|
|
|
|
|
2021-08-15 18:14:33 +00:00
|
|
|
const JanetAbstractType janet_channel_type = {
|
|
|
|
"core/channel",
|
|
|
|
janet_chanat_gc,
|
|
|
|
janet_chanat_mark,
|
|
|
|
janet_chanat_get,
|
|
|
|
NULL, /* put */
|
2022-11-10 22:32:54 +00:00
|
|
|
janet_chanat_marshal,
|
|
|
|
janet_chanat_unmarshal,
|
2021-08-15 18:14:33 +00:00
|
|
|
NULL, /* tostring */
|
|
|
|
NULL, /* compare */
|
|
|
|
NULL, /* hash */
|
|
|
|
janet_chanat_next,
|
|
|
|
JANET_ATEND_NEXT
|
|
|
|
};
|
|
|
|
|
2020-05-28 21:51:11 +00:00
|
|
|
/* Main event loop */
|
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
void janet_loop1_impl(int has_timeout, JanetTimestamp timeout);
|
2020-05-28 21:51:11 +00:00
|
|
|
|
2021-07-25 01:30:36 +00:00
|
|
|
int janet_loop_done(void) {
|
2023-09-25 07:43:36 +00:00
|
|
|
return !((janet_vm.spawn.head != janet_vm.spawn.tail) ||
|
2021-07-26 02:47:52 +00:00
|
|
|
janet_vm.tq_count ||
|
2023-10-21 15:40:57 +00:00
|
|
|
janet_atomic_load(&janet_vm.listener_count));
|
2021-07-25 01:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JanetFiber *janet_loop1(void) {
|
2020-10-11 14:07:11 +00:00
|
|
|
/* Schedule expired timers */
|
|
|
|
JanetTimeout to;
|
|
|
|
JanetTimestamp now = ts_now();
|
|
|
|
while (peek_timeout(&to) && to.when <= now) {
|
|
|
|
pop_timeout(0);
|
2020-12-05 16:32:34 +00:00
|
|
|
if (to.curr_fiber != NULL) {
|
2023-04-01 23:57:13 +00:00
|
|
|
if (janet_fiber_can_resume(to.curr_fiber)) {
|
2020-12-06 20:33:08 +00:00
|
|
|
janet_cancel(to.fiber, janet_cstringv("deadline expired"));
|
|
|
|
}
|
2020-12-05 16:32:34 +00:00
|
|
|
} else {
|
|
|
|
/* This is a timeout (for a function call, not a whole fiber) */
|
|
|
|
if (to.fiber->sched_id == to.sched_id) {
|
|
|
|
if (to.is_error) {
|
|
|
|
janet_cancel(to.fiber, janet_cstringv("timeout"));
|
|
|
|
} else {
|
|
|
|
janet_schedule(to.fiber, janet_wrap_nil());
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 21:36:21 +00:00
|
|
|
|
2023-08-24 01:14:38 +00:00
|
|
|
/* Run scheduled fibers unless interrupts need to be handled. */
|
2021-07-17 01:59:03 +00:00
|
|
|
while (janet_vm.spawn.head != janet_vm.spawn.tail) {
|
2023-08-24 01:14:38 +00:00
|
|
|
/* Don't run until all interrupts have been marked as handled by calling janet_interpreter_interrupt_handled */
|
|
|
|
if (janet_vm.auto_suspend) break;
|
2021-09-28 20:41:41 +00:00
|
|
|
JanetTask task = {NULL, janet_wrap_nil(), JANET_SIGNAL_OK, 0};
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_q_pop(&janet_vm.spawn, &task, sizeof(task));
|
2021-12-10 00:44:55 +00:00
|
|
|
if (task.fiber->gc.flags & JANET_FIBER_EV_FLAG_SUSPENDED) janet_ev_dec_refcount();
|
|
|
|
task.fiber->gc.flags &= ~(JANET_FIBER_EV_FLAG_CANCELED | JANET_FIBER_EV_FLAG_SUSPENDED);
|
2021-09-28 20:41:41 +00:00
|
|
|
if (task.expected_sched_id != task.fiber->sched_id) continue;
|
2021-07-25 01:30:36 +00:00
|
|
|
Janet res;
|
|
|
|
JanetSignal sig = janet_continue_signal(task.fiber, task.value, &res, task.sig);
|
2023-04-01 23:57:13 +00:00
|
|
|
if (!janet_fiber_can_resume(task.fiber)) {
|
|
|
|
janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(task.fiber));
|
|
|
|
}
|
2021-08-17 02:14:06 +00:00
|
|
|
void *sv = task.fiber->supervisor_channel;
|
2021-09-02 02:05:05 +00:00
|
|
|
int is_suspended = sig == JANET_SIGNAL_EVENT || sig == JANET_SIGNAL_YIELD || sig == JANET_SIGNAL_INTERRUPT;
|
2021-12-10 00:44:55 +00:00
|
|
|
if (is_suspended) {
|
|
|
|
task.fiber->gc.flags |= JANET_FIBER_EV_FLAG_SUSPENDED;
|
|
|
|
janet_ev_inc_refcount();
|
|
|
|
}
|
2021-08-17 02:14:06 +00:00
|
|
|
if (NULL == sv) {
|
2021-09-02 02:05:05 +00:00
|
|
|
if (!is_suspended) {
|
2021-12-06 14:51:40 +00:00
|
|
|
janet_stacktrace_ext(task.fiber, res, "");
|
2021-07-25 01:30:36 +00:00
|
|
|
}
|
|
|
|
} else if (sig == JANET_SIGNAL_OK || (task.fiber->flags & (1 << sig))) {
|
2021-08-17 02:14:06 +00:00
|
|
|
JanetChannel *chan = janet_channel_unwrap(sv);
|
2021-08-31 03:04:15 +00:00
|
|
|
janet_channel_push(chan, make_supervisor_event(janet_signal_names[sig],
|
2023-08-24 13:00:26 +00:00
|
|
|
task.fiber, chan->is_threaded), 2);
|
2021-09-02 02:05:05 +00:00
|
|
|
} else if (!is_suspended) {
|
2021-12-06 14:51:40 +00:00
|
|
|
janet_stacktrace_ext(task.fiber, res, "");
|
2021-07-25 01:30:36 +00:00
|
|
|
}
|
|
|
|
if (sig == JANET_SIGNAL_INTERRUPT) {
|
|
|
|
return task.fiber;
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
}
|
2020-11-29 21:36:21 +00:00
|
|
|
|
2023-08-24 01:14:38 +00:00
|
|
|
/* Poll for events */
|
2023-10-21 15:40:57 +00:00
|
|
|
if (janet_vm.tq_count || janet_atomic_load(&janet_vm.listener_count)) {
|
2023-08-24 01:14:38 +00:00
|
|
|
JanetTimeout to;
|
|
|
|
memset(&to, 0, sizeof(to));
|
|
|
|
int has_timeout;
|
|
|
|
/* Drop timeouts that are no longer needed */
|
|
|
|
while ((has_timeout = peek_timeout(&to))) {
|
|
|
|
if (to.curr_fiber != NULL) {
|
|
|
|
if (!janet_fiber_can_resume(to.curr_fiber)) {
|
|
|
|
janet_table_remove(&janet_vm.active_tasks, janet_wrap_fiber(to.curr_fiber));
|
|
|
|
pop_timeout(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (to.fiber->sched_id != to.sched_id) {
|
|
|
|
pop_timeout(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Run polling implementation only if pending timeouts or pending events */
|
2023-10-21 15:40:57 +00:00
|
|
|
if (janet_vm.tq_count || janet_atomic_load(&janet_vm.listener_count)) {
|
2023-08-24 01:14:38 +00:00
|
|
|
janet_loop1_impl(has_timeout, to.when);
|
|
|
|
}
|
|
|
|
}
|
2021-07-25 01:30:36 +00:00
|
|
|
|
|
|
|
/* No fiber was interrupted */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as janet_interpreter_interrupt, but will also
|
|
|
|
* break out of the event loop if waiting for an event
|
|
|
|
* (say, waiting for ev/sleep to finish). Does this by pushing
|
|
|
|
* an empty event to the event loop. */
|
|
|
|
void janet_loop1_interrupt(JanetVM *vm) {
|
|
|
|
janet_interpreter_interrupt(vm);
|
|
|
|
JanetEVGenericMessage msg = {0};
|
|
|
|
JanetCallback cb = NULL;
|
|
|
|
janet_ev_post_event(vm, cb, msg);
|
2020-05-28 21:51:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 14:07:11 +00:00
|
|
|
void janet_loop(void) {
|
2021-07-25 01:30:36 +00:00
|
|
|
while (!janet_loop_done()) {
|
|
|
|
JanetFiber *interrupted_fiber = janet_loop1();
|
|
|
|
if (NULL != interrupted_fiber) {
|
|
|
|
janet_schedule(interrupted_fiber, janet_wrap_nil());
|
|
|
|
}
|
2020-10-11 14:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-28 21:51:11 +00:00
|
|
|
|
2020-11-29 21:36:21 +00:00
|
|
|
/*
|
2020-12-31 17:22:18 +00:00
|
|
|
* Self-pipe handling code.
|
2020-11-29 21:36:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
|
2021-01-03 17:04:21 +00:00
|
|
|
/* On windows, use PostQueuedCompletionStatus instead for
|
|
|
|
* custom events */
|
|
|
|
|
2020-11-29 21:36:21 +00:00
|
|
|
#else
|
|
|
|
|
2021-01-03 17:04:21 +00:00
|
|
|
static void janet_ev_setup_selfpipe(void) {
|
2023-10-08 18:46:24 +00:00
|
|
|
if (janet_make_pipe(janet_vm.selfpipe, 1)) {
|
2021-01-03 17:04:21 +00:00
|
|
|
JANET_EXIT("failed to initialize self pipe in event loop");
|
|
|
|
}
|
|
|
|
}
|
2020-11-29 21:36:21 +00:00
|
|
|
|
2020-12-31 17:22:18 +00:00
|
|
|
/* Handle events from the self pipe inside the event loop */
|
|
|
|
static void janet_ev_handle_selfpipe(void) {
|
2020-12-31 22:12:42 +00:00
|
|
|
JanetSelfPipeEvent response;
|
2023-10-08 20:09:53 +00:00
|
|
|
int status;
|
2023-10-08 20:37:23 +00:00
|
|
|
recur:
|
2023-10-08 20:09:53 +00:00
|
|
|
do {
|
|
|
|
status = read(janet_vm.selfpipe[0], &response, sizeof(response));
|
|
|
|
} while (status == -1 && errno == EINTR);
|
|
|
|
if (status > 0) {
|
2021-07-25 01:30:36 +00:00
|
|
|
if (NULL != response.cb) {
|
|
|
|
response.cb(response.msg);
|
2023-10-08 18:14:36 +00:00
|
|
|
janet_ev_dec_refcount();
|
2021-07-25 01:30:36 +00:00
|
|
|
}
|
2023-10-08 20:09:53 +00:00
|
|
|
goto recur;
|
2020-11-29 21:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-31 17:22:18 +00:00
|
|
|
static void janet_ev_cleanup_selfpipe(void) {
|
2021-07-17 01:59:03 +00:00
|
|
|
close(janet_vm.selfpipe[0]);
|
|
|
|
close(janet_vm.selfpipe[1]);
|
2020-12-31 17:22:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 21:36:21 +00:00
|
|
|
#endif
|
|
|
|
|
2020-10-04 17:46:15 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
|
|
|
|
static JanetTimestamp ts_now(void) {
|
2020-10-04 20:18:31 +00:00
|
|
|
return (JanetTimestamp) GetTickCount64();
|
2020-10-04 17:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_init(void) {
|
|
|
|
janet_ev_init_common();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
|
|
|
if (NULL == janet_vm.iocp) janet_panic("could not create io completion port");
|
2020-10-04 17:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_deinit(void) {
|
|
|
|
janet_ev_deinit_common();
|
2021-07-17 01:59:03 +00:00
|
|
|
CloseHandle(janet_vm.iocp);
|
2020-10-04 17:46:15 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 19:19:23 +00:00
|
|
|
static void janet_register_stream(JanetStream *stream) {
|
2023-10-07 15:01:35 +00:00
|
|
|
if (NULL == CreateIoCompletionPort(stream->handle, janet_vm.iocp, (ULONG_PTR) stream, 0)) {
|
|
|
|
janet_panicf("failed to listen for events: %V", janet_ev_lasterr());
|
2020-11-08 16:38:28 +00:00
|
|
|
}
|
2020-10-04 17:46:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:19:53 +00:00
|
|
|
void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
|
2020-10-04 17:46:15 +00:00
|
|
|
ULONG_PTR completionKey = 0;
|
|
|
|
DWORD num_bytes_transfered = 0;
|
2021-01-18 17:43:53 +00:00
|
|
|
LPOVERLAPPED overlapped = NULL;
|
2020-10-04 17:46:15 +00:00
|
|
|
|
|
|
|
/* Calculate how long to wait before timeout */
|
|
|
|
uint64_t waittime;
|
|
|
|
if (has_timeout) {
|
|
|
|
JanetTimestamp now = ts_now();
|
2020-11-02 14:27:45 +00:00
|
|
|
if (now > to) {
|
2020-10-04 17:46:15 +00:00
|
|
|
waittime = 0;
|
|
|
|
} else {
|
2020-11-02 14:27:45 +00:00
|
|
|
waittime = (uint64_t)(to - now);
|
2020-10-04 17:46:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
waittime = INFINITE;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
BOOL result = GetQueuedCompletionStatus(janet_vm.iocp, &num_bytes_transfered, &completionKey, &overlapped, (DWORD) waittime);
|
2020-10-04 17:46:15 +00:00
|
|
|
|
2023-10-03 05:14:25 +00:00
|
|
|
if (result || overlapped) {
|
2021-01-18 02:41:59 +00:00
|
|
|
if (0 == completionKey) {
|
|
|
|
/* Custom event */
|
|
|
|
JanetSelfPipeEvent *response = (JanetSelfPipeEvent *)(overlapped);
|
2021-07-25 01:30:36 +00:00
|
|
|
if (NULL != response->cb) {
|
|
|
|
response->cb(response->msg);
|
|
|
|
}
|
2023-10-08 18:14:36 +00:00
|
|
|
janet_ev_dec_refcount();
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(response);
|
2021-01-18 02:41:59 +00:00
|
|
|
} else {
|
|
|
|
/* Normal event */
|
|
|
|
JanetStream *stream = (JanetStream *) completionKey;
|
2023-10-07 15:01:35 +00:00
|
|
|
JanetFiber *fiber = NULL;
|
|
|
|
if (stream->read_fiber && stream->read_fiber->ev_state == overlapped) {
|
|
|
|
fiber = stream->read_fiber;
|
|
|
|
} else if (stream->write_fiber && stream->write_fiber->ev_state == overlapped) {
|
|
|
|
fiber = stream->write_fiber;
|
2023-09-25 07:43:36 +00:00
|
|
|
}
|
2023-10-07 15:01:35 +00:00
|
|
|
if (fiber != NULL) {
|
2023-10-07 19:07:05 +00:00
|
|
|
fiber->flags &= ~JANET_FIBER_EV_FLAG_IN_FLIGHT;
|
2023-10-07 18:25:20 +00:00
|
|
|
/* System is done with this, we can reused this data */
|
|
|
|
overlapped->InternalHigh = (ULONG_PTR) num_bytes_transfered;
|
2023-10-07 15:01:35 +00:00
|
|
|
fiber->ev_callback(fiber, result ? JANET_ASYNC_EVENT_COMPLETE : JANET_ASYNC_EVENT_FAILED);
|
|
|
|
} else {
|
|
|
|
janet_free((void *) overlapped);
|
|
|
|
janet_ev_dec_refcount();
|
2020-11-08 16:38:28 +00:00
|
|
|
}
|
2023-09-24 19:28:35 +00:00
|
|
|
janet_stream_checktoclose(stream);
|
2020-11-08 16:38:28 +00:00
|
|
|
}
|
2020-10-04 17:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 00:28:20 +00:00
|
|
|
void janet_stream_edge_triggered(JanetStream *stream) {
|
|
|
|
(void) stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_stream_level_triggered(JanetStream *stream) {
|
|
|
|
(void) stream;
|
|
|
|
}
|
|
|
|
|
2020-11-16 01:40:47 +00:00
|
|
|
#elif defined(JANET_EV_EPOLL)
|
2020-05-28 21:51:11 +00:00
|
|
|
|
|
|
|
static JanetTimestamp ts_now(void) {
|
|
|
|
struct timespec now;
|
|
|
|
janet_assert(-1 != clock_gettime(CLOCK_MONOTONIC, &now), "failed to get time");
|
|
|
|
uint64_t res = 1000 * now.tv_sec;
|
|
|
|
res += now.tv_nsec / 1000000;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Wait for the next event */
|
2024-04-27 00:28:20 +00:00
|
|
|
static void janet_register_stream_impl(JanetStream *stream, int mod, int edge_trigger) {
|
2023-10-06 05:37:19 +00:00
|
|
|
struct epoll_event ev;
|
2024-04-27 00:28:20 +00:00
|
|
|
ev.events = edge_trigger ? EPOLLET : 0;
|
2023-10-08 20:26:02 +00:00
|
|
|
if (stream->flags & (JANET_STREAM_READABLE | JANET_STREAM_ACCEPTABLE)) ev.events |= EPOLLIN;
|
|
|
|
if (stream->flags & JANET_STREAM_WRITABLE) ev.events |= EPOLLOUT;
|
2023-10-06 05:37:19 +00:00
|
|
|
ev.data.ptr = stream;
|
|
|
|
int status;
|
|
|
|
do {
|
2024-04-27 00:28:20 +00:00
|
|
|
status = epoll_ctl(janet_vm.epoll, mod ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, stream->handle, &ev);
|
2023-10-06 05:37:19 +00:00
|
|
|
} while (status == -1 && errno == EINTR);
|
|
|
|
if (status == -1) {
|
|
|
|
if (errno == EPERM) {
|
|
|
|
/* Couldn't add to event loop, so assume that it completes
|
|
|
|
* synchronously. */
|
|
|
|
stream->flags |= JANET_STREAM_UNREGISTERED;
|
|
|
|
} else {
|
|
|
|
/* Unexpected error */
|
|
|
|
janet_panicv(janet_ev_lasterr());
|
2021-07-26 02:47:52 +00:00
|
|
|
}
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 00:28:20 +00:00
|
|
|
static void janet_register_stream(JanetStream *stream) {
|
|
|
|
janet_register_stream_impl(stream, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_stream_edge_triggered(JanetStream *stream) {
|
|
|
|
janet_register_stream_impl(stream, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_stream_level_triggered(JanetStream *stream) {
|
|
|
|
janet_register_stream_impl(stream, 1, 0);
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
#define JANET_EPOLL_MAX_EVENTS 64
|
2020-10-11 14:07:11 +00:00
|
|
|
void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
2020-11-16 01:40:47 +00:00
|
|
|
struct itimerspec its;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.timer_enabled || has_timeout) {
|
2020-05-28 21:51:11 +00:00
|
|
|
memset(&its, 0, sizeof(its));
|
|
|
|
if (has_timeout) {
|
2020-10-11 14:07:11 +00:00
|
|
|
its.it_value.tv_sec = timeout / 1000;
|
|
|
|
its.it_value.tv_nsec = (timeout % 1000) * 1000000;
|
2020-05-28 21:51:11 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
timerfd_settime(janet_vm.timerfd, TFD_TIMER_ABSTIME, &its, NULL);
|
2020-05-28 21:51:11 +00:00
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.timer_enabled = has_timeout;
|
2020-05-28 21:51:11 +00:00
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Poll for events */
|
|
|
|
struct epoll_event events[JANET_EPOLL_MAX_EVENTS];
|
|
|
|
int ready;
|
|
|
|
do {
|
2021-07-17 01:59:03 +00:00
|
|
|
ready = epoll_wait(janet_vm.epoll, events, JANET_EPOLL_MAX_EVENTS, -1);
|
2020-05-28 15:39:40 +00:00
|
|
|
} while (ready == -1 && errno == EINTR);
|
|
|
|
if (ready == -1) {
|
|
|
|
JANET_EXIT("failed to poll events");
|
|
|
|
}
|
2020-05-28 21:51:11 +00:00
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* Step state machines */
|
|
|
|
for (int i = 0; i < ready; i++) {
|
2020-11-29 21:36:21 +00:00
|
|
|
void *p = events[i].data.ptr;
|
2021-07-17 01:59:03 +00:00
|
|
|
if (&janet_vm.timerfd == p) {
|
2020-11-29 21:36:21 +00:00
|
|
|
/* Timer expired, ignore */;
|
2021-07-17 01:59:03 +00:00
|
|
|
} else if (janet_vm.selfpipe == p) {
|
2020-12-31 17:22:18 +00:00
|
|
|
/* Self-pipe handling */
|
|
|
|
janet_ev_handle_selfpipe();
|
2020-11-29 21:36:21 +00:00
|
|
|
} else {
|
|
|
|
JanetStream *stream = p;
|
2020-05-28 21:51:11 +00:00
|
|
|
int mask = events[i].events;
|
2023-10-06 05:37:19 +00:00
|
|
|
int has_err = mask & EPOLLERR;
|
|
|
|
int has_hup = mask & EPOLLHUP;
|
|
|
|
JanetFiber *rf = stream->read_fiber;
|
|
|
|
JanetFiber *wf = stream->write_fiber;
|
|
|
|
if (rf) {
|
|
|
|
if (rf->ev_callback && (mask & EPOLLIN)) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_READ);
|
|
|
|
}
|
|
|
|
if (rf->ev_callback && has_err) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_ERR);
|
|
|
|
}
|
|
|
|
if (rf->ev_callback && has_hup) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_HUP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wf) {
|
|
|
|
if (wf->ev_callback && (mask & EPOLLOUT)) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_WRITE);
|
|
|
|
}
|
|
|
|
if (wf->ev_callback && has_err) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_ERR);
|
|
|
|
}
|
|
|
|
if (wf->ev_callback && has_hup) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_HUP);
|
2023-09-26 01:52:15 +00:00
|
|
|
}
|
2020-05-28 21:51:11 +00:00
|
|
|
}
|
2023-09-24 19:28:35 +00:00
|
|
|
janet_stream_checktoclose(stream);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_init(void) {
|
|
|
|
janet_ev_init_common();
|
2020-12-31 17:22:18 +00:00
|
|
|
janet_ev_setup_selfpipe();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.epoll = epoll_create1(EPOLL_CLOEXEC);
|
|
|
|
janet_vm.timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
|
|
|
|
janet_vm.timer_enabled = 0;
|
|
|
|
if (janet_vm.epoll == -1 || janet_vm.timerfd == -1) goto error;
|
2020-05-28 21:51:11 +00:00
|
|
|
struct epoll_event ev;
|
|
|
|
ev.events = EPOLLIN | EPOLLET;
|
2021-07-17 01:59:03 +00:00
|
|
|
ev.data.ptr = &janet_vm.timerfd;
|
|
|
|
if (-1 == epoll_ctl(janet_vm.epoll, EPOLL_CTL_ADD, janet_vm.timerfd, &ev)) goto error;
|
2020-11-29 21:36:21 +00:00
|
|
|
ev.events = EPOLLIN | EPOLLET;
|
2021-07-17 01:59:03 +00:00
|
|
|
ev.data.ptr = janet_vm.selfpipe;
|
|
|
|
if (-1 == epoll_ctl(janet_vm.epoll, EPOLL_CTL_ADD, janet_vm.selfpipe[0], &ev)) goto error;
|
2020-05-28 21:51:11 +00:00
|
|
|
return;
|
|
|
|
error:
|
|
|
|
JANET_EXIT("failed to initialize event loop");
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_deinit(void) {
|
|
|
|
janet_ev_deinit_common();
|
2021-07-17 01:59:03 +00:00
|
|
|
close(janet_vm.epoll);
|
|
|
|
close(janet_vm.timerfd);
|
2020-12-31 17:22:18 +00:00
|
|
|
janet_ev_cleanup_selfpipe();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.epoll = 0;
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End epoll implementation
|
|
|
|
*/
|
|
|
|
|
2021-09-03 19:29:13 +00:00
|
|
|
#elif defined(JANET_EV_KQUEUE)
|
2021-09-05 12:30:22 +00:00
|
|
|
/* Definition from:
|
|
|
|
* https://github.com/wahern/cqueues/blob/master/src/lib/kpoll.c
|
|
|
|
* NetBSD uses intptr_t while others use void * for .udata */
|
|
|
|
#define EV_SETx(ev, a, b, c, d, e, f) EV_SET((ev), (a), (b), (c), (d), (e), ((__typeof__((ev)->udata))(f)))
|
2021-09-06 02:46:55 +00:00
|
|
|
#define JANET_KQUEUE_MIN_INTERVAL 0
|
|
|
|
|
2021-09-06 01:48:42 +00:00
|
|
|
/* NOTE:
|
2021-09-28 22:32:10 +00:00
|
|
|
* NetBSD and OpenBSD expect things are always intervals, and FreeBSD doesn't
|
|
|
|
* like an ABSTIME in the past so just use intervals always. Introduces a
|
|
|
|
* calculation to determine the minimum timeout per timeout requested of
|
|
|
|
* kqueue. Also note that NetBSD doesn't accept timeout intervals less than 1
|
|
|
|
* millisecond, so correct all intervals on that platform to be at least 1
|
|
|
|
* millisecond.*/
|
2021-09-29 20:19:30 +00:00
|
|
|
JanetTimestamp to_interval(const JanetTimestamp ts) {
|
2021-09-06 02:46:55 +00:00
|
|
|
return ts >= JANET_KQUEUE_MIN_INTERVAL ? ts : JANET_KQUEUE_MIN_INTERVAL;
|
|
|
|
}
|
2021-09-29 20:19:30 +00:00
|
|
|
#define JANET_KQUEUE_INTERVAL(timestamp) (to_interval((timestamp - ts_now())))
|
2021-09-03 19:29:13 +00:00
|
|
|
|
|
|
|
static JanetTimestamp ts_now(void) {
|
|
|
|
struct timespec now;
|
|
|
|
janet_assert(-1 != clock_gettime(CLOCK_MONOTONIC, &now), "failed to get time");
|
2021-09-03 21:29:39 +00:00
|
|
|
uint64_t res = 1000 * now.tv_sec;
|
2021-09-03 19:29:13 +00:00
|
|
|
res += now.tv_nsec / 1000000;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:19:30 +00:00
|
|
|
/* NOTE: Assumes Janet's timestamp precision is in milliseconds. */
|
|
|
|
static void timestamp2timespec(struct timespec *t, JanetTimestamp ts) {
|
|
|
|
t->tv_sec = ts == 0 ? 0 : ts / 1000;
|
|
|
|
t->tv_nsec = ts == 0 ? 0 : (ts % 1000) * 1000000;
|
|
|
|
}
|
|
|
|
|
2024-04-27 00:28:20 +00:00
|
|
|
void janet_register_stream_impl(JanetStream *stream, int edge_trigger) {
|
2023-10-07 19:53:59 +00:00
|
|
|
struct kevent kevs[2];
|
2023-10-08 20:09:53 +00:00
|
|
|
int length = 0;
|
2024-04-27 00:28:20 +00:00
|
|
|
int clear = edge_trigger ? EV_CLEAR : 0;
|
2023-10-08 20:26:02 +00:00
|
|
|
if (stream->flags & (JANET_STREAM_READABLE | JANET_STREAM_ACCEPTABLE)) {
|
2024-04-27 00:28:20 +00:00
|
|
|
EV_SETx(&kevs[length++], stream->handle, EVFILT_READ, EV_ADD | EV_ENABLE | clear, 0, 0, stream);
|
2023-10-08 20:09:53 +00:00
|
|
|
}
|
|
|
|
if (stream->flags & JANET_STREAM_WRITABLE) {
|
2024-04-27 00:28:20 +00:00
|
|
|
EV_SETx(&kevs[length++], stream->handle, EVFILT_WRITE, EV_ADD | EV_ENABLE | clear, 0, 0, stream);
|
2023-10-08 20:09:53 +00:00
|
|
|
}
|
2023-10-06 05:48:05 +00:00
|
|
|
int status;
|
|
|
|
do {
|
2023-10-08 20:09:53 +00:00
|
|
|
status = kevent(janet_vm.kq, kevs, length, NULL, 0, NULL);
|
|
|
|
} while (status == -1 && errno == EINTR);
|
2023-10-06 05:48:05 +00:00
|
|
|
if (status == -1) {
|
2023-10-08 22:21:42 +00:00
|
|
|
stream->flags |= JANET_STREAM_UNREGISTERED;
|
2021-09-04 00:44:27 +00:00
|
|
|
}
|
2021-09-03 19:29:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 00:32:47 +00:00
|
|
|
void janet_register_stream(JanetStream *stream) {
|
|
|
|
janet_register_stream_impl(stream, 1);
|
|
|
|
}
|
|
|
|
|
2024-04-27 00:28:20 +00:00
|
|
|
void janet_stream_edge_triggered(JanetStream *stream) {
|
|
|
|
janet_register_stream_impl(stream, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_stream_level_triggered(JanetStream *stream) {
|
|
|
|
janet_register_stream_impl(stream, 0);
|
|
|
|
}
|
|
|
|
|
2021-09-03 19:29:13 +00:00
|
|
|
#define JANET_KQUEUE_MAX_EVENTS 64
|
|
|
|
|
|
|
|
void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
2021-09-04 13:23:03 +00:00
|
|
|
/* Poll for events */
|
2021-09-29 20:19:30 +00:00
|
|
|
/* NOTE:
|
|
|
|
* We calculate the timeout interval per iteration. When the interval
|
|
|
|
* drops to 0 or negative, we effect a timeout of 0. Effecting a timeout
|
|
|
|
* of infinity will not work and could make other fibers with timeouts
|
|
|
|
* miss their timeouts if we did so.
|
|
|
|
* JANET_KQUEUE_INTERVAL insures we have a timeout of no less than 0. */
|
2021-09-03 19:29:13 +00:00
|
|
|
int status;
|
2021-09-29 20:19:30 +00:00
|
|
|
struct timespec ts;
|
2021-09-03 19:29:13 +00:00
|
|
|
struct kevent events[JANET_KQUEUE_MAX_EVENTS];
|
|
|
|
do {
|
2021-09-29 20:19:30 +00:00
|
|
|
if (janet_vm.timer_enabled || has_timeout) {
|
|
|
|
timestamp2timespec(&ts, JANET_KQUEUE_INTERVAL(timeout));
|
|
|
|
status = kevent(janet_vm.kq, NULL, 0, events,
|
|
|
|
JANET_KQUEUE_MAX_EVENTS, &ts);
|
|
|
|
} else {
|
|
|
|
status = kevent(janet_vm.kq, NULL, 0, events,
|
|
|
|
JANET_KQUEUE_MAX_EVENTS, NULL);
|
|
|
|
}
|
2021-09-03 19:29:13 +00:00
|
|
|
} while (status == -1 && errno == EINTR);
|
2023-10-06 22:39:52 +00:00
|
|
|
if (status == -1) {
|
2021-09-03 19:29:13 +00:00
|
|
|
JANET_EXIT("failed to poll events");
|
2023-10-06 22:39:52 +00:00
|
|
|
}
|
2021-09-03 19:29:13 +00:00
|
|
|
|
2021-09-29 20:19:30 +00:00
|
|
|
/* Make sure timer is set accordingly. */
|
|
|
|
janet_vm.timer_enabled = has_timeout;
|
|
|
|
|
2021-09-04 13:23:03 +00:00
|
|
|
/* Step state machines */
|
2021-09-04 14:34:47 +00:00
|
|
|
for (int i = 0; i < status; i++) {
|
2021-09-18 18:40:32 +00:00
|
|
|
void *p = (void *) events[i].udata;
|
2021-09-29 20:19:30 +00:00
|
|
|
if (janet_vm.selfpipe == p) {
|
2021-09-03 19:29:13 +00:00
|
|
|
/* Self-pipe handling */
|
|
|
|
janet_ev_handle_selfpipe();
|
|
|
|
} else {
|
|
|
|
JanetStream *stream = p;
|
2023-10-06 05:48:05 +00:00
|
|
|
int filt = events[i].filter;
|
|
|
|
int has_err = events[i].flags & EV_ERROR;
|
|
|
|
int has_hup = events[i].flags & EV_EOF;
|
2023-10-07 19:40:43 +00:00
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
JanetFiber *f = j ? stream->read_fiber : stream->write_fiber;
|
|
|
|
if (!f) continue;
|
|
|
|
if (f->ev_callback && has_err) {
|
|
|
|
f->ev_callback(f, JANET_ASYNC_EVENT_ERR);
|
2023-10-08 15:52:27 +00:00
|
|
|
}
|
|
|
|
if (f->ev_callback && (filt == EVFILT_READ) && f == stream->read_fiber) {
|
|
|
|
f->ev_callback(f, JANET_ASYNC_EVENT_READ);
|
|
|
|
}
|
|
|
|
if (f->ev_callback && (filt == EVFILT_WRITE) && f == stream->write_fiber) {
|
|
|
|
f->ev_callback(f, JANET_ASYNC_EVENT_WRITE);
|
|
|
|
}
|
|
|
|
if (f->ev_callback && has_hup) {
|
|
|
|
f->ev_callback(f, JANET_ASYNC_EVENT_HUP);
|
2023-09-24 22:32:34 +00:00
|
|
|
}
|
2021-09-04 04:22:07 +00:00
|
|
|
}
|
2023-09-24 19:28:35 +00:00
|
|
|
janet_stream_checktoclose(stream);
|
2021-09-03 19:29:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_init(void) {
|
|
|
|
janet_ev_init_common();
|
|
|
|
janet_ev_setup_selfpipe();
|
|
|
|
janet_vm.kq = kqueue();
|
|
|
|
janet_vm.timer_enabled = 0;
|
|
|
|
if (janet_vm.kq == -1) goto error;
|
2021-09-29 20:19:30 +00:00
|
|
|
struct kevent event;
|
2023-10-08 18:46:24 +00:00
|
|
|
EV_SETx(&event, janet_vm.selfpipe[0], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, janet_vm.selfpipe);
|
2023-10-05 04:27:56 +00:00
|
|
|
int status;
|
2023-10-05 04:24:40 +00:00
|
|
|
do {
|
|
|
|
status = kevent(janet_vm.kq, &event, 1, NULL, 0, NULL);
|
2023-10-05 04:29:54 +00:00
|
|
|
} while (status == -1 && errno != EINTR);
|
2023-10-05 04:24:40 +00:00
|
|
|
if (status == -1) goto error;
|
2021-09-03 19:29:13 +00:00
|
|
|
return;
|
|
|
|
error:
|
|
|
|
JANET_EXIT("failed to initialize event loop");
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_deinit(void) {
|
|
|
|
janet_ev_deinit_common();
|
2021-09-03 21:29:39 +00:00
|
|
|
close(janet_vm.kq);
|
2021-09-03 19:29:13 +00:00
|
|
|
janet_ev_cleanup_selfpipe();
|
|
|
|
janet_vm.kq = 0;
|
|
|
|
}
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
#elif defined(JANET_EV_POLL)
|
2020-05-29 00:14:35 +00:00
|
|
|
|
2023-10-06 05:48:05 +00:00
|
|
|
/* Simple poll implementation. Efficiency is not the goal here, although the poll implementation should be farily efficient
|
|
|
|
* for low numbers of concurrent file descriptors. Rather, the code should be simple, portable, correct, and mirror the
|
|
|
|
* epoll and kqueue code. */
|
|
|
|
|
2020-09-07 17:52:50 +00:00
|
|
|
static JanetTimestamp ts_now(void) {
|
|
|
|
struct timespec now;
|
|
|
|
janet_assert(-1 != clock_gettime(CLOCK_REALTIME, &now), "failed to get time");
|
|
|
|
uint64_t res = 1000 * now.tv_sec;
|
|
|
|
res += now.tv_nsec / 1000000;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:30:04 +00:00
|
|
|
/* Wait for the next event */
|
2023-10-06 05:37:19 +00:00
|
|
|
void janet_register_stream(JanetStream *stream) {
|
|
|
|
struct pollfd ev = {0};
|
|
|
|
stream->index = (uint32_t) janet_vm.stream_count;
|
|
|
|
size_t new_count = janet_vm.stream_count + 1;
|
|
|
|
if (new_count > janet_vm.stream_capacity) {
|
|
|
|
size_t new_cap = new_count * 2;
|
|
|
|
janet_vm.fds = janet_realloc(janet_vm.fds, (1 + new_cap) * sizeof(struct pollfd));
|
|
|
|
janet_vm.streams = janet_realloc(janet_vm.streams, new_cap * sizeof(JanetStream *));
|
|
|
|
if (!janet_vm.fds || !janet_vm.streams) {
|
2020-09-07 17:52:50 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_vm.stream_capacity = new_cap;
|
2020-09-07 17:52:50 +00:00
|
|
|
}
|
2020-11-14 20:28:10 +00:00
|
|
|
ev.fd = stream->handle;
|
2023-10-06 05:37:19 +00:00
|
|
|
ev.events = POLLIN | POLLOUT;
|
|
|
|
janet_vm.fds[janet_vm.stream_count + 1] = ev;
|
|
|
|
janet_vm.streams[janet_vm.stream_count] = stream;
|
|
|
|
janet_vm.stream_count = new_count;
|
2020-09-07 17:52:50 +00:00
|
|
|
}
|
|
|
|
|
2024-04-27 00:28:20 +00:00
|
|
|
void janet_stream_edge_triggered(JanetStream *stream) {
|
|
|
|
(void) stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_stream_level_triggered(JanetStream *stream) {
|
|
|
|
(void) stream;
|
|
|
|
}
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
|
|
|
|
|
|
|
|
/* set event flags */
|
|
|
|
for (size_t i = 0; i < janet_vm.stream_count; i++) {
|
|
|
|
JanetStream *stream = janet_vm.streams[i];
|
2024-04-15 23:12:05 +00:00
|
|
|
struct pollfd *pfd = janet_vm.fds + i + 1;
|
|
|
|
pfd->events = 0;
|
|
|
|
pfd->revents = 0;
|
|
|
|
JanetFiber *rf = stream->read_fiber;
|
|
|
|
JanetFiber *wf = stream->write_fiber;
|
|
|
|
if (rf && rf->ev_callback) pfd->events |= POLLIN;
|
|
|
|
if (wf && wf->ev_callback) pfd->events |= POLLOUT;
|
|
|
|
/* Hack to ignore a file descriptor - make file descriptor negative if we want to ignore */
|
|
|
|
if (!pfd->events) {
|
|
|
|
pfd->fd = -pfd->fd;
|
|
|
|
}
|
2023-09-27 03:57:01 +00:00
|
|
|
}
|
2020-09-07 17:52:50 +00:00
|
|
|
|
|
|
|
/* Poll for events */
|
|
|
|
int ready;
|
|
|
|
do {
|
2020-11-16 15:30:04 +00:00
|
|
|
int to = -1;
|
2020-09-07 17:52:50 +00:00
|
|
|
if (has_timeout) {
|
2020-10-11 14:07:11 +00:00
|
|
|
JanetTimestamp now = ts_now();
|
2020-11-16 15:30:04 +00:00
|
|
|
to = now > timeout ? 0 : (int)(timeout - now);
|
2020-09-07 17:52:50 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
ready = poll(janet_vm.fds, janet_vm.stream_count + 1, to);
|
2020-09-07 17:52:50 +00:00
|
|
|
} while (ready == -1 && errno == EINTR);
|
|
|
|
if (ready == -1) {
|
|
|
|
JANET_EXIT("failed to poll events");
|
|
|
|
}
|
2020-05-29 00:14:35 +00:00
|
|
|
|
2024-04-15 23:12:05 +00:00
|
|
|
/* Undo negative hack */
|
|
|
|
for (size_t i = 0; i < janet_vm.stream_count; i++) {
|
|
|
|
struct pollfd *pfd = janet_vm.fds + i + 1;
|
2024-04-16 02:32:17 +00:00
|
|
|
if (pfd->fd < 0) {
|
2024-04-15 23:12:05 +00:00
|
|
|
pfd->fd = -pfd->fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:36:21 +00:00
|
|
|
/* Check selfpipe */
|
2021-07-17 01:59:03 +00:00
|
|
|
if (janet_vm.fds[0].revents & POLLIN) {
|
|
|
|
janet_vm.fds[0].revents = 0;
|
2020-12-31 17:22:18 +00:00
|
|
|
janet_ev_handle_selfpipe();
|
2020-11-29 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 17:52:50 +00:00
|
|
|
/* Step state machines */
|
2023-10-06 05:37:19 +00:00
|
|
|
for (size_t i = 0; i < janet_vm.stream_count; i++) {
|
2021-07-17 01:59:03 +00:00
|
|
|
struct pollfd *pfd = janet_vm.fds + i + 1;
|
2023-10-06 05:37:19 +00:00
|
|
|
JanetStream *stream = janet_vm.streams[i];
|
2020-11-29 21:36:21 +00:00
|
|
|
int mask = pfd->revents;
|
2023-10-06 05:37:19 +00:00
|
|
|
if (!mask) continue;
|
|
|
|
int has_err = mask & POLLERR;
|
|
|
|
int has_hup = mask & POLLHUP;
|
|
|
|
JanetFiber *rf = stream->read_fiber;
|
|
|
|
JanetFiber *wf = stream->write_fiber;
|
|
|
|
if (rf) {
|
|
|
|
if (rf->ev_callback && (mask & POLLIN)) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_READ);
|
|
|
|
} else if (rf->ev_callback && has_hup) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_HUP);
|
|
|
|
} else if (rf->ev_callback && has_err) {
|
|
|
|
rf->ev_callback(rf, JANET_ASYNC_EVENT_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wf) {
|
|
|
|
if (wf->ev_callback && (mask & POLLOUT)) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_WRITE);
|
|
|
|
} else if (wf->ev_callback && has_hup) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_HUP);
|
|
|
|
} else if (wf->ev_callback && has_err) {
|
|
|
|
wf->ev_callback(wf, JANET_ASYNC_EVENT_ERR);
|
|
|
|
}
|
2023-09-24 19:28:35 +00:00
|
|
|
}
|
|
|
|
janet_stream_checktoclose(stream);
|
2020-09-07 17:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_init(void) {
|
|
|
|
janet_ev_init_common();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.fds = NULL;
|
2020-12-31 17:22:18 +00:00
|
|
|
janet_ev_setup_selfpipe();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.fds = janet_malloc(sizeof(struct pollfd));
|
|
|
|
if (NULL == janet_vm.fds) {
|
2020-11-29 21:36:21 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.fds[0].fd = janet_vm.selfpipe[0];
|
|
|
|
janet_vm.fds[0].events = POLLIN;
|
|
|
|
janet_vm.fds[0].revents = 0;
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_vm.streams = NULL;
|
|
|
|
janet_vm.stream_count = 0;
|
|
|
|
janet_vm.stream_capacity = 0;
|
2020-09-07 17:52:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void janet_ev_deinit(void) {
|
|
|
|
janet_ev_deinit_common();
|
2020-12-31 17:22:18 +00:00
|
|
|
janet_ev_cleanup_selfpipe();
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_free(janet_vm.fds);
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_free(janet_vm.streams);
|
2021-07-17 01:59:03 +00:00
|
|
|
janet_vm.fds = NULL;
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_vm.streams = NULL;
|
2020-09-07 17:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2020-05-29 00:14:35 +00:00
|
|
|
|
2021-01-03 17:04:21 +00:00
|
|
|
/*
|
|
|
|
* End poll implementation
|
|
|
|
*/
|
|
|
|
|
2021-07-25 01:30:36 +00:00
|
|
|
/*
|
|
|
|
* Generic Callback system. Post a function pointer + data to the event loop (from another
|
|
|
|
* thread or even a signal handler). Allows posting events from another thread or signal handler.
|
|
|
|
*/
|
|
|
|
void janet_ev_post_event(JanetVM *vm, JanetCallback cb, JanetEVGenericMessage msg) {
|
|
|
|
vm = vm ? vm : &janet_vm;
|
2023-10-08 18:14:36 +00:00
|
|
|
janet_atomic_inc(&vm->listener_count);
|
2021-07-25 01:30:36 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
JanetHandle iocp = vm->iocp;
|
|
|
|
JanetSelfPipeEvent *event = janet_malloc(sizeof(JanetSelfPipeEvent));
|
|
|
|
if (NULL == event) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
event->msg = msg;
|
|
|
|
event->cb = cb;
|
|
|
|
janet_assert(PostQueuedCompletionStatus(iocp,
|
2021-07-26 02:47:52 +00:00
|
|
|
sizeof(JanetSelfPipeEvent),
|
|
|
|
0,
|
|
|
|
(LPOVERLAPPED) event),
|
|
|
|
"failed to post completion event");
|
2021-07-25 01:30:36 +00:00
|
|
|
#else
|
|
|
|
JanetSelfPipeEvent event;
|
2021-08-15 18:14:33 +00:00
|
|
|
memset(&event, 0, sizeof(event));
|
2021-07-25 01:30:36 +00:00
|
|
|
event.msg = msg;
|
|
|
|
event.cb = cb;
|
2021-07-26 02:47:52 +00:00
|
|
|
int fd = vm->selfpipe[1];
|
2021-07-25 01:30:36 +00:00
|
|
|
/* handle a bit of back pressure before giving up. */
|
|
|
|
int tries = 4;
|
|
|
|
while (tries > 0) {
|
|
|
|
int status;
|
|
|
|
do {
|
|
|
|
status = write(fd, &event, sizeof(event));
|
|
|
|
} while (status == -1 && errno == EINTR);
|
|
|
|
if (status > 0) break;
|
2021-08-15 20:25:07 +00:00
|
|
|
sleep(0);
|
2021-07-25 01:30:36 +00:00
|
|
|
tries--;
|
|
|
|
}
|
2021-08-15 21:12:40 +00:00
|
|
|
janet_assert(tries > 0, "failed to write event to self-pipe");
|
2021-07-25 01:30:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-01-03 17:04:21 +00:00
|
|
|
/*
|
|
|
|
* Threaded calls
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
static DWORD WINAPI janet_thread_body(LPVOID ptr) {
|
|
|
|
JanetEVThreadInit *init = (JanetEVThreadInit *)ptr;
|
|
|
|
JanetEVGenericMessage msg = init->msg;
|
|
|
|
JanetThreadedSubroutine subr = init->subr;
|
|
|
|
JanetThreadedCallback cb = init->cb;
|
|
|
|
JanetHandle iocp = init->write_pipe;
|
|
|
|
/* Reuse memory from thread init for returning data */
|
|
|
|
init->msg = subr(msg);
|
|
|
|
init->cb = cb;
|
2021-01-03 17:21:44 +00:00
|
|
|
janet_assert(PostQueuedCompletionStatus(iocp,
|
|
|
|
sizeof(JanetSelfPipeEvent),
|
|
|
|
0,
|
|
|
|
(LPOVERLAPPED) init),
|
2021-01-03 17:04:21 +00:00
|
|
|
"failed to post completion event");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void *janet_thread_body(void *ptr) {
|
|
|
|
JanetEVThreadInit *init = (JanetEVThreadInit *)ptr;
|
|
|
|
JanetEVGenericMessage msg = init->msg;
|
|
|
|
JanetThreadedSubroutine subr = init->subr;
|
|
|
|
JanetThreadedCallback cb = init->cb;
|
|
|
|
int fd = init->write_pipe;
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(init);
|
2021-01-03 17:04:21 +00:00
|
|
|
JanetSelfPipeEvent response;
|
2021-08-15 18:14:33 +00:00
|
|
|
memset(&response, 0, sizeof(response));
|
2021-01-03 17:04:21 +00:00
|
|
|
response.msg = subr(msg);
|
|
|
|
response.cb = cb;
|
|
|
|
/* handle a bit of back pressure before giving up. */
|
|
|
|
int tries = 4;
|
|
|
|
while (tries > 0) {
|
|
|
|
int status;
|
|
|
|
do {
|
|
|
|
status = write(fd, &response, sizeof(response));
|
|
|
|
} while (status == -1 && errno == EINTR);
|
|
|
|
if (status > 0) break;
|
|
|
|
sleep(1);
|
|
|
|
tries--;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void janet_ev_threaded_call(JanetThreadedSubroutine fp, JanetEVGenericMessage arguments, JanetThreadedCallback cb) {
|
2021-03-23 10:00:48 +00:00
|
|
|
JanetEVThreadInit *init = janet_malloc(sizeof(JanetEVThreadInit));
|
2021-01-03 17:04:21 +00:00
|
|
|
if (NULL == init) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
init->msg = arguments;
|
|
|
|
init->subr = fp;
|
|
|
|
init->cb = cb;
|
|
|
|
|
|
|
|
#ifdef JANET_WINDOWS
|
2021-07-17 01:59:03 +00:00
|
|
|
init->write_pipe = janet_vm.iocp;
|
2021-01-03 17:04:21 +00:00
|
|
|
HANDLE thread_handle = CreateThread(NULL, 0, janet_thread_body, init, 0, NULL);
|
|
|
|
if (NULL == thread_handle) {
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(init);
|
2021-01-03 17:04:21 +00:00
|
|
|
janet_panic("failed to create thread");
|
|
|
|
}
|
|
|
|
CloseHandle(thread_handle); /* detach from thread */
|
|
|
|
#else
|
2021-07-17 01:59:03 +00:00
|
|
|
init->write_pipe = janet_vm.selfpipe[1];
|
2021-01-03 17:04:21 +00:00
|
|
|
pthread_t waiter_thread;
|
2022-11-11 17:01:59 +00:00
|
|
|
int err = pthread_create(&waiter_thread, &janet_vm.new_thread_attr, janet_thread_body, init);
|
2021-01-03 17:04:21 +00:00
|
|
|
if (err) {
|
2021-03-23 10:00:48 +00:00
|
|
|
janet_free(init);
|
2021-01-03 17:04:21 +00:00
|
|
|
janet_panicf("%s", strerror(err));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Increment ev refcount so we don't quit while waiting for a subprocess */
|
|
|
|
janet_ev_inc_refcount();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Default callback for janet_ev_threaded_await. */
|
|
|
|
void janet_ev_default_threaded_callback(JanetEVGenericMessage return_value) {
|
2021-07-17 20:13:28 +00:00
|
|
|
if (return_value.fiber == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-16 22:15:46 +00:00
|
|
|
if (janet_fiber_can_resume(return_value.fiber)) {
|
|
|
|
switch (return_value.tag) {
|
|
|
|
default:
|
|
|
|
case JANET_EV_TCTAG_NIL:
|
|
|
|
janet_schedule(return_value.fiber, janet_wrap_nil());
|
|
|
|
break;
|
|
|
|
case JANET_EV_TCTAG_INTEGER:
|
|
|
|
janet_schedule(return_value.fiber, janet_wrap_integer(return_value.argi));
|
|
|
|
break;
|
|
|
|
case JANET_EV_TCTAG_STRING:
|
|
|
|
case JANET_EV_TCTAG_STRINGF:
|
|
|
|
janet_schedule(return_value.fiber, janet_cstringv((const char *) return_value.argp));
|
|
|
|
if (return_value.tag == JANET_EV_TCTAG_STRINGF) janet_free(return_value.argp);
|
|
|
|
break;
|
|
|
|
case JANET_EV_TCTAG_KEYWORD:
|
|
|
|
janet_schedule(return_value.fiber, janet_ckeywordv((const char *) return_value.argp));
|
|
|
|
break;
|
|
|
|
case JANET_EV_TCTAG_ERR_STRING:
|
|
|
|
case JANET_EV_TCTAG_ERR_STRINGF:
|
|
|
|
janet_cancel(return_value.fiber, janet_cstringv((const char *) return_value.argp));
|
|
|
|
if (return_value.tag == JANET_EV_TCTAG_STRINGF) janet_free(return_value.argp);
|
|
|
|
break;
|
|
|
|
case JANET_EV_TCTAG_ERR_KEYWORD:
|
|
|
|
janet_cancel(return_value.fiber, janet_ckeywordv((const char *) return_value.argp));
|
|
|
|
break;
|
|
|
|
case JANET_EV_TCTAG_BOOLEAN:
|
|
|
|
janet_schedule(return_value.fiber, janet_wrap_boolean(return_value.argi));
|
|
|
|
break;
|
|
|
|
}
|
2021-01-03 17:04:21 +00:00
|
|
|
}
|
|
|
|
janet_gcunroot(janet_wrap_fiber(return_value.fiber));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convenience method for common case */
|
2021-01-18 22:44:22 +00:00
|
|
|
JANET_NO_RETURN
|
2021-01-03 17:04:21 +00:00
|
|
|
void janet_ev_threaded_await(JanetThreadedSubroutine fp, int tag, int argi, void *argp) {
|
|
|
|
JanetEVGenericMessage arguments;
|
2021-08-15 20:25:07 +00:00
|
|
|
memset(&arguments, 0, sizeof(arguments));
|
2021-01-03 17:04:21 +00:00
|
|
|
arguments.tag = tag;
|
|
|
|
arguments.argi = argi;
|
|
|
|
arguments.argp = argp;
|
|
|
|
arguments.fiber = janet_root_fiber();
|
|
|
|
janet_gcroot(janet_wrap_fiber(arguments.fiber));
|
|
|
|
janet_ev_threaded_call(fp, arguments, janet_ev_default_threaded_callback);
|
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* C API helpers for reading and writing from streams.
|
2020-11-14 17:48:23 +00:00
|
|
|
* There is some networking code in here as well as generic
|
2021-01-03 17:04:21 +00:00
|
|
|
* reading and writing primitives.
|
|
|
|
*/
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2020-11-15 16:06:20 +00:00
|
|
|
void janet_stream_flags(JanetStream *stream, uint32_t flags) {
|
|
|
|
if (stream->flags & JANET_STREAM_CLOSED) {
|
|
|
|
janet_panic("stream is closed");
|
|
|
|
}
|
|
|
|
if ((stream->flags & flags) != flags) {
|
|
|
|
const char *rmsg = "", *wmsg = "", *amsg = "", *dmsg = "", *smsg = "stream";
|
|
|
|
if (flags & JANET_STREAM_READABLE) rmsg = "readable ";
|
|
|
|
if (flags & JANET_STREAM_WRITABLE) wmsg = "writable ";
|
|
|
|
if (flags & JANET_STREAM_ACCEPTABLE) amsg = "server ";
|
|
|
|
if (flags & JANET_STREAM_UDPSERVER) dmsg = "datagram ";
|
|
|
|
if (flags & JANET_STREAM_SOCKET) smsg = "socket";
|
|
|
|
janet_panicf("bad stream, expected %s%s%s%s%s", rmsg, wmsg, amsg, dmsg, smsg);
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
|
|
|
|
/* When there is an IO error, we need to be able to convert it to a Janet
|
|
|
|
* string to raise a Janet error. */
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
#define JANET_EV_CHUNKSIZE 4096
|
|
|
|
Janet janet_ev_lasterr(void) {
|
|
|
|
int code = GetLastError();
|
|
|
|
char msgbuf[256];
|
|
|
|
msgbuf[0] = '\0';
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
NULL,
|
|
|
|
code,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
msgbuf,
|
|
|
|
sizeof(msgbuf),
|
|
|
|
NULL);
|
|
|
|
if (!*msgbuf) sprintf(msgbuf, "%d", code);
|
|
|
|
char *c = msgbuf;
|
|
|
|
while (*c) {
|
|
|
|
if (*c == '\n' || *c == '\r') {
|
|
|
|
*c = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return janet_cstringv(msgbuf);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Janet janet_ev_lasterr(void) {
|
|
|
|
return janet_cstringv(strerror(errno));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* State machine for read/recv/recvfrom */
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
JANET_ASYNC_READMODE_READ,
|
|
|
|
JANET_ASYNC_READMODE_RECV,
|
|
|
|
JANET_ASYNC_READMODE_RECVFROM
|
|
|
|
} JanetReadMode;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
OVERLAPPED overlapped;
|
2023-01-21 16:55:42 +00:00
|
|
|
DWORD flags;
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
WSABUF wbuf;
|
|
|
|
struct sockaddr from;
|
|
|
|
int fromlen;
|
|
|
|
#endif
|
|
|
|
uint8_t chunk_buf[JANET_EV_CHUNKSIZE];
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
#endif
|
2023-10-07 15:01:35 +00:00
|
|
|
int32_t bytes_left;
|
|
|
|
int32_t bytes_read;
|
|
|
|
JanetBuffer *buf;
|
|
|
|
int is_chunk;
|
|
|
|
JanetReadMode mode;
|
2020-11-14 17:48:23 +00:00
|
|
|
} StateRead;
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
|
|
|
|
JanetStream *stream = fiber->ev_stream;
|
|
|
|
StateRead *state = (StateRead *) fiber->ev_state;
|
2020-11-14 17:48:23 +00:00
|
|
|
switch (event) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case JANET_ASYNC_EVENT_MARK:
|
|
|
|
janet_mark(janet_wrap_buffer(state->buf));
|
|
|
|
break;
|
|
|
|
case JANET_ASYNC_EVENT_CLOSE:
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2023-10-07 15:01:35 +00:00
|
|
|
case JANET_ASYNC_EVENT_FAILED:
|
2020-11-14 17:48:23 +00:00
|
|
|
case JANET_ASYNC_EVENT_COMPLETE: {
|
|
|
|
/* Called when read finished */
|
2023-10-07 19:11:14 +00:00
|
|
|
uint32_t ev_bytes = (uint32_t) state->overlapped.InternalHigh;
|
2023-10-07 18:25:20 +00:00
|
|
|
state->bytes_read += ev_bytes;
|
2020-11-14 20:28:10 +00:00
|
|
|
if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 18:25:20 +00:00
|
|
|
janet_buffer_push_bytes(state->buf, state->chunk_buf, ev_bytes);
|
|
|
|
state->bytes_left -= ev_bytes;
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2023-10-07 18:25:20 +00:00
|
|
|
if (state->bytes_left == 0 || !state->is_chunk || ev_bytes == 0) {
|
2020-11-14 17:48:23 +00:00
|
|
|
Janet resume_val;
|
|
|
|
#ifdef JANET_NET
|
|
|
|
if (state->mode == JANET_ASYNC_READMODE_RECVFROM) {
|
|
|
|
void *abst = janet_abstract(&janet_address_type, state->fromlen);
|
|
|
|
memcpy(abst, &state->from, state->fromlen);
|
|
|
|
resume_val = janet_wrap_abstract(abst);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
resume_val = janet_wrap_buffer(state->buf);
|
|
|
|
}
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_schedule(fiber, resume_val);
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fallthrough */
|
2023-10-08 23:25:46 +00:00
|
|
|
case JANET_ASYNC_EVENT_INIT: {
|
2020-11-14 17:48:23 +00:00
|
|
|
int32_t chunk_size = state->bytes_left > JANET_EV_CHUNKSIZE ? JANET_EV_CHUNKSIZE : state->bytes_left;
|
|
|
|
memset(&(state->overlapped), 0, sizeof(OVERLAPPED));
|
|
|
|
int status;
|
|
|
|
#ifdef JANET_NET
|
2021-01-12 00:00:31 +00:00
|
|
|
if (state->mode == JANET_ASYNC_READMODE_RECVFROM) {
|
2020-11-14 17:48:23 +00:00
|
|
|
state->wbuf.len = (ULONG) chunk_size;
|
2023-01-21 16:37:34 +00:00
|
|
|
state->wbuf.buf = (char *) state->chunk_buf;
|
2023-05-30 17:33:34 +00:00
|
|
|
state->fromlen = sizeof(state->from);
|
2023-10-07 15:01:35 +00:00
|
|
|
status = WSARecvFrom((SOCKET) stream->handle, &state->wbuf, 1,
|
2021-01-12 00:00:31 +00:00
|
|
|
NULL, &state->flags, &state->from, &state->fromlen, &state->overlapped, NULL);
|
2020-11-14 20:40:39 +00:00
|
|
|
if (status && (WSA_IO_PENDING != WSAGetLastError())) {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_cancel(fiber, janet_ev_lasterr());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
2020-11-14 20:40:39 +00:00
|
|
|
}
|
|
|
|
} else
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
2020-11-14 20:40:39 +00:00
|
|
|
{
|
2023-10-07 15:01:35 +00:00
|
|
|
/* Some handles (not all) read from the offset in lpOverlapped
|
2022-01-08 17:27:14 +00:00
|
|
|
* if its not set before calling `ReadFile` these streams will always read from offset 0 */
|
2022-01-07 21:32:39 +00:00
|
|
|
state->overlapped.Offset = (DWORD) state->bytes_read;
|
|
|
|
|
2023-10-07 15:01:35 +00:00
|
|
|
status = ReadFile(stream->handle, state->chunk_buf, chunk_size, NULL, &state->overlapped);
|
2023-01-21 16:55:42 +00:00
|
|
|
if (!status && (ERROR_IO_PENDING != GetLastError())) {
|
|
|
|
if (GetLastError() == ERROR_BROKEN_PIPE) {
|
2021-01-12 00:32:56 +00:00
|
|
|
if (state->bytes_read) {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_buffer(state->buf));
|
2021-01-12 00:32:56 +00:00
|
|
|
} else {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
2021-01-12 00:32:56 +00:00
|
|
|
}
|
2020-11-14 22:03:51 +00:00
|
|
|
} else {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_cancel(fiber, janet_ev_lasterr());
|
2020-11-14 22:03:51 +00:00
|
|
|
}
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
2020-11-14 20:40:39 +00:00
|
|
|
}
|
2020-11-14 20:28:10 +00:00
|
|
|
}
|
2023-10-08 23:25:46 +00:00
|
|
|
janet_async_in_flight(fiber);
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
2020-11-14 20:40:39 +00:00
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
#else
|
2021-01-23 20:26:24 +00:00
|
|
|
case JANET_ASYNC_EVENT_ERR: {
|
2020-11-14 21:01:52 +00:00
|
|
|
if (state->bytes_read) {
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_buffer(state->buf));
|
2020-11-14 21:01:52 +00:00
|
|
|
} else {
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
2020-11-14 20:28:10 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 21:01:52 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
|
|
|
|
read_more:
|
2021-01-23 20:26:24 +00:00
|
|
|
case JANET_ASYNC_EVENT_HUP:
|
2023-10-08 23:25:46 +00:00
|
|
|
case JANET_ASYNC_EVENT_INIT:
|
2020-12-30 02:37:59 +00:00
|
|
|
case JANET_ASYNC_EVENT_READ: {
|
2020-11-14 21:01:52 +00:00
|
|
|
JanetBuffer *buffer = state->buf;
|
|
|
|
int32_t bytes_left = state->bytes_left;
|
2021-09-21 14:55:40 +00:00
|
|
|
int32_t read_limit = state->is_chunk ? (bytes_left > 4096 ? 4096 : bytes_left) : bytes_left;
|
2020-12-30 02:37:59 +00:00
|
|
|
janet_buffer_extra(buffer, read_limit);
|
2020-11-14 21:01:52 +00:00
|
|
|
ssize_t nread;
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
2020-11-14 21:01:52 +00:00
|
|
|
char saddr[256];
|
|
|
|
socklen_t socklen = sizeof(saddr);
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
2020-11-14 21:01:52 +00:00
|
|
|
do {
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
2020-11-14 21:01:52 +00:00
|
|
|
if (state->mode == JANET_ASYNC_READMODE_RECVFROM) {
|
2023-10-06 05:37:19 +00:00
|
|
|
nread = recvfrom(stream->handle, buffer->data + buffer->count, read_limit, state->flags,
|
2020-11-14 21:01:52 +00:00
|
|
|
(struct sockaddr *)&saddr, &socklen);
|
|
|
|
} else if (state->mode == JANET_ASYNC_READMODE_RECV) {
|
2023-10-06 05:37:19 +00:00
|
|
|
nread = recv(stream->handle, buffer->data + buffer->count, read_limit, state->flags);
|
2020-11-14 21:01:52 +00:00
|
|
|
} else
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
2020-11-14 21:01:52 +00:00
|
|
|
{
|
2023-10-06 05:37:19 +00:00
|
|
|
nread = read(stream->handle, buffer->data + buffer->count, read_limit);
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
2020-11-14 21:01:52 +00:00
|
|
|
} while (nread == -1 && errno == EINTR);
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2020-11-14 21:01:52 +00:00
|
|
|
/* Check for errors - special case errors that can just be waited on to fix */
|
|
|
|
if (nread == -1) {
|
2020-11-16 22:42:09 +00:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
2023-10-06 05:37:19 +00:00
|
|
|
break;
|
2020-11-16 22:42:09 +00:00
|
|
|
}
|
|
|
|
/* In stream protocols, a pipe error is end of stream */
|
|
|
|
if (errno == EPIPE && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
|
|
|
|
nread = 0;
|
|
|
|
} else {
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_cancel(fiber, janet_ev_lasterr());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-16 22:42:09 +00:00
|
|
|
}
|
2020-11-14 21:01:52 +00:00
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2020-11-14 21:01:52 +00:00
|
|
|
/* Only allow 0-length packets in recv-from. In stream protocols, a zero length packet is EOS. */
|
|
|
|
state->bytes_read += nread;
|
|
|
|
if (state->bytes_read == 0 && (state->mode != JANET_ASYNC_READMODE_RECVFROM)) {
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 21:01:52 +00:00
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
|
2020-11-14 21:01:52 +00:00
|
|
|
/* Increment buffer counts */
|
|
|
|
buffer->count += nread;
|
|
|
|
bytes_left -= nread;
|
|
|
|
state->bytes_left = bytes_left;
|
|
|
|
|
|
|
|
/* Resume if done */
|
|
|
|
if (!state->is_chunk || bytes_left == 0 || nread == 0) {
|
|
|
|
Janet resume_val;
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
2020-11-14 21:01:52 +00:00
|
|
|
if (state->mode == JANET_ASYNC_READMODE_RECVFROM) {
|
|
|
|
void *abst = janet_abstract(&janet_address_type, socklen);
|
|
|
|
memcpy(abst, &saddr, socklen);
|
|
|
|
resume_val = janet_wrap_abstract(abst);
|
|
|
|
} else
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
2020-11-14 21:01:52 +00:00
|
|
|
{
|
|
|
|
resume_val = janet_wrap_buffer(buffer);
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_schedule(fiber, resume_val);
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
2023-10-06 05:37:19 +00:00
|
|
|
|
|
|
|
/* Read some more if possible */
|
|
|
|
goto read_more;
|
2020-11-14 21:01:52 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
#endif
|
2020-11-14 21:01:52 +00:00
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
static JANET_NO_RETURN void janet_ev_read_generic(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int is_chunked, JanetReadMode mode, int flags) {
|
|
|
|
StateRead *state = janet_malloc(sizeof(StateRead));
|
2020-11-14 17:48:23 +00:00
|
|
|
state->is_chunk = is_chunked;
|
|
|
|
state->buf = buf;
|
|
|
|
state->bytes_left = nbytes;
|
2020-11-14 20:28:10 +00:00
|
|
|
state->bytes_read = 0;
|
2020-11-14 17:48:23 +00:00
|
|
|
state->mode = mode;
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
state->flags = (DWORD) flags;
|
|
|
|
#else
|
|
|
|
state->flags = flags;
|
|
|
|
#endif
|
2023-10-08 23:25:46 +00:00
|
|
|
janet_async_start(stream, JANET_ASYNC_LISTEN_READ, ev_callback_read, state);
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_read(JanetStream *stream, JanetBuffer *buf, int32_t nbytes) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_read_generic(stream, buf, nbytes, 0, JANET_ASYNC_READMODE_READ, 0);
|
|
|
|
}
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_readchunk(JanetStream *stream, JanetBuffer *buf, int32_t nbytes) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_read_generic(stream, buf, nbytes, 1, JANET_ASYNC_READMODE_READ, 0);
|
|
|
|
}
|
|
|
|
#ifdef JANET_NET
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_recv(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_read_generic(stream, buf, nbytes, 0, JANET_ASYNC_READMODE_RECV, flags);
|
|
|
|
}
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_recvchunk(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_read_generic(stream, buf, nbytes, 1, JANET_ASYNC_READMODE_RECV, flags);
|
|
|
|
}
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_recvfrom(JanetStream *stream, JanetBuffer *buf, int32_t nbytes, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_read_generic(stream, buf, nbytes, 0, JANET_ASYNC_READMODE_RECVFROM, flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* State machine for write/send/send-to
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
JANET_ASYNC_WRITEMODE_WRITE,
|
|
|
|
JANET_ASYNC_WRITEMODE_SEND,
|
2023-09-24 17:08:40 +00:00
|
|
|
JANET_ASYNC_WRITEMODE_SENDTO
|
2020-11-14 17:48:23 +00:00
|
|
|
} JanetWriteMode;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
OVERLAPPED overlapped;
|
2023-01-21 16:55:42 +00:00
|
|
|
DWORD flags;
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_NET
|
|
|
|
WSABUF wbuf;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
int32_t start;
|
|
|
|
#endif
|
2023-10-07 15:01:35 +00:00
|
|
|
union {
|
|
|
|
JanetBuffer *buf;
|
|
|
|
const uint8_t *str;
|
|
|
|
} src;
|
|
|
|
int is_buffer;
|
|
|
|
JanetWriteMode mode;
|
|
|
|
void *dest_abst;
|
2020-11-14 17:48:23 +00:00
|
|
|
} StateWrite;
|
|
|
|
|
2023-10-06 05:37:19 +00:00
|
|
|
void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
|
|
|
|
JanetStream *stream = fiber->ev_stream;
|
|
|
|
StateWrite *state = (StateWrite *) fiber->ev_state;
|
2020-11-14 17:48:23 +00:00
|
|
|
switch (event) {
|
|
|
|
default:
|
|
|
|
break;
|
2023-08-13 16:59:21 +00:00
|
|
|
case JANET_ASYNC_EVENT_MARK: {
|
2023-09-24 17:08:40 +00:00
|
|
|
janet_mark(state->is_buffer
|
|
|
|
? janet_wrap_buffer(state->src.buf)
|
|
|
|
: janet_wrap_string(state->src.str));
|
2020-11-14 17:48:23 +00:00
|
|
|
if (state->mode == JANET_ASYNC_WRITEMODE_SENDTO) {
|
|
|
|
janet_mark(janet_wrap_abstract(state->dest_abst));
|
|
|
|
}
|
|
|
|
break;
|
2023-08-13 16:59:21 +00:00
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
case JANET_ASYNC_EVENT_CLOSE:
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_cancel(fiber, janet_cstringv("stream closed"));
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2023-10-07 15:01:35 +00:00
|
|
|
case JANET_ASYNC_EVENT_FAILED:
|
2020-11-14 17:48:23 +00:00
|
|
|
case JANET_ASYNC_EVENT_COMPLETE: {
|
|
|
|
/* Called when write finished */
|
2023-10-07 19:11:14 +00:00
|
|
|
uint32_t ev_bytes = (uint32_t) state->overlapped.InternalHigh;
|
2023-10-07 18:25:20 +00:00
|
|
|
if (ev_bytes == 0 && (state->mode != JANET_ASYNC_WRITEMODE_SENDTO)) {
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_cancel(fiber, janet_cstringv("disconnect"));
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 15:01:35 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
break;
|
2023-10-08 23:25:46 +00:00
|
|
|
case JANET_ASYNC_EVENT_INIT: {
|
2020-11-14 17:48:23 +00:00
|
|
|
/* Begin write */
|
2020-11-14 21:01:52 +00:00
|
|
|
int32_t len;
|
2020-11-14 17:48:23 +00:00
|
|
|
const uint8_t *bytes;
|
|
|
|
if (state->is_buffer) {
|
|
|
|
/* If buffer, convert to string. */
|
|
|
|
/* TODO - be more efficient about this */
|
|
|
|
JanetBuffer *buffer = state->src.buf;
|
|
|
|
JanetString str = janet_string(buffer->data, buffer->count);
|
|
|
|
bytes = str;
|
|
|
|
len = buffer->count;
|
|
|
|
state->is_buffer = 0;
|
|
|
|
state->src.str = str;
|
|
|
|
} else {
|
|
|
|
bytes = state->src.str;
|
|
|
|
len = janet_string_length(bytes);
|
|
|
|
}
|
|
|
|
memset(&(state->overlapped), 0, sizeof(WSAOVERLAPPED));
|
|
|
|
|
|
|
|
int status;
|
|
|
|
#ifdef JANET_NET
|
2021-01-12 00:00:31 +00:00
|
|
|
if (state->mode == JANET_ASYNC_WRITEMODE_SENDTO) {
|
2023-10-07 15:01:35 +00:00
|
|
|
SOCKET sock = (SOCKET) stream->handle;
|
2020-11-14 17:48:23 +00:00
|
|
|
state->wbuf.buf = (char *) bytes;
|
|
|
|
state->wbuf.len = len;
|
2021-01-12 00:00:31 +00:00
|
|
|
const struct sockaddr *to = state->dest_abst;
|
|
|
|
int tolen = (int) janet_abstract_size((void *) to);
|
|
|
|
status = WSASendTo(sock, &state->wbuf, 1, NULL, state->flags, to, tolen, &state->overlapped, NULL);
|
2023-10-07 15:01:35 +00:00
|
|
|
if (status) {
|
|
|
|
if (WSA_IO_PENDING == WSAGetLastError()) {
|
2023-10-08 23:25:46 +00:00
|
|
|
janet_async_in_flight(fiber);
|
2023-10-07 15:01:35 +00:00
|
|
|
} else {
|
|
|
|
janet_cancel(fiber, janet_ev_lasterr());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2021-07-25 01:30:36 +00:00
|
|
|
/*
|
|
|
|
* File handles in IOCP need to specify this if they are writing to the
|
2021-07-24 16:55:04 +00:00
|
|
|
* ends of files, like how this is used here.
|
|
|
|
* If the underlying resource doesn't support seeking
|
|
|
|
* byte offsets, they will be ignored
|
|
|
|
* but this otherwise writes to the end of the file in question
|
|
|
|
* Right now, os/open streams aren't seekable, so this works.
|
|
|
|
* for more details see the lpOverlapped parameter in
|
|
|
|
* https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefile
|
|
|
|
*/
|
2021-07-24 08:30:00 +00:00
|
|
|
state->overlapped.Offset = (DWORD) 0xFFFFFFFF;
|
|
|
|
state->overlapped.OffsetHigh = (DWORD) 0xFFFFFFFF;
|
2023-10-07 15:01:35 +00:00
|
|
|
status = WriteFile(stream->handle, bytes, len, NULL, &state->overlapped);
|
|
|
|
if (!status) {
|
|
|
|
if (ERROR_IO_PENDING == GetLastError()) {
|
2023-10-08 23:25:46 +00:00
|
|
|
janet_async_in_flight(fiber);
|
2023-10-07 15:01:35 +00:00
|
|
|
} else {
|
|
|
|
janet_cancel(fiber, janet_ev_lasterr());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
return;
|
|
|
|
}
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#else
|
2020-11-14 20:28:10 +00:00
|
|
|
case JANET_ASYNC_EVENT_ERR:
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_cancel(fiber, janet_cstringv("stream err"));
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 20:28:10 +00:00
|
|
|
case JANET_ASYNC_EVENT_HUP:
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_cancel(fiber, janet_cstringv("stream hup"));
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2023-10-08 23:25:46 +00:00
|
|
|
case JANET_ASYNC_EVENT_INIT:
|
2020-11-14 17:48:23 +00:00
|
|
|
case JANET_ASYNC_EVENT_WRITE: {
|
|
|
|
int32_t start, len;
|
|
|
|
const uint8_t *bytes;
|
|
|
|
start = state->start;
|
|
|
|
if (state->is_buffer) {
|
|
|
|
JanetBuffer *buffer = state->src.buf;
|
|
|
|
bytes = buffer->data;
|
|
|
|
len = buffer->count;
|
|
|
|
} else {
|
|
|
|
bytes = state->src.str;
|
|
|
|
len = janet_string_length(bytes);
|
|
|
|
}
|
|
|
|
ssize_t nwrote = 0;
|
|
|
|
if (start < len) {
|
|
|
|
int32_t nbytes = len - start;
|
|
|
|
void *dest_abst = state->dest_abst;
|
|
|
|
do {
|
|
|
|
#ifdef JANET_NET
|
|
|
|
if (state->mode == JANET_ASYNC_WRITEMODE_SENDTO) {
|
2023-10-06 05:37:19 +00:00
|
|
|
nwrote = sendto(stream->handle, bytes + start, nbytes, state->flags,
|
2020-11-14 17:48:23 +00:00
|
|
|
(struct sockaddr *) dest_abst, janet_abstract_size(dest_abst));
|
|
|
|
} else if (state->mode == JANET_ASYNC_WRITEMODE_SEND) {
|
2023-10-06 05:37:19 +00:00
|
|
|
nwrote = send(stream->handle, bytes + start, nbytes, state->flags);
|
2020-11-14 17:48:23 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2023-10-06 05:37:19 +00:00
|
|
|
nwrote = write(stream->handle, bytes + start, nbytes);
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
} while (nwrote == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
/* Handle write errors */
|
|
|
|
if (nwrote == -1) {
|
2023-10-08 20:26:02 +00:00
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_cancel(fiber, janet_ev_lasterr());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unless using datagrams, empty message is a disconnect */
|
|
|
|
if (nwrote == 0 && !dest_abst) {
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_cancel(fiber, janet_cstringv("disconnect"));
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nwrote > 0) {
|
|
|
|
start += nwrote;
|
|
|
|
} else {
|
|
|
|
start = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state->start = start;
|
|
|
|
if (start >= len) {
|
2023-10-06 05:37:19 +00:00
|
|
|
janet_schedule(fiber, janet_wrap_nil());
|
|
|
|
janet_async_end(fiber);
|
|
|
|
break;
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
static JANET_NO_RETURN void janet_ev_write_generic(JanetStream *stream, void *buf, void *dest_abst, JanetWriteMode mode, int is_buffer, int flags) {
|
|
|
|
StateWrite *state = janet_malloc(sizeof(StateWrite));
|
2020-11-14 17:48:23 +00:00
|
|
|
state->is_buffer = is_buffer;
|
|
|
|
state->src.buf = buf;
|
|
|
|
state->dest_abst = dest_abst;
|
|
|
|
state->mode = mode;
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
state->flags = (DWORD) flags;
|
|
|
|
#else
|
|
|
|
state->flags = flags;
|
2023-10-05 03:35:04 +00:00
|
|
|
state->start = 0;
|
2023-10-07 15:01:35 +00:00
|
|
|
#endif
|
2023-10-08 23:25:46 +00:00
|
|
|
janet_async_start(stream, JANET_ASYNC_LISTEN_WRITE, ev_callback_write, state);
|
2020-11-14 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_write_buffer(JanetStream *stream, JanetBuffer *buf) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_write_generic(stream, buf, NULL, JANET_ASYNC_WRITEMODE_WRITE, 1, 0);
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_write_string(JanetStream *stream, JanetString str) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_write_generic(stream, (void *) str, NULL, JANET_ASYNC_WRITEMODE_WRITE, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef JANET_NET
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_send_buffer(JanetStream *stream, JanetBuffer *buf, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_write_generic(stream, buf, NULL, JANET_ASYNC_WRITEMODE_SEND, 1, flags);
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_send_string(JanetStream *stream, JanetString str, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_write_generic(stream, (void *) str, NULL, JANET_ASYNC_WRITEMODE_SEND, 0, flags);
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_sendto_buffer(JanetStream *stream, JanetBuffer *buf, void *dest, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_write_generic(stream, buf, dest, JANET_ASYNC_WRITEMODE_SENDTO, 1, flags);
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:25:46 +00:00
|
|
|
JANET_NO_RETURN void janet_ev_sendto_string(JanetStream *stream, JanetString str, void *dest, int flags) {
|
2020-11-14 17:48:23 +00:00
|
|
|
janet_ev_write_generic(stream, (void *) str, dest, JANET_ASYNC_WRITEMODE_SENDTO, 0, flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-18 16:53:36 +00:00
|
|
|
/* For a pipe ID */
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
static volatile long PipeSerialNumber;
|
|
|
|
#endif
|
|
|
|
|
2021-10-14 18:57:51 +00:00
|
|
|
/*
|
|
|
|
* mode = 0: both sides non-blocking.
|
|
|
|
* mode = 1: only read side non-blocking: write side sent to subprocess
|
|
|
|
* mode = 2: only write side non-blocking: read side sent to subprocess
|
|
|
|
*/
|
2021-01-16 21:11:07 +00:00
|
|
|
int janet_make_pipe(JanetHandle handles[2], int mode) {
|
2020-11-18 16:53:36 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
/*
|
|
|
|
* On windows, the built in CreatePipe function doesn't support overlapped IO
|
|
|
|
* so we lift from the windows source code and modify for our own version.
|
|
|
|
*/
|
2021-01-16 21:11:07 +00:00
|
|
|
JanetHandle shandle, chandle;
|
2023-01-21 16:37:34 +00:00
|
|
|
CHAR PipeNameBuffer[MAX_PATH];
|
2021-01-11 17:10:23 +00:00
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
memset(&saAttr, 0, sizeof(saAttr));
|
|
|
|
saAttr.nLength = sizeof(saAttr);
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
2020-11-18 16:53:36 +00:00
|
|
|
sprintf(PipeNameBuffer,
|
|
|
|
"\\\\.\\Pipe\\JanetPipeFile.%08x.%08x",
|
2023-01-21 16:37:34 +00:00
|
|
|
(unsigned int) GetCurrentProcessId(),
|
|
|
|
(unsigned int) InterlockedIncrement(&PipeSerialNumber));
|
2021-01-16 21:11:07 +00:00
|
|
|
|
|
|
|
/* server handle goes to subprocess */
|
|
|
|
shandle = CreateNamedPipeA(
|
2020-11-18 16:53:36 +00:00
|
|
|
PipeNameBuffer,
|
2021-01-16 21:11:07 +00:00
|
|
|
(mode == 2 ? PIPE_ACCESS_INBOUND : PIPE_ACCESS_OUTBOUND) | FILE_FLAG_OVERLAPPED,
|
|
|
|
PIPE_TYPE_BYTE | PIPE_WAIT,
|
|
|
|
255, /* Max number of pipes for duplication. */
|
2020-11-18 16:53:36 +00:00
|
|
|
4096, /* Out buffer size */
|
|
|
|
4096, /* In buffer size */
|
|
|
|
120 * 1000, /* Timeout in ms */
|
2021-01-11 17:10:23 +00:00
|
|
|
&saAttr);
|
2021-01-16 21:11:07 +00:00
|
|
|
if (shandle == INVALID_HANDLE_VALUE) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we keep client handle */
|
|
|
|
chandle = CreateFileA(
|
2020-11-18 16:53:36 +00:00
|
|
|
PipeNameBuffer,
|
2021-01-16 21:11:07 +00:00
|
|
|
(mode == 2 ? GENERIC_WRITE : GENERIC_READ),
|
2020-11-18 16:53:36 +00:00
|
|
|
0,
|
2021-01-11 17:10:23 +00:00
|
|
|
&saAttr,
|
2020-11-18 16:53:36 +00:00
|
|
|
OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
|
|
|
NULL);
|
2021-01-16 21:11:07 +00:00
|
|
|
|
|
|
|
if (chandle == INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(shandle);
|
2020-11-18 16:53:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-01-16 21:11:07 +00:00
|
|
|
if (mode == 2) {
|
|
|
|
handles[0] = shandle;
|
|
|
|
handles[1] = chandle;
|
|
|
|
} else {
|
|
|
|
handles[0] = chandle;
|
|
|
|
handles[1] = shandle;
|
|
|
|
}
|
2020-11-18 16:53:36 +00:00
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
if (pipe(handles)) return -1;
|
2021-11-27 00:44:11 +00:00
|
|
|
if (mode != 2 && fcntl(handles[0], F_SETFD, FD_CLOEXEC)) goto error;
|
|
|
|
if (mode != 1 && fcntl(handles[1], F_SETFD, FD_CLOEXEC)) goto error;
|
2021-10-14 18:57:51 +00:00
|
|
|
if (mode != 2 && fcntl(handles[0], F_SETFL, O_NONBLOCK)) goto error;
|
|
|
|
if (mode != 1 && fcntl(handles[1], F_SETFL, O_NONBLOCK)) goto error;
|
2020-11-18 16:53:36 +00:00
|
|
|
return 0;
|
2021-01-03 17:04:21 +00:00
|
|
|
error:
|
|
|
|
close(handles[0]);
|
|
|
|
close(handles[1]);
|
|
|
|
return -1;
|
2020-11-18 16:53:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
/* C functions */
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_ev_go,
|
2022-09-14 00:17:53 +00:00
|
|
|
"(ev/go fiber-or-fun &opt value supervisor)",
|
2023-01-19 03:41:06 +00:00
|
|
|
"Put a fiber on the event loop to be resumed later. If a function is used, it is wrapped "
|
2022-09-14 00:17:53 +00:00
|
|
|
"with `fiber/new` first. "
|
|
|
|
"Optionally pass a value to resume with, otherwise resumes with nil. Returns the fiber. "
|
2021-08-27 07:06:14 +00:00
|
|
|
"An optional `core/channel` can be provided as a supervisor. When various "
|
2021-07-30 02:29:08 +00:00
|
|
|
"events occur in the newly scheduled fiber, an event will be pushed to the supervisor. "
|
|
|
|
"If not provided, the new fiber will inherit the current supervisor.") {
|
2021-01-13 03:35:28 +00:00
|
|
|
janet_arity(argc, 1, 3);
|
2021-06-25 23:58:19 +00:00
|
|
|
Janet value = argc >= 2 ? argv[1] : janet_wrap_nil();
|
2021-08-17 02:14:06 +00:00
|
|
|
void *supervisor = janet_optabstract(argv, argc, 2, &janet_channel_type, janet_vm.root_fiber->supervisor_channel);
|
2021-08-31 03:22:22 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
if (janet_checktype(argv[0], JANET_FUNCTION)) {
|
|
|
|
/* Create a fiber for the user */
|
|
|
|
JanetFunction *func = janet_unwrap_function(argv[0]);
|
|
|
|
if (func->def->min_arity > 1) {
|
|
|
|
janet_panicf("task function must accept 0 or 1 arguments");
|
|
|
|
}
|
|
|
|
fiber = janet_fiber(func, 64, func->def->min_arity, &value);
|
|
|
|
fiber->flags |=
|
|
|
|
JANET_FIBER_MASK_ERROR |
|
|
|
|
JANET_FIBER_MASK_USER0 |
|
|
|
|
JANET_FIBER_MASK_USER1 |
|
|
|
|
JANET_FIBER_MASK_USER2 |
|
|
|
|
JANET_FIBER_MASK_USER3 |
|
|
|
|
JANET_FIBER_MASK_USER4;
|
|
|
|
if (!janet_vm.fiber->env) {
|
|
|
|
janet_vm.fiber->env = janet_table(0);
|
|
|
|
}
|
|
|
|
fiber->env = janet_table(0);
|
|
|
|
fiber->env->proto = janet_vm.fiber->env;
|
|
|
|
} else {
|
|
|
|
fiber = janet_getfiber(argv, 0);
|
|
|
|
}
|
2021-08-17 02:14:06 +00:00
|
|
|
fiber->supervisor_channel = supervisor;
|
2021-01-18 22:44:22 +00:00
|
|
|
janet_schedule(fiber, value);
|
2021-08-31 03:22:22 +00:00
|
|
|
return janet_wrap_fiber(fiber);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
2022-07-03 17:08:21 +00:00
|
|
|
#define JANET_THREAD_SUPERVISOR_FLAG 0x100
|
|
|
|
|
2021-01-22 18:52:45 +00:00
|
|
|
/* For ev/thread - Run an interpreter in the new thread. */
|
|
|
|
static JanetEVGenericMessage janet_go_thread_subr(JanetEVGenericMessage args) {
|
|
|
|
JanetBuffer *buffer = (JanetBuffer *) args.argp;
|
|
|
|
const uint8_t *nextbytes = buffer->data;
|
|
|
|
const uint8_t *endbytes = nextbytes + buffer->count;
|
2021-07-30 02:29:08 +00:00
|
|
|
uint32_t flags = args.tag;
|
|
|
|
args.tag = 0;
|
2021-01-22 18:52:45 +00:00
|
|
|
janet_init();
|
2023-02-06 14:41:04 +00:00
|
|
|
janet_vm.sandbox_flags = (uint32_t) args.argi;
|
2021-01-22 18:52:45 +00:00
|
|
|
JanetTryState tstate;
|
|
|
|
JanetSignal signal = janet_try(&tstate);
|
|
|
|
if (!signal) {
|
2021-07-30 02:29:08 +00:00
|
|
|
|
|
|
|
/* Set abstract registry */
|
2021-08-15 18:14:33 +00:00
|
|
|
if (!(flags & 0x2)) {
|
2021-07-30 02:29:08 +00:00
|
|
|
Janet aregv = janet_unmarshal(nextbytes, endbytes - nextbytes,
|
|
|
|
JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
|
|
|
|
if (!janet_checktype(aregv, JANET_TABLE)) janet_panic("expected table for abstract registry");
|
|
|
|
janet_vm.abstract_registry = janet_unwrap_table(aregv);
|
2021-08-20 02:51:53 +00:00
|
|
|
janet_gcroot(janet_wrap_table(janet_vm.abstract_registry));
|
2021-07-30 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 02:14:06 +00:00
|
|
|
/* Get supervsior */
|
2022-07-03 17:08:21 +00:00
|
|
|
if (flags & JANET_THREAD_SUPERVISOR_FLAG) {
|
2021-08-17 02:14:06 +00:00
|
|
|
Janet sup =
|
|
|
|
janet_unmarshal(nextbytes, endbytes - nextbytes,
|
|
|
|
JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
|
2021-08-27 16:46:42 +00:00
|
|
|
/* Hack - use a global variable to avoid longjmp clobber */
|
|
|
|
janet_vm.user = janet_unwrap_pointer(sup);
|
2021-08-17 02:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
/* Set cfunction registry */
|
2021-08-15 18:14:33 +00:00
|
|
|
if (!(flags & 0x4)) {
|
2021-07-30 02:29:08 +00:00
|
|
|
uint32_t count1;
|
|
|
|
memcpy(&count1, nextbytes, sizeof(count1));
|
|
|
|
size_t count = (size_t) count1;
|
|
|
|
if (count > (endbytes - nextbytes) * sizeof(JanetCFunRegistry)) {
|
|
|
|
janet_panic("thread message invalid");
|
|
|
|
}
|
|
|
|
janet_vm.registry_count = count;
|
|
|
|
janet_vm.registry_cap = count;
|
|
|
|
janet_vm.registry = janet_malloc(count * sizeof(JanetCFunRegistry));
|
|
|
|
if (janet_vm.registry == NULL) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
janet_vm.registry_dirty = 1;
|
2021-08-15 18:14:33 +00:00
|
|
|
nextbytes += sizeof(uint32_t);
|
2021-07-30 02:29:08 +00:00
|
|
|
memcpy(janet_vm.registry, nextbytes, count * sizeof(JanetCFunRegistry));
|
2021-08-15 18:14:33 +00:00
|
|
|
nextbytes += count * sizeof(JanetCFunRegistry);
|
2021-07-30 02:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 18:52:45 +00:00
|
|
|
Janet fiberv = janet_unmarshal(nextbytes, endbytes - nextbytes,
|
|
|
|
JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
|
|
|
|
Janet value = janet_unmarshal(nextbytes, endbytes - nextbytes,
|
|
|
|
JANET_MARSHAL_UNSAFE, NULL, &nextbytes);
|
2021-08-31 03:04:15 +00:00
|
|
|
JanetFiber *fiber;
|
|
|
|
if (!janet_checktype(fiberv, JANET_FIBER)) {
|
|
|
|
if (!janet_checktype(fiberv, JANET_FUNCTION)) {
|
2023-05-23 04:58:52 +00:00
|
|
|
janet_panicf("expected function or fiber, got %v", fiberv);
|
2021-08-31 03:04:15 +00:00
|
|
|
}
|
|
|
|
JanetFunction *func = janet_unwrap_function(fiberv);
|
2023-06-04 17:55:44 +00:00
|
|
|
fiber = janet_fiber(func, 64, func->def->min_arity, &value);
|
|
|
|
if (fiber == NULL) {
|
2021-08-31 03:04:15 +00:00
|
|
|
janet_panicf("thread function must accept 0 or 1 arguments");
|
|
|
|
}
|
|
|
|
fiber->flags |=
|
|
|
|
JANET_FIBER_MASK_ERROR |
|
|
|
|
JANET_FIBER_MASK_USER0 |
|
|
|
|
JANET_FIBER_MASK_USER1 |
|
|
|
|
JANET_FIBER_MASK_USER2 |
|
|
|
|
JANET_FIBER_MASK_USER3 |
|
|
|
|
JANET_FIBER_MASK_USER4;
|
|
|
|
} else {
|
|
|
|
fiber = janet_unwrap_fiber(fiberv);
|
|
|
|
}
|
2022-07-03 17:08:21 +00:00
|
|
|
if (flags & 0x8) {
|
|
|
|
if (NULL == fiber->env) fiber->env = janet_table(0);
|
|
|
|
janet_table_put(fiber->env, janet_ckeywordv("task-id"), value);
|
|
|
|
}
|
2021-08-27 16:46:42 +00:00
|
|
|
fiber->supervisor_channel = janet_vm.user;
|
2021-01-22 18:52:45 +00:00
|
|
|
janet_schedule(fiber, value);
|
|
|
|
janet_loop();
|
|
|
|
args.tag = JANET_EV_TCTAG_NIL;
|
|
|
|
} else {
|
2021-08-27 16:46:42 +00:00
|
|
|
void *supervisor = janet_vm.user;
|
|
|
|
if (NULL != supervisor) {
|
|
|
|
/* Got a supervisor, write error there */
|
|
|
|
Janet pair[] = {
|
|
|
|
janet_ckeywordv("error"),
|
|
|
|
tstate.payload
|
|
|
|
};
|
|
|
|
janet_channel_push((JanetChannel *)supervisor,
|
2021-08-29 16:29:09 +00:00
|
|
|
janet_wrap_tuple(janet_tuple_n(pair, 2)), 2);
|
2021-08-27 16:46:42 +00:00
|
|
|
} else if (flags & 0x1) {
|
|
|
|
/* No wait, just print to stderr */
|
|
|
|
janet_eprintf("thread start failure: %v\n", tstate.payload);
|
2021-01-22 18:52:45 +00:00
|
|
|
} else {
|
2021-08-27 16:46:42 +00:00
|
|
|
/* Make ev/thread call from parent thread error */
|
|
|
|
if (janet_checktype(tstate.payload, JANET_STRING)) {
|
|
|
|
args.tag = JANET_EV_TCTAG_ERR_STRINGF;
|
|
|
|
args.argp = strdup((const char *) janet_unwrap_string(tstate.payload));
|
|
|
|
} else {
|
|
|
|
args.tag = JANET_EV_TCTAG_ERR_STRING;
|
|
|
|
args.argp = "failed to start thread";
|
|
|
|
}
|
2021-01-22 18:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-21 18:30:09 +00:00
|
|
|
janet_restore(&tstate);
|
2021-01-22 18:52:45 +00:00
|
|
|
janet_buffer_deinit(buffer);
|
2021-08-15 18:14:33 +00:00
|
|
|
janet_free(buffer);
|
2021-01-22 18:52:45 +00:00
|
|
|
janet_deinit();
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(cfun_ev_thread,
|
2021-08-31 03:04:15 +00:00
|
|
|
"(ev/thread main &opt value flags supervisor)",
|
|
|
|
"Run `main` in a new operating system thread, optionally passing `value` "
|
|
|
|
"to resume with. The parameter `main` can either be a fiber, or a function that accepts "
|
|
|
|
"0 or 1 arguments. "
|
2021-07-30 02:29:08 +00:00
|
|
|
"Unlike `ev/go`, this function will suspend the current fiber until the thread is complete. "
|
|
|
|
"If you want to run the thread without waiting for a result, pass the `:n` flag to return nil immediately. "
|
2021-08-27 16:52:03 +00:00
|
|
|
"Otherwise, returns nil. Available flags:\n\n"
|
2021-07-30 02:29:08 +00:00
|
|
|
"* `:n` - return immediately\n"
|
2022-07-03 17:08:21 +00:00
|
|
|
"* `:t` - set the task-id of the new thread to value. The task-id is passed in messages to the supervisor channel.\n"
|
2021-07-30 02:29:08 +00:00
|
|
|
"* `:a` - don't copy abstract registry to new thread (performance optimization)\n"
|
|
|
|
"* `:c` - don't copy cfunction registry to new thread (performance optimization)") {
|
2021-08-17 02:14:06 +00:00
|
|
|
janet_arity(argc, 1, 4);
|
2021-07-17 20:13:28 +00:00
|
|
|
Janet value = argc >= 2 ? argv[1] : janet_wrap_nil();
|
2021-08-31 03:04:15 +00:00
|
|
|
if (!janet_checktype(argv[0], JANET_FUNCTION)) janet_getfiber(argv, 0);
|
2021-07-17 20:13:28 +00:00
|
|
|
uint64_t flags = 0;
|
|
|
|
if (argc >= 3) {
|
2022-07-03 17:08:21 +00:00
|
|
|
flags = janet_getflags(argv, 2, "nact");
|
2021-07-17 20:13:28 +00:00
|
|
|
}
|
2021-08-17 02:14:06 +00:00
|
|
|
void *supervisor = janet_optabstract(argv, argc, 3, &janet_channel_type, janet_vm.root_fiber->supervisor_channel);
|
2022-07-03 17:08:21 +00:00
|
|
|
if (NULL != supervisor) flags |= JANET_THREAD_SUPERVISOR_FLAG;
|
2021-08-17 02:14:06 +00:00
|
|
|
|
2021-01-22 18:52:45 +00:00
|
|
|
/* Marshal arguments for the new thread. */
|
2021-03-23 10:00:48 +00:00
|
|
|
JanetBuffer *buffer = janet_malloc(sizeof(JanetBuffer));
|
2021-01-22 18:52:45 +00:00
|
|
|
if (NULL == buffer) {
|
|
|
|
JANET_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
janet_buffer_init(buffer, 0);
|
2021-08-15 18:14:33 +00:00
|
|
|
if (!(flags & 0x2)) {
|
|
|
|
janet_marshal(buffer, janet_wrap_table(janet_vm.abstract_registry), NULL, JANET_MARSHAL_UNSAFE);
|
|
|
|
}
|
2022-07-03 17:08:21 +00:00
|
|
|
if (flags & JANET_THREAD_SUPERVISOR_FLAG) {
|
2021-08-17 02:14:06 +00:00
|
|
|
janet_marshal(buffer, janet_wrap_abstract(supervisor), NULL, JANET_MARSHAL_UNSAFE);
|
|
|
|
}
|
2021-08-15 18:14:33 +00:00
|
|
|
if (!(flags & 0x4)) {
|
2021-08-27 16:46:42 +00:00
|
|
|
janet_assert(janet_vm.registry_count <= INT32_MAX, "assert failed size check");
|
2021-07-30 02:29:08 +00:00
|
|
|
uint32_t temp = (uint32_t) janet_vm.registry_count;
|
|
|
|
janet_buffer_push_bytes(buffer, (uint8_t *) &temp, sizeof(temp));
|
2021-08-21 00:48:48 +00:00
|
|
|
janet_buffer_push_bytes(buffer, (uint8_t *) janet_vm.registry, (int32_t) janet_vm.registry_count * sizeof(JanetCFunRegistry));
|
2021-07-30 02:29:08 +00:00
|
|
|
}
|
2021-01-22 18:52:45 +00:00
|
|
|
janet_marshal(buffer, argv[0], NULL, JANET_MARSHAL_UNSAFE);
|
|
|
|
janet_marshal(buffer, value, NULL, JANET_MARSHAL_UNSAFE);
|
2021-07-17 20:13:28 +00:00
|
|
|
if (flags & 0x1) {
|
|
|
|
/* Return immediately */
|
|
|
|
JanetEVGenericMessage arguments;
|
2021-08-15 20:25:07 +00:00
|
|
|
memset(&arguments, 0, sizeof(arguments));
|
2021-08-15 18:14:33 +00:00
|
|
|
arguments.tag = (uint32_t) flags;
|
2023-02-06 14:41:04 +00:00
|
|
|
arguments.argi = (uint32_t) janet_vm.sandbox_flags;
|
2021-07-17 20:13:28 +00:00
|
|
|
arguments.argp = buffer;
|
|
|
|
arguments.fiber = NULL;
|
|
|
|
janet_ev_threaded_call(janet_go_thread_subr, arguments, janet_ev_default_threaded_callback);
|
|
|
|
return janet_wrap_nil();
|
|
|
|
} else {
|
2023-02-06 14:41:04 +00:00
|
|
|
janet_ev_threaded_await(janet_go_thread_subr, (uint32_t) flags, (uint32_t) janet_vm.sandbox_flags, buffer);
|
2021-07-17 20:13:28 +00:00
|
|
|
}
|
2021-01-22 18:52:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(cfun_ev_give_supervisor,
|
|
|
|
"(ev/give-supervisor tag & payload)",
|
2023-01-19 03:41:06 +00:00
|
|
|
"Send a message to the current supervisor channel if there is one. The message will be a "
|
2021-07-30 02:29:08 +00:00
|
|
|
"tuple of all of the arguments combined into a single message, where the first element is tag. "
|
|
|
|
"By convention, tag should be a keyword indicating the type of message. Returns nil.") {
|
2021-01-16 13:18:07 +00:00
|
|
|
janet_arity(argc, 1, -1);
|
2021-08-17 02:14:06 +00:00
|
|
|
void *chanv = janet_vm.root_fiber->supervisor_channel;
|
|
|
|
if (NULL != chanv) {
|
|
|
|
JanetChannel *chan = janet_channel_unwrap(chanv);
|
2021-01-16 13:18:07 +00:00
|
|
|
if (janet_channel_push(chan, janet_wrap_tuple(janet_tuple_n(argv, argc)), 0)) {
|
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:36:21 +00:00
|
|
|
JANET_NO_RETURN void janet_sleep_await(double sec) {
|
2020-05-28 21:51:11 +00:00
|
|
|
JanetTimeout to;
|
|
|
|
to.when = ts_delta(ts_now(), sec);
|
2021-07-17 01:59:03 +00:00
|
|
|
to.fiber = janet_vm.root_fiber;
|
2020-08-16 22:29:57 +00:00
|
|
|
to.is_error = 0;
|
2020-10-11 14:07:11 +00:00
|
|
|
to.sched_id = to.fiber->sched_id;
|
2020-12-05 16:32:34 +00:00
|
|
|
to.curr_fiber = NULL;
|
2020-05-28 21:51:11 +00:00
|
|
|
add_timeout(to);
|
|
|
|
janet_await();
|
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_ev_sleep,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/sleep sec)",
|
|
|
|
"Suspend the current fiber for sec seconds without blocking the event loop.") {
|
2020-11-29 21:36:21 +00:00
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
double sec = janet_getnumber(argv, 0);
|
|
|
|
janet_sleep_await(sec);
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(cfun_ev_deadline,
|
|
|
|
"(ev/deadline sec &opt tocancel tocheck)",
|
2024-02-20 12:59:37 +00:00
|
|
|
"Schedules the event loop to try to cancel the `tocancel` "
|
|
|
|
"task as with `ev/cancel`. After `sec` seconds, the event "
|
|
|
|
"loop will attempt cancellation of `tocancel` if the "
|
|
|
|
"`tocheck` fiber is resumable. `sec` is a number that can "
|
|
|
|
"have a fractional part. `tocancel` defaults to "
|
|
|
|
"`(fiber/root)`, but if specified, must be a task (root "
|
|
|
|
"fiber). `tocheck` defaults to `(fiber/current)`, but if "
|
|
|
|
"specified, should be a fiber. Returns `tocancel` "
|
|
|
|
"immediately.") {
|
2020-12-05 16:32:34 +00:00
|
|
|
janet_arity(argc, 1, 3);
|
|
|
|
double sec = janet_getnumber(argv, 0);
|
2021-07-17 01:59:03 +00:00
|
|
|
JanetFiber *tocancel = janet_optfiber(argv, argc, 1, janet_vm.root_fiber);
|
|
|
|
JanetFiber *tocheck = janet_optfiber(argv, argc, 2, janet_vm.fiber);
|
2020-12-05 16:32:34 +00:00
|
|
|
JanetTimeout to;
|
|
|
|
to.when = ts_delta(ts_now(), sec);
|
|
|
|
to.fiber = tocancel;
|
|
|
|
to.curr_fiber = tocheck;
|
|
|
|
to.is_error = 0;
|
|
|
|
to.sched_id = to.fiber->sched_id;
|
|
|
|
add_timeout(to);
|
|
|
|
return janet_wrap_fiber(tocancel);
|
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(cfun_ev_cancel,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/cancel fiber err)",
|
2021-09-28 20:41:41 +00:00
|
|
|
"Cancel a suspended fiber in the event loop. Differs from cancel in that it returns the canceled fiber immediately.") {
|
2020-08-23 16:25:04 +00:00
|
|
|
janet_fixarity(argc, 2);
|
|
|
|
JanetFiber *fiber = janet_getfiber(argv, 0);
|
|
|
|
Janet err = argv[1];
|
|
|
|
janet_cancel(fiber, err);
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(janet_cfun_stream_close,
|
|
|
|
"(ev/close stream)",
|
|
|
|
"Close a stream. This should be the same as calling (:close stream) for all streams.") {
|
2020-11-14 20:28:10 +00:00
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_close(stream);
|
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_FN(janet_cfun_stream_read,
|
2021-07-30 02:29:08 +00:00
|
|
|
"(ev/read stream n &opt buffer timeout)",
|
|
|
|
"Read up to n bytes into a buffer asynchronously from a stream. `n` can also be the keyword "
|
|
|
|
"`:all` to read into the buffer until end of stream. "
|
|
|
|
"Optionally provide a buffer to write into "
|
|
|
|
"as well as a timeout in seconds after which to cancel the operation and raise an error. "
|
|
|
|
"Returns the buffer if the read was successful or nil if end-of-stream reached. Will raise an "
|
|
|
|
"error if there are problems with the IO operation.") {
|
2020-11-14 20:28:10 +00:00
|
|
|
janet_arity(argc, 2, 4);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_READABLE);
|
|
|
|
JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, 10);
|
|
|
|
double to = janet_optnumber(argv, argc, 3, INFINITY);
|
2020-12-30 02:37:59 +00:00
|
|
|
if (janet_keyeq(argv[1], "all")) {
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
2021-01-11 17:10:23 +00:00
|
|
|
janet_ev_readchunk(stream, buffer, INT32_MAX);
|
2020-12-30 02:37:59 +00:00
|
|
|
} else {
|
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
|
|
|
janet_ev_read(stream, buffer, n);
|
|
|
|
}
|
2020-11-14 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(janet_cfun_stream_chunk,
|
|
|
|
"(ev/chunk stream n &opt buffer timeout)",
|
|
|
|
"Same as ev/read, but will not return early if less than n bytes are available. If an end of "
|
|
|
|
"stream is reached, will also return early with the collected bytes.") {
|
2020-11-14 20:28:10 +00:00
|
|
|
janet_arity(argc, 2, 4);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_READABLE);
|
|
|
|
int32_t n = janet_getnat(argv, 1);
|
|
|
|
JanetBuffer *buffer = janet_optbuffer(argv, argc, 2, 10);
|
|
|
|
double to = janet_optnumber(argv, argc, 3, INFINITY);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
|
|
|
janet_ev_readchunk(stream, buffer, n);
|
|
|
|
}
|
|
|
|
|
2021-07-30 02:29:08 +00:00
|
|
|
JANET_CORE_FN(janet_cfun_stream_write,
|
|
|
|
"(ev/write stream data &opt timeout)",
|
|
|
|
"Write data to a stream, suspending the current fiber until the write "
|
|
|
|
"completes. Takes an optional timeout in seconds, after which will return nil. "
|
|
|
|
"Returns nil, or raises an error if the write failed.") {
|
2020-11-14 20:28:10 +00:00
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
|
|
|
|
janet_stream_flags(stream, JANET_STREAM_WRITABLE);
|
|
|
|
double to = janet_optnumber(argv, argc, 2, INFINITY);
|
|
|
|
if (janet_checktype(argv[1], JANET_BUFFER)) {
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
|
|
|
janet_ev_write_buffer(stream, janet_getbuffer(argv, 1));
|
|
|
|
} else {
|
|
|
|
JanetByteView bytes = janet_getbytes(argv, 1);
|
|
|
|
if (to != INFINITY) janet_addtimeout(to);
|
|
|
|
janet_ev_write_string(stream, bytes.bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:24:34 +00:00
|
|
|
static int mutexgc(void *p, size_t size) {
|
|
|
|
(void) size;
|
2022-06-17 22:13:58 +00:00
|
|
|
janet_os_mutex_deinit(p);
|
2022-06-05 20:24:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JanetAbstractType janet_mutex_type = {
|
|
|
|
"core/lock",
|
|
|
|
mutexgc,
|
|
|
|
JANET_ATEND_GC
|
|
|
|
};
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_mutex,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/lock)",
|
|
|
|
"Create a new lock to coordinate threads.") {
|
2022-06-05 20:24:34 +00:00
|
|
|
janet_fixarity(argc, 0);
|
|
|
|
(void) argv;
|
2022-06-17 22:13:58 +00:00
|
|
|
void *mutex = janet_abstract_threaded(&janet_mutex_type, janet_os_mutex_size());
|
|
|
|
janet_os_mutex_init(mutex);
|
2022-06-05 20:24:34 +00:00
|
|
|
return janet_wrap_abstract(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_mutex_acquire,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/acquire-lock lock)",
|
|
|
|
"Acquire a lock such that this operating system thread is the only thread with access to this resource."
|
|
|
|
" This will block this entire thread until the lock becomes available, and will not yield to other fibers "
|
|
|
|
"on this system thread.") {
|
2022-06-05 20:24:34 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2022-06-17 22:13:58 +00:00
|
|
|
void *mutex = janet_getabstract(argv, 0, &janet_mutex_type);
|
|
|
|
janet_os_mutex_lock(mutex);
|
2022-06-05 20:24:34 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_mutex_release,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/release-lock lock)",
|
|
|
|
"Release a lock such that other threads may acquire it.") {
|
2022-06-05 20:24:34 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2022-06-17 22:13:58 +00:00
|
|
|
void *mutex = janet_getabstract(argv, 0, &janet_mutex_type);
|
|
|
|
janet_os_mutex_unlock(mutex);
|
2022-06-05 20:24:34 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2022-06-05 21:42:18 +00:00
|
|
|
static int rwlockgc(void *p, size_t size) {
|
|
|
|
(void) size;
|
2022-06-17 22:13:58 +00:00
|
|
|
janet_os_rwlock_deinit(p);
|
2022-06-05 21:42:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const JanetAbstractType janet_rwlock_type = {
|
|
|
|
"core/rwlock",
|
|
|
|
rwlockgc,
|
|
|
|
JANET_ATEND_GC
|
|
|
|
};
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_rwlock,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/rwlock)",
|
|
|
|
"Create a new read-write lock to coordinate threads.") {
|
2022-06-05 21:42:18 +00:00
|
|
|
janet_fixarity(argc, 0);
|
|
|
|
(void) argv;
|
2022-06-17 22:13:58 +00:00
|
|
|
void *rwlock = janet_abstract_threaded(&janet_rwlock_type, janet_os_rwlock_size());
|
|
|
|
janet_os_rwlock_init(rwlock);
|
2022-06-05 21:42:18 +00:00
|
|
|
return janet_wrap_abstract(rwlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_rwlock_read_lock,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/acquire-rlock rwlock)",
|
|
|
|
"Acquire a read lock an a read-write lock.") {
|
2022-06-05 21:42:18 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2022-06-17 22:13:58 +00:00
|
|
|
void *rwlock = janet_getabstract(argv, 0, &janet_rwlock_type);
|
|
|
|
janet_os_rwlock_rlock(rwlock);
|
2022-06-05 21:42:18 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_rwlock_write_lock,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/acquire-wlock rwlock)",
|
|
|
|
"Acquire a write lock on a read-write lock.") {
|
2022-06-05 21:42:18 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2022-06-17 22:13:58 +00:00
|
|
|
void *rwlock = janet_getabstract(argv, 0, &janet_rwlock_type);
|
|
|
|
janet_os_rwlock_wlock(rwlock);
|
2022-06-05 21:42:18 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_rwlock_read_release,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/release-rlock rwlock)",
|
|
|
|
"Release a read lock on a read-write lock") {
|
2022-06-05 21:42:18 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2022-06-17 22:13:58 +00:00
|
|
|
void *rwlock = janet_getabstract(argv, 0, &janet_rwlock_type);
|
|
|
|
janet_os_rwlock_runlock(rwlock);
|
2022-06-05 21:42:18 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
JANET_CORE_FN(janet_cfun_rwlock_write_release,
|
2022-06-06 15:49:30 +00:00
|
|
|
"(ev/release-wlock rwlock)",
|
|
|
|
"Release a write lock on a read-write lock") {
|
2022-06-05 21:42:18 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2022-06-17 22:13:58 +00:00
|
|
|
void *rwlock = janet_getabstract(argv, 0, &janet_rwlock_type);
|
|
|
|
janet_os_rwlock_wunlock(rwlock);
|
2022-06-05 21:42:18 +00:00
|
|
|
return argv[0];
|
|
|
|
}
|
|
|
|
|
2023-04-01 23:57:13 +00:00
|
|
|
JANET_CORE_FN(janet_cfun_ev_all_tasks,
|
|
|
|
"(ev/all-tasks)",
|
|
|
|
"Get an array of all active fibers that are being used by the scheduler.") {
|
|
|
|
janet_fixarity(argc, 0);
|
|
|
|
(void) argv;
|
|
|
|
JanetArray *array = janet_array(janet_vm.active_tasks.count);
|
|
|
|
for (int32_t i = 0; i < janet_vm.active_tasks.capacity; i++) {
|
|
|
|
if (!janet_checktype(janet_vm.active_tasks.data[i].key, JANET_NIL)) {
|
|
|
|
janet_array_push(array, janet_vm.active_tasks.data[i].key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return janet_wrap_array(array);
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:39:40 +00:00
|
|
|
void janet_lib_ev(JanetTable *env) {
|
2021-07-26 05:25:38 +00:00
|
|
|
JanetRegExt ev_cfuns_ext[] = {
|
|
|
|
JANET_CORE_REG("ev/give", cfun_channel_push),
|
|
|
|
JANET_CORE_REG("ev/take", cfun_channel_pop),
|
|
|
|
JANET_CORE_REG("ev/full", cfun_channel_full),
|
|
|
|
JANET_CORE_REG("ev/capacity", cfun_channel_capacity),
|
|
|
|
JANET_CORE_REG("ev/count", cfun_channel_count),
|
|
|
|
JANET_CORE_REG("ev/select", cfun_channel_choice),
|
2021-07-27 06:48:28 +00:00
|
|
|
JANET_CORE_REG("ev/rselect", cfun_channel_rchoice),
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_REG("ev/chan", cfun_channel_new),
|
2021-08-15 16:48:13 +00:00
|
|
|
JANET_CORE_REG("ev/thread-chan", cfun_channel_new_threaded),
|
|
|
|
JANET_CORE_REG("ev/chan-close", cfun_channel_close),
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_CORE_REG("ev/go", cfun_ev_go),
|
|
|
|
JANET_CORE_REG("ev/thread", cfun_ev_thread),
|
|
|
|
JANET_CORE_REG("ev/give-supervisor", cfun_ev_give_supervisor),
|
|
|
|
JANET_CORE_REG("ev/sleep", cfun_ev_sleep),
|
|
|
|
JANET_CORE_REG("ev/deadline", cfun_ev_deadline),
|
|
|
|
JANET_CORE_REG("ev/cancel", cfun_ev_cancel),
|
|
|
|
JANET_CORE_REG("ev/close", janet_cfun_stream_close),
|
|
|
|
JANET_CORE_REG("ev/read", janet_cfun_stream_read),
|
|
|
|
JANET_CORE_REG("ev/chunk", janet_cfun_stream_chunk),
|
|
|
|
JANET_CORE_REG("ev/write", janet_cfun_stream_write),
|
2022-06-05 20:24:34 +00:00
|
|
|
JANET_CORE_REG("ev/lock", janet_cfun_mutex),
|
|
|
|
JANET_CORE_REG("ev/acquire-lock", janet_cfun_mutex_acquire),
|
|
|
|
JANET_CORE_REG("ev/release-lock", janet_cfun_mutex_release),
|
2022-06-05 21:42:18 +00:00
|
|
|
JANET_CORE_REG("ev/rwlock", janet_cfun_rwlock),
|
|
|
|
JANET_CORE_REG("ev/acquire-rlock", janet_cfun_rwlock_read_lock),
|
|
|
|
JANET_CORE_REG("ev/acquire-wlock", janet_cfun_rwlock_write_lock),
|
|
|
|
JANET_CORE_REG("ev/release-rlock", janet_cfun_rwlock_read_release),
|
|
|
|
JANET_CORE_REG("ev/release-wlock", janet_cfun_rwlock_write_release),
|
2023-04-01 23:57:13 +00:00
|
|
|
JANET_CORE_REG("ev/all-tasks", janet_cfun_ev_all_tasks),
|
2021-07-26 05:25:38 +00:00
|
|
|
JANET_REG_END
|
|
|
|
};
|
|
|
|
|
|
|
|
janet_core_cfuns_ext(env, NULL, ev_cfuns_ext);
|
2021-01-24 22:48:46 +00:00
|
|
|
janet_register_abstract_type(&janet_stream_type);
|
2021-08-15 18:14:33 +00:00
|
|
|
janet_register_abstract_type(&janet_channel_type);
|
2022-06-05 20:24:34 +00:00
|
|
|
janet_register_abstract_type(&janet_mutex_type);
|
2022-06-05 21:42:18 +00:00
|
|
|
janet_register_abstract_type(&janet_rwlock_type);
|
2020-05-28 15:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|