From 2e80d01fa7dc36c5f2533a363c6c0680f7896dc5 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Wed, 14 Aug 2019 20:54:56 +0000 Subject: [PATCH] Fix thread comparison by making sure we never pass our special 'not a thread' value to the underlying implementation. Fixes PR#42918. llvm-svn: 368916 --- libcxx/include/__threading_support | 14 +++++++++++--- libcxx/src/mutex.cpp | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libcxx/include/__threading_support b/libcxx/include/__threading_support index 29a2907..0331b7c 100644 --- a/libcxx/include/__threading_support +++ b/libcxx/include/__threading_support @@ -420,13 +420,21 @@ public: friend _LIBCPP_INLINE_VISIBILITY bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT - {return __libcpp_thread_id_equal(__x.__id_, __y.__id_);} + { // 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 - {return __libcpp_thread_id_less(__x.__id_, __y.__id_);} + { // 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);} @@ -438,7 +446,7 @@ public: {return !(__x < __y);} _LIBCPP_INLINE_VISIBILITY - void reset() { __id_ = 0; } + void __reset() { __id_ = 0; } template friend diff --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp index ddd36b4..1827857 100644 --- a/libcxx/src/mutex.cpp +++ b/libcxx/src/mutex.cpp @@ -181,7 +181,7 @@ recursive_timed_mutex::unlock() _NOEXCEPT unique_lock lk(__m_); if (--__count_ == 0) { - __id_.reset(); + __id_.__reset(); lk.unlock(); __cv_.notify_one(); } -- 2.7.4