Pay off more technical debt

This makes breaking changes to add underscores to many non-standard
function names provided by the c library. MODE=tiny is now tinier and
we now use smaller locks that are better for tiny apps in this mode.
Some headers have been renamed to be in the same folder as the build
package, so it'll be easier to know which build dependency is needed.
Certain old misguided interfaces have been removed. Intel intrinsics
headers are now listed in libc/isystem (but not in the amalgamation)
to help further improve open source compatibility. Header complexity
has also been reduced. Lastly, more shell scripts are now available.
This commit is contained in:
Justine Tunney 2022-09-12 23:10:38 -07:00
parent b69f3d2488
commit 6f7d0cb1c3
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
960 changed files with 4072 additions and 4873 deletions

View file

@ -1,5 +1,8 @@
// clang-format off
#include "third_party/nsync/mu.h"
#include "libc/atomic.h"
#include "libc/intrin/atomic.h"
#include "libc/calls/calls.h"
#include "libc/thread/tls.h"
/* --------------------------- Lock preliminaries ------------------------ */
@ -32,12 +35,40 @@
*/
static int malloc_lock(atomic_int *lk) {
if (!__threaded) return 0;
while (atomic_exchange_explicit(lk, 1, memory_order_acquire)) {
sched_yield();
}
return 0;
}
static int malloc_trylock(atomic_int *lk) {
if (!__threaded) return 1;
return !atomic_exchange_explicit(lk, 1, memory_order_acquire);
}
static inline int malloc_unlock(atomic_int *lk) {
atomic_store_explicit(lk, 0, memory_order_release);
return 0;
}
#if !USE_LOCKS
#define USE_LOCK_BIT (0U)
#define INITIAL_LOCK(l) (0)
#define DESTROY_LOCK(l) (0)
#define ACQUIRE_MALLOC_GLOBAL_LOCK()
#define RELEASE_MALLOC_GLOBAL_LOCK()
#elif defined(TINY)
#define MLOCK_T atomic_int
#define ACQUIRE_LOCK(lk) malloc_lock(lk)
#define RELEASE_LOCK(lk) malloc_unlock(lk)
#define TRY_LOCK(lk) malloc_trylock(lk)
#define INITIAL_LOCK(lk) (*lk = 0, 0)
#define DESTROY_LOCK(lk)
#define ACQUIRE_MALLOC_GLOBAL_LOCK() ACQUIRE_LOCK(&malloc_global_mutex);
#define RELEASE_MALLOC_GLOBAL_LOCK() RELEASE_LOCK(&malloc_global_mutex);
static MLOCK_T malloc_global_mutex;
#else
#define MLOCK_T nsync_mu
#define ACQUIRE_LOCK(lk) (__threaded && (nsync_mu_lock(lk), 0))