[v3] 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
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 | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
Comments
On Tue, Dec 14, 2021 at 4:31 AM Jangwoong Kim via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> 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 | 5 +----
> 1 file changed, 1 insertion(+), 4 deletions(-)
>
> diff --git a/nptl/pthread_mutex_lock.c b/nptl/pthread_mutex_lock.c
> index 47b88a6b5b..06274c68e4 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 (LLL_MUTEX_READ_LOCK (mutex) != 0 || LLL_MUTEX_TRYLOCK (mutex) != 0)
> {
> if (cnt++ >= max_cnt)
> {
> @@ -146,10 +146,7 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
> break;
> }
> atomic_spin_nop ();
> - if (LLL_MUTEX_READ_LOCK (mutex) != 0)
> - continue;
> }
> - while (LLL_MUTEX_TRYLOCK (mutex) != 0);
>
> mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
> }
> --
> 2.25.1
>
LGTM.
Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
Thanks.
@@ -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 (LLL_MUTEX_READ_LOCK (mutex) != 0 || LLL_MUTEX_TRYLOCK (mutex) != 0)
{
if (cnt++ >= max_cnt)
{
@@ -146,10 +146,7 @@ PTHREAD_MUTEX_LOCK (pthread_mutex_t *mutex)
break;
}
atomic_spin_nop ();
- if (LLL_MUTEX_READ_LOCK (mutex) != 0)
- continue;
}
- while (LLL_MUTEX_TRYLOCK (mutex) != 0);
mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
}