[v2] nptl: Effectively skip CAS in spinlock loop

Message ID 20211214110119.592-1-6812skiii@gmail.com
State Superseded
Headers
Series [v2] nptl: Effectively skip CAS in spinlock loop |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Jangwoong Kim Dec. 14, 2021, 11:01 a.m. UTC
  The commit:
"Add LLL_MUTEX_READ_LOCK [BZ #28537]"
SHA1: d672a98a1af106bd68deb15576710cd61363f7a6

introduced LLL_MUTEX_READ_LOCK, to skip CAS in spinlock loop
if atomic load fails. But, "continue" inside of do-while loop
does not skip the evaluation of escape expression, thus CAS
is not skipped.

Replace do-while with while and skip LLL_MUTEX_TRYLOCK if LLL_MUTEX_READ_LOCK fails.
---
 nptl/pthread_mutex_lock.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
  

Comments

Andreas Schwab Dec. 14, 2021, 11:42 a.m. UTC | #1
On Dez 14 2021, Jangwoong Kim via Libc-alpha wrote:

> diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c
> index 47b88a6b5b..3ad199a5e1 100644
> --- a/nptl/pthread_mutex_lock.c
> +++ b/nptl/pthread_mutex_lock.c
> @@ -138,7 +138,7 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
>  	  int cnt = 0;
>  	  int max_cnt = MIN (max_adaptive_count (),
>  			     mutex->__data.__spins * 2 + 10);
> -	  do
> +	  while (1)
>  	    {
>  	      if (cnt++ >= max_cnt)
>  		{
> @@ -146,10 +146,9 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
>  		  break;
>  		}
>  	      atomic_spin_nop ();
> -	      if (LLL_MUTEX_READ_LOCK (mutex) != 0)
> -		continue;
> +	      if (LLL_MUTEX_READ_LOCK (mutex) == 0 && LLL_MUTEX_TRYLOCK (mutex) == 0)
> +		break;
>  	    }
> -	  while (LLL_MUTEX_TRYLOCK (mutex) != 0);

You can keep the condition in the loop trailer.
  

Patch

diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c
index 47b88a6b5b..3ad199a5e1 100644
--- a/nptl/pthread_mutex_lock.c
+++ b/nptl/pthread_mutex_lock.c
@@ -138,7 +138,7 @@  PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
 	  int cnt = 0;
 	  int max_cnt = MIN (max_adaptive_count (),
 			     mutex->__data.__spins * 2 + 10);
-	  do
+	  while (1)
 	    {
 	      if (cnt++ >= max_cnt)
 		{
@@ -146,10 +146,9 @@  PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
 		  break;
 		}
 	      atomic_spin_nop ();
-	      if (LLL_MUTEX_READ_LOCK (mutex) != 0)
-		continue;
+	      if (LLL_MUTEX_READ_LOCK (mutex) == 0 && LLL_MUTEX_TRYLOCK (mutex) == 0)
+		break;
 	    }
-	  while (LLL_MUTEX_TRYLOCK (mutex) != 0);
 
 	  mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
 	}