Make improvements

- Improved async signal safety of read() particularly for longjmp()
- Started adding cancel cleanup handlers for locks / etc on Windows
- Make /dev/tty work better particularly for uses like `foo | less`
- Eagerly read console input into a linked list, so poll can signal
- Fix some libc definitional bugs, which configure scripts detected
This commit is contained in:
Justine Tunney 2023-09-21 07:30:39 -07:00
parent d6c2830850
commit 0c5dd7b342
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
85 changed files with 1062 additions and 671 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/cp.internal.h"
#include "libc/calls/internal.h"
@ -26,12 +27,14 @@
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/runtime/runtime.h"
#include "libc/sock/internal.h"
#include "libc/sysv/consts/nr.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/testlib/ezbench.h"
#include "libc/testlib/testlib.h"
#include "libc/thread/thread.h"
void SetUpOnce(void) {
testlib_enable_tmp_setup_teardown();
@ -82,6 +85,57 @@ TEST(read_directory, eisdir) {
ASSERT_SYS(0, 0, close(3));
}
int fds[2];
jmp_buf jb;
pthread_t th;
atomic_bool isdone;
void *GenerateSignals(void *arg) {
while (!isdone) {
usleep(123);
pthread_kill(th, SIGINT);
}
return 0;
}
void *GenerateData(void *arg) {
for (;;) {
usleep(223);
int rc = write(fds[1], "hi", 2);
if (rc == -1 && errno == EPIPE) break;
ASSERT_EQ(2, rc);
}
return 0;
}
void OnSig(int sig) {
char buf[8];
ASSERT_SYS(0, 2, read(fds[0], buf, 8));
longjmp(jb, 1);
}
TEST(read, whatEmacsDoes) {
pthread_t sigth;
sighandler_t sh1 = signal(SIGINT, SIG_IGN);
sighandler_t sh2 = signal(SIGPIPE, SIG_IGN);
ASSERT_SYS(0, 0, pipe(fds));
ASSERT_EQ(0, pthread_create(&th, 0, GenerateData, 0));
ASSERT_EQ(0, pthread_create(&sigth, 0, GenerateSignals, 0));
for (int i = 0; i < 100; ++i) {
if (!setjmp(jb)) {
char buf[8];
ASSERT_GE(read(fds[0], buf, 8), 2);
}
}
isdone = true;
ASSERT_SYS(0, 0, close(fds[0]));
ASSERT_EQ(0, pthread_join(sigth, 0));
ASSERT_EQ(0, pthread_join(th, 0));
ASSERT_SYS(0, 0, close(fds[1]));
signal(SIGPIPE, sh2);
signal(SIGINT, sh1);
}
////////////////////////////////////////////////////////////////////////////////
BENCH(read, bench) {

View file

@ -25,11 +25,6 @@
#include "libc/testlib/testlib.h"
void SetUpOnce(void) {
if (!IsWindows()) {
// TODO(jart): mock out that win32 i/o call
tinyprint(2, program_invocation_name, ": skipping on non-windows\n", NULL);
exit(0);
}
testlib_enable_tmp_setup_teardown();
}
@ -197,16 +192,3 @@ TEST(GetDosArgv, cmdToil) {
free(argv);
free(buf);
}
TEST(GetDosArgv, canonicalizesCurrentDirectoryCommandPath) {
size_t max = 4;
size_t size = ARG_MAX / 2;
char *buf = malloc(size * sizeof(char));
char **argv = malloc(max * sizeof(char *));
ASSERT_SYS(0, 0, touch("emacs.com", 0755));
EXPECT_EQ(1, GetDosArgv(u"emacs.com", buf, size, argv, max));
EXPECT_STREQ(".\\emacs.com", argv[0]);
EXPECT_EQ(NULL, argv[1]);
free(argv);
free(buf);
}

View file

@ -32,6 +32,7 @@
#include "libc/thread/thread2.h"
int pfds[2];
atomic_bool ready;
pthread_cond_t cv;
pthread_mutex_t mu;
atomic_int gotcleanup;
@ -72,6 +73,7 @@ TEST(pthread_cancel, self_deferred_waitsForCancellationPoint) {
void *Worker(void *arg) {
char buf[8];
ready = true;
pthread_cleanup_push(OnCleanup, 0);
read(pfds[0], buf, sizeof(buf));
pthread_cleanup_pop(0);
@ -91,11 +93,12 @@ TEST(pthread_cancel, synchronous) {
ASSERT_SYS(0, 0, close(pfds[0]));
}
TEST(pthread_cancel, synchronous_delayed) {
TEST(pthread_cancel, synchronous_deferred) {
void *rc;
pthread_t th;
ASSERT_SYS(0, 0, pipe(pfds));
ASSERT_EQ(0, pthread_create(&th, 0, Worker, 0));
while (!ready) pthread_yield();
ASSERT_SYS(0, 0, usleep(10));
EXPECT_EQ(0, pthread_cancel(th));
EXPECT_EQ(0, pthread_join(th, &rc));