mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-06 03:08:31 +00:00
Make spin locks go faster
This commit is contained in:
parent
a31d5ea399
commit
c8e25d811c
16 changed files with 150 additions and 123 deletions
|
@ -86,27 +86,15 @@ void __maps_init(void) {
|
|||
|
||||
privileged bool __maps_lock(void) {
|
||||
struct CosmoTib *tib;
|
||||
if (!__tls_enabled)
|
||||
if (__tls_enabled)
|
||||
return false;
|
||||
tib = __get_tls_privileged();
|
||||
if (atomic_fetch_add_explicit(&tib->tib_relock_maps, 1, memory_order_relaxed))
|
||||
return true;
|
||||
int backoff = 0;
|
||||
while (atomic_exchange_explicit(&__maps.lock, 1, memory_order_acquire)) {
|
||||
if (backoff < 7) {
|
||||
volatile int i;
|
||||
for (i = 0; i != 1 << backoff; i++) {
|
||||
}
|
||||
backoff++;
|
||||
} else {
|
||||
// STRACE("pthread_delay_np(__maps)");
|
||||
#if defined(__GNUC__) && defined(__aarch64__)
|
||||
__asm__ volatile("yield");
|
||||
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
|
||||
__asm__ volatile("pause");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
while (atomic_exchange_explicit(&__maps.lock, 1, memory_order_acquire))
|
||||
for (;;)
|
||||
if (!atomic_load_explicit(&__maps.lock, memory_order_relaxed))
|
||||
break;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue