Clean up threading code some more

This commit is contained in:
Justine Tunney 2022-09-13 14:57:38 -07:00
parent 6a3330d7c9
commit 654ceaba7d
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
28 changed files with 119 additions and 134 deletions

View file

@ -19,7 +19,6 @@
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/likely.h"
#include "libc/intrin/weaken.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
@ -34,10 +33,10 @@
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
int c, t;
if (LIKELY(__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_unlock))) {
if (__tls_enabled && //
mutex->_type == PTHREAD_MUTEX_NORMAL && //
mutex->_pshared == PTHREAD_PROCESS_PRIVATE && //
_weaken(nsync_mu_unlock)) {
_weaken(nsync_mu_unlock)((nsync_mu *)mutex);
return 0;
}
@ -48,21 +47,17 @@ int pthread_mutex_unlock(pthread_mutex_t *mutex) {
}
t = gettid();
if (mutex->_type == PTHREAD_MUTEX_ERRORCHECK) {
c = atomic_load_explicit(&mutex->_lock, memory_order_relaxed);
if ((c & 0x000fffff) != t) {
return EPERM;
}
if (mutex->_owner != t) {
return EPERM;
}
c = atomic_fetch_sub(&mutex->_lock, 0x00100000);
if ((c & 0x0ff00000) == 0x00100000) {
atomic_store_explicit(&mutex->_lock, 0, memory_order_release);
if ((c & 0xf0000000) == 0x20000000) {
// _futex_wake(&mutex->_lock, 1, mutex->_pshared);
pthread_yield();
}
if (mutex->_depth) {
--mutex->_depth;
return 0;
}
mutex->_owner = 0;
atomic_store_explicit(&mutex->_lock, 0, memory_order_release);
return 0;
}