Begin implementing async subproccesses for windows.

This commit is contained in:
Calvin Rose 2021-01-03 11:21:44 -06:00
parent 1c7ed8ca48
commit 47bb7fd21b
2 changed files with 27 additions and 11 deletions

View File

@ -990,7 +990,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
if (!has_timeout) {
/* queue emptied */
}
} else if (NULL == completionKey) {
} else if (0 == completionKey) {
/* Custom event */
JanetSelfPipeEvent *response = (JanetSelfPipeEvent *)(overlapped);
response->cb(response->msg);
@ -1308,7 +1308,10 @@ static DWORD WINAPI janet_thread_body(LPVOID ptr) {
/* Reuse memory from thread init for returning data */
init->msg = subr(msg);
init->cb = cb;
janet_assert(PostQueuedCompletionStatus(iocp, sizeof(JanetSelfPipeEvent), NULL, init),
janet_assert(PostQueuedCompletionStatus(iocp,
sizeof(JanetSelfPipeEvent),
0,
(LPOVERLAPPED) init),
"failed to post completion event");
return 0;
}

View File

@ -344,6 +344,17 @@ typedef struct {
#ifdef JANET_EV
#ifdef JANET_WINDOWS
static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
JanetProc *proc = (JanetProc *) args.argp;
WaitForSingleObject(proc->pHandle, INFINITE);
GetExitCodeProcess(proc->pHandle, &args.argi);
return args;
}
#else /* windows check */
/* Function that is called in separate thread to wait on a pid */
static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
JanetProc *proc = (JanetProc *) args.argp;
@ -352,14 +363,6 @@ static JanetEVGenericMessage janet_proc_wait_subr(JanetEVGenericMessage args) {
do {
result = waitpid(proc->pid, &status, 0);
} while (result == -1 && errno == EINTR);
args.argi = status;
return args;
}
/* Callback that is called in main thread when subroutine completes. */
static void janet_proc_wait_cb(JanetEVGenericMessage args) {
int status = args.argi;
JanetProc *proc = (JanetProc *) args.argp;
/* Use POSIX shell semantics for interpreting signals */
if (WIFEXITED(status)) {
status = WEXITSTATUS(status);
@ -368,6 +371,16 @@ static void janet_proc_wait_cb(JanetEVGenericMessage args) {
} else {
status = WTERMSIG(status) + 128;
}
args.argi = status;
return args;
}
#endif /* End windows check */
/* Callback that is called in main thread when subroutine completes. */
static void janet_proc_wait_cb(JanetEVGenericMessage args) {
int status = args.argi;
JanetProc *proc = (JanetProc *) args.argp;
if (NULL != proc) {
proc->return_code = (int32_t) status;
proc->flags |= JANET_PROC_WAITED;
@ -383,7 +396,7 @@ static void janet_proc_wait_cb(JanetEVGenericMessage args) {
}
}
#endif
#endif /* End ev check */
static int janet_proc_gc(void *p, size_t s) {
(void) s;