Use *NSYNC for POSIX threads locking APIs

Condition variables, barriers, and r/w locks now work very well.
This commit is contained in:
Justine Tunney 2022-09-11 11:02:07 -07:00
parent 3de35e196c
commit b5cb71ab84
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
197 changed files with 3734 additions and 3817 deletions

View file

@ -16,24 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/atomic.h"
#include "libc/intrin/futex.internal.h"
#include "libc/thread/thread.h"
static int pthread_rwlock_rdlock_spin(pthread_rwlock_t *rwlock, int expect,
int tries) {
if (tries < 7) {
volatile int i;
for (i = 0; i != 1 << tries; i++) {
}
tries++;
} else {
atomic_fetch_add(&rwlock->waits, 1);
_futex_wait(&rwlock->lock, expect, rwlock->pshared, &(struct timespec){1});
atomic_fetch_sub(&rwlock->waits, 1);
}
return tries;
}
#include "third_party/nsync/mu.h"
/**
* Acquires read lock on read-write lock.
@ -41,18 +25,6 @@ static int pthread_rwlock_rdlock_spin(pthread_rwlock_t *rwlock, int expect,
* @return 0 on success, or errno on error
*/
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) {
int old, tries;
for (tries = 0;;) {
old = atomic_load_explicit(&rwlock->lock, memory_order_relaxed);
if (old >= 0) {
do {
if (atomic_compare_exchange_weak_explicit(&rwlock->lock, &old, old + 1,
memory_order_acquire,
memory_order_relaxed)) {
return 0;
}
} while (old >= 0);
}
tries = pthread_rwlock_rdlock_spin(rwlock, old, tries);
}
nsync_mu_rlock((nsync_mu *)rwlock);
return 0;
}