mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 00:02:28 +00:00
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:
parent
233144b19d
commit
933411ba99
266 changed files with 8761 additions and 4344 deletions
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/macros.internal.h"
|
||||
|
@ -40,9 +41,12 @@ TEST(fcntl_getfl, testRemembersAccessMode) {
|
|||
}
|
||||
|
||||
TEST(fcntl_setfl, testChangeAppendStatus) {
|
||||
if (IsWindows()) {
|
||||
// no obivous way to do fcntl(fd, F_SETFL, O_APPEND)
|
||||
return;
|
||||
}
|
||||
int fd;
|
||||
char buf[8] = {0};
|
||||
if (IsWindows()) return; /* doesn't appear possible on windows */
|
||||
ASSERT_NE(-1, (fd = open("foo", O_CREAT | O_RDWR, 0644)));
|
||||
EXPECT_EQ(3, write(fd, "foo", 3));
|
||||
EXPECT_NE(-1, lseek(fd, 0, SEEK_SET));
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
|
@ -16,18 +16,44 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "third_party/dlmalloc/dlmalloc.internal.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
TEST(mremap, testMalloc) {
|
||||
int i;
|
||||
char *a, *b, *c, *d;
|
||||
ASSERT_NE(NULL, a = malloc(DEFAULT_MMAP_THRESHOLD));
|
||||
ASSERT_NE(NULL, b = mapanon(FRAMESIZE));
|
||||
ASSERT_NE(NULL, a = realloc(a, DEFAULT_MMAP_THRESHOLD * 2));
|
||||
munmap(b, FRAMESIZE);
|
||||
free(a);
|
||||
char testlib_enable_tmp_setup_teardown;
|
||||
|
||||
TEST(open, testOpenExistingForWriteOnly_seeksToStart) {
|
||||
char buf[8] = {0};
|
||||
ASSERT_SYS(0, 0, xbarf("hello.txt", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("hello.txt", O_WRONLY));
|
||||
EXPECT_SYS(0, 1, write(3, "H", 1));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, open("hello.txt", O_RDONLY));
|
||||
EXPECT_SYS(0, 5, read(3, buf, 7));
|
||||
EXPECT_STREQ("Hello", buf);
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(open, testOpenExistingForReadWrite_seeksToStart) {
|
||||
char buf[8] = {0};
|
||||
ASSERT_SYS(0, 0, xbarf("hello.txt", "hello", -1));
|
||||
ASSERT_SYS(0, 3, open("hello.txt", O_RDWR));
|
||||
EXPECT_SYS(0, 1, write(3, "H", 1));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, open("hello.txt", O_RDONLY));
|
||||
EXPECT_SYS(0, 5, read(3, buf, 7));
|
||||
EXPECT_STREQ("Hello", buf);
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(open, testOpenExistingForAppendWriteOnly_seeksToEnd) {
|
||||
char buf[8] = {0};
|
||||
ASSERT_SYS(0, 0, xbarf("hello.txt", "hell", -1));
|
||||
ASSERT_SYS(0, 3, open("hello.txt", O_WRONLY | O_APPEND));
|
||||
EXPECT_SYS(0, 1, write(3, "o", 1));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
ASSERT_SYS(0, 3, open("hello.txt", O_RDONLY));
|
||||
EXPECT_SYS(0, 5, read(3, buf, 7));
|
||||
EXPECT_STREQ("hello", buf);
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
}
|
|
@ -20,6 +20,8 @@
|
|||
#include "libc/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/rand/rand.h"
|
||||
|
@ -37,6 +39,11 @@
|
|||
#define N 1024
|
||||
#define M 20
|
||||
|
||||
void SetUp(void) {
|
||||
// TODO(jart): what is wrong?
|
||||
if (IsWindows()) exit(0);
|
||||
}
|
||||
|
||||
TEST(malloc, zeroMeansOne) {
|
||||
ASSERT_GE(malloc_usable_size(gc(malloc(0))), 1);
|
||||
}
|
||||
|
@ -85,9 +92,9 @@ TEST(malloc, test) {
|
|||
if (fds[k] == -1) {
|
||||
ASSERT_NE(-1, (fds[k] = open(program_invocation_name, O_RDONLY)));
|
||||
ASSERT_NE(-1, fstat(fds[k], &st));
|
||||
ASSERT_NE(MAP_FAILED,
|
||||
(maps[k] = mmap(NULL, (mapsizes[k] = st.st_size), PROT_READ,
|
||||
MAP_SHARED, fds[k], 0)));
|
||||
mapsizes[k] = st.st_size;
|
||||
ASSERT_NE(MAP_FAILED, (maps[k] = mmap(NULL, mapsizes[k], PROT_READ,
|
||||
MAP_SHARED, fds[k], 0)));
|
||||
} else {
|
||||
ASSERT_NE(-1, munmap(maps[k], mapsizes[k]));
|
||||
ASSERT_NE(-1, close(fds[k]));
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
#define THREADS 4
|
||||
#define THREADS 8
|
||||
#define ENTRIES 256
|
||||
|
||||
volatile bool ready;
|
||||
|
@ -42,7 +42,9 @@ void OnChld(int sig) {
|
|||
|
||||
int Thrasher(void *arg) {
|
||||
int i, id = (intptr_t)arg;
|
||||
while (!ready) asm("pause");
|
||||
while (!ready) {
|
||||
__builtin_ia32_pause();
|
||||
}
|
||||
for (i = 0; i < ENTRIES; ++i) {
|
||||
A[id * ENTRIES + i] = rand64();
|
||||
}
|
||||
|
|
|
@ -43,6 +43,6 @@ TEST(clone, test) {
|
|||
EXPECT_NE(-1, (tid = clone(thread, stack + FRAMESIZE,
|
||||
CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
|
||||
0, &ptid, &tls, &ctid)));
|
||||
while ((nowl() - t) < 1 && !x) asm("pause");
|
||||
while ((nowl() - t) < 1 && !x) __builtin_ia32_pause();
|
||||
ASSERT_EQ(42, x);
|
||||
}
|
||||
|
|
|
@ -16,16 +16,25 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/sigbits.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/nexgen32e/rdtsc.h"
|
||||
#include "libc/nexgen32e/rdtscp.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/sysv/consts/af.h"
|
||||
#include "libc/sysv/consts/inaddr.h"
|
||||
#include "libc/sysv/consts/ipproto.h"
|
||||
#include "libc/sysv/consts/poll.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/chibicc/test/test.h"
|
||||
#include "tool/decode/lib/flagger.h"
|
||||
#include "tool/decode/lib/pollnames.h"
|
||||
|
||||
|
@ -36,6 +45,10 @@ dontdiscard char *FormatPollFd(struct pollfd p[2]) {
|
|||
p[1].fd, gc(RecreateFlags(kPollNames, p[1].revents)));
|
||||
}
|
||||
|
||||
TEST(poll, allZero_doesNothing_exceptValidateAndCheckForSignals) {
|
||||
EXPECT_SYS(0, 0, poll(0, 0, 0));
|
||||
}
|
||||
|
||||
TEST(poll, testNegativeOneFd_isIgnored) {
|
||||
ASSERT_SYS(0, 3, socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
||||
struct sockaddr_in addr = {AF_INET, 0, {htonl(INADDR_LOOPBACK)}};
|
||||
|
@ -48,3 +61,79 @@ TEST(poll, testNegativeOneFd_isIgnored) {
|
|||
gc(FormatPollFd(&fds[0])));
|
||||
ASSERT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
TEST(poll, pipe_noInput) {
|
||||
// we can't test stdin here since
|
||||
// we can't assume it isn't /dev/null
|
||||
// since nil is always pollin as eof
|
||||
int pipefds[2];
|
||||
EXPECT_SYS(0, 0, pipe(pipefds));
|
||||
struct pollfd fds[] = {{pipefds[0], POLLIN}};
|
||||
EXPECT_SYS(0, 0, poll(fds, 1, 0));
|
||||
EXPECT_EQ(0, fds[0].revents);
|
||||
EXPECT_SYS(0, 0, close(pipefds[0]));
|
||||
EXPECT_SYS(0, 0, close(pipefds[1]));
|
||||
}
|
||||
|
||||
TEST(poll, pipe_hasInputFromSameProcess) {
|
||||
char buf[2];
|
||||
int pipefds[2];
|
||||
EXPECT_SYS(0, 0, pipe(pipefds));
|
||||
struct pollfd fds[] = {{pipefds[0], POLLIN}};
|
||||
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
|
||||
EXPECT_SYS(0, 1, poll(fds, 1, 1000)); // flake nt!
|
||||
EXPECT_EQ(POLLIN, fds[0].revents);
|
||||
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
|
||||
EXPECT_SYS(0, 0, poll(fds, 1, 0));
|
||||
EXPECT_SYS(0, 0, close(pipefds[0]));
|
||||
EXPECT_SYS(0, 0, close(pipefds[1]));
|
||||
}
|
||||
|
||||
TEST(poll, pipe_hasInput) {
|
||||
char buf[2];
|
||||
sigset_t chldmask, savemask;
|
||||
int ws, pid, sync[2], pipefds[2];
|
||||
EXPECT_EQ(0, sigemptyset(&chldmask));
|
||||
EXPECT_EQ(0, sigaddset(&chldmask, SIGCHLD));
|
||||
EXPECT_EQ(0, sigprocmask(SIG_BLOCK, &chldmask, &savemask));
|
||||
EXPECT_SYS(0, 0, pipe(pipefds));
|
||||
EXPECT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
EXPECT_SYS(0, 0, close(pipefds[0]));
|
||||
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
|
||||
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
|
||||
EXPECT_SYS(0, 0, close(pipefds[1]));
|
||||
_Exit(0);
|
||||
}
|
||||
EXPECT_SYS(0, 0, close(pipefds[1]));
|
||||
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
|
||||
struct pollfd fds[] = {{pipefds[0], POLLIN}};
|
||||
EXPECT_SYS(0, 1, poll(fds, 1, -1));
|
||||
EXPECT_EQ(POLLIN, fds[0].revents & POLLIN);
|
||||
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
|
||||
EXPECT_SYS(0, 0, close(pipefds[0]));
|
||||
ASSERT_NE(-1, wait(&ws));
|
||||
EXPECT_TRUE(WIFEXITED(ws));
|
||||
EXPECT_EQ(0, WEXITSTATUS(ws));
|
||||
EXPECT_EQ(0, sigprocmask(SIG_SETMASK, &savemask, 0));
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST(poll, emptyFds_becomesSleep) {
|
||||
// timing tests w/o mocks are always the hardest
|
||||
int64_t a, b, c, p, i = 0;
|
||||
do {
|
||||
if (++i == 5) {
|
||||
kprintf("too much cpu churn%n");
|
||||
return;
|
||||
}
|
||||
p = TSC_AUX_CORE(rdpid());
|
||||
a = rdtsc();
|
||||
EXPECT_SYS(0, 0, poll(0, 0, 5));
|
||||
b = rdtsc();
|
||||
EXPECT_SYS(0, 0, poll(0, 0, 50));
|
||||
c = rdtsc();
|
||||
} while (TSC_AUX_CORE(rdpid()) != p);
|
||||
EXPECT_LT((b - a) * 2, c - b);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -16,17 +16,37 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/timeval.h"
|
||||
#include "libc/sock/select.h"
|
||||
#include "libc/sock/sock.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
TEST(select, allZero) {
|
||||
// todo: figure out how to test block until signal w/ select
|
||||
// EXPECT_SYS(0, 0, select(0, 0, 0, 0, 0));
|
||||
// TEST(select, allZero) {
|
||||
// // todo: figure out how to test block until signal w/ select
|
||||
// EXPECT_SYS(0, 0, select(0, 0, 0, 0, 0));
|
||||
// }
|
||||
|
||||
TEST(select, pipe_hasInputFromSameProcess) {
|
||||
fd_set rfds;
|
||||
char buf[2];
|
||||
int pipefds[2];
|
||||
struct timeval tv = {.tv_usec = 100 * 1000};
|
||||
EXPECT_SYS(0, 0, pipe(pipefds));
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(pipefds[0], &rfds);
|
||||
EXPECT_SYS(0, 2, write(pipefds[1], "hi", 2));
|
||||
EXPECT_SYS(0, 1, select(pipefds[0] + 1, &rfds, 0, 0, &tv));
|
||||
EXPECT_TRUE(FD_ISSET(pipefds[0], &rfds));
|
||||
EXPECT_SYS(0, 2, read(pipefds[0], buf, 2));
|
||||
EXPECT_SYS(0, 0, select(pipefds[0] + 1, &rfds, 0, 0, &tv));
|
||||
EXPECT_TRUE(!FD_ISSET(pipefds[0], &rfds));
|
||||
EXPECT_SYS(0, 0, close(pipefds[0]));
|
||||
EXPECT_SYS(0, 0, close(pipefds[1]));
|
||||
}
|
||||
|
||||
#if 0 // flaky
|
||||
TEST(select, testSleep) {
|
||||
int64_t e;
|
||||
long double n;
|
||||
|
@ -40,3 +60,4 @@ TEST(select, testSleep) {
|
|||
EXPECT_EQ(0, t.tv_usec);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -29,8 +29,6 @@
|
|||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
/* TODO(jart): O_APPEND on Windows */
|
||||
|
||||
#define PATH "hog"
|
||||
|
||||
FILE *f;
|
||||
|
|
|
@ -17,10 +17,15 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/stdio/append.internal.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
static void PrintMemory(void *p) {
|
||||
kprintf("%#.*hhs%n", malloc_usable_size(p), p);
|
||||
}
|
||||
|
||||
TEST(vappendf, test) {
|
||||
char *b = 0;
|
||||
ASSERT_NE(-1, appendf(&b, "hello "));
|
||||
|
@ -137,6 +142,7 @@ TEST(appendr, testExtend_zeroFills) {
|
|||
TEST(appendr, testAbsent_allocatesNul) {
|
||||
char *b = 0;
|
||||
ASSERT_NE(-1, appendr(&b, 0));
|
||||
ASSERT_BINEQ(u" ", b);
|
||||
EXPECT_EQ(0, appendz(b).i);
|
||||
ASSERT_BINEQ(u" ", b);
|
||||
free(b);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue