Skip to content

Commit

Permalink
Remove workaround for lack of std::atomic_init (#996)
Browse files Browse the repository at this point in the history
Summary:
- Since GCC 5 and later has `std::atomic_init`, remove the workaround
  present in `Tearable.h` to default initialize atomic variables.
- Default initialization of atomics do not work as you would expect. See
  http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0883r0.pdf
  for the explanation why.
- To get around the default initialization issue, we just call
  `std::atomic_init` for each element in the array of atomics.
Pull Request resolved: #996

Reviewed By: LeeHowes

Differential Revision: D13648263

Pulled By: yfeldblum

fbshipit-source-id: 6f3c84089f9158bc5c0ad5efac13d49ef69f1770
  • Loading branch information
JoeLoser authored and facebook-github-bot committed Jan 18, 2019
1 parent 960bd85 commit 05c10a5
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions folly/synchronization/Tearable.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ class Tearable {
is_trivially_copyable<T>::value,
"Tearable types must be trivially copyable.");

Tearable() = default;
Tearable() noexcept {
for (std::size_t i = 0; i < kNumDataWords; ++i) {
std::atomic_init(&data_[i], RawWord{});
}
}

Tearable(const T& val) : Tearable() {
store(val);
Expand Down Expand Up @@ -87,15 +91,10 @@ class Tearable {
// trailing data word in write(), for instance).
unsigned char data alignas(void*)[sizeof(void*)];
};
// Because std::atomic_init is declared but undefined in libstdc++-v4.9.2:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64658.
struct AtomicWord : std::atomic<RawWord> {
AtomicWord() noexcept : std::atomic<RawWord>{RawWord{}} {}
};
const static std::size_t kNumDataWords =
(sizeof(T) + sizeof(RawWord) - 1) / sizeof(RawWord);

AtomicWord data_[kNumDataWords];
std::atomic<RawWord> data_[kNumDataWords];
};

} // namespace folly

0 comments on commit 05c10a5

Please sign in to comment.