public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] nptl: Effectively skip CAS in spinlock loop
@ 2021-12-14 11:01 Jangwoong Kim
  2021-12-14 11:42 ` Andreas Schwab
  0 siblings, 1 reply; 2+ messages in thread
From: Jangwoong Kim @ 2021-12-14 11:01 UTC (permalink / raw)
  To: libc-alpha; +Cc: Jangwoong Kim

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(-)

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;
 	}
-- 
2.25.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] nptl: Effectively skip CAS in spinlock loop
  2021-12-14 11:01 [PATCH v2] nptl: Effectively skip CAS in spinlock loop Jangwoong Kim
@ 2021-12-14 11:42 ` Andreas Schwab
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Schwab @ 2021-12-14 11:42 UTC (permalink / raw)
  To: Jangwoong Kim via Libc-alpha; +Cc: Jangwoong Kim

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.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-14 11:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-14 11:01 [PATCH v2] nptl: Effectively skip CAS in spinlock loop Jangwoong Kim
2021-12-14 11:42 ` Andreas Schwab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).