Skip to content

Commit

Permalink
enabled support for BOOST_ASIO_NO_TS_EXECUTORS
Browse files Browse the repository at this point in the history
Closes #214.
  • Loading branch information
klemens-morgenstern committed Dec 13, 2024
1 parent 25157fd commit 2a49752
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions include/boost/cobalt/detail/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ BOOST_COBALT_DECL std::exception_ptr wait_not_ready();
BOOST_COBALT_DECL std::exception_ptr already_awaited();
BOOST_COBALT_DECL std::exception_ptr allocation_failed();

BOOST_COBALT_DECL BOOST_NORETURN void throw_bad_executor(const boost::source_location & loc = BOOST_CURRENT_LOCATION);

template<typename >
std::exception_ptr wait_not_ready() { return boost::cobalt::detail::wait_not_ready();}

Expand Down
2 changes: 1 addition & 1 deletion include/boost/cobalt/detail/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ struct task_promise
const executor_type & get_executor() const
{
if (!exec)
throw_exception(asio::bad_executor());
detail::throw_bad_executor();
BOOST_ASSERT(exec_);
return *exec_;
}
Expand Down
3 changes: 2 additions & 1 deletion include/boost/cobalt/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define BOOST_COBALT_THREAD_HPP

#include <boost/cobalt/detail/thread.hpp>
#include <boost/cobalt/detail/exception.hpp>
#include <boost/cobalt/detail/await_result_helper.hpp>


Expand Down Expand Up @@ -56,7 +57,7 @@ struct thread
{
auto st = state_;
if (!st || st->done)
boost::throw_exception(asio::execution::bad_executor(), loc);
cobalt::detail::throw_bad_executor(loc);

return st ->ctx.get_executor();
}
Expand Down
11 changes: 11 additions & 0 deletions src/detail/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <boost/cobalt/detail/exception.hpp>
#include <boost/cobalt/error.hpp>

#include <boost/asio/executor.hpp>

namespace boost::cobalt::detail
{

Expand Down Expand Up @@ -62,5 +64,14 @@ std::exception_ptr allocation_failed()
return ep;
}

void throw_bad_executor(const boost::source_location & loc)
{
#if defined(BOOST_ASIO_NO_TS_EXECUTORS)
boost::throw_exception(boost::asio::execution::bad_executor(), loc);
#else
boost::throw_exception(boost::asio::bad_executor(), loc);
#endif
}


}
12 changes: 7 additions & 5 deletions src/this_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#include <boost/cobalt/this_thread.hpp>
#include <boost/cobalt/detail/exception.hpp>
#include <boost/asio/any_io_executor.hpp>

#include <boost/asio/executor.hpp>
Expand Down Expand Up @@ -53,7 +54,8 @@ bool has_executor()
executor & get_executor(const boost::source_location & loc)
{
if (!detail::executor)
throw_exception(asio::bad_executor(), loc);
cobalt::detail::throw_bad_executor(loc);

return *detail::executor;
}

Expand All @@ -67,15 +69,16 @@ struct this_thread_service : asio::detail::execution_context_service_base<this_t

void shutdown() override
{
if (detail::executor && (&detail::executor->context() == &this->context()))

if (detail::executor && (&asio::query(*detail::executor, asio::execution::context) == &this->context()))
detail::executor.reset();
}
};

void set_executor(executor exec) noexcept
{
detail::executor = std::move(exec);
asio::use_service<this_thread_service>(detail::executor->context());
asio::use_service<this_thread_service>(asio::query(*detail::executor, asio::execution::context));
}
}

Expand All @@ -88,8 +91,7 @@ extract_executor(asio::any_io_executor exec)
{
auto t = exec.target<executor>();
if (t == nullptr)
throw_exception(asio::bad_executor());

cobalt::detail::throw_bad_executor(loc);
return *t;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/promise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(bad_executor_)
auto t = test0();
BOOST_FAIL("Should throw");
}
catch(asio::bad_executor &) {}
catch(std::exception & e) {BOOST_CHECK_EQUAL(e.what(), std::string_view("bad executor"));}

}

Expand Down
4 changes: 2 additions & 2 deletions test/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(run)
auto t = thr();

t.join();
BOOST_CHECK_THROW(t.get_executor(), boost::asio::execution::bad_executor);
try {t.get_executor();} catch(std::exception & e) {BOOST_CHECK_EQUAL(e.what(), std::string_view("bad executor")); }
}


Expand All @@ -44,7 +44,7 @@ boost::cobalt::thread thr_stop()
auto exc = co_await boost::asio::this_coro::executor;
#endif

exc.context().stop();
boost::asio::query(exc, boost::asio::execution::context).stop();
co_await tim.async_wait(boost::cobalt::use_op);
}

Expand Down

0 comments on commit 2a49752

Please sign in to comment.