mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-29 00:32:29 +00:00
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:
parent
58d252f3db
commit
2f48a02b44
37 changed files with 2684 additions and 2209 deletions
|
@ -95,7 +95,6 @@ TEST(pthread_mutex_lock, recursive) {
|
|||
}
|
||||
ASSERT_EQ(0, pthread_mutex_lock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_unlock(&lock));
|
||||
ASSERT_EQ(0, pthread_mutex_destroy(&lock));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#define USE POSIX
|
||||
#define USE POSIX_RECURSIVE
|
||||
#define ITERATIONS 100000
|
||||
#define THREADS 30
|
||||
|
||||
|
|
|
@ -2,10 +2,11 @@
|
|||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
||||
int got_cleanup;
|
||||
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
|
||||
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_cond_t cv;
|
||||
pthread_mutex_t mu;
|
||||
|
||||
void cleanup(void* arg) {
|
||||
got_cleanup = 1;
|
||||
|
@ -23,6 +24,12 @@ void* worker(void* arg) {
|
|||
int main(int argc, char* argv[]) {
|
||||
void* rc;
|
||||
pthread_t th;
|
||||
pthread_mutexattr_t at;
|
||||
pthread_mutexattr_init(&at);
|
||||
pthread_mutexattr_settype(&at, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutex_init(&mu, &at);
|
||||
pthread_mutexattr_destroy(&at);
|
||||
pthread_cond_init(&cv, 0);
|
||||
if (pthread_create(&th, 0, worker, 0))
|
||||
return 2;
|
||||
if (pthread_cancel(th))
|
||||
|
@ -37,4 +44,6 @@ int main(int argc, char* argv[]) {
|
|||
return 7;
|
||||
if (pthread_mutex_unlock(&mu))
|
||||
return 8;
|
||||
pthread_mutex_destroy(&mu);
|
||||
pthread_cond_destroy(&cv);
|
||||
}
|
||||
|
|
|
@ -40,6 +40,12 @@ atomic_int gotcleanup;
|
|||
|
||||
void SetUpOnce(void) {
|
||||
testlib_enable_tmp_setup_teardown();
|
||||
pthread_mutexattr_t at;
|
||||
pthread_mutexattr_init(&at);
|
||||
pthread_mutexattr_settype(&at, PTHREAD_MUTEX_NORMAL);
|
||||
pthread_mutex_init(&mu, &at);
|
||||
pthread_mutexattr_destroy(&at);
|
||||
pthread_cond_init(&cv, 0);
|
||||
}
|
||||
|
||||
void SetUp(void) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue