aboutsummaryrefslogtreecommitdiffstats
path: root/src/mutex.c
diff options
context:
space:
mode:
authorDavid Goldblatt <davidgoldblatt@fb.com>2017-05-15 15:38:15 -0700
committerDavid Goldblatt <davidtgoldblatt@gmail.com>2017-05-19 14:21:27 -0700
commit26c792e61a163b38b373023bca2947283dcd1fc8 (patch)
tree89c4b0e1275707f6e671a23ad6ed548484becabc /src/mutex.c
parent6e62c6286258e340308b4a989b4bd80232fed8e1 (diff)
downloadplatform_external_jemalloc_new-26c792e61a163b38b373023bca2947283dcd1fc8.tar.gz
platform_external_jemalloc_new-26c792e61a163b38b373023bca2947283dcd1fc8.tar.bz2
platform_external_jemalloc_new-26c792e61a163b38b373023bca2947283dcd1fc8.zip
Allow mutexes to take a lock ordering enum at construction.
This lets us specify whether and how mutexes of the same rank are allowed to be acquired. Currently, we only allow two polices (only a single mutex at a given rank at a time, and mutexes acquired in ascending order), but we can plausibly allow more (e.g. the "release uncontended mutexes before blocking").
Diffstat (limited to 'src/mutex.c')
-rw-r--r--src/mutex.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/mutex.c b/src/mutex.c
index 3eec970f..b15bbf6e 100644
--- a/src/mutex.c
+++ b/src/mutex.c
@@ -138,9 +138,25 @@ malloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex) {
mutex_prof_data_init(&mutex->prof_data);
}
+static int
+mutex_addr_comp(const witness_t *witness1, void *mutex1,
+ const witness_t *witness2, void *mutex2) {
+ assert(mutex1 != NULL);
+ assert(mutex2 != NULL);
+ uintptr_t mu1int = (uintptr_t)mutex1;
+ uintptr_t mu2int = (uintptr_t)mutex2;
+ if (mu1int < mu2int) {
+ return -1;
+ } else if (mu1int == mu2int) {
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
bool
malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
- witness_rank_t rank) {
+ witness_rank_t rank, malloc_mutex_lock_order_t lock_order) {
mutex_prof_data_init(&mutex->prof_data);
#ifdef _WIN32
# if _WIN32_WINNT >= 0x0600
@@ -179,7 +195,13 @@ malloc_mutex_init(malloc_mutex_t *mutex, const char *name,
pthread_mutexattr_destroy(&attr);
#endif
if (config_debug) {
- witness_init(&mutex->witness, name, rank, NULL, NULL);
+ mutex->lock_order = lock_order;
+ if (lock_order == malloc_mutex_address_ordered) {
+ witness_init(&mutex->witness, name, rank,
+ mutex_addr_comp, &mutex);
+ } else {
+ witness_init(&mutex->witness, name, rank, NULL, NULL);
+ }
}
return false;
}
@@ -200,7 +222,7 @@ malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex) {
malloc_mutex_unlock(tsdn, mutex);
#else
if (malloc_mutex_init(mutex, mutex->witness.name,
- mutex->witness.rank)) {
+ mutex->witness.rank, mutex->lock_order)) {
malloc_printf("<jemalloc>: Error re-initializing mutex in "
"child\n");
if (opt_abort) {