diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2020-08-11 16:16:21 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2020-08-11 16:16:21 +0100 |
commit | 5bbb1f3000c57fd4d95969b30fa0e35be6d54ffb (patch) | |
tree | 3a4d5f7d465de0a98c1f1f3938e9746c772a03a8 /libstdc++-v3/src | |
parent | 7840b4dc05539cf5575b3e9ff57ff5f6c3da2cae (diff) | |
download | gcc-5bbb1f3000c57fd4d95969b30fa0e35be6d54ffb.zip gcc-5bbb1f3000c57fd4d95969b30fa0e35be6d54ffb.tar.gz gcc-5bbb1f3000c57fd4d95969b30fa0e35be6d54ffb.tar.bz2 |
libstdc++: Make std::this_thread functions work without gthreads
The only function in namespace std::this_thread that actually depends on
thread support being present is this_thread::get_id(). The other
functions (yield, sleep_for and sleep_until) can be defined for targets
without gthreads.
A small change is needed in std::this_thread::sleep_for which currently
uses the __gthread_time_t typedef. Since it just calls nanosleep
directly, it should use timespec directly instead of the typedef.
Even std::this_thread::get_id() could be made to work, the only
difficulty is that it returns a value of type std::thread::id and
std::thread is only defined when gthreads support exists.
libstdc++-v3/ChangeLog:
* include/std/thread [!_GLIBCXX_HAS_GTHREADS] (this_thread::yield)
(this_thread::sleep_until): Define.
[!_GLIBCXX_HAS_GTHREADS] (this_thread::sleep_for): Define. Replace
use of __gthread_time_t typedef with timespec.
* src/c++11/thread.cc [!_GLIBCXX_HAS_GTHREADS] (__sleep_for):
Likewise.
* testsuite/30_threads/this_thread/2.cc: Moved to...
* testsuite/30_threads/this_thread/yield.cc: ...here.
* testsuite/30_threads/this_thread/3.cc: Moved to...
* testsuite/30_threads/this_thread/sleep_for-mt.cc: ...here.
* testsuite/30_threads/this_thread/4.cc: Moved to...
* testsuite/30_threads/this_thread/sleep_until-mt.cc: ...here.
* testsuite/30_threads/this_thread/58038.cc: Add
dg-require-sleep.
* testsuite/30_threads/this_thread/60421.cc: Likewise.
* testsuite/30_threads/this_thread/sleep_for.cc: New test.
* testsuite/30_threads/this_thread/sleep_until.cc: New test.
Diffstat (limited to 'libstdc++-v3/src')
-rw-r--r-- | libstdc++-v3/src/c++11/thread.cc | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/libstdc++-v3/src/c++11/thread.cc b/libstdc++-v3/src/c++11/thread.cc index 2d87817..a4c87d8 100644 --- a/libstdc++-v3/src/c++11/thread.cc +++ b/libstdc++-v3/src/c++11/thread.cc @@ -29,6 +29,16 @@ #include <cerrno> #include <cxxabi_forced.h> +#ifndef _GLIBCXX_USE_NANOSLEEP +# ifdef _GLIBCXX_HAVE_SLEEP +# include <unistd.h> +# elif defined(_GLIBCXX_HAVE_WIN32_SLEEP) +# include <windows.h> +# else +# error "No sleep function known for this target" +# endif +#endif + #ifdef _GLIBCXX_HAS_GTHREADS #if defined(_GLIBCXX_USE_GET_NPROCS) @@ -59,16 +69,6 @@ static inline int get_nprocs() # define _GLIBCXX_NPROCS 0 #endif -#ifndef _GLIBCXX_USE_NANOSLEEP -# ifdef _GLIBCXX_HAVE_SLEEP -# include <unistd.h> -# elif defined(_GLIBCXX_HAVE_WIN32_SLEEP) -# include <windows.h> -# else -# error "No sleep function known for this target" -# endif -#endif - namespace std _GLIBCXX_VISIBILITY(default) { extern "C" @@ -180,13 +180,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __n; } +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std + +#endif // _GLIBCXX_HAS_GTHREADS + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION namespace this_thread { void __sleep_for(chrono::seconds __s, chrono::nanoseconds __ns) { #ifdef _GLIBCXX_USE_NANOSLEEP - __gthread_time_t __ts = + struct ::timespec __ts = { static_cast<std::time_t>(__s.count()), static_cast<long>(__ns.count()) @@ -231,8 +239,5 @@ namespace this_thread #endif } } - _GLIBCXX_END_NAMESPACE_VERSION } // namespace std - -#endif // _GLIBCXX_HAS_GTHREADS |