Make recursive mutexes faster

Recursive mutexes now go as fast as normal mutexes. The tradeoff is they
are no longer safe to use in signal handlers. However you can still have
signal safe mutexes if you set your mutex to both recursive and pshared.
You can also make functions that use recursive mutexes signal safe using
sigprocmask to ensure recursion doesn't happen due to any signal handler

The impact of this change is that, on Windows, many functions which edit
the file descriptor table rely on recursive mutexes, e.g. open(). If you
develop your app so it uses pread() and pwrite() then your app should go
very fast when performing a heavily multithreaded and contended workload

For example, when scaling to 40+ cores, *NSYNC mutexes can go as much as
1000x faster (in CPU time) than the naive recursive lock implementation.
Now recursive will use *NSYNC under the hood when it's possible to do so
This commit is contained in:
Justine Tunney 2024-09-09 22:07:03 -07:00
parent 58d252f3db
commit 2f48a02b44
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
37 changed files with 2684 additions and 2209 deletions

View file

@ -16,6 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/sysv/consts/clock.h"
#include "libc/thread/thread.h"
@ -29,8 +30,11 @@ errno_t pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *attr) {
*cond = (pthread_cond_t){0};
if (attr) {
cond->_footek = IsXnuSilicon() || attr->_pshared;
cond->_pshared = attr->_pshared;
cond->_clock = attr->_clock;
} else {
cond->_footek = IsXnuSilicon();
}
return 0;
}

View file

@ -42,9 +42,14 @@
errno_t pthread_cond_signal(pthread_cond_t *cond) {
#if PTHREAD_USE_NSYNC
// do nothing if pthread_cond_timedwait() hasn't been called yet
// this is because we dont know for certain if nsync is safe
if (!atomic_load_explicit(&cond->_waited, memory_order_acquire))
return 0;
// favor *NSYNC if this is a process private condition variable
// if using Mike Burrows' code isn't possible, use a naive impl
if (!cond->_pshared && !IsXnuSilicon()) {
if (!cond->_footek) {
nsync_cv_signal((nsync_cv *)cond);
return 0;
}

View file

@ -20,6 +20,7 @@
#include "libc/calls/cp.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/atomic.h"
#include "libc/sysv/consts/clock.h"
#include "libc/thread/lock.h"
#include "libc/thread/posixthread.internal.h"
@ -116,17 +117,30 @@ errno_t pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
MUTEX_OWNER(muword) != gettid())
return EPERM;
// if condition variable is shared then mutex must be too
if (cond->_pshared)
if (MUTEX_PSHARED(muword) != PTHREAD_PROCESS_SHARED)
#if PTHREAD_USE_NSYNC
// the first time pthread_cond_timedwait() is called we learn if the
// associated mutex is normal and private. that means *NSYNC is safe
// this decision is permanent. you can't use a recursive mutex later
if (!atomic_load_explicit(&cond->_waited, memory_order_acquire)) {
if (!cond->_footek)
if (MUTEX_TYPE(muword) != PTHREAD_MUTEX_NORMAL ||
MUTEX_PSHARED(muword) != PTHREAD_PROCESS_PRIVATE)
cond->_footek = true;
atomic_store_explicit(&cond->_waited, true, memory_order_release);
} else if (!cond->_footek) {
if (MUTEX_TYPE(muword) != PTHREAD_MUTEX_NORMAL ||
MUTEX_PSHARED(muword) != PTHREAD_PROCESS_PRIVATE)
return EINVAL;
}
#endif
// now perform the actual wait
errno_t err;
BEGIN_CANCELATION_POINT;
#if PTHREAD_USE_NSYNC
// favor *NSYNC if this is a process private condition variable
// if using Mike Burrows' code isn't possible, use a naive impl
if (!cond->_pshared && !IsXnuSilicon()) {
if (!cond->_footek) {
err = nsync_cv_wait_with_deadline(
(nsync_cv *)cond, (nsync_mu *)mutex, cond->_clock,
abstime ? *abstime : nsync_time_no_deadline, 0);

View file

@ -8,7 +8,6 @@
#define PTHREAD_BARRIER_SERIAL_THREAD 31337
#define PTHREAD_MUTEX_DEFAULT 0
#define PTHREAD_MUTEX_NORMAL 0
#define PTHREAD_MUTEX_RECURSIVE 1
#define PTHREAD_MUTEX_ERRORCHECK 2
@ -77,6 +76,7 @@ typedef struct pthread_mutex_s {
};
/* this cleverly overlaps with NSYNC struct Dll *waiters; */
_PTHREAD_ATOMIC(uint64_t) _word;
long _nsyncx[2];
} pthread_mutex_t;
typedef struct pthread_mutexattr_s {
@ -95,6 +95,8 @@ typedef struct pthread_cond_s {
uint32_t _nsync;
char _pshared;
char _clock;
char _footek;
_PTHREAD_ATOMIC(char) _waited;
};
};
_PTHREAD_ATOMIC(uint32_t) _sequence;