Skip to content

Commit

Permalink
mutex: Give back the spinning count in case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Oct 17, 2024
1 parent ed20182 commit d8f64cb
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mcfgthread/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ _MCF_mutex_lock_slow(_MCF_mutex* mutex, const int64_t* timeout_opt)

uint32_t my_mask = (uint32_t) (old.__sp_mask ^ new.__sp_mask);
if(my_mask != 0) {
__MCF_ASSERT((my_mask & (my_mask - 1U)) == 0);
__MCF_ASSERT(spin_count > 0);
spin_count *= (int) (__MCF_MUTEX_MAX_SPIN_COUNT / __MCF_MUTEX_SP_NFAIL_THRESHOLD);

Expand Down Expand Up @@ -116,7 +115,8 @@ _MCF_mutex_lock_slow(_MCF_mutex* mutex, const int64_t* timeout_opt)
}
}

/* We have wasted some time. Now allocate a sleeping count.
/* We have wasted some time, so return the spinning mask and allocate
* a sleeping count.
* IMPORTANT! We can increment the sleeping counter ONLY IF the mutex
* is being locked by another thread. Otherwise, if the other thread
* had unlocked the mutex before we incremented the sleeping counter,
Expand All @@ -126,7 +126,7 @@ _MCF_mutex_lock_slow(_MCF_mutex* mutex, const int64_t* timeout_opt)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
new.__locked = 1;
new.__sp_mask = old.__sp_mask;
new.__sp_mask = old.__sp_mask & ~my_mask;
new.__sp_nfail = do_adjust_sp_nfail(old.__sp_nfail, (int) old.__locked - 1);
new.__nsleep = old.__nsleep + old.__locked;
#pragma GCC diagnostic pop
Expand Down

0 comments on commit d8f64cb

Please sign in to comment.