Skip to content

Commit

Permalink
In TsSharedMutex.h, make error reporting thread-safe. (apache#8636)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4b0d3cb)
  • Loading branch information
Walt Karas committed Feb 3, 2022
1 parent 67c0d69 commit bd07eca
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions include/tscpp/util/TsSharedMutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma once

#include <pthread.h>
#include <string.h>

#if __has_include(<ts/ts.h>)
#include <ts/ts.h>
Expand Down Expand Up @@ -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
Expand All @@ -70,7 +95,7 @@ class shared_mutex
{
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;)
}
Expand All @@ -83,7 +108,7 @@ class shared_mutex
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;)

Expand All @@ -104,7 +129,7 @@ class shared_mutex
{
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;)
}
Expand All @@ -117,7 +142,7 @@ class shared_mutex
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;)
Expand All @@ -139,7 +164,7 @@ class shared_mutex
{
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);
}
}

Expand All @@ -157,7 +182,7 @@ class shared_mutex
{
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);
}
}

Expand All @@ -175,6 +200,12 @@ class shared_mutex
#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;)
Expand Down

0 comments on commit bd07eca

Please sign in to comment.