Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libpthread libpthread: Fix membars around rwlocks.



details:   https://anonhg.NetBSD.org/src/rev/f57ec24a4fb4
branches:  trunk
changeset: 361126:f57ec24a4fb4
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Fri Feb 11 21:40:58 2022 +0000

description:
libpthread: Fix membars around rwlocks.

1. After loading self->pt_rwlocked, membar_enter() must not be
   conditional on PTHREAD__ATOMIC_IS_MEMBAR because there is no
   atomic r/m/w operation here which could imply the acquire barrier.

   (This should maybe just be a load-acquire operation, but we don't
   have atomic_load_acquire in userland at the moment -- TBD.)

2. Before storing thread->pt_rwlocked, must issue membar_exit() so
   that this is a store-release operation -- except if we had just
   done an atomic r/m/w and PTHREAD__ATOMIC_IS_MEMBAR is set, in
   which case it can be elided.

   The second membar_exit() added here might be safely hoisted out of
   the loop but I'm not sure -- needs more analysis to prove that
   would be safe.

diffstat:

 lib/libpthread/pthread_rwlock.c |  12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)

diffs (56 lines):

diff -r e0e53d64b031 -r f57ec24a4fb4 lib/libpthread/pthread_rwlock.c
--- a/lib/libpthread/pthread_rwlock.c   Fri Feb 11 21:36:46 2022 +0000
+++ b/lib/libpthread/pthread_rwlock.c   Fri Feb 11 21:40:58 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_rwlock.c,v 1.42 2020/06/02 00:29:53 joerg Exp $ */
+/*     $NetBSD: pthread_rwlock.c,v 1.43 2022/02/11 21:40:58 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2020 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_rwlock.c,v 1.42 2020/06/02 00:29:53 joerg Exp $");
+__RCSID("$NetBSD: pthread_rwlock.c,v 1.43 2022/02/11 21:40:58 riastradh Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
@@ -228,9 +228,7 @@
 
                /* Did we get the lock? */
                if (self->pt_rwlocked == _RW_LOCKED) {
-#ifndef PTHREAD__ATOMIC_IS_MEMBAR
                        membar_enter();
-#endif
                        return 0;
                }
                if (error != 0)
@@ -352,9 +350,7 @@
 
                /* Did we get the lock? */
                if (self->pt_rwlocked == _RW_LOCKED) {
-#ifndef PTHREAD__ATOMIC_IS_MEMBAR
                        membar_enter();
-#endif
                        return 0;
                }
                if (error != 0)
@@ -526,6 +522,9 @@
                         * by the writer that we are about to wake.
                         */
                        (void)atomic_swap_ptr(&ptr->ptr_owner, (void *)new);
+#ifndef PTHREAD__ATOMIC_IS_MEMBAR
+                       membar_exit();
+#endif
 
                        /* Wake the writer. */
                        thread->pt_rwlocked = _RW_LOCKED;
@@ -543,6 +542,7 @@
                                if (thread->pt_sleepobj == NULL)
                                        continue;
                                new += RW_READ_INCR;
+                               membar_exit();
                                thread->pt_rwlocked = _RW_LOCKED;
                        }
 



Home | Main Index | Thread Index | Old Index