Skip to content

Commit 47b9276

Browse files
cfriedt-friedtcostephanosio
authored andcommitted
libstdc++-v3: add support for --enable-libstdcxx-time=c11
Add support for --enable-libstdcxx-time=c11 via the ISO C11 threads.h API function thrd_sleep(). Signed-off-by: Christopher Friedt <chris@friedt.co>
1 parent 52dc7f4 commit 47b9276

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

libstdc++-v3/acinclude.m4

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,8 @@ dnl not always desirable because, in glibc 2.16 and earlier, for
13201320
dnl example, in turn it triggers the linking of libpthread too,
13211321
dnl which activates locking,
13221322
dnl a large overhead for single-thread programs.
1323+
dnl --enable-libstdcxx-time=c11
1324+
dnl checks for the availability of <threads.h> and thrd_sleep in libc.
13231325
dnl --enable-libstdcxx-time=no
13241326
dnl --disable-libstdcxx-time
13251327
dnl disables the checks completely
@@ -1333,7 +1335,7 @@ AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME], [
13331335
13341336
GLIBCXX_ENABLE(libstdcxx-time,auto,[[[=KIND]]],
13351337
[use KIND for check type],
1336-
[permit yes|no|rt])
1338+
[permit yes|no|rt|c11])
13371339
13381340
AC_LANG_SAVE
13391341
AC_LANG_CPLUSPLUS
@@ -1345,6 +1347,9 @@ AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME], [
13451347
ac_has_clock_realtime=no
13461348
ac_has_nanosleep=no
13471349
ac_has_sched_yield=no
1350+
ac_has_thrd_sleep=no
1351+
1352+
AC_CHECK_HEADERS(threads.h, ac_has_threads_h=yes, ac_has_threads_h=no)
13481353
13491354
if test x"$enable_libstdcxx_time" = x"auto"; then
13501355
@@ -1396,11 +1401,20 @@ AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME], [
13961401
ac_has_sched_yield=yes
13971402
esac
13981403
1404+
elif test x"$enable_libstdcxx_time" = x"c11"; then
1405+
1406+
# workaround for the error below until ac_cv_func_thrd_sleep is available when cross-compiling
1407+
# checking for library containing thrd_sleep...
1408+
# configure: error: Link tests are not allowed after GCC_NO_EXECUTABLES.
1409+
ac_has_thrd_sleep=yes
1410+
13991411
elif test x"$enable_libstdcxx_time" != x"no"; then
14001412
14011413
if test x"$enable_libstdcxx_time" = x"rt"; then
14021414
AC_SEARCH_LIBS(clock_gettime, [rt])
14031415
AC_SEARCH_LIBS(nanosleep, [rt])
1416+
elif test x"$enable_libstdcxx_time" = x"c11"; then
1417+
AC_CHECK_FUNC(thrd_sleep)
14041418
else
14051419
AC_CHECK_FUNC(clock_gettime)
14061420
AC_CHECK_FUNC(nanosleep)
@@ -1537,6 +1551,9 @@ AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME], [
15371551
if test x"$ac_has_nanosleep" = x"yes"; then
15381552
AC_DEFINE(_GLIBCXX_USE_NANOSLEEP, 1,
15391553
[ Defined if nanosleep is available. ])
1554+
elif test x"$ac_has_thrd_sleep" = x"yes"; then
1555+
AC_DEFINE(_GLIBCXX_USE_THRD_SLEEP, 1,
1556+
[ Defined if thrd_sleep is available. ])
15401557
else
15411558
AC_MSG_CHECKING([for sleep])
15421559
AC_TRY_COMPILE([#include <unistd.h>],
@@ -1557,7 +1574,7 @@ AC_DEFUN([GLIBCXX_ENABLE_LIBSTDCXX_TIME], [
15571574
AC_MSG_RESULT($ac_has_usleep)
15581575
fi
15591576
1560-
if test x"$ac_has_nanosleep$ac_has_sleep" = x"nono"; then
1577+
if test x"$ac_has_nanosleep$ac_has_sleep$ac_has_thrd_sleep" = x"nonono"; then
15611578
ac_no_sleep=yes
15621579
AC_MSG_CHECKING([for Sleep])
15631580
AC_TRY_COMPILE([#include <windows.h>],

libstdc++-v3/configure

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20493,7 +20493,7 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
2049320493
if test "${enable_libstdcxx_time+set}" = set; then :
2049420494
enableval=$enable_libstdcxx_time;
2049520495
case "$enableval" in
20496-
yes|no|rt) ;;
20496+
yes|no|rt|c11) ;;
2049720497
*) as_fn_error $? "Unknown argument to enable/disable libstdcxx-time" "$LINENO" 5 ;;
2049820498
esac
2049920499

@@ -20518,6 +20518,21 @@ ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
2051820518
ac_has_clock_realtime=no
2051920519
ac_has_nanosleep=no
2052020520
ac_has_sched_yield=no
20521+
ac_has_thrd_sleep=no
20522+
20523+
for ac_header in threads.h
20524+
do :
20525+
ac_fn_cxx_check_header_mongrel "$LINENO" "threads.h" "ac_cv_header_threads_h" "$ac_includes_default"
20526+
if test "x$ac_cv_header_threads_h" = xyes; then :
20527+
cat >>confdefs.h <<_ACEOF
20528+
#define HAVE_THREADS_H 1
20529+
_ACEOF
20530+
ac_has_threads_h=yes
20531+
else
20532+
ac_has_threads_h=no
20533+
fi
20534+
20535+
done
2052120536

2052220537
if test x"$enable_libstdcxx_time" = x"auto"; then
2052320538

@@ -20627,6 +20642,13 @@ fi
2062720642
ac_has_sched_yield=yes
2062820643
esac
2062920644

20645+
elif test x"$enable_libstdcxx_time" = x"c11"; then
20646+
20647+
# workaround for the error below until ac_cv_func_thrd_sleep is available when cross-compiling
20648+
# checking for library containing thrd_sleep...
20649+
# configure: error: Link tests are not allowed after GCC_NO_EXECUTABLES.
20650+
ac_has_thrd_sleep=yes
20651+
2063020652
elif test x"$enable_libstdcxx_time" != x"no"; then
2063120653

2063220654
if test x"$enable_libstdcxx_time" = x"rt"; then
@@ -20748,6 +20770,11 @@ if test "$ac_res" != no; then :
2074820770

2074920771
fi
2075020772

20773+
elif test x"$enable_libstdcxx_time" = x"c11"; then
20774+
ac_fn_cxx_check_func "$LINENO" "thrd_sleep" "ac_cv_func_thrd_sleep"
20775+
if test "x$ac_cv_func_thrd_sleep" = xyes; then :
20776+
20777+
fi
2075120778
else
2075220779
ac_fn_cxx_check_func "$LINENO" "clock_gettime" "ac_cv_func_clock_gettime"
2075320780
if test "x$ac_cv_func_clock_gettime" = xyes; then :
@@ -21058,6 +21085,10 @@ $as_echo "#define _GLIBCXX_USE_SCHED_YIELD 1" >>confdefs.h
2105821085

2105921086
$as_echo "#define _GLIBCXX_USE_NANOSLEEP 1" >>confdefs.h
2106021087

21088+
elif test x"$ac_has_thrd_sleep" = x"yes"; then
21089+
21090+
$as_echo "#define _GLIBCXX_USE_THRD_SLEEP 1" >>confdefs.h
21091+
2106121092
else
2106221093
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sleep" >&5
2106321094
$as_echo_n "checking for sleep... " >&6; }
@@ -21114,7 +21145,7 @@ $as_echo "#define HAVE_USLEEP 1" >>confdefs.h
2111421145
$as_echo "$ac_has_usleep" >&6; }
2111521146
fi
2111621147

21117-
if test x"$ac_has_nanosleep$ac_has_sleep" = x"nono"; then
21148+
if test x"$ac_has_nanosleep$ac_has_sleep$ac_has_thrd_sleep" = x"nonono"; then
2111821149
ac_no_sleep=yes
2111921150
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for Sleep" >&5
2112021151
$as_echo_n "checking for Sleep... " >&6; }

libstdc++-v3/include/bits/this_thread_sleep.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
# include <time.h> // nanosleep
4141
#endif
4242

43+
#ifdef _GLIBCXX_USE_THRD_SLEEP
44+
#include <threads.h>
45+
#endif
46+
4347
namespace std _GLIBCXX_VISIBILITY(default)
4448
{
4549
_GLIBCXX_BEGIN_NAMESPACE_VERSION
@@ -57,7 +61,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
5761
{
5862
#ifndef _GLIBCXX_NO_SLEEP
5963

60-
#ifndef _GLIBCXX_USE_NANOSLEEP
64+
#if !(defined(_GLIBCXX_USE_NANOSLEEP) || defined(_GLIBCXX_USE_THRD_SLEEP))
6165
void
6266
__sleep_for(chrono::seconds, chrono::nanoseconds);
6367
#endif
@@ -79,6 +83,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
7983
};
8084
while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
8185
{ }
86+
#elif defined(_GLIBCXX_USE_THRD_SLEEP)
87+
struct ::timespec __ts =
88+
{
89+
static_cast<std::time_t>(__s.count()),
90+
static_cast<long>(__ns.count())
91+
};
92+
::thrd_sleep(&__ts, &__ts);
8293
#else
8394
__sleep_for(__s, __ns);
8495
#endif

libstdc++-v3/src/c++11/thread.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ namespace this_thread
214214
};
215215
while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
216216
{ }
217+
#elif defined(_GLIBCXX_HAVE_THRD_SLEEP)
218+
struct ::timespec __ts =
219+
{
220+
static_cast<std::time_t>(__s.count()),
221+
static_cast<long>(__ns.count())
222+
};
223+
::thrd_sleep(&__ts, &__ts);
217224
#elif defined(_GLIBCXX_HAVE_SLEEP)
218225
const auto target = chrono::steady_clock::now() + __s + __ns;
219226
while (true)

0 commit comments

Comments
 (0)