Skip to content

Commit

Permalink
Merge pull request #19 from bemanproject/abstract-event_type
Browse files Browse the repository at this point in the history
abstracted the completion events into an enum class
  • Loading branch information
dietmarkuehl authored Feb 23, 2025
2 parents 062d439 + bbb3a56 commit 63ec68e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

SANITIZERS = none debug msan asan usan tsan
.PHONY: default gcc clang run update check ce todo distclean clean build test all $(SANITIZERS)
.PHONY: default gcc clang run update check ce todo distclean clean build test all format $(SANITIZERS)

COMPILER=system
CXX_BASE=$(CXX:$(dir $(CXX))%=%)
Expand Down Expand Up @@ -88,6 +88,9 @@ check:
todo:
bin/mk-todo.py

format:
git clang-format main

clean:
$(RM) -r $(BUILD)
$(RM) mkerr olderr *~
Expand Down
19 changes: 19 additions & 0 deletions include/beman/net/detail/event_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// include/beman/net/detail/event_type.hpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef INCLUDED_INCLUDE_BEMAN_NET_DETAIL_EVENT_TYPE
#define INCLUDED_INCLUDE_BEMAN_NET_DETAIL_EVENT_TYPE

#include <cinttypes>

// ----------------------------------------------------------------------------

namespace beman::net {
enum class event_type { none = 0x00, in = 0x01, out = 0x02, in_out = 0x03 };
constexpr ::beman::net::event_type operator&(::beman::net::event_type e0, ::beman::net::event_type e1) {
return ::beman::net::event_type(::std::uint8_t(e0) & ::std::uint8_t(e1));
}
} // namespace beman::net
// ----------------------------------------------------------------------------

#endif
7 changes: 4 additions & 3 deletions include/beman/net/detail/io_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define INCLUDED_BEMAN_NET_DETAIL_IO_BASE

#include <beman/net/detail/netfwd.hpp>
#include <beman/net/detail/event_type.hpp>
#include <memory>
#include <system_error>
#include <ostream>
Expand Down Expand Up @@ -51,11 +52,11 @@ struct beman::net::detail::io_base
io_base* next{nullptr}; // used for an intrusive list
::beman::net::detail::context_base* context{nullptr};
::beman::net::detail::socket_id id; // the entity affected
int event; // mask for expected events
::beman::net::event_type event; // mask for expected events
work_t work;
extra_t extra{nullptr, +[](void*){}};

io_base(::beman::net::detail::socket_id i, int ev) : id(i), event(ev) {}
io_base(::beman::net::detail::socket_id i, ::beman::net::event_type ev) : id(i), event(ev) {}
virtual ~io_base() = default;

virtual auto complete() -> void = 0;
Expand All @@ -73,7 +74,7 @@ struct beman::net::detail::io_operation
, Data
{
template <typename D = Data>
io_operation(::beman::net::detail::socket_id i, int ev, D&& a = Data())
io_operation(::beman::net::detail::socket_id i, ::beman::net::event_type ev, D&& a = Data())
: io_base(i, ev), Data(::std::forward<D>(a)) {}
};

Expand Down
13 changes: 7 additions & 6 deletions include/beman/net/detail/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define INCLUDED_INCLUDE_BEMAN_NET_DETAIL_OPERATIONS

#include <beman/net/detail/context_base.hpp>
#include <beman/net/detail/event_type.hpp>
#include <beman/net/detail/sender.hpp>

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -67,7 +68,7 @@ struct beman::net::detail::accept_desc
data(acceptor_t& a): d_acceptor(a) {}

auto id() const { return this->d_acceptor.id(); }
auto events() const { return POLLIN; }
auto events() const { return ::beman::net::event_type::in; }
auto get_scheduler() { return this->d_acceptor.get_scheduler(); }
auto set_value(operation& o, auto&& receiver)
{
Expand Down Expand Up @@ -95,7 +96,7 @@ struct beman::net::detail::connect_desc
Socket& d_socket;

auto id() const { return this->d_socket.id(); }
auto events() const { return POLLIN | POLLOUT; }
auto events() const { return ::beman::net::event_type::in_out; }
auto get_scheduler() { return this->d_socket.get_scheduler(); }
auto set_value(operation&, auto&& receiver)
{
Expand All @@ -121,7 +122,7 @@ struct beman::net::detail::send_desc
Buffers d_buffers;

auto id() const { return this->d_stream.id(); }
auto events() const { return POLLOUT; }
auto events() const { return ::beman::net::event_type::out; }
auto get_scheduler() { return this->d_stream.get_scheduler(); }
auto set_value(operation& o, auto&& receiver)
{
Expand Down Expand Up @@ -156,7 +157,7 @@ struct beman::net::detail::send_to_desc
Endpoint d_endpoint;

auto id() const { return this->d_stream.id(); }
auto events() const { return POLLOUT; }
auto events() const { return ::beman::net::event_type::out; }
auto get_scheduler() { return this->d_stream.get_scheduler(); }
auto set_value(operation& o, auto&& receiver)
{
Expand Down Expand Up @@ -185,7 +186,7 @@ struct beman::net::detail::receive_desc
Buffers d_buffers;

auto id() const { return this->d_stream.id(); }
auto events() const { return POLLIN; }
auto events() const { return ::beman::net::event_type::in; }
auto get_scheduler() { return this->d_stream.get_scheduler(); }
auto set_value(operation& o, auto&& receiver)
{
Expand Down Expand Up @@ -219,7 +220,7 @@ struct beman::net::detail::receive_from_desc
Endpoint d_endpoint;

auto id() const { return this->d_stream.id(); }
auto events() const { return POLLIN; }
auto events() const { return ::beman::net::event_type::in; }
auto get_scheduler() { return this->d_stream.get_scheduler(); }
auto set_value(operation& o, auto&& receiver)
{
Expand Down
9 changes: 8 additions & 1 deletion include/beman/net/detail/poll_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,14 @@ struct beman::net::detail::poll_context final
if (this->d_sockets[id].blocking
|| completion->work(*this, completion) == ::beman::net::detail::submit_result::submit)
{
this->d_poll.emplace_back(::pollfd{this->native_handle(id), short(completion->event), short()});
decltype(pollfd().events) events{};
if (bool(completion->event & ::beman::net::event_type::in)) {
events |= POLLIN;
}
if (bool(completion->event & ::beman::net::event_type::out)) {
events |= POLLOUT;
}
this->d_poll.emplace_back(::pollfd{this->native_handle(id), events, short()});
this->d_outstanding.emplace_back(completion);
this->wakeup();
return ::beman::net::detail::submit_result::submit;
Expand Down
14 changes: 5 additions & 9 deletions include/beman/net/detail/sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ struct beman::net::detail::sender_state
{
sender_state* d_state;
cancel_callback(sender_state* s)
: ::beman::net::detail::io_base(::beman::net::detail::socket_id(), 0)
, d_state(s)
{
}
: ::beman::net::detail::io_base(::beman::net::detail::socket_id(), ::beman::net::event_type::none),
d_state(s) {}
cancel_callback(cancel_callback&&) = default;
auto operator()()
{
Expand Down Expand Up @@ -248,11 +246,9 @@ struct beman::net::detail::sender_cpo
template <::beman::net::detail::ex::sender Upstream, typename... Args>
auto operator()(Upstream&& u, Args&&... args) const
{
using data = Desc::template data<::std::decay_t<Args>...>;
return ::beman::net::detail::sender<Desc, data, ::std::remove_cvref_t<Upstream>>{
data{::std::forward<Args>(args)...},
::std::forward<Upstream>(u)
};
using Data = Desc::template data<::std::decay_t<Args>...>;
return ::beman::net::detail::sender<Desc, Data, ::std::remove_cvref_t<Upstream>>{
Data{::std::forward<Args>(args)...}, ::std::forward<Upstream>(u)};
}
};

Expand Down
5 changes: 3 additions & 2 deletions include/beman/net/detail/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <beman/net/detail/netfwd.hpp>
#include <beman/net/detail/sender.hpp>
#include <beman/net/detail/event_type.hpp>

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -40,7 +41,7 @@ struct beman::net::detail::resume_after_desc
::std::chrono::microseconds d_duration;

auto id() const -> ::beman::net::detail::socket_id { return {}; }
auto events() const { return decltype(POLLIN)(); }
auto events() const { return ::beman::net::event_type::none; }
auto get_scheduler() { return this->d_scheduler; }
auto set_value(operation&, auto&& receiver)
{
Expand Down Expand Up @@ -68,7 +69,7 @@ struct beman::net::detail::resume_at_desc
::std::chrono::system_clock::time_point d_time;

auto id() const -> ::beman::net::detail::socket_id { return {}; }
auto events() const { return decltype(POLLIN)(); }
auto events() const { return ::beman::net::event_type::none; }
auto get_scheduler() { return this->d_scheduler; }
auto set_value(operation&, auto&& receiver)
{
Expand Down

0 comments on commit 63ec68e

Please sign in to comment.