Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 424113966
  • Loading branch information
achoum authored and copybara-github committed Jan 25, 2022
1 parent 1d0d6eb commit 945af7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 9 additions & 0 deletions yggdrasil_decision_forests/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ cc_library_ydf(
"//yggdrasil_decision_forests:std_synchronization_primitives": [
"YGG_STD_MUTEX",
"YGG_STD_BLOCKING_COUNTER_AND_NOTIFICATION",
# When YGG_STD_MUTEX and YGG_STD_BLOCKING_COUNTER_AND_NOTIFICATION are set,
# this build rule relies on c++17 and c++20 primitives (see the comments in
# synchronization_primitives.h). However, this rule can effectively be compiled with
# c++14 using a modern enought compiler with availabilities disabled (done by defining
# _LIBCPP_DISABLE_AVAILABILITY).
#
# See:
# https://releases.llvm.org/3.1/tools/clang/docs/LanguageExtensions.html#availability
"_LIBCPP_DISABLE_AVAILABILITY",
],
"//conditions:default": [],
}),
Expand Down
22 changes: 15 additions & 7 deletions yggdrasil_decision_forests/utils/synchronization_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifdef YGG_STD_MUTEX
#include <condition_variable> // c++11
#include <mutex> // c++11
#include <shared_mutex> // c++17
#include <shared_mutex> // c++14 and c++17
#else
#include "absl/synchronization/mutex.h"
#endif
Expand All @@ -41,6 +41,14 @@ namespace utils {
namespace concurrency {

#ifdef YGG_STD_MUTEX

#if _LIBCPP_STD_VER >= 17
// shared_mutex was introduced in c++17.
using std_shared_mutex = std::shared_mutex;
#else
using std_shared_mutex = std::shared_timed_mutex;
#endif

class Mutex {
public:
std::mutex& std() { return mu_; }
Expand All @@ -51,10 +59,10 @@ class Mutex {

class SharedMutex {
public:
std::shared_mutex& std() { return mu_; }
std_shared_mutex& std() { return mu_; }

private:
std::shared_mutex mu_;
std_shared_mutex mu_;
};

class MutexLock {
Expand All @@ -69,19 +77,19 @@ class MutexLock {
class WriterMutexLock {
public:
WriterMutexLock(SharedMutex* mutex) : lock_(mutex->std()) {}
std::unique_lock<std::shared_mutex>& std() { return lock_; }
std::unique_lock<std_shared_mutex>& std() { return lock_; }

private:
std::unique_lock<std::shared_mutex> lock_;
std::unique_lock<std_shared_mutex> lock_;
};

class ReaderMutexLock {
public:
ReaderMutexLock(SharedMutex* mutex) : lock_(mutex->std()) {}
std::shared_lock<std::shared_mutex>& std() { return lock_; }
std::shared_lock<std_shared_mutex>& std() { return lock_; }

private:
std::shared_lock<std::shared_mutex> lock_;
std::shared_lock<std_shared_mutex> lock_;
};

class CondVar {
Expand Down

0 comments on commit 945af7a

Please sign in to comment.