diff --git a/include/boost/fiber/bounded_channel.hpp b/include/boost/fiber/bounded_channel.hpp index 6daba6c3..89a4d0a1 100644 --- a/include/boost/fiber/bounded_channel.hpp +++ b/include/boost/fiber/bounded_channel.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -205,11 +206,11 @@ public: hwm_{ hwm }, lwm_{ lwm } { if ( hwm_ <= lwm_) { - throw invalid_argument( static_cast< int >( std::errc::invalid_argument), + throw invalid_argument( std::make_error_code( std::errc::invalid_argument), "boost fiber: high-watermark is less than or equal to low-watermark for bounded_channel"); } if ( 0 == hwm) { - throw invalid_argument( static_cast< int >( std::errc::invalid_argument), + throw invalid_argument( std::make_error_code( std::errc::invalid_argument), "boost fiber: high-watermark is zero"); } } @@ -221,7 +222,7 @@ public: hwm_{ wm }, lwm_{ wm - 1 } { if ( 0 == wm) { - throw invalid_argument( static_cast< int >( std::errc::invalid_argument), + throw invalid_argument( std::make_error_code( std::errc::invalid_argument), "boost fiber: watermark is zero"); } } @@ -361,7 +362,9 @@ public: return is_closed_() || ! is_empty_(); }); if ( is_closed_() && is_empty_() ) { - throw logic_error("boost fiber: queue is closed"); + throw fiber_resource_error( + std::make_error_code( std::errc::operation_not_permitted), + "boost fiber: queue is closed"); } return value_pop_( lk); } diff --git a/include/boost/fiber/exceptions.hpp b/include/boost/fiber/exceptions.hpp index 06647595..bbcec5e2 100644 --- a/include/boost/fiber/exceptions.hpp +++ b/include/boost/fiber/exceptions.hpp @@ -1,4 +1,4 @@ - +// // Copyright Oliver Kowalke 2013. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -6,8 +6,8 @@ // based on boost.thread -#ifndef BOOST_fiber_EXCEPTIONS_H -#define BOOST_fiber_EXCEPTIONS_H +#ifndef BOOST_fiber_errorS_H +#define BOOST_fiber_errorS_H #include #include @@ -25,137 +25,73 @@ namespace boost { namespace fibers { -class fiber_exception : public std::system_error { +class fiber_error : public std::system_error { public: - fiber_exception() : - std::system_error( 0, std::system_category() ) { + fiber_error( std::error_code ec) : + std::system_error( ec) { } - fiber_exception( int sys_error_code) : - std::system_error( sys_error_code, std::system_category() ) { + fiber_error( std::error_code ec, const char * what_arg) : + std::system_error( ec, what_arg) { } - fiber_exception( int ev, const char * what_arg) : - std::system_error( std::error_code( ev, std::system_category() ), what_arg) { + fiber_error( std::error_code ec, std::string const& what_arg) : + std::system_error( ec, what_arg) { } - fiber_exception( int ev, const std::string & what_arg) : - std::system_error( std::error_code( ev, std::system_category() ), what_arg) { + virtual ~fiber_error() = default; +}; + +class lock_error : public fiber_error { +public: + lock_error( std::error_code ec) : + fiber_error( ec) { } - virtual ~fiber_exception() throw() { + lock_error( std::error_code ec, const char * what_arg) : + fiber_error( ec, what_arg) { + } + + lock_error( std::error_code ec, std::string const& what_arg) : + fiber_error( ec, what_arg) { } }; -class condition_error : public fiber_exception { +class fiber_resource_error : public fiber_error { public: - condition_error() : - fiber_exception( 0, "Condition error") { + fiber_resource_error( std::error_code ec) : + fiber_error( ec, "boost::fiber_resource_error") { } - condition_error( int ev) : - fiber_exception( ev, "Condition error") { + fiber_resource_error( std::error_code ec, const char * what_arg) : + fiber_error( ec, what_arg) { } - condition_error( int ev, const char * what_arg) : - fiber_exception( ev, what_arg) { - } - - condition_error( int ev, const std::string & what_arg) : - fiber_exception( ev, what_arg) { + fiber_resource_error( std::error_code ec, std::string const& what_arg) : + fiber_error( ec, what_arg) { } }; -class lock_error : public fiber_exception { +class invalid_argument : public fiber_error { public: - lock_error() : - fiber_exception( 0, "boost::lock_error") { + invalid_argument( std::error_code ec) : + fiber_error( ec, "boost::invalid_argument") { } - lock_error( int ev) : - fiber_exception( ev, "boost::lock_error") { + invalid_argument( std::error_code ec, const char * what_arg) : + fiber_error( ec, what_arg) { } - lock_error( int ev, const char * what_arg) : - fiber_exception( ev, what_arg) { - } - - lock_error( int ev, const std::string & what_arg) : - fiber_exception( ev, what_arg) { + invalid_argument( std::error_code ec, std::string const& what_arg) : + fiber_error( ec, what_arg) { } }; -class fiber_resource_error : public fiber_exception { -public: - fiber_resource_error() : - fiber_exception( - static_cast< int >( std::errc::resource_unavailable_try_again), - "boost::fiber_resource_error") { - } - - fiber_resource_error( int ev) : - fiber_exception( ev, "boost::fiber_resource_error") { - } - - fiber_resource_error( int ev, const char * what_arg) : - fiber_exception( ev, what_arg) { - } - - fiber_resource_error( int ev, const std::string & what_arg) : - fiber_exception( ev, what_arg) { - } -}; - -class invalid_argument : public fiber_exception { -public: - invalid_argument() : - fiber_exception( - static_cast< int >( std::errc::invalid_argument), - "boost::invalid_argument") { - } - - invalid_argument( int ev) : - fiber_exception( ev, "boost::invalid_argument") { - } - - invalid_argument( int ev, const char * what_arg) : - fiber_exception( ev, what_arg) { - } - - invalid_argument( int ev, const std::string & what_arg) : - fiber_exception( ev, what_arg) { - } -}; - -class logic_error : public fiber_exception { -public: - logic_error() : - fiber_exception( 0, "boost::logic_error") { - } - - logic_error( const char * what_arg) : - fiber_exception( 0, what_arg) { - } - - logic_error( int ev) : - fiber_exception( ev, "boost::logic_error") { - } - - logic_error( int ev, const char * what_arg) : - fiber_exception( ev, what_arg) { - } - - logic_error( int ev, const std::string & what_arg) : - fiber_exception( ev, what_arg) { - } -}; - -class fiber_interrupted : public fiber_exception { +class fiber_interrupted : public fiber_error { public: fiber_interrupted() : - fiber_exception( - static_cast< int >( std::errc::interrupted), - "boost::fiber_interrupted") { + fiber_error( + std::make_error_code( std::errc::interrupted) ) { } }; @@ -193,22 +129,10 @@ std::error_condition make_error_condition( boost::fibers::future_errc e) noexcep namespace boost { namespace fibers { -class future_error : public std::logic_error { -private: - std::error_code ec_; - +class future_error : public fiber_error { public: future_error( std::error_code ec) : - logic_error( ec.message() ), - ec_( ec) { - } - - std::error_code const& code() const noexcept { - return ec_; - } - - const char* what() const throw() { - return code().message().c_str(); + fiber_error( ec) { } }; @@ -260,4 +184,4 @@ public: # include BOOST_ABI_SUFFIX #endif -#endif // BOOST_fiber_EXCEPTIONS_H +#endif // BOOST_fiber_errorS_H diff --git a/include/boost/fiber/unbounded_channel.hpp b/include/boost/fiber/unbounded_channel.hpp index 92e54368..25568ec7 100644 --- a/include/boost/fiber/unbounded_channel.hpp +++ b/include/boost/fiber/unbounded_channel.hpp @@ -203,7 +203,9 @@ public: return is_closed_() || ! is_empty_(); }); if ( is_closed_() && is_empty_() ) { - throw logic_error("boost fiber: queue is closed"); + throw fiber_resource_error( + std::make_error_code( std::errc::operation_not_permitted), + "boost fiber: queue is closed"); } return value_pop_( lk); } diff --git a/src/barrier.cpp b/src/barrier.cpp index a23ab50d..88f18199 100644 --- a/src/barrier.cpp +++ b/src/barrier.cpp @@ -21,7 +21,7 @@ barrier::barrier( std::size_t initial) : initial_{ initial }, current_{ initial_ } { if ( 0 == initial) { - throw invalid_argument( static_cast< int >( std::errc::invalid_argument), + throw invalid_argument( std::make_error_code( std::errc::invalid_argument), "boost fiber: zero initial barrier count"); } } diff --git a/src/fiber.cpp b/src/fiber.cpp index e2c7a9a2..b88d05a3 100644 --- a/src/fiber.cpp +++ b/src/fiber.cpp @@ -31,11 +31,11 @@ void fiber::join() { // FIXME: must fiber::join() be synchronized? if ( context::active()->get_id() == get_id() ) { - throw fiber_resource_error( static_cast< int >( std::errc::resource_deadlock_would_occur), + throw fiber_resource_error( std::make_error_code( std::errc::resource_deadlock_would_occur), "boost fiber: trying to join itself"); } if ( ! joinable() ) { - throw fiber_resource_error( static_cast< int >( std::errc::invalid_argument), + throw fiber_resource_error( std::make_error_code( std::errc::invalid_argument), "boost fiber: fiber not joinable"); } @@ -53,7 +53,7 @@ fiber::interrupt() noexcept { void fiber::detach() { if ( ! joinable() ) { - throw fiber_resource_error( static_cast< int >( std::errc::invalid_argument), + throw fiber_resource_error( std::make_error_code( std::errc::invalid_argument), "boost fiber: fiber not joinable"); } impl_.reset(); diff --git a/src/mutex.cpp b/src/mutex.cpp index 6067f7a4..44b54453 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -26,7 +26,8 @@ mutex::lock() { // store this fiber in order to be notified later detail::spinlock_lock lk( wait_queue_splk_); if ( ctx == owner_) { - throw lock_error( static_cast< int >( std::errc::resource_deadlock_would_occur), + throw lock_error( + std::make_error_code( std::errc::resource_deadlock_would_occur), "boost fiber: a deadlock is detected"); } else if ( nullptr == owner_) { owner_ = ctx; @@ -57,7 +58,8 @@ mutex::unlock() { context * ctx = context::active(); detail::spinlock_lock lk( wait_queue_splk_); if ( ctx != owner_) { - throw lock_error( static_cast< int >( std::errc::operation_not_permitted), + throw lock_error( + std::make_error_code( std::errc::operation_not_permitted), "boost fiber: no privilege to perform the operation"); } if ( ! wait_queue_.empty() ) { diff --git a/src/recursive_mutex.cpp b/src/recursive_mutex.cpp index bb3342a9..d04680cb 100644 --- a/src/recursive_mutex.cpp +++ b/src/recursive_mutex.cpp @@ -60,7 +60,8 @@ recursive_mutex::unlock() { context * ctx = context::active(); detail::spinlock_lock lk( wait_queue_splk_); if ( ctx != owner_) { - throw lock_error( static_cast< int >( std::errc::operation_not_permitted), + throw lock_error( + std::make_error_code( std::errc::operation_not_permitted), "boost fiber: no privilege to perform the operation"); } if ( 0 == --count_) { diff --git a/src/recursive_timed_mutex.cpp b/src/recursive_timed_mutex.cpp index 12a99a72..af6c9b19 100644 --- a/src/recursive_timed_mutex.cpp +++ b/src/recursive_timed_mutex.cpp @@ -89,7 +89,8 @@ recursive_timed_mutex::unlock() { context * ctx = context::active(); detail::spinlock_lock lk( wait_queue_splk_); if ( ctx != owner_) { - throw lock_error( static_cast< int >( std::errc::operation_not_permitted), + throw lock_error( + std::make_error_code( std::errc::operation_not_permitted), "boost fiber: no privilege to perform the operation"); } if ( 0 == --count_) { diff --git a/src/timed_mutex.cpp b/src/timed_mutex.cpp index f05b5663..1e822dd2 100644 --- a/src/timed_mutex.cpp +++ b/src/timed_mutex.cpp @@ -50,7 +50,8 @@ timed_mutex::lock() { // store this fiber in order to be notified later detail::spinlock_lock lk( wait_queue_splk_); if ( ctx == owner_) { - throw lock_error( static_cast< int >( std::errc::resource_deadlock_would_occur), + throw lock_error( + std::make_error_code( std::errc::resource_deadlock_would_occur), "boost fiber: a deadlock is detected"); } else if ( nullptr == owner_) { owner_ = ctx; @@ -81,7 +82,8 @@ timed_mutex::unlock() { context * ctx = context::active(); detail::spinlock_lock lk( wait_queue_splk_); if ( ctx != owner_) { - throw lock_error( static_cast< int >( std::errc::operation_not_permitted), + throw lock_error( + std::make_error_code( std::errc::operation_not_permitted), "boost fiber: no privilege to perform the operation"); } if ( ! wait_queue_.empty() ) { diff --git a/test/test_bounded_channel.cpp b/test/test_bounded_channel.cpp index 67e0fabf..0e45d213 100644 --- a/test/test_bounded_channel.cpp +++ b/test/test_bounded_channel.cpp @@ -49,7 +49,7 @@ void test_zero_wm_1() { bool thrown = false; try { boost::fibers::bounded_channel< int > c( 0); - } catch ( boost::fibers::fiber_exception const&) { + } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown); @@ -59,7 +59,7 @@ void test_zero_wm_2() { bool thrown = false; try { boost::fibers::bounded_channel< int > c( 0, 0); - } catch ( boost::fibers::fiber_exception const&) { + } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown); @@ -69,7 +69,7 @@ void test_hwm_less_lwm() { bool thrown = false; try { boost::fibers::bounded_channel< int > c( 2, 3); - } catch ( boost::fibers::fiber_exception const&) { + } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown); @@ -79,7 +79,7 @@ void test_hwm_equal_lwm() { bool thrown = false; try { boost::fibers::bounded_channel< int > c( 3, 3); - } catch ( boost::fibers::fiber_exception const&) { + } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown); @@ -205,7 +205,7 @@ void test_value_pop_closed() { bool thrown = false; try { c.value_pop(); - } catch ( boost::fibers::fiber_exception const&) { + } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown); diff --git a/test/test_unbounded_channel.cpp b/test/test_unbounded_channel.cpp index 1eb8612d..e0ab73a0 100644 --- a/test/test_unbounded_channel.cpp +++ b/test/test_unbounded_channel.cpp @@ -105,7 +105,7 @@ void test_value_pop_closed() { bool thrown = false; try { c.value_pop(); - } catch ( boost::fibers::fiber_exception const&) { + } catch ( boost::fibers::fiber_error const&) { thrown = true; } BOOST_CHECK( thrown);