Make mutex calling code 10x tinier

Calls to lock/unlock functions are now NOPs by default. The first time
clone() is called, they get turned into CALL instructions. Doing this
caused funcctions like fputc() to shrink from 85 bytes to 45+4 bytes.
Since the ANSI solution of `(__threaded && lock())` inlines os much
superfluous binary content into functions all over the place.
This commit is contained in:
Justine Tunney 2022-06-12 19:33:42 -07:00
parent 8cdec62f5b
commit 8b72490431
32 changed files with 494 additions and 210 deletions

View file

@ -4,6 +4,7 @@
#define LOCALTIME_IMPLEMENTATION
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/intrin/nopl.h"
#include "libc/intrin/pthread.h"
#include "libc/intrin/spinlock.h"
#include "libc/mem/mem.h"
@ -46,17 +47,22 @@ STATIC_YOINK("usr/share/zoneinfo/UTC");
static pthread_mutex_t locallock;
static int localtime_lock(void) {
int localtime_lock(void) {
pthread_mutex_lock(&locallock);
return 0;
}
static void localtime_unlock(void) {
void localtime_unlock(void) {
pthread_mutex_unlock(&locallock);
}
#if defined(__GNUC__) && !defined(__llvm__) && !defined(__STRICT_ANSI__)
#define localtime_lock() _NOPL0("__threadcalls", localtime_lock)
#define localtime_unlock() _NOPL0("__threadcalls", localtime_unlock)
#else
#define localtime_lock() (__threaded ? localtime_lock() : 0)
#define localtime_unlock() (__threaded ? localtime_unlock() : 0)
#endif
#ifndef TZ_ABBR_MAX_LEN
#define TZ_ABBR_MAX_LEN 16
@ -1743,7 +1749,7 @@ localtime_timesub(const time_t *timep, int_fast32_t offset,
** Normalize logic courtesy Paul Eggert.
*/
static inline bool
forceinline bool
increment_overflow(int *ip, int j)
{
#if defined(__GNUC__) && __GNUC__ >= 6
@ -1766,7 +1772,7 @@ increment_overflow(int *ip, int j)
#endif
}
static inline bool
forceinline bool
increment_overflow32(int_fast32_t *const lp, int const m)
{
#if defined(__GNUC__) && __GNUC__ >= 6
@ -1783,7 +1789,7 @@ increment_overflow32(int_fast32_t *const lp, int const m)
#endif
}
static inline bool
forceinline bool
increment_overflow_time(time_t *tp, int_fast32_t j)
{
#if defined(__GNUC__) && __GNUC__ >= 6