Skip to content

Commit

Permalink
In TsSharedMutex.h, make error reporting thread-safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter Karas committed Jan 31, 2022
1 parent d7b1154 commit 72209a9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 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 All @@ -51,8 +52,13 @@

#endif

namespace ts
{
#if F
#error "F preprocessor symbol defined"
#endif

#define F(FUNC_NAME, PTR, ENUM) \
TSFatal("%s(%p) failed: %s (%s) (%d)", (FUNC_NAME), (PTR), strerrordesc_np(ENUM), strerrorname_np(ENUM), (ENUM));

// 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 +76,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);
F("pthread_rwlock_wrlock", &_lock, error);
}
X(_exclusive = true;)
}
Expand All @@ -83,7 +89,7 @@ class shared_mutex
return false;
}
if (error != 0) {
TSFatal("pthread_rwlock_trywrlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
F("pthread_rwlock_trywrlock", &_lock, error);
}
X(_exclusive = true;)

Expand All @@ -104,7 +110,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);
F("pthread_rwlock_rdlock", &_lock, error);
}
X(++_shared;)
}
Expand All @@ -117,7 +123,7 @@ class shared_mutex
return false;
}
if (error != 0) {
TSFatal("pthread_rwlock_tryrdlock(%p) failed: %s (%d)", &_lock, strerror(error), error);
F("pthread_rwlock_tryrdlock", &_lock, error);
}

X(++_shared;)
Expand All @@ -139,7 +145,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);
F("pthread_rwlock_destroy", &_lock, error);
}
}

Expand All @@ -157,7 +163,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);
F("pthread_rwlock_unlock", &_lock, error);
}
}

Expand All @@ -184,3 +190,4 @@ class shared_mutex
} // end namespace ts

#undef X
#undef F

0 comments on commit 72209a9

Please sign in to comment.