/*
- * Copyright 2014 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.
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();
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_;
};
/*
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) noexcept : 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;
}
}
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 {
}
T* release() {
- threadlocal_detail::ElementWrapper& w =
- threadlocal_detail::StaticMeta<Tag>::get(id_);
+ 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);
*/
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);
Accessor accessAllThreads() const {
static_assert(!std::is_same<Tag, void>::value,
"Must use a unique Tag to use the accessAllThreads feature");
- return Accessor(id_);
+ 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;
- uint32_t id_; // every instantiation has a unique id
+ mutable typename StaticMeta::EntryID id_;
};
} // namespace folly