Move folly/Hash.h to folly/hash/, leaving a shim
[folly.git] / folly / ThreadLocal.h
index 8f98ead62ffc1cc402bab5ba42720dc4ebc9263c..3c05074463e8ded6ff54928886b5fd748a8d2a63 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
  * objects of a parent.  accessAllThreads() initializes an accessor which holds
  * a global lock *that blocks all creation and destruction of ThreadLocal
  * objects with the same Tag* and can be used as an iterable container.
+ * accessAllThreads() can race with destruction of thread-local elements. We
+ * provide a strict mode which is dangerous because it requires the access lock
+ * to be held while destroying thread-local elements which could cause
+ * deadlocks. We gate this mode behind the AccessModeStrict template parameter.
  *
  * Intended use is for frequent write, infrequent read data access patterns such
  * as counters.
  * @author Spencer Ahrens (sahrens)
  */
 
-#ifndef FOLLY_THREADLOCAL_H_
-#define FOLLY_THREADLOCAL_H_
+#pragma once
 
-#include <folly/Portability.h>
 #include <boost/iterator/iterator_facade.hpp>
 #include <folly/Likely.h>
-#include <type_traits>
-
-
-namespace folly {
-enum class TLPDestructionMode {
-  THIS_THREAD,
-  ALL_THREADS
-};
-}  // namespace
-
+#include <folly/Portability.h>
+#include <folly/ScopeGuard.h>
+#include <folly/SharedMutex.h>
 #include <folly/detail/ThreadLocalDetail.h>
+#include <type_traits>
+#include <utility>
 
 namespace folly {
 
-template<class T, class Tag> class ThreadLocalPtr;
+template <class T, class Tag, class AccessMode>
+class ThreadLocalPtr;
 
-template<class T, class Tag=void>
+template <class T, class Tag = void, class AccessMode = void>
 class ThreadLocal {
  public:
   constexpr ThreadLocal() : constructor_([]() {
@@ -89,7 +87,7 @@ class ThreadLocal {
     tlp_.reset(newPtr);
   }
 
-  typedef typename ThreadLocalPtr<T,Tag>::Accessor Accessor;
+  typedef typename ThreadLocalPtr<T, Tag, AccessMode>::Accessor Accessor;
   Accessor accessAllThreads() const {
     return tlp_.accessAllThreads();
   }
@@ -109,7 +107,7 @@ class ThreadLocal {
     return ptr;
   }
 
-  mutable ThreadLocalPtr<T,Tag> tlp_;
+  mutable ThreadLocalPtr<T, Tag, AccessMode> tlp_;
   std::function<T*()> constructor_;
 };
 
@@ -139,10 +137,11 @@ class ThreadLocal {
  *       with __declspec(thread)
  */
 
-template<class T, class Tag=void>
+template <class T, class Tag = void, class AccessMode = void>
 class ThreadLocalPtr {
  private:
-  typedef threadlocal_detail::StaticMeta<Tag> StaticMeta;
+  typedef threadlocal_detail::StaticMeta<Tag, AccessMode> StaticMeta;
+
  public:
   constexpr ThreadLocalPtr() : id_() {}
 
@@ -162,7 +161,7 @@ class ThreadLocalPtr {
   }
 
   T* get() const {
-    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
+    threadlocal_detail::ElementWrapper& w = StaticMeta::instance().get(&id_);
     return static_cast<T*>(w.ptr);
   }
 
@@ -175,47 +174,81 @@ class ThreadLocalPtr {
   }
 
   T* release() {
-    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
+    threadlocal_detail::ElementWrapper& w = StaticMeta::instance().get(&id_);
 
     return static_cast<T*>(w.release());
   }
 
   void reset(T* newPtr = nullptr) {
-    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
+    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 {
     return get() != nullptr;
   }
 
+  /**
+   * reset() that transfers ownership from a smart pointer
+   */
+  template <
+      typename SourceT,
+      typename Deleter,
+      typename = typename std::enable_if<
+          std::is_convertible<SourceT*, T*>::value>::type>
+  void reset(std::unique_ptr<SourceT, Deleter> source) {
+    auto deleter = [delegate = source.get_deleter()](
+        T * ptr, TLPDestructionMode) {
+      delegate(ptr);
+    };
+    reset(source.release(), deleter);
+  }
+
+  /**
+   * reset() that transfers ownership from a smart pointer with the default
+   * deleter
+   */
+  template <
+      typename SourceT,
+      typename = typename std::enable_if<
+          std::is_convertible<SourceT*, T*>::value>::type>
+  void reset(std::unique_ptr<SourceT> source) {
+    reset(source.release());
+  }
+
   /**
    * reset() with a custom deleter:
    * 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) {
-    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
-    if (w.ptr != newPtr) {
-      w.dispose(TLPDestructionMode::THIS_THREAD);
-      w.set(newPtr, 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_);
+    w.dispose(TLPDestructionMode::THIS_THREAD);
+    guard.dismiss();
+    w.set(newPtr, deleter);
   }
 
   // Holds a global lock for iteration through all thread local child objects.
   // Can be used as an iterable container.
   // Use accessAllThreads() to obtain one.
   class Accessor {
-    friend class ThreadLocalPtr<T,Tag>;
+    friend class ThreadLocalPtr<T, Tag, AccessMode>;
 
-    threadlocal_detail::StaticMeta<Tag>& meta_;
+    threadlocal_detail::StaticMetaBase& meta_;
+    SharedMutex* accessAllThreadsLock_;
     std::mutex* lock_;
     uint32_t id_;
 
@@ -230,7 +263,7 @@ class ThreadLocalPtr {
           boost::bidirectional_traversal_tag> {   // traversal
       friend class Accessor;
       friend class boost::iterator_core_access;
-      const Accessor* const accessor_;
+      const Accessor* accessor_;
       threadlocal_detail::ThreadEntry* e_;
 
       void increment() {
@@ -288,10 +321,12 @@ class ThreadLocalPtr {
     Accessor& operator=(const Accessor&) = delete;
 
     Accessor(Accessor&& other) noexcept
-      : meta_(other.meta_),
-        lock_(other.lock_),
-        id_(other.id_) {
+        : meta_(other.meta_),
+          accessAllThreadsLock_(other.accessAllThreadsLock_),
+          lock_(other.lock_),
+          id_(other.id_) {
       other.id_ = 0;
+      other.accessAllThreadsLock_ = nullptr;
       other.lock_ = nullptr;
     }
 
@@ -305,20 +340,23 @@ class ThreadLocalPtr {
       assert(&meta_ == &other.meta_);
       assert(lock_ == nullptr);
       using std::swap;
+      swap(accessAllThreadsLock_, other.accessAllThreadsLock_);
       swap(lock_, other.lock_);
       swap(id_, other.id_);
     }
 
     Accessor()
-      : meta_(threadlocal_detail::StaticMeta<Tag>::instance()),
-        lock_(nullptr),
-        id_(0) {
-    }
+        : meta_(threadlocal_detail::StaticMeta<Tag, AccessMode>::instance()),
+          accessAllThreadsLock_(nullptr),
+          lock_(nullptr),
+          id_(0) {}
 
    private:
     explicit Accessor(uint32_t id)
-      : meta_(threadlocal_detail::StaticMeta<Tag>::instance()),
-        lock_(&meta_.lock_) {
+        : meta_(threadlocal_detail::StaticMeta<Tag, AccessMode>::instance()),
+          accessAllThreadsLock_(&meta_.accessAllThreadsLock_),
+          lock_(&meta_.lock_) {
+      accessAllThreadsLock_->lock();
       lock_->lock();
       id_ = id;
     }
@@ -326,8 +364,11 @@ class ThreadLocalPtr {
     void release() {
       if (lock_) {
         lock_->unlock();
+        DCHECK(accessAllThreadsLock_ != nullptr);
+        accessAllThreadsLock_->unlock();
         id_ = 0;
         lock_ = nullptr;
+        accessAllThreadsLock_ = nullptr;
       }
     }
   };
@@ -337,12 +378,12 @@ class ThreadLocalPtr {
   Accessor accessAllThreads() const {
     static_assert(!std::is_same<Tag, void>::value,
                   "Must use a unique Tag to use the accessAllThreads feature");
-    return Accessor(id_.getOrAllocate());
+    return Accessor(id_.getOrAllocate(StaticMeta::instance()));
   }
 
  private:
   void destroy() {
-    StaticMeta::destroy(&id_);
+    StaticMeta::instance().destroy(&id_);
   }
 
   // non-copyable
@@ -352,6 +393,4 @@ class ThreadLocalPtr {
   mutable typename StaticMeta::EntryID id_;
 };
 
-}  // namespace folly
-
-#endif /* FOLLY_THREADLOCAL_H_ */
+} // namespace folly