This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
consistently parenthesise the RW lock macro arguments
authorTony Cook <tony@develop-help.com>
Mon, 14 Dec 2020 04:54:25 +0000 (15:54 +1100)
committerKarl Williamson <khw@cpan.org>
Mon, 14 Dec 2020 20:25:17 +0000 (13:25 -0700)
and fix the mixed up pointer vs non-pointer use of the mutex
argument.

Without the parentheses (or without the new tests in the previous
commit) for code like;

 PERL_WRITE_LOCK(&PL_some_mutex);

the MUTEX_LOCK(mutex.lock) in that code would expand to:

  MUTEX_LOCK(&PL_some_mutex.lock);

and happen to work,  even though the next line has:

  if ((mutex)->readers_count)

treating the mutex parameter as a pointer.

With the parentheses the MUTEX_LOCK(mutex.lock) becomes:

  MUTEX_LOCK((&PL_some_mutex).lock);

which as expected fails to compile in a similar way to the pointer
test code in the previous commit.

thread.h

index 3802071..4c4966f 100644 (file)
--- a/thread.h
+++ b/thread.h
 
 #  define PERL_READ_LOCK(mutex)                                     \
     STMT_START {                                                    \
-        MUTEX_LOCK(mutex.lock);                                     \
+        MUTEX_LOCK(&(mutex)->lock);                                 \
         (mutex)->readers_count++;                                   \
-        MUTEX_UNLOCK(mutex.lock);                                   \
+        MUTEX_UNLOCK(&(mutex)->lock);                               \
     } STMT_END
 
 #  define PERL_READ_UNLOCK(mutex)                                   \
     STMT_START {                                                    \
-        MUTEX_LOCK(mutex.lock);                                     \
+        MUTEX_LOCK(&(mutex)->lock);                                 \
         (mutex)->readers_count--;                                   \
         if ((mutex)->readers_count <= 0) {                          \
-            COND_SIGNAL(mutex.wakeup);                              \
+            COND_SIGNAL(&(mutex)->wakeup);                          \
             (mutex)->readers_count = 0;                             \
         }                                                           \
-        MUTEX_UNLOCK(mutex.lock);                                   \
+        MUTEX_UNLOCK(&(mutex)->lock);                               \
     } STMT_END
 
 #  define PERL_WRITE_LOCK(mutex)                                    \
     STMT_START {                                                    \
-        MUTEX_LOCK(mutex.lock);                                     \
+        MUTEX_LOCK(&(mutex)->lock);                                 \
         do {                                                        \
             if ((mutex)->readers_count == 0)                        \
                 break;                                              \
-            COND_WAIT(mutex.wakeup, mutex.lock);                    \
+            COND_WAIT(&(mutex)->wakeup, &(mutex)->lock);            \
         }                                                           \
         while (1);                                                  \
                                                                     \
 
 #  define PERL_WRITE_UNLOCK(mutex)                                  \
     STMT_START {                                                    \
-        COND_SIGNAL(mutex.wakeup);                                  \
-        MUTEX_UNLOCK(mutex.lock);                                   \
+        COND_SIGNAL(&(mutex)->wakeup);                              \
+        MUTEX_UNLOCK(&(mutex)->lock);                               \
     } STMT_END
 
 #  define PERL_RW_MUTEX_INIT(mutex)                                 \
     STMT_START {                                                    \
-        MUTEX_INIT(mutex.lock);                                     \
-        COND_INIT(mutex.wakeup);                                    \
+        MUTEX_INIT(&(mutex)->lock);                                 \
+        COND_INIT(&(mutex)->wakeup);                                \
         (mutex)->readers_count = 0;                                 \
     } STMT_END
 
 #  define PERL_RW_MUTEX_DESTROY(mutex)                              \
     STMT_START {                                                    \
-        COND_DESTROY(mutex.wakeup);                                 \
-        MUTEX_DESTROY(mutex.lock);                                  \
+        COND_DESTROY(&(mutex)->wakeup);                             \
+        MUTEX_DESTROY(&(mutex)->lock);                              \
     } STMT_END
 
 #endif