Skip to content

Commit 2b1d425

Browse files
committed
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
llvm-svn: 368867
1 parent 2be5917 commit 2b1d425

File tree

4 files changed

+87
-78
lines changed

4 files changed

+87
-78
lines changed

libcxx/include/__threading_support

Lines changed: 73 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,78 @@ 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+
{return __libcpp_thread_id_equal(__x.__id_, __y.__id_);}
424+
friend _LIBCPP_INLINE_VISIBILITY
425+
bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
426+
{return !(__x == __y);}
427+
friend _LIBCPP_INLINE_VISIBILITY
428+
bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
429+
{return __libcpp_thread_id_less(__x.__id_, __y.__id_);}
430+
friend _LIBCPP_INLINE_VISIBILITY
431+
bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
432+
{return !(__y < __x);}
433+
friend _LIBCPP_INLINE_VISIBILITY
434+
bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
435+
{return __y < __x ;}
436+
friend _LIBCPP_INLINE_VISIBILITY
437+
bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
438+
{return !(__x < __y);}
439+
440+
_LIBCPP_INLINE_VISIBILITY
441+
void reset() { __id_ = 0; }
442+
443+
template<class _CharT, class _Traits>
444+
friend
445+
_LIBCPP_INLINE_VISIBILITY
446+
basic_ostream<_CharT, _Traits>&
447+
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
448+
449+
private:
450+
_LIBCPP_INLINE_VISIBILITY
451+
__thread_id(__libcpp_thread_id __id) : __id_(__id) {}
452+
453+
friend __thread_id this_thread::get_id() _NOEXCEPT;
454+
friend class _LIBCPP_TYPE_VIS thread;
455+
friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
456+
};
457+
458+
namespace this_thread
459+
{
460+
461+
inline _LIBCPP_INLINE_VISIBILITY
462+
__thread_id
463+
get_id() _NOEXCEPT
464+
{
465+
return __libcpp_thread_get_current_id();
466+
}
467+
468+
} // this_thread
469+
397470
_LIBCPP_END_NAMESPACE_STD
398471

399472
_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
@@ -196,64 +196,6 @@ __thread_specific_ptr<_Tp>::set_pointer(pointer __p)
196196
__libcpp_tls_set(__key_, __p);
197197
}
198198

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

268-
namespace this_thread
269-
{
270-
271-
inline _LIBCPP_INLINE_VISIBILITY
272-
__thread_id
273-
get_id() _NOEXCEPT
274-
{
275-
return __libcpp_thread_get_current_id();
276-
}
277-
278-
} // this_thread
210+
template<class _CharT, class _Traits>
211+
_LIBCPP_INLINE_VISIBILITY
212+
basic_ostream<_CharT, _Traits>&
213+
operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id)
214+
{return __os << __id.__id_;}
279215

280216
class _LIBCPP_TYPE_VIS thread
281217
{

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)