#pragma once
+#include <folly/Likely.h>
#include <folly/Portability.h>
+#include <folly/ScopeGuard.h>
#include <boost/iterator/iterator_facade.hpp>
-#include <folly/Likely.h>
#include <type_traits>
-
+#include <utility>
namespace folly {
enum class TLPDestructionMode {
}
void reset(T* newPtr = nullptr) {
+ auto guard = makeGuard([&] { delete newPtr; });
threadlocal_detail::ElementWrapper& w = StaticMeta::instance().get(&id_);
- if (w.ptr != newPtr) {
- w.dispose(TLPDestructionMode::THIS_THREAD);
- w.set(newPtr);
- }
+ w.dispose(TLPDestructionMode::THIS_THREAD);
+ guard.dismiss();
+ w.set(newPtr);
}
explicit operator bool() const {
* deleter(T* ptr, TLPDestructionMode mode)
* "mode" is ALL_THREADS if we're destructing this ThreadLocalPtr (and thus
* deleting pointers for all threads), and THIS_THREAD if we're only deleting
- * the member for one thread (because of thread exit or reset())
+ * the member for one thread (because of thread exit or reset()).
+ * Invoking the deleter must not throw.
*/
template <class Deleter>
- void reset(T* newPtr, Deleter deleter) {
+ void reset(T* newPtr, const Deleter& deleter) {
+ auto guard = makeGuard([&] {
+ if (newPtr) {
+ deleter(newPtr, TLPDestructionMode::THIS_THREAD);
+ }
+ });
threadlocal_detail::ElementWrapper& w = StaticMeta::instance().get(&id_);
- if (w.ptr != newPtr) {
- w.dispose(TLPDestructionMode::THIS_THREAD);
- w.set(newPtr, deleter);
- }
+ w.dispose(TLPDestructionMode::THIS_THREAD);
+ guard.dismiss();
+ w.set(newPtr, deleter);
}
// Holds a global lock for iteration through all thread local child objects.
#include <folly/Malloc.h>
#include <folly/MicroSpinLock.h>
#include <folly/Portability.h>
+#include <folly/ScopeGuard.h>
#include <folly/detail/StaticSingletonManager.h>
template <class Ptr>
void set(Ptr p) {
+ auto guard = makeGuard([&] { delete p; });
DCHECK(ptr == nullptr);
DCHECK(deleter1 == nullptr);
delete static_cast<Ptr>(pt);
};
ownsDeleter = false;
+ guard.dismiss();
}
}
template <class Ptr, class Deleter>
- void set(Ptr p, Deleter d) {
+ void set(Ptr p, const Deleter& d) {
+ auto guard = makeGuard([&] {
+ if (p) {
+ d(p, TLPDestructionMode::THIS_THREAD);
+ }
+ });
DCHECK(ptr == nullptr);
DCHECK(deleter2 == nullptr);
if (p) {
ptr = p;
- deleter2 = new std::function<DeleterFunType>(
- [d](void* pt, TLPDestructionMode mode) {
- d(static_cast<Ptr>(pt), mode);
- });
+ deleter2 = new std::function<DeleterFunType>([d = d](
+ void* pt, TLPDestructionMode mode) {
+ d(static_cast<Ptr>(pt), mode);
+ });
ownsDeleter = true;
+ guard.dismiss();
}
}