Improve synchronization

- Fix bugs in kDos2Errno definition
- malloc() should now be thread safe
- Fix bug in rollup.com header generator
- Fix open(O_APPEND) on the New Technology
- Fix select() on the New Technology and test it
- Work towards refactoring i/o for thread safety
- Socket reads and writes on NT now poll for signals
- Work towards i/o completion ports on the New Technology
- Make read() and write() intermittently check for signals
- Blinkenlights keyboard i/o so much better on NT w/ poll()
- You can now poll() files and sockets at the same time on NT
- Fix bug in appendr() that manifests with dlmalloc footers off
This commit is contained in:
Justine Tunney 2022-04-14 23:39:48 -07:00
parent 233144b19d
commit 933411ba99
266 changed files with 8761 additions and 4344 deletions

View file

@ -700,14 +700,12 @@ static void LoadSyms(void) {
static int DrainInput(int fd) {
char buf[32];
struct pollfd fds[1];
if (!IsWindows()) {
for (;;) {
fds[0].fd = fd;
fds[0].events = POLLIN;
if (poll(fds, ARRAYLEN(fds), 0) == -1) return -1;
if (!(fds[0].revents & POLLIN)) break;
if (read(fd, buf, sizeof(buf)) == -1) return -1;
}
for (;;) {
fds[0].fd = fd;
fds[0].events = POLLIN;
if (poll(fds, ARRAYLEN(fds), 0) == -1) return -1;
if (!(fds[0].revents & POLLIN)) break;
if (read(fd, buf, sizeof(buf)) == -1) return -1;
}
return 0;
}
@ -1928,25 +1926,21 @@ static int OnPtyFdPoll(struct pollfd *fds, size_t nfds, int ms) {
ReactiveDraw();
once = true;
}
if (!IsWindows()) {
p2.fd = fds[i].fd;
p2.events = fds[i].events;
switch (poll(&p2, 1, ms)) {
case -1:
re = POLLERR;
++t;
break;
case 0:
break;
case 1:
re = p2.revents;
++t;
break;
default:
unreachable;
}
} else {
re = POLLIN | POLLOUT; /* xxx */
p2.fd = fds[i].fd;
p2.events = fds[i].events;
switch (poll(&p2, 1, ms)) {
case -1:
re = POLLERR;
++t;
break;
case 0:
break;
case 1:
re = p2.revents;
++t;
break;
default:
unreachable;
}
}
}
@ -2608,11 +2602,7 @@ static bool HasPendingKeyboard(void) {
}
static void Sleep(int ms) {
if (IsWindows()) {
usleep(ms * 1000L);
} else {
poll((struct pollfd[]){{ttyin, POLLIN}}, 1, ms);
}
poll((struct pollfd[]){{ttyin, POLLIN}}, 1, ms);
}
static void OnMouseWheelUp(struct Panel *p, int y, int x) {

View file

@ -40,5 +40,5 @@ int main() {
SetVideoMode(7);
SetEs(0xb0000 >> 4);
MdaTest((void *)0);
for (;;) asm("pause");
for (;;) __builtin_ia32_pause();
}

View file

@ -21,6 +21,7 @@
#include "libc/bits/safemacros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/crc32.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/x/x.h"
#include "tool/build/lib/interner.h"
@ -92,10 +93,12 @@ size_t interncount(const struct Interner *t) {
* @note use consistent size w/ non-string items
*/
size_t internobj(struct Interner *t, const void *data, size_t size) {
char *p2;
size_t n2;
char *item;
unsigned hash;
size_t i, step;
struct InternerObject *it;
size_t i, off, step, need, bytes;
step = 0;
item = data;
it = (struct InternerObject *)t;
@ -117,9 +120,25 @@ size_t internobj(struct Interner *t, const void *data, size_t size) {
step++;
} while (it->p[i].hash);
}
off = it->pool.i;
if (__builtin_add_overflow(off, size, &need)) abort();
if (__builtin_add_overflow(need, 1, &need)) abort();
if (need > it->pool.n) {
if (__builtin_add_overflow(it->pool.n, 1, &n2)) abort();
do {
if (__builtin_add_overflow(n2, n2 >> 1, &n2)) abort();
} while (need > n2);
if (__builtin_mul_overflow(n2, sizeof(*it->pool.p), &bytes)) abort();
if (!(p2 = realloc(it->pool.p, bytes))) abort();
it->pool.p = p2;
it->pool.n = n2;
}
memcpy(it->pool.p + off, data, size);
it->pool.p[off + size] = 0;
it->p[i].hash = hash;
return (it->p[i].index =
CONCAT(&it->pool.p, &it->pool.i, &it->pool.n, item, size));
it->p[i].index = off;
it->pool.i += size;
return off;
}
/**

View file

@ -69,11 +69,7 @@ static int OpSerialIn(struct Machine *m, int r) {
return 0x01;
}
case UART_LSR:
if (IsWindows()) {
p = POLLIN | POLLOUT; /* XXX */
} else {
if ((p = OpE9Poll(m)) == -1) return -1;
}
if ((p = OpE9Poll(m)) == -1) return -1;
s = UART_TTYIDL;
if (p & POLLIN) s |= UART_TTYDA;
if (p & POLLOUT) s |= UART_TTYTXR;

View file

@ -37,13 +37,9 @@ void OpPause(struct Machine *m, uint32_t rde) {
interactive = isatty(0);
once = true;
}
if (!IsWindows() && interactive) {
pf.fd = 0;
pf.events = POLLIN;
poll(&pf, 1, 20); /* make spin loops less brutal */
} else {
sched_yield();
}
pf.fd = 0;
pf.events = POLLIN;
poll(&pf, 1, 20); /* make spin loops less brutal */
}
void OpRdtsc(struct Machine *m, uint32_t rde) {

View file

@ -37,7 +37,7 @@
#include "libc/sysv/consts/sa.h"
#include "libc/sysv/consts/sig.h"
#include "libc/x/x.h"
#include "third_party/dlmalloc/dlmalloc.internal.h"
#include "third_party/dlmalloc/dlmalloc.h"
#include "third_party/getopt/getopt.h"
/**

View file

@ -20,62 +20,45 @@
#include "libc/alg/arraylist2.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/append.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "tool/build/lib/getargs.h"
#include "tool/build/lib/interner.h"
#define LOOKINGAT(p, pe, s) LookingAt(p, pe, s, strlen(s))
#define APPENDSTR(s) AppendData(s, strlen(s))
struct Output {
size_t i, n;
const char *p;
};
struct Visited {
size_t i, n;
const char **p;
};
static struct stat st;
static struct Output output;
static struct Visited visited;
char *output;
struct Interner *visited;
static void Visit(const char *);
void Visit(const char *);
static bool HasVisited(const char *path) {
int i;
for (i = 0; i < visited.i; ++i) {
if (strcmp(path, visited.p[i]) == 0) {
return true;
}
}
return false;
size_t GetFdSize(int fd) {
struct stat st;
CHECK_NE(-1, fstat(fd, &st));
return st.st_size;
}
static void AppendData(const char *s, size_t n) {
CONCAT(&output.p, &output.i, &output.n, s, n);
}
static void AppendInt(long x) {
char ibuf[21];
AppendData(ibuf, int64toarray_radix10(x, ibuf));
}
static bool LookingAt(const char *p, const char *pe, const char *s, size_t n) {
bool LookingAt(const char *p, const char *pe, const char *s, size_t n) {
return pe - p >= n && memcmp(p, s, n) == 0;
}
static void Process(const char *p, const char *pe, const char *path,
bool isheader) {
void Process(const char *p, const char *pe, const char *path, bool isheader) {
int level;
bool noformat;
const char *p2, *dq, *name;
@ -101,16 +84,18 @@ static void Process(const char *p, const char *pe, const char *path,
continue;
}
}
AppendData(p, p2 - p);
appendd(&output, p, p2 - p);
}
if (noformat) {
APPENDSTR("/* clang-format on */\n");
appends(&output, "/* clang-format on */\n");
}
kprintf("finished%n");
}
static void Visit(const char *path) {
void Visit(const char *path) {
int fd;
char *map;
size_t size;
bool isheader;
if (!endswith(path, ".h") && !endswith(path, ".inc")) return;
if (endswith(path, ".internal.h")) return;
@ -118,43 +103,62 @@ static void Visit(const char *path) {
if (endswith(path, ".internal.inc")) return;
if (endswith(path, "/internal.inc")) return;
isheader = endswith(path, ".h");
if (isheader && HasVisited(path)) return;
APPENDSTR("\n\f\n/*!BEGIN ");
APPENDSTR(path);
APPENDSTR(" */\n\n");
APPEND(&visited.p, &visited.i, &visited.n, &path);
if (isheader && isinterned(visited, path)) return;
appends(&output, "\n\f\n/*!BEGIN ");
appends(&output, path);
appends(&output, " */\n\n");
intern(visited, path);
if ((fd = open(path, O_RDONLY)) == -1) {
fprintf(stderr, "error: %s: failed to open\n", path);
exit(1);
}
CHECK_NE(-1, fstat(fd, &st));
if (st.st_size) {
if ((size = GetFdSize(fd))) {
kprintf("size 1 = %'zu%n", size);
CHECK_NE(MAP_FAILED,
(map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)));
Process(map, map + st.st_size, path, isheader);
LOGIFNEG1(munmap(map, st.st_size));
(map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0)));
Process(map, map + size, path, isheader);
kprintf("size = %'zu%n", size);
CHECK_EQ(0, munmap(map, size), "p=%p z=%'zu path=%s", map, size, path);
}
LOGIFNEG1(close(fd));
CHECK_EQ(0, close(fd));
}
ssize_t WriteAll(int fd, const char *p, size_t n) {
ssize_t rc;
size_t i, got;
for (i = 0; i < n;) {
rc = write(fd, p + i, n - i);
if (rc != -1) {
got = rc;
i += got;
} else if (errno != EINTR) {
return -1;
}
}
return i;
}
int main(int argc, char *argv[]) {
const char *src;
struct GetArgs ga;
APPENDSTR("#ifndef COSMOPOLITAN_H_\n");
APPENDSTR("#define COSMOPOLITAN_H_\n");
/* APPENDSTR("#define IMAGE_BASE_VIRTUAL "); */
/* AppendInt(IMAGE_BASE_VIRTUAL); */
/* APPENDSTR("\n"); */
/* APPENDSTR("#define IMAGE_BASE_PHYSICAL "); */
/* AppendInt(IMAGE_BASE_PHYSICAL); */
/* APPENDSTR("\n"); */
ShowCrashReports();
visited = newinterner();
appends(&output, "#ifndef COSMOPOLITAN_H_\n");
appends(&output, "#define COSMOPOLITAN_H_\n");
/* appends(&output, "#define IMAGE_BASE_VIRTUAL "); */
/* appendf(&output, "%p", IMAGE_BASE_VIRTUAL); */
/* appends(&output, "\n"); */
/* appends(&output, "#define IMAGE_BASE_PHYSICAL "); */
/* appendf(&output, "%p", IMAGE_BASE_PHYSICAL); */
/* appends(&output, "\n"); */
getargs_init(&ga, argv + 1);
while ((src = getargs_next(&ga))) {
Visit(src);
}
getargs_destroy(&ga);
APPENDSTR("\n");
APPENDSTR("#endif /* COSMOPOLITAN_H_ */\n");
CHECK_EQ(output.i, write(1, output.p, output.i));
appends(&output, "\n");
appends(&output, "#endif /* COSMOPOLITAN_H_ */\n");
CHECK_NE(-1, WriteAll(1, output, appendz(output).i));
freeinterner(visited);
return 0;
}

View file

@ -92,6 +92,8 @@
#define kLogFile "o/runitd.log"
#define kLogMaxBytes (2 * 1000 * 1000)
bool use_ftrace;
bool use_strace;
char *g_exepath;
volatile bool g_interrupted;
struct sockaddr_in g_servaddr;
@ -136,9 +138,15 @@ void GetOpts(int argc, char *argv[]) {
g_servaddr.sin_family = AF_INET;
g_servaddr.sin_port = htons(RUNITD_PORT);
g_servaddr.sin_addr.s_addr = INADDR_ANY;
while ((opt = getopt(argc, argv, "hvsdrl:p:t:w:")) != -1) {
while ((opt = getopt(argc, argv, "fqhvsdrl:p:t:w:")) != -1) {
switch (opt) {
case 'f':
use_ftrace = true;
break;
case 's':
use_strace = true;
break;
case 'q':
--__log_level;
break;
case 'v':
@ -340,7 +348,12 @@ void HandleClient(void) {
dup2(pipefds[1], 2);
if (pipefds[0] > 2) close(pipefds[1]);
if (g_devnullfd > 2) close(g_devnullfd);
execv(g_exepath, (char *const[]){g_exepath, NULL});
int i = 0;
char *args[4] = {0};
args[i++] = g_exepath;
if (use_strace) args[i++] = "--strace";
if (use_ftrace) args[i++] = "--ftrace";
execv(g_exepath, args);
_exit(127);
}
LOGIFNEG1(close(pipefds[1]));