1
0
mirror of https://github.com/janet-lang/janet synced 2025-10-17 08:47:39 +00:00

Several changes to move pipe creation back into ev.c

This commit is contained in:
Calvin Rose
2020-11-18 10:53:36 -06:00
parent 2dc04d2957
commit 53aa19a916
5 changed files with 53 additions and 79 deletions

View File

@@ -1747,56 +1747,13 @@ static Janet os_open(int32_t argc, Janet *argv) {
return janet_wrap_abstract(janet_stream(fd, stream_flags, NULL));
}
/* For a pipe ID */
#ifdef JANET_WINDOWS
static volatile long PipeSerialNumber;
#endif
static Janet os_pipe(int32_t argc, Janet *argv) {
(void) argv;
janet_fixarity(argc, 0);
#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.
*/
JanetHandle rhandle, whandle;
UCHAR PipeNameBuffer[MAX_PATH];
sprintf(PipeNameBuffer,
"\\\\.\\Pipe\\JanetPipeFile.%08x.%08x",
GetCurrentProcessId(),
InterlockedIncrement(&PipeSerialNumber));
rhandle = CreateNamedPipeA(
PipeNameBuffer,
PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_WAIT,
1, /* Number of pipes */
4096, /* Out buffer size */
4096, /* In buffer size */
120 * 1000, /* Timeout in ms */
NULL);
if (!rhandle) janet_panicv(janet_ev_lasterr());
whandle = CreateFileA(
PipeNameBuffer,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (whandle == INVALID_HANDLE_VALUE) {
Janet x = janet_ev_lasterr();
CloseHandle(rhandle);
janet_panicv(x);
}
JanetStream *reader = janet_stream(rhandle, JANET_STREAM_READABLE, NULL);
JanetStream *writer = janet_stream(whandle, JANET_STREAM_WRITABLE, NULL);
#else
int fds[2];
if (pipe(fds)) janet_panicv(janet_ev_lasterr());
JanetHandle fds[2];
if (janet_make_pipe(fds)) janet_panicv(janet_ev_lasterr());
JanetStream *reader = janet_stream(fds[0], JANET_STREAM_READABLE, NULL);
JanetStream *writer = janet_stream(fds[1], JANET_STREAM_WRITABLE, NULL);
#endif
Janet tup[2] = {janet_wrap_abstract(reader), janet_wrap_abstract(writer)};
return janet_wrap_tuple(janet_tuple_n(tup, 2));
}