X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FThreadLocal.h;h=5b518c93bdd27808269ad657b1e66dbfb128ea4b;hb=1509cebb0407aee1ca0a381af6f737d53aa2325b;hp=b4c031e750b3d8d2b0a47e863918ade33b313b4e;hpb=ce4dec4c322aee6b9905be54bb67956d76270b65;p=folly.git diff --git a/folly/ThreadLocal.h b/folly/ThreadLocal.h index b4c031e7..5b518c93 100644 --- a/folly/ThreadLocal.h +++ b/folly/ThreadLocal.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 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. @@ -37,9 +37,9 @@ #ifndef FOLLY_THREADLOCAL_H_ #define FOLLY_THREADLOCAL_H_ -#include "folly/Portability.h" +#include #include -#include "folly/Likely.h" +#include #include @@ -50,7 +50,7 @@ enum class TLPDestructionMode { }; } // namespace -#include "folly/detail/ThreadLocalDetail.h" +#include namespace folly { @@ -124,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 @@ -131,7 +137,7 @@ class ThreadLocalPtr { public: ThreadLocalPtr() : id_(threadlocal_detail::StaticMeta::create()) { } - ThreadLocalPtr(ThreadLocalPtr&& other) : id_(other.id_) { + ThreadLocalPtr(ThreadLocalPtr&& other) noexcept : id_(other.id_) { other.id_ = 0; } @@ -159,6 +165,13 @@ class ThreadLocalPtr { return *get(); } + T* release() { + threadlocal_detail::ElementWrapper& w = + threadlocal_detail::StaticMeta::get(id_); + + return static_cast(w.release()); + } + void reset(T* newPtr = nullptr) { threadlocal_detail::ElementWrapper& w = threadlocal_detail::StaticMeta::get(id_); @@ -196,8 +209,8 @@ class ThreadLocalPtr { friend class ThreadLocalPtr; threadlocal_detail::StaticMeta& meta_; - boost::mutex* lock_; - int id_; + std::mutex* lock_; + uint32_t id_; public: class Iterator; @@ -296,7 +309,7 @@ class ThreadLocalPtr { } private: - explicit Accessor(int id) + explicit Accessor(uint32_t id) : meta_(threadlocal_detail::StaticMeta::instance()), lock_(&meta_.lock_) { lock_->lock(); @@ -331,7 +344,7 @@ class ThreadLocalPtr { ThreadLocalPtr(const ThreadLocalPtr&) = delete; ThreadLocalPtr& operator=(const ThreadLocalPtr&) = delete; - int id_; // every instantiation has a unique id + uint32_t id_; // every instantiation has a unique id }; } // namespace folly