aboutsummaryrefslogtreecommitdiff
path: root/src/core/io_linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/io_linux.c')
-rw-r--r--src/core/io_linux.c33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/core/io_linux.c b/src/core/io_linux.c
index 8a024aa..aaf6a18 100644
--- a/src/core/io_linux.c
+++ b/src/core/io_linux.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
@@ -7,7 +8,12 @@
#include <poll.h>
#endif
-int J;
+#if !defined(_WIN32) && !defined(__wasm__)
+#define VM_TLS _Thread_local
+#else
+#define VM_TLS
+#endif
+VM_TLS int J;
#ifdef _WIN32
static int pk(void){int c=getchar();ungetc(c,stdin);return c==EOF;}
@@ -19,8 +25,8 @@ char Q(void){int c=getchar();J=pk();return c;}
one would wait for a message that comes only after the answer. so J is 1 only
when the end is known, and 0 when more may come. the output is flushed before
any read that can wait, for the same reason. */
-static unsigned char ib[65536];
-static int ip, in, ieof;
+static VM_TLS unsigned char ib[65536];
+static VM_TLS int ip, in, ieof;
/* make a byte available. wait is 0 to look without waiting. returns 1 when one is ready */
static int fill(int wait)
{
@@ -46,6 +52,27 @@ char Q(void)
return (char)c;
}
#endif
+/* 1 when a byte can be read without waiting: buffered, or already on the pipe */
+int input_pending(void)
+{
+#ifdef _WIN32
+ return 0;
+#else
+ return fill(0);
+#endif
+}
+#ifndef _WIN32
+struct IoSnapshot { unsigned char bytes[65536]; int position, count, ended, flag; };
+size_t io_context_size(void) { return sizeof(struct IoSnapshot); }
+void io_context_snapshot(void *destination) {
+ struct IoSnapshot *snapshot = destination;
+ memcpy(snapshot->bytes, ib, sizeof ib); snapshot->position = ip; snapshot->count = in; snapshot->ended = ieof; snapshot->flag = J;
+}
+void io_context_restore(const void *source) {
+ const struct IoSnapshot *snapshot = source;
+ memcpy(ib, snapshot->bytes, sizeof ib); ip = snapshot->position; in = snapshot->count; ieof = snapshot->ended; J = snapshot->flag;
+}
+#endif
void G(char c){putchar(c);}
static void init_io(void) {