You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2022/02/17 00:08:50 UTC

[trafficserver] branch 9.2.x updated: In TsSharedMutex.h, make error reporting thread-safe. (#8636)

This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch 9.2.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/9.2.x by this push:
     new 1798829  In TsSharedMutex.h, make error reporting thread-safe. (#8636)
1798829 is described below

commit 1798829d2512077765a71f172a8950f0c0575712
Author: Walt Karas <wk...@verizonmedia.com>
AuthorDate: Wed Feb 2 17:55:16 2022 -0600

    In TsSharedMutex.h, make error reporting thread-safe. (#8636)
    
    (cherry picked from commit 4b0d3cb39102c287da2279906d87aa3aaea98b52)
---
 include/tscpp/util/TsSharedMutex.h | 43 ++++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/include/tscpp/util/TsSharedMutex.h b/include/tscpp/util/TsSharedMutex.h
index dfaf70f..fa00fc6 100644
--- a/include/tscpp/util/TsSharedMutex.h
+++ b/include/tscpp/util/TsSharedMutex.h
@@ -25,6 +25,7 @@
 #pragma once
 
 #include <pthread.h>
+#include <string.h>
 
 #if __has_include(<ts/ts.h>)
 #include <ts/ts.h>
@@ -53,6 +54,30 @@
 
 namespace ts
 {
+class Strerror
+{
+public:
+  Strerror(int err_num)
+  {
+    _c_str = strerror_r(err_num, _buf, 256);
+    if (!_c_str) {
+      _c_str = "strerror_r() call failed";
+    } else {
+      _buf[255] = '\0';
+    }
+  }
+
+  char const *
+  c_str() const
+  {
+    return (_c_str);
+  }
+
+private:
+  char _buf[256];
+  char const *_c_str;
+};
+
 // A class with the same interface as std::shared_mutex, but which is not prone to writer starvation.
 //
 class shared_mutex
@@ -70,7 +95,7 @@ public:
   {
     int error = pthread_rwlock_wrlock(&_lock);
     if (error != 0) {
-      TSFatal("pthread_rwlock_wrlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
+      _call_fatal("pthread_rwlock_wrlock", &_lock, error);
     }
     X(_exclusive = true;)
   }
@@ -83,7 +108,7 @@ public:
       return false;
     }
     if (error != 0) {
-      TSFatal("pthread_rwlock_trywrlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
+      _call_fatal("pthread_rwlock_trywrlock", &_lock, error);
     }
     X(_exclusive = true;)
 
@@ -104,7 +129,7 @@ public:
   {
     int error = pthread_rwlock_rdlock(&_lock);
     if (error != 0) {
-      TSFatal("pthread_rwlock_rdlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
+      _call_fatal("pthread_rwlock_rdlock", &_lock, error);
     }
     X(++_shared;)
   }
@@ -117,7 +142,7 @@ public:
       return false;
     }
     if (error != 0) {
-      TSFatal("pthread_rwlock_tryrdlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
+      _call_fatal("pthread_rwlock_tryrdlock", &_lock, error);
     }
 
     X(++_shared;)
@@ -139,7 +164,7 @@ public:
   {
     int error = pthread_rwlock_destroy(&_lock);
     if (error != 0) {
-      TSFatal("pthread_rwlock_destory(%p) failed: %s (%d)", &_lock, strerror(error), error);
+      _call_fatal("pthread_rwlock_destroy", &_lock, error);
     }
   }
 
@@ -157,7 +182,7 @@ private:
   {
     int error = pthread_rwlock_unlock(&_lock);
     if (error != 0) {
-      TSFatal("pthread_rwlock_unlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
+      _call_fatal("pthread_rwlock_unlock", &_lock, error);
     }
   }
 
@@ -175,6 +200,12 @@ private:
 #endif
 #endif
 
+  void
+  _call_fatal(char const *func_name, void *ptr, int errnum)
+  {
+    TSFatal("%s(%p) failed: %s (%d)", func_name, ptr, Strerror(errnum).c_str(), errnum);
+  }
+
   // In debug builds, make sure shared vs. exlusive locks and unlocks are properly paired.
   //
   X(std::atomic<bool> _exclusive;)