Revert "Make spin locks go faster"

This reverts commit c8e25d811c.
This commit is contained in:
Justine Tunney 2024-07-25 22:24:32 -07:00
parent 0679cfeb41
commit 02e1cbcd00
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 122 additions and 149 deletions

View file

@ -21,46 +21,30 @@
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"
static int count;
static void (*funcs[32])(void);
static pthread_mutex_t __quick_exit_lock_obj;
static void __quick_exit_wipe(void) {
pthread_mutex_init(&__quick_exit_lock_obj, 0);
}
static void __quick_exit_lock(void) {
pthread_mutex_lock(&__quick_exit_lock_obj);
}
static void __quick_exit_unlock(void) {
pthread_mutex_unlock(&__quick_exit_lock_obj);
}
static int count;
static pthread_spinlock_t lock;
pthread_spinlock_t *const __at_quick_exit_lockptr = &lock;
void __funcs_on_quick_exit(void) {
void (*func)(void);
__quick_exit_lock();
pthread_spin_lock(&lock);
while (count) {
func = funcs[--count];
__quick_exit_unlock();
pthread_spin_unlock(&lock);
func();
__quick_exit_lock();
pthread_spin_lock(&lock);
}
}
int at_quick_exit(void func(void)) {
int res = 0;
__quick_exit_lock();
pthread_spin_lock(&lock);
if (count == ARRAYLEN(funcs)) {
res = -1;
} else {
funcs[count++] = func;
}
__quick_exit_unlock();
pthread_spin_unlock(&lock);
return res;
}
__attribute__((__constructor__(10))) textstartup void __quick_exit_init(void) {
pthread_atfork(__quick_exit_lock, __quick_exit_unlock, __quick_exit_wipe);
__quick_exit_wipe();
}

View file

@ -17,8 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/cosmo.h"
#include "libc/errno.h"
#include "libc/intrin/promises.h"
#include "libc/intrin/strace.h"
@ -29,12 +27,14 @@
#include "libc/runtime/symbols.internal.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/str/str.h"
#include "libc/thread/thread.h"
#include "libc/x/x.h"
#include "libc/zip.internal.h"
#include "third_party/puff/puff.h"
__static_yoink("__get_symbol");
static pthread_spinlock_t g_lock;
struct SymbolTable *__symtab; // for kprintf
static ssize_t GetZipFile(struct Zipos *zipos, const char *name) {
@ -100,25 +100,6 @@ static struct SymbolTable *GetSymbolTableFromElf(void) {
}
}
static void GetSymbolTableInit(void) {
struct Zipos *z;
int e = errno;
if (!__symtab && !__isworker) {
if (_weaken(__zipos_get) && (z = _weaken(__zipos_get)())) {
if ((__symtab = GetSymbolTableFromZip(z))) {
__symtab->names =
(uint32_t *)((char *)__symtab + __symtab->names_offset);
__symtab->name_base =
(char *)((char *)__symtab + __symtab->name_base_offset);
}
}
if (!__symtab) {
__symtab = GetSymbolTableFromElf();
}
}
errno = e;
}
/**
* Returns symbol table singleton.
*
@ -140,7 +121,24 @@ static void GetSymbolTableInit(void) {
* @return symbol table, or NULL if not found
*/
struct SymbolTable *GetSymbolTable(void) {
static atomic_uint once;
cosmo_once(&once, GetSymbolTableInit);
struct Zipos *z;
if (pthread_spin_trylock(&g_lock))
return 0;
int e = errno;
if (!__symtab && !__isworker) {
if (_weaken(__zipos_get) && (z = _weaken(__zipos_get)())) {
if ((__symtab = GetSymbolTableFromZip(z))) {
__symtab->names =
(uint32_t *)((char *)__symtab + __symtab->names_offset);
__symtab->name_base =
(char *)((char *)__symtab + __symtab->name_base_offset);
}
}
if (!__symtab) {
__symtab = GetSymbolTableFromElf();
}
}
errno = e;
pthread_spin_unlock(&g_lock);
return __symtab;
}