1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

Fix issues with #282

Bad handling of write errors, as well as janet_root_fiber().
This commit is contained in:
Calvin Rose 2020-02-20 19:54:31 -06:00
parent f4a46ba6ea
commit 0df220780a
2 changed files with 21 additions and 7 deletions

View File

@ -26,6 +26,8 @@
#include "util.h"
#endif
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
@ -146,6 +148,9 @@ JANET_THREAD_LOCAL int janet_vm_loop_count;
void janet_net_markloop(void) {
for (int i = 0; i < janet_vm_loop_count; i++) {
JanetLoopFD lfd = janet_vm_loopfds[i];
if (lfd.fiber != NULL) {
janet_mark(janet_wrap_fiber(lfd.fiber));
}
janet_mark(janet_wrap_abstract(lfd.stream));
switch (lfd.event_type) {
default:
@ -175,9 +180,6 @@ static int janet_loop_schedule(JanetLoopFD lfd) {
}
int index = janet_vm_loop_count;
janet_vm_loopfds[janet_vm_loop_count++] = lfd;
if (NULL != lfd.fiber) {
janet_gcroot(janet_wrap_fiber(lfd.fiber));
}
return index;
}
@ -274,9 +276,10 @@ static size_t janet_loop_event(size_t index) {
if (start < len) {
int32_t nbytes = len - start;
ssize_t nwrote;
errno = 0;
do {
nwrote = write(fd, bytes + start, nbytes);
} while (nwrote == EINTR);
} while (errno == EINTR);
if (nwrote > 0) {
start += nwrote;
} else {
@ -422,7 +425,7 @@ static struct addrinfo *janet_get_addrinfo(Janet *argv, int32_t offset) {
*/
static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
janet_arity(argc, 2, -1);
struct addrinfo *ai = janet_get_addrinfo(argv, 0);
@ -433,6 +436,16 @@ static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_panic("could not create socket");
}
/* Set socket opts */
/*for (int32_t argi = 1; argi < argc; argi++) {
const uint8_t *kw = janet_getkeyword(argv, argi);
if (janet_cstrcmp(kw, "no-delay")) {
int one = 1;
setsockopt(sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
}
}*/
/* Connect to socket */
int status = connect(sock, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(ai);

View File

@ -1280,7 +1280,7 @@ JanetSignal janet_continue(JanetFiber *fiber, Janet in, Janet *out) {
Janet *old_vm_return_reg = janet_vm_return_reg;
/* Setup fiber */
if (oldn == 0) janet_vm_root_fiber = fiber;
if (janet_vm_root_fiber == NULL) janet_vm_root_fiber = fiber;
janet_vm_fiber = fiber;
janet_gcroot(janet_wrap_fiber(fiber));
janet_fiber_set_status(fiber, JANET_STATUS_ALIVE);
@ -1306,7 +1306,7 @@ JanetSignal janet_continue(JanetFiber *fiber, Janet in, Janet *out) {
janet_gcunroot(janet_wrap_fiber(fiber));
/* Restore global state */
if (oldn == 0) janet_vm_root_fiber = NULL;
if (janet_vm_root_fiber == fiber) janet_vm_root_fiber = NULL;
janet_vm_gc_suspend = handle;
janet_vm_fiber = old_vm_fiber;
janet_vm_stackn = oldn;
@ -1377,6 +1377,7 @@ int janet_init(void) {
/* Fibers */
janet_vm_fiber = NULL;
janet_vm_root_fiber = NULL;
janet_vm_stackn = 0;
/* Threads */
#ifdef JANET_THREADS
janet_threads_init();