Skip to content

Commit

Permalink
Cease dependence on Thread
Browse files Browse the repository at this point in the history
On C++11 static local variables are initialized in thread-safe manner, but even on C++03 it should not be a problem because in our case variables are of trivial types, which means double initialization is not an issue, and they are initialized with the same value in every thread.
  • Loading branch information
Kojoley committed Jun 12, 2021
1 parent 131ec9d commit 8425e83
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 81 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ target_link_libraries(boost_context
Boost::pool
Boost::predef
Boost::smart_ptr
PRIVATE
Boost::thread
)

target_compile_definitions(boost_context
Expand Down
13 changes: 0 additions & 13 deletions build/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -850,26 +850,13 @@ alias impl_sources

explicit impl_sources ;

obj cxx11_hdr_mutex_check : ../build/cxx11_hdr_mutex.cpp ;
explicit cxx11_hdr_mutex_check ;
local cxx11_mutex = [ check-target-builds
cxx11_hdr_mutex_check "C++11 mutex"
:
: <library>/boost/thread//boost_thread
] ;

alias stack_traits_sources
: windows/stack_traits.cpp
: <target-os>windows
:
: $(cxx11_mutex)
;

alias stack_traits_sources
: posix/stack_traits.cpp
:
:
: $(cxx11_mutex)
;

explicit stack_traits_sources ;
Expand Down
10 changes: 0 additions & 10 deletions build/cxx11_hdr_mutex.cpp

This file was deleted.

45 changes: 12 additions & 33 deletions src/posix/stack_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ extern "C" {

#include <boost/assert.hpp>
#include <boost/config.hpp>
#if defined(BOOST_NO_CXX11_HDR_MUTEX)
# include <boost/thread.hpp>
#else
# include <mutex>
#endif

#if !defined (SIGSTKSZ)
# define SIGSTKSZ (32768) // 32kb minimum allowable stack
Expand All @@ -42,37 +37,20 @@ extern "C" {

namespace {

void pagesize_( std::size_t * size) BOOST_NOEXCEPT_OR_NOTHROW {
std::size_t pagesize() BOOST_NOEXCEPT_OR_NOTHROW {
// conform to POSIX.1-2001
* size = ::sysconf( _SC_PAGESIZE);
return ::sysconf( _SC_PAGESIZE);
}

void stacksize_limit_( rlimit * limit) BOOST_NOEXCEPT_OR_NOTHROW {
rlim_t stacksize_limit_() BOOST_NOEXCEPT_OR_NOTHROW {
rlimit limit;
// conforming to POSIX.1-2001
::getrlimit( RLIMIT_STACK, limit);
::getrlimit( RLIMIT_STACK, & limit);
return limit.rlim_max;
}

std::size_t pagesize() BOOST_NOEXCEPT_OR_NOTHROW {
static std::size_t size = 0;
#if defined(BOOST_NO_CXX11_HDR_MUTEX)
static boost::once_flag flag = BOOST_ONCE_INIT;
boost::call_once( flag, pagesize_, & size);
#else
static std::once_flag flag;
std::call_once( flag, pagesize_, & size);
#endif
return size;
}

rlimit stacksize_limit() BOOST_NOEXCEPT_OR_NOTHROW {
static rlimit limit;
#if defined(BOOST_NO_CXX11_HDR_MUTEX)
static boost::once_flag flag = BOOST_ONCE_INIT;
boost::call_once( flag, stacksize_limit_, & limit);
#else
static std::once_flag flag;
std::call_once( flag, stacksize_limit_, & limit);
#endif
rlim_t stacksize_limit() BOOST_NOEXCEPT_OR_NOTHROW {
static rlim_t limit = stacksize_limit_();
return limit;
}

Expand All @@ -83,12 +61,13 @@ namespace context {

bool
stack_traits::is_unbounded() BOOST_NOEXCEPT_OR_NOTHROW {
return RLIM_INFINITY == stacksize_limit().rlim_max;
return RLIM_INFINITY == stacksize_limit();
}

std::size_t
stack_traits::page_size() BOOST_NOEXCEPT_OR_NOTHROW {
return pagesize();
static std::size_t size = pagesize();
return size;
}

std::size_t
Expand All @@ -104,7 +83,7 @@ stack_traits::minimum_size() BOOST_NOEXCEPT_OR_NOTHROW {
std::size_t
stack_traits::maximum_size() BOOST_NOEXCEPT_OR_NOTHROW {
BOOST_ASSERT( ! is_unbounded() );
return static_cast< std::size_t >( stacksize_limit().rlim_max);
return static_cast< std::size_t >( stacksize_limit() );
}

}}
Expand Down
28 changes: 5 additions & 23 deletions src/windows/stack_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ extern "C" {

#include <boost/assert.hpp>
#include <boost/context/detail/config.hpp>
#if defined(BOOST_NO_CXX11_HDR_MUTEX)
# include <boost/thread.hpp>
#else
# include <mutex>
#endif

#include <boost/context/stack_context.hpp>

Expand All @@ -48,24 +43,10 @@ extern "C" {

namespace {

void system_info_( SYSTEM_INFO * si) BOOST_NOEXCEPT_OR_NOTHROW {
::GetSystemInfo( si);
}

SYSTEM_INFO system_info() BOOST_NOEXCEPT_OR_NOTHROW {
static SYSTEM_INFO si;
#if defined(BOOST_NO_CXX11_HDR_MUTEX)
static boost::once_flag flag = BOOST_ONCE_INIT;
boost::call_once( flag, static_cast< void(*)( SYSTEM_INFO *) >( system_info_), & si);
#else
static std::once_flag flag;
std::call_once( flag, static_cast< void(*)( SYSTEM_INFO *) >( system_info_), & si);
#endif
return si;
}

std::size_t pagesize() BOOST_NOEXCEPT_OR_NOTHROW {
return static_cast< std::size_t >( system_info().dwPageSize);
SYSTEM_INFO si;
::GetSystemInfo(&si);
return static_cast< std::size_t >( si.dwPageSize );
}

}
Expand All @@ -84,7 +65,8 @@ stack_traits::is_unbounded() BOOST_NOEXCEPT_OR_NOTHROW {
BOOST_CONTEXT_DECL
std::size_t
stack_traits::page_size() BOOST_NOEXCEPT_OR_NOTHROW {
return pagesize();
static std::size_t size = pagesize();
return size;
}

BOOST_CONTEXT_DECL
Expand Down

0 comments on commit 8425e83

Please sign in to comment.