[libcxx-commits] [libcxx] r369369 - Merging r368867 and r368916:

Hans Wennborg via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 20 04:31:28 PDT 2019


Author: hans
Date: Tue Aug 20 04:31:28 2019
New Revision: 369369

URL: http://llvm.org/viewvc/llvm-project?rev=369369&view=rev
Log:
Merging r368867 and r368916:
------------------------------------------------------------------------
r368867 | marshall | 2019-08-14 18:21:27 +0200 (Wed, 14 Aug 2019) | 1 line

Rework recursive_timed_mutex so that it uses __thread_id instead of using the lower-level __libcpp_thread_id. This is prep for fixing PR42918. Reviewed as https://reviews.llvm.org/D65895
------------------------------------------------------------------------

------------------------------------------------------------------------
r368916 | marshall | 2019-08-14 22:54:56 +0200 (Wed, 14 Aug 2019) | 1 line

Fix thread comparison by making sure we never pass our special 'not a thread' value to the underlying implementation. Fixes PR#42918.
------------------------------------------------------------------------

Modified:
    libcxx/branches/release_90/   (props changed)
    libcxx/branches/release_90/include/__threading_support
    libcxx/branches/release_90/include/mutex
    libcxx/branches/release_90/include/thread
    libcxx/branches/release_90/src/mutex.cpp

Propchange: libcxx/branches/release_90/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Aug 20 04:31:28 2019
@@ -1,2 +1,2 @@
 /libcxx/branches/apple:136569-137939
-/libcxx/trunk:366868
+/libcxx/trunk:366868,368867,368916

Modified: libcxx/branches/release_90/include/__threading_support
URL: http://llvm.org/viewvc/llvm-project/libcxx/branches/release_90/include/__threading_support?rev=369369&r1=369368&r2=369369&view=diff
==============================================================================
--- libcxx/branches/release_90/include/__threading_support (original)
+++ libcxx/branches/release_90/include/__threading_support Tue Aug 20 04:31:28 2019
@@ -12,6 +12,7 @@
 
 #include <__config>
 #include <chrono>
+#include <iosfwd>
 #include <errno.h>
 
 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
@@ -394,6 +395,86 @@ int __libcpp_tls_set(__libcpp_tls_key __
 
 #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
 
+class _LIBCPP_TYPE_VIS thread;
+class _LIBCPP_TYPE_VIS __thread_id;
+
+namespace this_thread
+{
+
+_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
+
+}  // this_thread
+
+template<> struct hash<__thread_id>;
+
+class _LIBCPP_TEMPLATE_VIS __thread_id
+{
+    // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
+    // NULL is the no-thread value on Darwin.  Someone needs to check
+    // on other platforms.  We assume 0 works everywhere for now.
+    __libcpp_thread_id __id_;
+
+public:
+    _LIBCPP_INLINE_VISIBILITY
+    __thread_id() _NOEXCEPT : __id_(0) {}
+
+    friend _LIBCPP_INLINE_VISIBILITY
+        bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
+        { // don't pass id==0 to underlying routines
+        if (__x.__id_ == 0) return __y.__id_ == 0;
+        if (__y.__id_ == 0) return false;
+        return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
+        }
+    friend _LIBCPP_INLINE_VISIBILITY
+        bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
+        {return !(__x == __y);}
+    friend _LIBCPP_INLINE_VISIBILITY
+        bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
+        { // id==0 is always less than any other thread_id
+        if (__x.__id_ == 0) return __y.__id_ != 0;
+        if (__y.__id_ == 0) return false;
+        return  __libcpp_thread_id_less(__x.__id_, __y.__id_);
+        }
+    friend _LIBCPP_INLINE_VISIBILITY
+        bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
+        {return !(__y < __x);}
+    friend _LIBCPP_INLINE_VISIBILITY
+        bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
+        {return   __y < __x ;}
+    friend _LIBCPP_INLINE_VISIBILITY
+        bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
+        {return !(__x < __y);}
+
+    _LIBCPP_INLINE_VISIBILITY
+    void __reset() { __id_ = 0; }
+    
+    template<class _CharT, class _Traits>
+    friend
+    _LIBCPP_INLINE_VISIBILITY
+    basic_ostream<_CharT, _Traits>&
+    operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
+
+private:
+    _LIBCPP_INLINE_VISIBILITY
+    __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
+
+    friend __thread_id this_thread::get_id() _NOEXCEPT;
+    friend class _LIBCPP_TYPE_VIS thread;
+    friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
+};
+
+namespace this_thread
+{
+
+inline _LIBCPP_INLINE_VISIBILITY
+__thread_id
+get_id() _NOEXCEPT
+{
+    return __libcpp_thread_get_current_id();
+}
+
+}  // this_thread
+
 _LIBCPP_END_NAMESPACE_STD
 
 _LIBCPP_POP_MACROS

Modified: libcxx/branches/release_90/include/mutex
URL: http://llvm.org/viewvc/llvm-project/libcxx/branches/release_90/include/mutex?rev=369369&r1=369368&r2=369369&view=diff
==============================================================================
--- libcxx/branches/release_90/include/mutex (original)
+++ libcxx/branches/release_90/include/mutex Tue Aug 20 04:31:28 2019
@@ -280,7 +280,7 @@ class _LIBCPP_TYPE_VIS recursive_timed_m
     mutex              __m_;
     condition_variable __cv_;
     size_t             __count_;
-    __libcpp_thread_id __id_;
+    __thread_id        __id_;
 public:
      recursive_timed_mutex();
      ~recursive_timed_mutex();
@@ -307,9 +307,9 @@ bool
 recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
 {
     using namespace chrono;
-    __libcpp_thread_id __id = __libcpp_thread_get_current_id();
+    __thread_id __id = this_thread::get_id();
     unique_lock<mutex> lk(__m_);
-    if (__libcpp_thread_id_equal(__id, __id_))
+    if (__id == __id_)
     {
         if (__count_ == numeric_limits<size_t>::max())
             return false;

Modified: libcxx/branches/release_90/include/thread
URL: http://llvm.org/viewvc/llvm-project/libcxx/branches/release_90/include/thread?rev=369369&r1=369368&r2=369369&view=diff
==============================================================================
--- libcxx/branches/release_90/include/thread (original)
+++ libcxx/branches/release_90/include/thread Tue Aug 20 04:31:28 2019
@@ -200,64 +200,6 @@ __thread_specific_ptr<_Tp>::set_pointer(
     __libcpp_tls_set(__key_, __p);
 }
 
-class _LIBCPP_TYPE_VIS thread;
-class _LIBCPP_TYPE_VIS __thread_id;
-
-namespace this_thread
-{
-
-_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
-
-}  // this_thread
-
-template<> struct hash<__thread_id>;
-
-class _LIBCPP_TEMPLATE_VIS __thread_id
-{
-    // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
-    // NULL is the no-thread value on Darwin.  Someone needs to check
-    // on other platforms.  We assume 0 works everywhere for now.
-    __libcpp_thread_id __id_;
-
-public:
-    _LIBCPP_INLINE_VISIBILITY
-    __thread_id() _NOEXCEPT : __id_(0) {}
-
-    friend _LIBCPP_INLINE_VISIBILITY
-        bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
-        {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
-    friend _LIBCPP_INLINE_VISIBILITY
-        bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
-        {return !(__x == __y);}
-    friend _LIBCPP_INLINE_VISIBILITY
-        bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
-        {return  __libcpp_thread_id_less(__x.__id_, __y.__id_);}
-    friend _LIBCPP_INLINE_VISIBILITY
-        bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
-        {return !(__y < __x);}
-    friend _LIBCPP_INLINE_VISIBILITY
-        bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
-        {return   __y < __x ;}
-    friend _LIBCPP_INLINE_VISIBILITY
-        bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
-        {return !(__x < __y);}
-
-    template<class _CharT, class _Traits>
-    friend
-    _LIBCPP_INLINE_VISIBILITY
-    basic_ostream<_CharT, _Traits>&
-    operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
-        {return __os << __id.__id_;}
-
-private:
-    _LIBCPP_INLINE_VISIBILITY
-    __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
-
-    friend __thread_id this_thread::get_id() _NOEXCEPT;
-    friend class _LIBCPP_TYPE_VIS thread;
-    friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
-};
-
 template<>
 struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
     : public unary_function<__thread_id, size_t>
@@ -269,17 +211,11 @@ struct _LIBCPP_TEMPLATE_VIS hash<__threa
     }
 };
 
-namespace this_thread
-{
-
-inline _LIBCPP_INLINE_VISIBILITY
-__thread_id
-get_id() _NOEXCEPT
-{
-    return __libcpp_thread_get_current_id();
-}
-
-}  // this_thread
+template<class _CharT, class _Traits>
+_LIBCPP_INLINE_VISIBILITY
+basic_ostream<_CharT, _Traits>&
+operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
+{return __os << __id.__id_;}
 
 class _LIBCPP_TYPE_VIS thread
 {

Modified: libcxx/branches/release_90/src/mutex.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/branches/release_90/src/mutex.cpp?rev=369369&r1=369368&r2=369369&view=diff
==============================================================================
--- libcxx/branches/release_90/src/mutex.cpp (original)
+++ libcxx/branches/release_90/src/mutex.cpp Tue Aug 20 04:31:28 2019
@@ -132,7 +132,7 @@ timed_mutex::unlock() _NOEXCEPT
 
 recursive_timed_mutex::recursive_timed_mutex()
     : __count_(0),
-      __id_(0)
+      __id_{}
 {
 }
 
@@ -144,9 +144,9 @@ recursive_timed_mutex::~recursive_timed_
 void
 recursive_timed_mutex::lock()
 {
-    __libcpp_thread_id id = __libcpp_thread_get_current_id();
+    __thread_id id = this_thread::get_id();
     unique_lock<mutex> lk(__m_);
-    if (__libcpp_thread_id_equal(id, __id_))
+    if (id ==__id_)
     {
         if (__count_ == numeric_limits<size_t>::max())
             __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
@@ -162,9 +162,9 @@ recursive_timed_mutex::lock()
 bool
 recursive_timed_mutex::try_lock() _NOEXCEPT
 {
-    __libcpp_thread_id id = __libcpp_thread_get_current_id();
+    __thread_id id = this_thread::get_id();
     unique_lock<mutex> lk(__m_, try_to_lock);
-    if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_)))
+    if (lk.owns_lock() && (__count_ == 0 || id == __id_))
     {
         if (__count_ == numeric_limits<size_t>::max())
             return false;
@@ -181,7 +181,7 @@ recursive_timed_mutex::unlock() _NOEXCEP
     unique_lock<mutex> lk(__m_);
     if (--__count_ == 0)
     {
-        __id_ = 0;
+        __id_.__reset();
         lk.unlock();
         __cv_.notify_one();
     }




More information about the libcxx-commits mailing list