Skip to content

Commit

Permalink
Merge 39861ac into dd94a58
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona authored Jun 24, 2020
2 parents dd94a58 + 39861ac commit 271d71e
Showing 1 changed file with 50 additions and 14 deletions.
64 changes: 50 additions & 14 deletions cpp/src/arrow/util/atomic_shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,73 @@
#include <memory>
#include <utility>

#include "arrow/type_traits.h"

namespace arrow {
namespace internal {

#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 5
// Atomic shared_ptr operations only appeared in libstdc++ since GCC 5,
// emulate them with unsafe ops if unavailable.
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57250

template <typename T, typename = void>
struct is_atomic_load_shared_ptr_available : std::false_type {};

template <typename T>
struct is_atomic_load_shared_ptr_available<
T, void_t<decltype(std::atomic_load(std::declval<const std::shared_ptr<T>*>()))>>
: std::true_type {};

template <typename T>
using enable_if_atomic_load_shared_ptr_available =
enable_if_t<is_atomic_load_shared_ptr_available<T>::value, T>;

// atomic shared_ptr operations only appeared in gcc 5,
// emulate them with unsafe ops on gcc 4.x.
template <typename T>
using enable_if_atomic_load_shared_ptr_unavailable =
enable_if_t<!is_atomic_load_shared_ptr_available<T>::value, T>;

template <class T>
inline std::shared_ptr<T> atomic_load(const std::shared_ptr<T>* p) {
return *p;
inline enable_if_atomic_load_shared_ptr_available<std::shared_ptr<T>> atomic_load(
const std::shared_ptr<T>* p) {
return std::atomic_load(p);
}

template <class T>
inline void atomic_store(std::shared_ptr<T>* p, std::shared_ptr<T> r) {
*p = r;
inline enable_if_atomic_load_shared_ptr_unavailable<std::shared_ptr<T>> atomic_load(
const std::shared_ptr<T>* p) {
return *p;
}

#else
template <typename T, typename = void>
struct is_atomic_store_shared_ptr_available : std::false_type {};

template <class T>
inline std::shared_ptr<T> atomic_load(const std::shared_ptr<T>* p) {
return std::atomic_load(p);
}
template <typename T>
struct is_atomic_store_shared_ptr_available<
T, void_t<decltype(std::atomic_store(std::declval<std::shared_ptr<T>*>(),
std::declval<std::shared_ptr<T>>()))>>
: std::true_type {};

template <typename T>
using enable_if_atomic_store_shared_ptr_available =
enable_if_t<is_atomic_store_shared_ptr_available<T>::value, T>;

template <typename T>
using enable_if_atomic_store_shared_ptr_unavailable =
enable_if_t<!is_atomic_store_shared_ptr_available<T>::value, T>;

template <class T>
inline void atomic_store(std::shared_ptr<T>* p, std::shared_ptr<T> r) {
inline void atomic_store(
enable_if_atomic_store_shared_ptr_available<std::shared_ptr<T>*> p,
std::shared_ptr<T> r) {
std::atomic_store(p, std::move(r));
}

#endif
template <class T>
inline void atomic_store(
enable_if_atomic_store_shared_ptr_unavailable<std::shared_ptr<T>*> p,
std::shared_ptr<T> r) {
*p = r;
}

} // namespace internal
} // namespace arrow

0 comments on commit 271d71e

Please sign in to comment.