This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make many-reader mutexes more resilient
authorKarl Williamson <khw@cpan.org>
Wed, 9 Dec 2020 01:30:56 +0000 (18:30 -0700)
committerKarl Williamson <khw@cpan.org>
Sun, 20 Dec 2020 05:00:29 +0000 (22:00 -0700)
These mutexes rely on a counter being accurate to work.  If for some
reason that I can't imagine happening, the count goes below 0, this
commit resets it to zero, which may be enough to cause the program to
continue.

thread.h

index 4c4966f..99679b2 100644 (file)
--- a/thread.h
+++ b/thread.h
         MUTEX_LOCK(&(mutex)->lock);                                 \
         (mutex)->readers_count--;                                   \
         if ((mutex)->readers_count <= 0) {                          \
+            assert((mutex)->readers_count == 0);                    \
             COND_SIGNAL(&(mutex)->wakeup);                          \
             (mutex)->readers_count = 0;                             \
         }                                                           \
     STMT_START {                                                    \
         MUTEX_LOCK(&(mutex)->lock);                                 \
         do {                                                        \
-            if ((mutex)->readers_count == 0)                        \
+            if ((mutex)->readers_count <= 0) {                      \
+                assert((mutex)->readers_count == 0);                \
+                (mutex)->readers_count = 0;                         \
                 break;                                              \
+            }                                                       \
             COND_WAIT(&(mutex)->wakeup, &(mutex)->lock);            \
         }                                                           \
         while (1);                                                  \