[libcxx-commits] [libcxx] r364012 - [libc++] Avoid using timespec when it might not be available

Mikhail Maltsev via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 21 01:33:47 PDT 2019


Author: miyuki
Date: Fri Jun 21 01:33:47 2019
New Revision: 364012

URL: http://llvm.org/viewvc/llvm-project?rev=364012&view=rev
Log:
[libc++] Avoid using timespec when it might not be available

Summary:
The type timespec is unconditionally used in __threading_support.
Since the C library is only required to provide it in C11, this might
cause problems for platforms with external thread porting layer (i.e.
when _LIBCPP_HAS_THREAD_API_EXTERNAL is defined) with pre-C11
C libraries.

In our downstream port of libc++ we used to provide a definition of
timespec in __external_threading, but this solution is not ideal
because timespec is not a reserved name.

This patch renames timespec into __libcpp_timespec_t in the
thread-related parts of libc++. For all cases except external
threading this type is an alias for ::timespec (and no functional
changes are intended).

In case of external threading it is expected that the
__external_threading header will either provide a similar typedef (if
timespec is available in the vendor's C library) or provide a
definition of __libcpp_timespec_t compatible with POSIX timespec.

Reviewers: ldionne, mclow.lists, EricWF

Reviewed By: ldionne

Subscribers: dexonsmith, libcxx-commits, christof, carwil

Tags: #libc

Differential Revision: https://reviews.llvm.org/D63328

Modified:
    libcxx/trunk/include/__threading_support
    libcxx/trunk/src/condition_variable.cpp
    libcxx/trunk/src/support/win32/thread_win32.cpp

Modified: libcxx/trunk/include/__threading_support
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__threading_support?rev=364012&r1=364011&r2=364012&view=diff
==============================================================================
--- libcxx/trunk/include/__threading_support (original)
+++ libcxx/trunk/include/__threading_support Fri Jun 21 01:33:47 2019
@@ -22,6 +22,8 @@
 # include <__external_threading>
 #elif !defined(_LIBCPP_HAS_NO_THREADS)
 
+typedef ::timespec __libcpp_timespec_t;
+
 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
 # include <pthread.h>
 # include <sched.h>
@@ -148,7 +150,7 @@ int __libcpp_condvar_wait(__libcpp_condv
 
 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
-                               timespec *__ts);
+                               __libcpp_timespec_t *__ts);
 
 _LIBCPP_THREAD_ABI_VISIBILITY
 int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
@@ -287,7 +289,7 @@ int __libcpp_condvar_wait(__libcpp_condv
 }
 
 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
-                               timespec *__ts)
+                               __libcpp_timespec_t *__ts)
 {
   return pthread_cond_timedwait(__cv, __m, __ts);
 }
@@ -356,7 +358,7 @@ void __libcpp_thread_sleep_for(const chr
 {
    using namespace chrono;
    seconds __s = duration_cast<seconds>(__ns);
-   timespec __ts;
+   __libcpp_timespec_t __ts;
    typedef decltype(__ts.tv_sec) ts_sec;
    _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max();
 

Modified: libcxx/trunk/src/condition_variable.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/condition_variable.cpp?rev=364012&r1=364011&r2=364012&view=diff
==============================================================================
--- libcxx/trunk/src/condition_variable.cpp (original)
+++ libcxx/trunk/src/condition_variable.cpp Fri Jun 21 01:33:47 2019
@@ -60,7 +60,7 @@ condition_variable::__do_timed_wait(uniq
     nanoseconds d = tp.time_since_epoch();
     if (d > nanoseconds(0x59682F000000E941))
         d = nanoseconds(0x59682F000000E941);
-    timespec ts;
+    __libcpp_timespec_t ts;
     seconds s = duration_cast<seconds>(d);
     typedef decltype(ts.tv_sec) ts_sec;
     _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();

Modified: libcxx/trunk/src/support/win32/thread_win32.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/support/win32/thread_win32.cpp?rev=364012&r1=364011&r2=364012&view=diff
==============================================================================
--- libcxx/trunk/src/support/win32/thread_win32.cpp (original)
+++ libcxx/trunk/src/support/win32/thread_win32.cpp Fri Jun 21 01:33:47 2019
@@ -110,7 +110,7 @@ int __libcpp_condvar_wait(__libcpp_condv
 }
 
 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
-                               timespec *__ts)
+                               __libcpp_timespec_t *__ts)
 {
   using namespace _VSTD::chrono;
 




More information about the libcxx-commits mailing list