1#ifndef BOOST_CORE_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
2#define BOOST_CORE_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
3
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10// boost/core/detail/sp_thread_sleep.hpp
11//
12// inline void bost::core::sp_thread_sleep();
13//
14// Cease execution for a while to yield to other threads,
15// as if by calling nanosleep() with an appropriate interval.
16//
17// Copyright 2008, 2020, 2023 Peter Dimov
18// Distributed under the Boost Software License, Version 1.0
19// https://www.boost.org/LICENSE_1_0.txt
20
21#include <boost/config.hpp>
22#include <boost/config/pragma_message.hpp>
23
24#if defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
25
26#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
27 BOOST_PRAGMA_MESSAGE("Using Sleep(1) in sp_thread_sleep")
28#endif
29
30#include <boost/core/detail/sp_win32_sleep.hpp>
31
32namespace boost
33{
34namespace core
35{
36namespace detail
37{
38
39inline void sp_thread_sleep() BOOST_NOEXCEPT
40{
41 Sleep( 1 );
42}
43
44} // namespace detail
45
46using boost::core::detail::sp_thread_sleep;
47
48} // namespace core
49} // namespace boost
50
51#elif defined(BOOST_HAS_NANOSLEEP)
52
53#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
54 BOOST_PRAGMA_MESSAGE("Using nanosleep() in sp_thread_sleep")
55#endif
56
57#include <time.h>
58
59#if defined(BOOST_HAS_PTHREADS) && !defined(__ANDROID__)
60# include <pthread.h>
61#endif
62
63namespace boost
64{
65namespace core
66{
67
68inline void sp_thread_sleep() BOOST_NOEXCEPT
69{
70#if defined(BOOST_HAS_PTHREADS) && !defined(__ANDROID__)
71
72 int oldst;
73 pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, oldstate: &oldst );
74
75#endif
76
77 // g++ -Wextra warns on {} or {0}
78 struct timespec rqtp = { .tv_sec: 0, .tv_nsec: 0 };
79
80 // POSIX says that timespec has tv_sec and tv_nsec
81 // But it doesn't guarantee order or placement
82
83 rqtp.tv_sec = 0;
84 rqtp.tv_nsec = 1000;
85
86 nanosleep( requested_time: &rqtp, remaining: 0 );
87
88#if defined(BOOST_HAS_PTHREADS) && !defined(__ANDROID__)
89
90 pthread_setcancelstate( state: oldst, oldstate: &oldst );
91
92#endif
93
94}
95
96} // namespace core
97} // namespace boost
98
99#else
100
101#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
102 BOOST_PRAGMA_MESSAGE("Using sp_thread_yield() in sp_thread_sleep")
103#endif
104
105#include <boost/core/detail/sp_thread_yield.hpp>
106
107namespace boost
108{
109namespace core
110{
111
112inline void sp_thread_sleep() BOOST_NOEXCEPT
113{
114 sp_thread_yield();
115}
116
117} // namespace core
118} // namespace boost
119
120#endif
121
122#endif // #ifndef BOOST_CORE_DETAIL_SP_THREAD_SLEEP_HPP_INCLUDED
123