Move ThreadLocal.h into the implementation
[folly.git] / folly / ThreadLocal.h
index 0663417f1f7fe0d7f46e54f299fa09284216cd0c..c55fdbeb2c9825b3d971c7a74e536ea347988730 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #ifndef FOLLY_THREADLOCAL_H_
 #define FOLLY_THREADLOCAL_H_
 
-#include "folly/Portability.h"
+#include <folly/Portability.h>
 #include <boost/iterator/iterator_facade.hpp>
-#include "folly/Likely.h"
+#include <folly/Likely.h>
 #include <type_traits>
 
-// Use noexcept on gcc 4.6 or higher
-#undef FOLLY_NOEXCEPT
-#ifdef __GNUC__
-# ifdef HAVE_FEATURES_H
-#  include <features.h>
-#  if __GNUC_PREREQ(4,6)
-#    define FOLLY_NOEXCEPT noexcept
-#    define FOLLY_ASSERT(x) x
-#  endif
-# endif
-#endif
-
-#ifndef FOLLY_NOEXCEPT
-#  define FOLLY_NOEXCEPT
-#  define FOLLY_ASSERT(x) /**/
-#endif
 
 namespace folly {
 enum class TLPDestructionMode {
@@ -66,7 +50,7 @@ enum class TLPDestructionMode {
 };
 }  // namespace
 
-#include "folly/detail/ThreadLocalDetail.h"
+#include <folly/detail/ThreadLocalDetail.h>
 
 namespace folly {
 
@@ -75,7 +59,13 @@ template<class T, class Tag> class ThreadLocalPtr;
 template<class T, class Tag=void>
 class ThreadLocal {
  public:
-  ThreadLocal() { }
+  constexpr ThreadLocal() : constructor_([]() {
+      return new T();
+    }) {}
+
+  explicit ThreadLocal(std::function<T*()> constructor) :
+      constructor_(constructor) {
+  }
 
   T* get() const {
     T* ptr = tlp_.get();
@@ -114,12 +104,13 @@ class ThreadLocal {
   ThreadLocal& operator=(const ThreadLocal&) = delete;
 
   T* makeTlp() const {
-    T* ptr = new T();
+    auto ptr = constructor_();
     tlp_.reset(ptr);
     return ptr;
   }
 
   mutable ThreadLocalPtr<T,Tag> tlp_;
+  std::function<T*()> constructor_;
 };
 
 /*
@@ -140,22 +131,29 @@ class ThreadLocal {
  * We use a single global pthread_key_t per Tag to manage object destruction and
  * memory cleanup upon thread exit because there is a finite number of
  * pthread_key_t's available per machine.
+ *
+ * NOTE: Apple platforms don't support the same semantics for __thread that
+ *       Linux does (and it's only supported at all on i386). For these, use
+ *       pthread_setspecific()/pthread_getspecific() for the per-thread
+ *       storage.  Windows (MSVC and GCC) does support the same semantics
+ *       with __declspec(thread)
  */
 
 template<class T, class Tag=void>
 class ThreadLocalPtr {
+ private:
+  typedef threadlocal_detail::StaticMeta<Tag> StaticMeta;
  public:
-  ThreadLocalPtr() : id_(threadlocal_detail::StaticMeta<Tag>::create()) { }
+  constexpr ThreadLocalPtr() : id_() {}
 
-  ThreadLocalPtr(ThreadLocalPtr&& other) : id_(other.id_) {
-    other.id_ = 0;
+  ThreadLocalPtr(ThreadLocalPtr&& other) noexcept :
+    id_(std::move(other.id_)) {
   }
 
   ThreadLocalPtr& operator=(ThreadLocalPtr&& other) {
     assert(this != &other);
     destroy();
-    id_ = other.id_;
-    other.id_ = 0;
+    id_ = std::move(other.id_);
     return *this;
   }
 
@@ -164,7 +162,8 @@ class ThreadLocalPtr {
   }
 
   T* get() const {
-    return static_cast<T*>(threadlocal_detail::StaticMeta<Tag>::get(id_).ptr);
+    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
+    return static_cast<T*>(w.ptr);
   }
 
   T* operator->() const {
@@ -175,9 +174,15 @@ class ThreadLocalPtr {
     return *get();
   }
 
+  T* release() {
+    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
+
+    return static_cast<T*>(w.release());
+  }
+
   void reset(T* newPtr = nullptr) {
-    threadlocal_detail::ElementWrapper& w =
-      threadlocal_detail::StaticMeta<Tag>::get(id_);
+    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
+
     if (w.ptr != newPtr) {
       w.dispose(TLPDestructionMode::THIS_THREAD);
       w.set(newPtr);
@@ -197,8 +202,7 @@ class ThreadLocalPtr {
    */
   template <class Deleter>
   void reset(T* newPtr, Deleter deleter) {
-    threadlocal_detail::ElementWrapper& w =
-      threadlocal_detail::StaticMeta<Tag>::get(id_);
+    threadlocal_detail::ElementWrapper& w = StaticMeta::get(&id_);
     if (w.ptr != newPtr) {
       w.dispose(TLPDestructionMode::THIS_THREAD);
       w.set(newPtr, deleter);
@@ -212,8 +216,8 @@ class ThreadLocalPtr {
     friend class ThreadLocalPtr<T,Tag>;
 
     threadlocal_detail::StaticMeta<Tag>& meta_;
-    boost::mutex* lock_;
-    int id_;
+    std::mutex* lock_;
+    uint32_t id_;
 
    public:
     class Iterator;
@@ -283,7 +287,7 @@ class ThreadLocalPtr {
     Accessor(const Accessor&) = delete;
     Accessor& operator=(const Accessor&) = delete;
 
-    Accessor(Accessor&& other) FOLLY_NOEXCEPT
+    Accessor(Accessor&& other) noexcept
       : meta_(other.meta_),
         lock_(other.lock_),
         id_(other.id_) {
@@ -291,7 +295,7 @@ class ThreadLocalPtr {
       other.lock_ = nullptr;
     }
 
-    Accessor& operator=(Accessor&& other) FOLLY_NOEXCEPT {
+    Accessor& operator=(Accessor&& other) noexcept {
       // Each Tag has its own unique meta, and accessors with different Tags
       // have different types.  So either *this is empty, or this and other
       // have the same tag.  But if they have the same tag, they have the same
@@ -312,7 +316,7 @@ class ThreadLocalPtr {
     }
 
    private:
-    explicit Accessor(int id)
+    explicit Accessor(uint32_t id)
       : meta_(threadlocal_detail::StaticMeta<Tag>::instance()),
         lock_(&meta_.lock_) {
       lock_->lock();
@@ -331,27 +335,23 @@ class ThreadLocalPtr {
   // accessor allows a client to iterate through all thread local child
   // elements of this ThreadLocal instance.  Holds a global lock for each <Tag>
   Accessor accessAllThreads() const {
-    FOLLY_ASSERT(static_assert(!std::is_same<Tag, void>::value,
-                 "Must use a unique Tag to use the accessAllThreads feature"));
-    return Accessor(id_);
+    static_assert(!std::is_same<Tag, void>::value,
+                  "Must use a unique Tag to use the accessAllThreads feature");
+    return Accessor(id_.getOrAllocate());
   }
 
  private:
   void destroy() {
-    if (id_) {
-      threadlocal_detail::StaticMeta<Tag>::destroy(id_);
-    }
+    StaticMeta::destroy(&id_);
   }
 
   // non-copyable
   ThreadLocalPtr(const ThreadLocalPtr&) = delete;
   ThreadLocalPtr& operator=(const ThreadLocalPtr&) = delete;
 
-  int id_;  // every instantiation has a unique id
+  mutable typename StaticMeta::EntryID id_;
 };
 
-#undef FOLLY_NOEXCEPT
-
 }  // namespace folly
 
 #endif /* FOLLY_THREADLOCAL_H_ */