From af75bf3b64b6464829f42cd44a4abedb181e5fd6 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 24 Jan 2021 16:48:46 -0600 Subject: [PATCH] Update for sending streams to new threads. --- src/core/ev.c | 16 +++++++++++++++- src/core/util.h | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/core/ev.c b/src/core/ev.c index 8ba83db6..25558983 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -393,7 +393,20 @@ static void janet_stream_marshal(void *p, JanetMarshalContext *ctx) { * while in transit, and it's value gets reused. DuplicateHandle does not work * for network sockets, and in general for winsock it is better to nipt duplicate * unless there is a need to. */ - janet_marshal_int64(ctx, (int64_t)(s->handle)); + HANDLE duph = INVALID_HANDLE_VALUE; + if (s->flags & JANET_STREAM_SOCKET) { + duph = s->handle; + } else { + DuplicateHandle( + GetCurrentProcess(), + s->handle, + GetCurrentProcess(), + &duph, + 0, + FALSE, + DUPLICATE_SAME_ACCESS); + } + janet_marshal_int64(ctx, (int64_t)(duph)); #else /* Marshal after dup becuse it is easier than maintaining our own ref counting. */ int duph = dup(s->handle); @@ -2314,6 +2327,7 @@ static const JanetReg ev_cfuns[] = { void janet_lib_ev(JanetTable *env) { janet_core_cfuns(env, NULL, ev_cfuns); + janet_register_abstract_type(&janet_stream_type); } #endif diff --git a/src/core/util.h b/src/core/util.h index 01d78ecb..4507838a 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -108,6 +108,11 @@ void janet_core_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cf int janet_gettime(struct timespec *spec); #endif +/* strdup */ +#ifdef JANET_WINDOWS +#define strdup(x) _strdup(x) +#endif + #define RETRY_EINTR(RC, CALL) do { (RC) = CALL; } while((RC) < 0 && errno == EINTR) /* Initialize builtin libraries */