/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
void increment(IntT inc) {
auto cache = cache_.get();
- if (UNLIKELY(cache == nullptr || cache->parent_ == nullptr)) {
+ if (UNLIKELY(cache == nullptr)) {
cache = new IntCache(*this);
cache_.reset(cache);
}
ThreadCachedInt& operator-=(IntT inc) { increment(-inc); return *this; }
// pre-increment (we don't support post-increment)
ThreadCachedInt& operator++() { increment(1); return *this; }
- ThreadCachedInt& operator--() { increment(-1); return *this; }
+ ThreadCachedInt& operator--() {
+ increment(IntT(-1));
+ return *this;
+ }
// Thread-safe set function.
// This is a best effort implementation. In some edge cases, there could be
target_.store(newVal, std::memory_order_release);
}
- // This is a little tricky - it's possible that our IntCaches are still alive
- // in another thread and will get destroyed after this destructor runs, so we
- // need to make sure we signal that this parent is dead.
- ~ThreadCachedInt() {
- for (auto& cache : cache_.accessAllThreads()) {
- cache.parent_ = nullptr;
- }
- }
-
private:
std::atomic<IntT> target_;
std::atomic<uint32_t> cacheSize_;
- ThreadLocalPtr<IntCache,Tag> cache_; // Must be last for dtor ordering
+ ThreadLocalPtr<IntCache, Tag, AccessModeStrict>
+ cache_; // Must be last for dtor ordering
// This should only ever be modified by one thread
struct IntCache {
}
~IntCache() {
- if (parent_) {
- flush();
- }
+ flush();
}
};
};
-}
+} // namespace folly