Fix the locked flag for malloc_mutex_trylock().

This commit is contained in:
Qi Wang 2024-09-19 23:48:55 -07:00 committed by Qi Wang
parent db4f0e7182
commit 661fb1e672
1 changed files with 8 additions and 2 deletions

View File

@ -36,6 +36,8 @@ struct malloc_mutex_s {
* Hint flag to avoid exclusive cache line contention * Hint flag to avoid exclusive cache line contention
* during spin waiting. Placed along with prof_data * during spin waiting. Placed along with prof_data
* since it's always modified even with no contention. * since it's always modified even with no contention.
* Modified by the lock owner only (after acquired, and
* before release), and may be read by other threads.
*/ */
atomic_b_t locked; atomic_b_t locked;
#ifdef _WIN32 #ifdef _WIN32
@ -156,7 +158,12 @@ malloc_mutex_lock_final(malloc_mutex_t *mutex) {
static inline bool static inline bool
malloc_mutex_trylock_final(malloc_mutex_t *mutex) { malloc_mutex_trylock_final(malloc_mutex_t *mutex) {
return MALLOC_MUTEX_TRYLOCK(mutex); bool failed = MALLOC_MUTEX_TRYLOCK(mutex);
if (!failed) {
atomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);
}
return failed;
} }
static inline void static inline void
@ -216,7 +223,6 @@ malloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex) {
if (isthreaded) { if (isthreaded) {
if (malloc_mutex_trylock_final(mutex)) { if (malloc_mutex_trylock_final(mutex)) {
malloc_mutex_lock_slow(mutex); malloc_mutex_lock_slow(mutex);
atomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);
} }
mutex_owner_stats_update(tsdn, mutex); mutex_owner_stats_update(tsdn, mutex);
} }