Skip to content

Commit 67a4a12

Browse files
committed
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. ------------------------------------------------------------------------ llvm-svn: 369369
1 parent d9e9479 commit 67a4a12

File tree

4 files changed

+95
-78
lines changed

4 files changed

+95
-78
lines changed

libcxx/include/__threading_support

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <__config>
1414
#include <chrono>
15+
#include <iosfwd>
1516
#include <errno.h>
1617

1718
#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
@@ -394,6 +395,86 @@ int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
394395

395396
#endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
396397

398+
class _LIBCPP_TYPE_VIS thread;
399+
class _LIBCPP_TYPE_VIS __thread_id;
400+
401+
namespace this_thread
402+
{
403+
404+
_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
405+
406+
} // this_thread
407+
408+
template<> struct hash<__thread_id>;
409+
410+
class _LIBCPP_TEMPLATE_VIS __thread_id
411+
{
412+
// FIXME: pthread_t is a pointer on Darwin but a long on Linux.
413+
// NULL is the no-thread value on Darwin. Someone needs to check
414+
// on other platforms. We assume 0 works everywhere for now.
415+
__libcpp_thread_id __id_;
416+
417+
public:
418+
_LIBCPP_INLINE_VISIBILITY
419+
__thread_id() _NOEXCEPT : __id_(0) {}
420+
421+
friend _LIBCPP_INLINE_VISIBILITY
422+
bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
423+
{ // don't pass id==0 to underlying routines
424+
if (__x.__id_ == 0) return __y.__id_ == 0;
425+
if (__y.__id_ == 0) return false;
426+
return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
427+
}
428+
friend _LIBCPP_INLINE_VISIBILITY
429+
bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
430+
{return !(__x == __y);}
431+
friend _LIBCPP_INLINE_VISIBILITY
432+
bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
433+
{ // id==0 is always less than any other thread_id
434+
if (__x.__id_ == 0) return __y.__id_ != 0;
435+
if (__y.__id_ == 0) return false;
436+
return __libcpp_thread_id_less(__x.__id_, __y.__id_);
437+
}
438+
friend _LIBCPP_INLINE_VISIBILITY
439+
bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
440+
{return !(__y < __x);}
441+
friend _LIBCPP_INLINE_VISIBILITY
442+
bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
443+
{return __y < __x ;}
444+
friend _LIBCPP_INLINE_VISIBILITY
445+
bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
446+
{return !(__x < __y);}
447+
448+
_LIBCPP_INLINE_VISIBILITY
449+
void __reset() { __id_ = 0; }
450+
451+
template<class _CharT, class _Traits>
452+
friend
453+
_LIBCPP_INLINE_VISIBILITY
454+
basic_ostream<_CharT, _Traits>&
455+
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
456+
457+
private:
458+
_LIBCPP_INLINE_VISIBILITY
459+
__thread_id(__libcpp_thread_id __id) : __id_(__id) {}
460+
461+
friend __thread_id this_thread::get_id() _NOEXCEPT;
462+
friend class _LIBCPP_TYPE_VIS thread;
463+
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
464+
};
465+
466+
namespace this_thread
467+
{
468+
469+
inline _LIBCPP_INLINE_VISIBILITY
470+
__thread_id
471+
get_id() _NOEXCEPT
472+
{
473+
return __libcpp_thread_get_current_id();
474+
}
475+
476+
} // this_thread
477+
397478
_LIBCPP_END_NAMESPACE_STD
398479

399480
_LIBCPP_POP_MACROS

libcxx/include/mutex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ class _LIBCPP_TYPE_VIS recursive_timed_mutex
280280
mutex __m_;
281281
condition_variable __cv_;
282282
size_t __count_;
283-
__libcpp_thread_id __id_;
283+
__thread_id __id_;
284284
public:
285285
recursive_timed_mutex();
286286
~recursive_timed_mutex();
@@ -307,9 +307,9 @@ bool
307307
recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
308308
{
309309
using namespace chrono;
310-
__libcpp_thread_id __id = __libcpp_thread_get_current_id();
310+
__thread_id __id = this_thread::get_id();
311311
unique_lock<mutex> lk(__m_);
312-
if (__libcpp_thread_id_equal(__id, __id_))
312+
if (__id == __id_)
313313
{
314314
if (__count_ == numeric_limits<size_t>::max())
315315
return false;

libcxx/include/thread

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -200,64 +200,6 @@ __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
200200
__libcpp_tls_set(__key_, __p);
201201
}
202202

203-
class _LIBCPP_TYPE_VIS thread;
204-
class _LIBCPP_TYPE_VIS __thread_id;
205-
206-
namespace this_thread
207-
{
208-
209-
_LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
210-
211-
} // this_thread
212-
213-
template<> struct hash<__thread_id>;
214-
215-
class _LIBCPP_TEMPLATE_VIS __thread_id
216-
{
217-
// FIXME: pthread_t is a pointer on Darwin but a long on Linux.
218-
// NULL is the no-thread value on Darwin. Someone needs to check
219-
// on other platforms. We assume 0 works everywhere for now.
220-
__libcpp_thread_id __id_;
221-
222-
public:
223-
_LIBCPP_INLINE_VISIBILITY
224-
__thread_id() _NOEXCEPT : __id_(0) {}
225-
226-
friend _LIBCPP_INLINE_VISIBILITY
227-
bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
228-
{return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
229-
friend _LIBCPP_INLINE_VISIBILITY
230-
bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
231-
{return !(__x == __y);}
232-
friend _LIBCPP_INLINE_VISIBILITY
233-
bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
234-
{return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
235-
friend _LIBCPP_INLINE_VISIBILITY
236-
bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
237-
{return !(__y < __x);}
238-
friend _LIBCPP_INLINE_VISIBILITY
239-
bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
240-
{return __y < __x ;}
241-
friend _LIBCPP_INLINE_VISIBILITY
242-
bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
243-
{return !(__x < __y);}
244-
245-
template<class _CharT, class _Traits>
246-
friend
247-
_LIBCPP_INLINE_VISIBILITY
248-
basic_ostream<_CharT, _Traits>&
249-
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
250-
{return __os << __id.__id_;}
251-
252-
private:
253-
_LIBCPP_INLINE_VISIBILITY
254-
__thread_id(__libcpp_thread_id __id) : __id_(__id) {}
255-
256-
friend __thread_id this_thread::get_id() _NOEXCEPT;
257-
friend class _LIBCPP_TYPE_VIS thread;
258-
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
259-
};
260-
261203
template<>
262204
struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
263205
: public unary_function<__thread_id, size_t>
@@ -269,17 +211,11 @@ struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>
269211
}
270212
};
271213

272-
namespace this_thread
273-
{
274-
275-
inline _LIBCPP_INLINE_VISIBILITY
276-
__thread_id
277-
get_id() _NOEXCEPT
278-
{
279-
return __libcpp_thread_get_current_id();
280-
}
281-
282-
} // this_thread
214+
template<class _CharT, class _Traits>
215+
_LIBCPP_INLINE_VISIBILITY
216+
basic_ostream<_CharT, _Traits>&
217+
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
218+
{return __os << __id.__id_;}
283219

284220
class _LIBCPP_TYPE_VIS thread
285221
{

libcxx/src/mutex.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ timed_mutex::unlock() _NOEXCEPT
132132

133133
recursive_timed_mutex::recursive_timed_mutex()
134134
: __count_(0),
135-
__id_(0)
135+
__id_{}
136136
{
137137
}
138138

@@ -144,9 +144,9 @@ recursive_timed_mutex::~recursive_timed_mutex()
144144
void
145145
recursive_timed_mutex::lock()
146146
{
147-
__libcpp_thread_id id = __libcpp_thread_get_current_id();
147+
__thread_id id = this_thread::get_id();
148148
unique_lock<mutex> lk(__m_);
149-
if (__libcpp_thread_id_equal(id, __id_))
149+
if (id ==__id_)
150150
{
151151
if (__count_ == numeric_limits<size_t>::max())
152152
__throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
@@ -162,9 +162,9 @@ recursive_timed_mutex::lock()
162162
bool
163163
recursive_timed_mutex::try_lock() _NOEXCEPT
164164
{
165-
__libcpp_thread_id id = __libcpp_thread_get_current_id();
165+
__thread_id id = this_thread::get_id();
166166
unique_lock<mutex> lk(__m_, try_to_lock);
167-
if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_)))
167+
if (lk.owns_lock() && (__count_ == 0 || id == __id_))
168168
{
169169
if (__count_ == numeric_limits<size_t>::max())
170170
return false;
@@ -181,7 +181,7 @@ recursive_timed_mutex::unlock() _NOEXCEPT
181181
unique_lock<mutex> lk(__m_);
182182
if (--__count_ == 0)
183183
{
184-
__id_ = 0;
184+
__id_.__reset();
185185
lk.unlock();
186186
__cv_.notify_one();
187187
}

0 commit comments

Comments
 (0)