• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 //          Copyright Oliver Kowalke 2009.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include "boost/coroutine/stack_traits.hpp"
8 
9 extern "C" {
10 #include <signal.h>
11 #include <sys/resource.h>
12 #include <sys/time.h>
13 #include <unistd.h>
14 }
15 
16 //#if _POSIX_C_SOURCE >= 200112L
17 
18 #include <algorithm>
19 #include <cmath>
20 
21 #include <boost/assert.hpp>
22 #include <boost/thread.hpp>
23 
24 #if !defined (SIGSTKSZ)
25 # define SIGSTKSZ (8 * 1024)
26 # define UDEF_SIGSTKSZ
27 #endif
28 
29 #ifdef BOOST_HAS_ABI_HEADERS
30 #  include BOOST_ABI_PREFIX
31 #endif
32 
33 namespace {
34 
pagesize_(std::size_t * size)35 void pagesize_( std::size_t * size)
36 {
37     // conform to POSIX.1-2001
38     * size = ::sysconf( _SC_PAGESIZE);
39 }
40 
stacksize_limit_(rlimit * limit)41 void stacksize_limit_( rlimit * limit)
42 {
43     // conforming to POSIX.1-2001
44 #if defined(BOOST_DISABLE_ASSERTS) || defined(NDEBUG)
45     ::getrlimit( RLIMIT_STACK, limit);
46 #else
47     const int result = ::getrlimit( RLIMIT_STACK, limit);
48     BOOST_ASSERT( 0 == result);
49 #endif
50 }
51 
pagesize()52 std::size_t pagesize()
53 {
54     static std::size_t size = 0;
55     static boost::once_flag flag;
56     boost::call_once( flag, pagesize_, & size);
57     return size;
58 }
59 
stacksize_limit()60 rlimit stacksize_limit()
61 {
62     static rlimit limit;
63     static boost::once_flag flag;
64     boost::call_once( flag, stacksize_limit_, & limit);
65     return limit;
66 }
67 
68 }
69 
70 namespace boost {
71 namespace coroutines {
72 
73 bool
is_unbounded()74 stack_traits::is_unbounded() BOOST_NOEXCEPT
75 { return RLIM_INFINITY == stacksize_limit().rlim_max; }
76 
77 std::size_t
page_size()78 stack_traits::page_size() BOOST_NOEXCEPT
79 { return pagesize(); }
80 
81 std::size_t
default_size()82 stack_traits::default_size() BOOST_NOEXCEPT
83 {
84     std::size_t size = 8 * minimum_size();
85     if ( is_unbounded() ) return size;
86 
87     BOOST_ASSERT( maximum_size() >= minimum_size() );
88     return maximum_size() == size
89         ? size
90         : (std::min)( size, maximum_size() );
91 }
92 
93 std::size_t
minimum_size()94 stack_traits::minimum_size() BOOST_NOEXCEPT
95 { return SIGSTKSZ; }
96 
97 std::size_t
maximum_size()98 stack_traits::maximum_size() BOOST_NOEXCEPT
99 {
100     BOOST_ASSERT( ! is_unbounded() );
101     return static_cast< std::size_t >( stacksize_limit().rlim_max);
102 }
103 
104 }}
105 
106 #ifdef BOOST_HAS_ABI_HEADERS
107 #  include BOOST_ABI_SUFFIX
108 #endif
109 
110 #ifdef UDEF_SIGSTKSZ
111 # undef SIGSTKSZ
112 #endif
113