Fix MODE=tinylinux build

This commit is contained in:
Justine Tunney 2024-07-06 01:39:15 -07:00
parent 8c645fa1ee
commit 6be030cd7c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
8 changed files with 77 additions and 98 deletions

View file

@ -46,6 +46,8 @@
#include "libc/thread/posixthread.internal.h" #include "libc/thread/posixthread.internal.h"
#include "libc/thread/tls.h" #include "libc/thread/tls.h"
__static_yoink("_pthread_atfork");
extern pthread_mutex_t _pthread_lock_obj; extern pthread_mutex_t _pthread_lock_obj;
static void _onfork_prepare(void) { static void _onfork_prepare(void) {

View file

@ -16,58 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h"
#include "libc/cxxabi.h" #include "libc/cxxabi.h"
#include "libc/errno.h"
#include "libc/intrin/pushpop.internal.h" #include "libc/intrin/pushpop.internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/arraylist.internal.h" #include "libc/mem/arraylist.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/fflush.internal.h" #include "libc/stdio/fflush.internal.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h"
void(__fflush_lock)(void) {
pthread_mutex_lock(&__fflush_lock_obj);
}
void(__fflush_unlock)(void) {
pthread_mutex_unlock(&__fflush_lock_obj);
}
static void __stdio_fork_prepare(void) {
FILE *f;
__fflush_lock();
for (int i = 0; i < __fflush.handles.i; ++i)
if ((f = __fflush.handles.p[i]))
pthread_mutex_lock(&f->lock);
}
static void __stdio_fork_parent(void) {
FILE *f;
for (int i = __fflush.handles.i; i--;)
if ((f = __fflush.handles.p[i]))
pthread_mutex_unlock(&f->lock);
__fflush_unlock();
}
static void __stdio_fork_child(void) {
FILE *f;
for (int i = __fflush.handles.i; i--;) {
if ((f = __fflush.handles.p[i])) {
bzero(&f->lock, sizeof(f->lock));
f->lock._word = PTHREAD_MUTEX_RECURSIVE;
}
}
pthread_mutex_init(&__fflush_lock_obj, 0);
}
__attribute__((__constructor__(60))) static textstartup void stdioinit(void) {
pthread_atfork(__stdio_fork_prepare, __stdio_fork_parent, __stdio_fork_child);
}
/** /**
* Blocks until data from stream buffer is written out. * Blocks until data from stream buffer is written out.

View file

@ -16,8 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/stdio/fflush.internal.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
/** /**
@ -26,3 +28,39 @@
void flockfile(FILE *f) { void flockfile(FILE *f) {
pthread_mutex_lock(&f->lock); pthread_mutex_lock(&f->lock);
} }
void(__fflush_lock)(void) {
pthread_mutex_lock(&__fflush_lock_obj);
}
void(__fflush_unlock)(void) {
pthread_mutex_unlock(&__fflush_lock_obj);
}
static void __stdio_fork_prepare(void) {
FILE *f;
__fflush_lock();
for (int i = 0; i < __fflush.handles.i; ++i)
if ((f = __fflush.handles.p[i]))
pthread_mutex_lock(&f->lock);
}
static void __stdio_fork_parent(void) {
FILE *f;
for (int i = __fflush.handles.i; i--;)
if ((f = __fflush.handles.p[i]))
pthread_mutex_unlock(&f->lock);
__fflush_unlock();
}
static void __stdio_fork_child(void) {
FILE *f;
for (int i = __fflush.handles.i; i--;)
if ((f = __fflush.handles.p[i]))
f->lock = (pthread_mutex_t)PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
pthread_mutex_init(&__fflush_lock_obj, 0);
}
__attribute__((__constructor__(60))) static textstartup void stdioinit(void) {
pthread_atfork(__stdio_fork_prepare, __stdio_fork_parent, __stdio_fork_child);
}

View file

@ -16,27 +16,25 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h" #include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
static FILE __stderr = {
.fd = STDERR_FILENO,
.bufmode = _IONBF,
.iomode = O_WRONLY,
.buf = __stderr.mem,
.size = sizeof(stderr->mem),
.lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
};
/** /**
* Pointer to standard error stream. * Pointer to standard error stream.
*/ */
FILE *stderr; FILE *stderr = &__stderr;
static FILE __stderr;
__attribute__((__constructor__(60))) static textstartup void errinit(void) { __attribute__((__constructor__(60))) static textstartup void errinit(void) {
stderr = &__stderr;
stderr->fd = STDERR_FILENO;
stderr->bufmode = _IONBF;
stderr->iomode = O_WRONLY;
stderr->buf = stderr->mem;
stderr->size = sizeof(stderr->mem);
stderr->lock._word = PTHREAD_MUTEX_RECURSIVE;
__fflush_register(stderr); __fflush_register(stderr);
} }

View file

@ -16,30 +16,29 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h" #include "libc/calls/struct/stat.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h" #include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/s.h" #include "libc/sysv/consts/s.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
static FILE __stdin = {
.fd = STDIN_FILENO,
.iomode = O_RDONLY,
.bufmode = _IOFBF,
.buf = __stdin.mem,
.size = sizeof(stdin->mem),
.lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
};
/** /**
* Pointer to standard input stream. * Pointer to standard input stream.
*/ */
FILE *stdin; FILE *stdin = &__stdin;
static FILE __stdin;
__attribute__((__constructor__(60))) static textstartup void initin(void) { __attribute__((__constructor__(60))) static textstartup void initin(void) {
struct stat st; struct stat st;
stdin = &__stdin;
stdin->fd = STDIN_FILENO;
stdin->iomode = O_RDONLY;
stdin->buf = stdin->mem;
stdin->size = sizeof(stdin->mem);
stdin->lock._word = PTHREAD_MUTEX_RECURSIVE;
if (fstat(STDIN_FILENO, &st) || !S_ISREG(st.st_mode)) if (fstat(STDIN_FILENO, &st) || !S_ISREG(st.st_mode))
stdin->bufmode = _IONBF; stdin->bufmode = _IONBF;
__fflush_register(stdin); __fflush_register(stdin);

View file

@ -16,39 +16,32 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/stdio/internal.h" #include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h" #include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/thread/thread.h" #include "libc/thread/thread.h"
static FILE __stdout = {
.fd = STDOUT_FILENO,
.iomode = O_WRONLY,
.buf = __stdout.mem,
.size = sizeof(stdout->mem),
.lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP,
// Unlike other C libraries we don't bother calling fstat() to check
// if stdio is a character device and we instead choose to always
// line buffer it. We need it because there's no way to use the
// unbuffer command on a statically linked binary. This still goes
// fast. We value latency more than throughput, and stdio isn't the
// best api when the goal is throughput.
.bufmode = _IOLBF,
};
/** /**
* Pointer to standard output stream. * Pointer to standard output stream.
*/ */
FILE *stdout; FILE *stdout = &__stdout;
static FILE __stdout;
__attribute__((__constructor__(60))) static textstartup void outinit(void) { __attribute__((__constructor__(60))) static textstartup void outinit(void) {
stdout = &__stdout;
stdout->fd = STDOUT_FILENO;
stdout->iomode = O_WRONLY;
stdout->buf = stdout->mem;
stdout->size = sizeof(stdout->mem);
stdout->lock._word = PTHREAD_MUTEX_RECURSIVE;
/*
* Unlike other C libraries we don't bother calling fstat() to check
* if stdio is a character device and we instead choose to always line
* buffer it. We need it because there's no way to use the unbuffer
* command on a statically linked binary. This still goes fast. We
* value latency more than throughput, and stdio isn't the best api
* when the goal is throughput.
*/
stdout->bufmode = _IOLBF;
__fflush_register(stdout); __fflush_register(stdout);
} }

View file

@ -83,12 +83,8 @@ void mu_wipe(void) {
pthread_mutex_init(&mu, 0); pthread_mutex_init(&mu, 0);
} }
static atomic_bool once;
void *Worker(void *arg) { void *Worker(void *arg) {
for (int i = 0; i < 20; ++i) { for (int i = 0; i < 20; ++i) {
if (!atomic_exchange(&once, true))
__print_maps(0);
mu_lock(); mu_lock();
usleep(20); usleep(20);
mu_unlock(); mu_unlock();