Fix many thread and file descriptor issues on Windows

The greenbean web server now works nearly perfectly on Windows with over
1000 threads. But some synchronization issues still remain which prevent
us from going over nine thousand.
This commit is contained in:
Justine Tunney 2022-05-20 18:51:41 -07:00
parent 96781d0679
commit 7838edae88
32 changed files with 363 additions and 192 deletions

View file

@ -19,6 +19,7 @@
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/spinlock.h"
#include "libc/mem/mem.h"
#include "libc/nt/files.h"
#include "libc/nt/struct/pollfd.h"
@ -44,7 +45,9 @@ textwindows int sys_accept_nt(struct Fd *fd, void *addr, uint32_t *addrsize,
for (;;) {
if (!WSAPoll(&(struct sys_pollfd_nt){fd->handle, POLLIN}, 1,
__SIG_POLLING_INTERVAL_MS)) {
if (_check_interrupts(true, g_fds.p)) return eintr();
if (_check_interrupts(true, g_fds.p)) {
return eintr();
}
continue;
}
if ((h = WSAAccept(fd->handle, addr, (int32_t *)addrsize, 0, 0)) != -1) {
@ -54,7 +57,8 @@ textwindows int sys_accept_nt(struct Fd *fd, void *addr, uint32_t *addrsize,
if ((!(flags & SOCK_NONBLOCK) ||
__sys_ioctlsocket_nt(h, FIONBIO, (uint32_t[]){1}) != -1) &&
(sockfd2 = calloc(1, sizeof(struct SockFd)))) {
if ((client = __reservefd(-1)) != -1) {
_spinlock(&__fds_lock);
if ((client = __reservefd_unlocked(-1)) != -1) {
sockfd2->family = sockfd->family;
sockfd2->type = sockfd->type;
sockfd2->protocol = sockfd->protocol;
@ -63,8 +67,10 @@ textwindows int sys_accept_nt(struct Fd *fd, void *addr, uint32_t *addrsize,
g_fds.p[client].mode = 0140666;
g_fds.p[client].handle = h;
g_fds.p[client].extra = (uintptr_t)sockfd2;
_spunlock(&__fds_lock);
return client;
}
_spunlock(&__fds_lock);
free(sockfd2);
}
__sys_closesocket_nt(h);

View file

@ -36,6 +36,7 @@
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/spinlock.h"
#include "libc/limits.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
@ -1324,7 +1325,8 @@ static textwindows dontinline int sys_epoll_create1_nt(uint32_t flags) {
struct PortState *port_state;
struct TsTreeNode *tree_node;
if (wepoll_init() < 0) return -1;
if ((fd = __reservefd(-1)) == -1) return -1;
fd = __reservefd(-1);
if (fd == -1) return -1;
port_state = port_new(&ephnd);
if (!port_state) {
__releasefd(fd);
@ -1338,10 +1340,12 @@ static textwindows dontinline int sys_epoll_create1_nt(uint32_t flags) {
__releasefd(fd);
return -1;
}
_spinlock(&__fds_lock);
g_fds.p[fd].kind = kFdEpoll;
g_fds.p[fd].handle = ephnd;
g_fds.p[fd].flags = flags;
g_fds.p[fd].mode = 0140666;
_spunlock(&__fds_lock);
return fd;
}

View file

@ -44,7 +44,8 @@ textwindows int sys_socket_nt(int family, int type, int protocol) {
int64_t h;
struct SockFd *sockfd;
int fd, oflags, truetype;
if ((fd = __reservefd(-1)) == -1) return -1;
fd = __reservefd(-1);
if (fd == -1) return -1;
truetype = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
if ((h = WSASocket(family, truetype, protocol, NULL, 0,
kNtWsaFlagOverlapped)) != -1) {

View file

@ -30,10 +30,10 @@
#include "libc/sysv/errfuns.h"
textwindows int sys_socketpair_nt(int family, int type, int proto, int sv[2]) {
int64_t hpipe, h1, h2;
int reader, writer, oflags;
char16_t pipename[64];
uint32_t mode;
char16_t pipename[64];
int64_t hpipe, h1, h2;
int rc, reader, writer, oflags;
// Supports only AF_UNIX
if (family != AF_UNIX) {
@ -53,9 +53,13 @@ textwindows int sys_socketpair_nt(int family, int type, int proto, int sv[2]) {
}
CreatePipeName(pipename);
if ((reader = __reservefd(-1)) == -1) return -1;
if ((writer = __reservefd(-1)) == -1) {
__releasefd(reader);
_spinlock(&__fds_lock);
reader = __reservefd_unlocked(-1);
writer = __reservefd_unlocked(-1);
_spunlock(&__fds_lock);
if (reader == -1 || writer == -1) {
if (reader != -1) __releasefd(reader);
if (writer != -1) __releasefd(writer);
return -1;
}
if ((hpipe = CreateNamedPipe(
@ -68,28 +72,33 @@ textwindows int sys_socketpair_nt(int family, int type, int proto, int sv[2]) {
h1 = CreateFile(pipename, kNtGenericWrite | kNtGenericRead, 0,
&kNtIsInheritable, kNtOpenExisting, kNtFileFlagOverlapped, 0);
if (h1 == -1) {
CloseHandle(hpipe);
__releasefd(writer);
__releasefd(reader);
return -1;
}
_spinlock(&__fds_lock);
g_fds.p[reader].kind = kFdFile;
g_fds.p[reader].flags = oflags;
g_fds.p[reader].mode = 0140444;
g_fds.p[reader].handle = hpipe;
if (h1 != -1) {
g_fds.p[writer].kind = kFdFile;
g_fds.p[writer].flags = oflags;
g_fds.p[writer].mode = 0140222;
g_fds.p[writer].handle = h1;
g_fds.p[reader].kind = kFdFile;
g_fds.p[reader].flags = oflags;
g_fds.p[reader].mode = 0140444;
g_fds.p[reader].handle = hpipe;
g_fds.p[writer].kind = kFdFile;
g_fds.p[writer].flags = oflags;
g_fds.p[writer].mode = 0140222;
g_fds.p[writer].handle = h1;
sv[0] = reader;
sv[1] = writer;
rc = 0;
} else {
CloseHandle(hpipe);
__releasefd_unlocked(writer);
__releasefd_unlocked(reader);
rc = -1;
}
_spunlock(&__fds_lock);
sv[0] = reader;
sv[1] = writer;
return 0;
return rc;
}