Move static global variable randomDevice inside the scope of the function
[folly.git] / folly / ThreadLocal.h
index a449ce67e62295aa6d95f386ee04be6ebe7c4232..ad111168ee55df75ff8e19e30fafe7fd267467fd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 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 {
 
@@ -79,11 +63,12 @@ class ThreadLocal {
 
   T* get() const {
     T* ptr = tlp_.get();
-    if (UNLIKELY(ptr == NULL)) {
-      ptr = new T();
-      tlp_.reset(ptr);
+    if (LIKELY(ptr != nullptr)) {
+      return ptr;
     }
-    return ptr;
+
+    // separated new item creation out to speed up the fast path.
+    return makeTlp();
   }
 
   T* operator->() const {
@@ -94,7 +79,7 @@ class ThreadLocal {
     return *get();
   }
 
-  void reset(T* newPtr = NULL) {
+  void reset(T* newPtr = nullptr) {
     tlp_.reset(newPtr);
   }
 
@@ -112,6 +97,12 @@ class ThreadLocal {
   ThreadLocal(const ThreadLocal&) = delete;
   ThreadLocal& operator=(const ThreadLocal&) = delete;
 
+  T* makeTlp() const {
+    T* ptr = new T();
+    tlp_.reset(ptr);
+    return ptr;
+  }
+
   mutable ThreadLocalPtr<T,Tag> tlp_;
 };
 
@@ -133,6 +124,12 @@ 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>
@@ -168,7 +165,14 @@ class ThreadLocalPtr {
     return *get();
   }
 
-  void reset(T* newPtr) {
+  T* release() {
+    threadlocal_detail::ElementWrapper& w =
+      threadlocal_detail::StaticMeta<Tag>::get(id_);
+
+    return static_cast<T*>(w.release());
+  }
+
+  void reset(T* newPtr = nullptr) {
     threadlocal_detail::ElementWrapper& w =
       threadlocal_detail::StaticMeta<Tag>::get(id_);
     if (w.ptr != newPtr) {
@@ -177,6 +181,10 @@ class ThreadLocalPtr {
     }
   }
 
+  explicit operator bool() const {
+    return get() != nullptr;
+  }
+
   /**
    * reset() with a custom deleter:
    * deleter(T* ptr, TLPDestructionMode mode)
@@ -201,7 +209,7 @@ class ThreadLocalPtr {
     friend class ThreadLocalPtr<T,Tag>;
 
     threadlocal_detail::StaticMeta<Tag>& meta_;
-    boost::mutex* lock_;
+    std::mutex* lock_;
     int id_;
 
    public:
@@ -272,15 +280,15 @@ 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_) {
       other.id_ = 0;
-      other.lock_ = NULL;
+      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
@@ -288,7 +296,7 @@ class ThreadLocalPtr {
       // which is impossible, which leaves only one possible scenario --
       // *this is empty.  Assert it.
       assert(&meta_ == &other.meta_);
-      assert(lock_ == NULL);
+      assert(lock_ == nullptr);
       using std::swap;
       swap(lock_, other.lock_);
       swap(id_, other.id_);
@@ -296,7 +304,7 @@ class ThreadLocalPtr {
 
     Accessor()
       : meta_(threadlocal_detail::StaticMeta<Tag>::instance()),
-        lock_(NULL),
+        lock_(nullptr),
         id_(0) {
     }
 
@@ -312,7 +320,7 @@ class ThreadLocalPtr {
       if (lock_) {
         lock_->unlock();
         id_ = 0;
-        lock_ = NULL;
+        lock_ = nullptr;
       }
     }
   };
@@ -320,8 +328,8 @@ 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"));
+    static_assert(!std::is_same<Tag, void>::value,
+                  "Must use a unique Tag to use the accessAllThreads feature");
     return Accessor(id_);
   }
 
@@ -339,8 +347,6 @@ class ThreadLocalPtr {
   int id_;  // every instantiation has a unique id
 };
 
-#undef FOLLY_NOEXCEPT
-
 }  // namespace folly
 
 #endif /* FOLLY_THREADLOCAL_H_ */