瀏覽代碼

Convert __thread_local_data to the singleton pattern

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@116500 91177308-0d34-0410-b5e6-96231b3b80d8
Howard Hinnant 15 年之前
父節點
當前提交
5306d68019
共有 5 個文件被更改,包括 13 次插入8 次删除
  1. 2 2
      include/future
  2. 2 2
      include/thread
  3. 1 1
      src/condition_variable.cpp
  4. 2 2
      src/future.cpp
  5. 6 1
      src/thread.cpp

+ 2 - 2
include/future

@@ -677,7 +677,7 @@ __assoc_state<_R>::set_value_at_thread_exit(_Arg& __arg)
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
     ::new(&__value_) _R(_STD::forward<_Arg>(__arg));
     ::new(&__value_) _R(_STD::forward<_Arg>(__arg));
     this->__state_ |= base::__constructed;
     this->__state_ |= base::__constructed;
-    __thread_local_data->__make_ready_at_thread_exit(this);
+    __thread_local_data()->__make_ready_at_thread_exit(this);
     __lk.unlock();
     __lk.unlock();
 }
 }
 
 
@@ -750,7 +750,7 @@ __assoc_state<_R&>::set_value_at_thread_exit(_R& __arg)
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
     __value_ = &__arg;
     __value_ = &__arg;
     this->__state_ |= base::__constructed;
     this->__state_ |= base::__constructed;
-    __thread_local_data->__make_ready_at_thread_exit(this);
+    __thread_local_data()->__make_ready_at_thread_exit(this);
     __lk.unlock();
     __lk.unlock();
 }
 }
 
 

+ 2 - 2
include/thread

@@ -318,13 +318,13 @@ public:
     void __make_ready_at_thread_exit(__assoc_sub_state*);
     void __make_ready_at_thread_exit(__assoc_sub_state*);
 };
 };
 
 
-extern __thread_specific_ptr<__thread_struct> __thread_local_data;
+__thread_specific_ptr<__thread_struct>& __thread_local_data();
 
 
 template <class _F>
 template <class _F>
 void*
 void*
 __thread_proxy(void* __vp)
 __thread_proxy(void* __vp)
 {
 {
-    __thread_local_data.reset(new __thread_struct);
+    __thread_local_data().reset(new __thread_struct);
     std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
     std::unique_ptr<_F> __p(static_cast<_F*>(__vp));
     (*__p)();
     (*__p)();
     return nullptr;
     return nullptr;

+ 1 - 1
src/condition_variable.cpp

@@ -64,7 +64,7 @@ condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
 void
 void
 notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
 notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
 {
 {
-    __thread_local_data->notify_all_at_thread_exit(&cond, lk.release());
+    __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
 }
 }
 
 
 _LIBCPP_END_NAMESPACE_STD
 _LIBCPP_END_NAMESPACE_STD

+ 2 - 2
src/future.cpp

@@ -83,7 +83,7 @@ __assoc_sub_state::set_value_at_thread_exit()
     if (__has_value())
     if (__has_value())
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
     __state_ |= __constructed;
     __state_ |= __constructed;
-    __thread_local_data->__make_ready_at_thread_exit(this);
+    __thread_local_data()->__make_ready_at_thread_exit(this);
     __lk.unlock();
     __lk.unlock();
 }
 }
 
 
@@ -106,7 +106,7 @@ __assoc_sub_state::set_exception_at_thread_exit(exception_ptr __p)
     if (__has_value())
     if (__has_value())
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
         throw future_error(make_error_code(future_errc::promise_already_satisfied));
     __exception_ = __p;
     __exception_ = __p;
-    __thread_local_data->__make_ready_at_thread_exit(this);
+    __thread_local_data()->__make_ready_at_thread_exit(this);
     __lk.unlock();
     __lk.unlock();
 }
 }
 
 

+ 6 - 1
src/thread.cpp

@@ -83,7 +83,12 @@ sleep_for(const chrono::nanoseconds& ns)
 
 
 }  // this_thread
 }  // this_thread
 
 
-__thread_specific_ptr<__thread_struct> __thread_local_data;
+__thread_specific_ptr<__thread_struct>&
+__thread_local_data()
+{
+    static __thread_specific_ptr<__thread_struct> __p;
+    return __p;
+}
 
 
 // __thread_struct_imp
 // __thread_struct_imp