Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/include/join/async_acceptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ namespace join

_acceptOp.remoteLen = sizeof (struct sockaddr_storage);
_acceptOp.acceptHandler = std::move (handler);
_acceptOp.op = IoOperation::makeAccept (_descriptor.handle (), _acceptOp.remote.addr (),
&_acceptOp.remoteLen, flags | SOCK_NONBLOCK, this);
_acceptOp.op = IoOperation::makeAccept (_descriptor, _acceptOp.remote.addr (), &_acceptOp.remoteLen,
flags | SOCK_NONBLOCK, this);

if (_proactor->submit (_acceptOp.op, flush, false) == -1)
{
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace join

_acceptOp.remote = Endpoint ();
_acceptOp.acceptHandler = std::move (handler);
_acceptOp.op = IoOperation::makeAcceptMulti (_descriptor.handle (), flags | SOCK_NONBLOCK, this);
_acceptOp.op = IoOperation::makeAcceptMulti (_descriptor, flags | SOCK_NONBLOCK, this);

if (_proactor->submit (_acceptOp.op, flush, false) == -1)
{
Expand Down
16 changes: 7 additions & 9 deletions core/include/join/async_datagram_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ namespace join
}

read->readHandler = std::move (handler);
read->op = IoOperation::makeRecv (this->descriptor ().handle (), data, maxSize, MSG_TRUNC, this, link);
read->op = IoOperation::makeRecv (this->descriptor (), data, maxSize, MSG_TRUNC, this, link);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -337,7 +337,7 @@ namespace join
read->msg.msg_control = control;
read->msg.msg_controllen = controlSize;
read->msg.msg_flags = 0;
read->op = IoOperation::makeRecvmsg (this->descriptor ().handle (), &read->msg, 0, this, link);
read->op = IoOperation::makeRecvmsg (this->descriptor (), &read->msg, 0, this, link);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -391,7 +391,7 @@ namespace join
read->msg.msg_control = nullptr;
read->msg.msg_controllen = controlSize;
read->msg.msg_flags = 0;
read->op = IoOperation::makeRecvmsgMulti (this->descriptor ().handle (), group, &read->msg, 0, this);
read->op = IoOperation::makeRecvmsgMulti (this->descriptor (), group, &read->msg, 0, this);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -475,18 +475,16 @@ namespace join
{
if (this->_proactor->fixedBuffer ().owns (data, size))
{
write->op =
IoOperation::makeSendZc (this->descriptor ().handle (), data, size, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSendZc (this->descriptor (), data, size, MSG_NOSIGNAL, this, link);
}
else
{
write->op =
IoOperation::makeSend (this->descriptor ().handle (), data, size, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSend (this->descriptor (), data, size, MSG_NOSIGNAL, this, link);
}
}
else
{
write->op = IoOperation::makeSend (this->descriptor ().handle (), data, size, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSend (this->descriptor (), data, size, MSG_NOSIGNAL, this, link);
}

write->op.state.store (IoOperation::State::Submitted, std::memory_order_release);
Expand Down Expand Up @@ -583,7 +581,7 @@ namespace join
write->msg.msg_control = const_cast<char*> (control);
write->msg.msg_controllen = controlSize;
write->msg.msg_flags = 0;
write->op = IoOperation::makeSendmsg (this->descriptor ().handle (), &write->msg, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSendmsg (this->descriptor (), &write->msg, MSG_NOSIGNAL, this, link);
write->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (write);
Expand Down
6 changes: 3 additions & 3 deletions core/include/join/async_raw_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ namespace join
read->msg.msg_control = nullptr;
read->msg.msg_controllen = 0;
read->msg.msg_flags = 0;
read->op = IoOperation::makeRecvmsg (this->descriptor ().handle (), &read->msg, 0, this, link);
read->op = IoOperation::makeRecvmsg (this->descriptor (), &read->msg, 0, this, link);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -323,7 +323,7 @@ namespace join
}

read->readHandler = std::move (handler);
read->op = IoOperation::makeRecvMulti (this->descriptor ().handle (), group, 0, this);
read->op = IoOperation::makeRecvMulti (this->descriptor (), group, 0, this);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -405,7 +405,7 @@ namespace join
write->msg.msg_control = nullptr;
write->msg.msg_controllen = 0;
write->msg.msg_flags = 0;
write->op = IoOperation::makeSendmsg (this->descriptor ().handle (), &write->msg, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSendmsg (this->descriptor (), &write->msg, MSG_NOSIGNAL, this, link);
write->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (write);
Expand Down
46 changes: 38 additions & 8 deletions core/include/join/async_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ namespace join
using AsyncWait = BasicAsyncWait<Protocol, ProactorType>;
using WaitHandler = typename AsyncWait::Wait;

/// number of allocator slots.
static constexpr size_t _slotCount = Allocator::count;

/// number of operations in flight.
static constexpr size_t _opCount = Allocator::count;
static constexpr size_t _opCount = _slotCount - 1;

static_assert (_opCount > 0, "a socket needs at least one operation slot");
static_assert (Allocator::pools == 1, "the operations allocator needs a single pool");
Expand Down Expand Up @@ -157,6 +160,8 @@ namespace join
{
other.suspendAll ();

_descriptor = std::exchange (other._descriptor, nullptr);

_allocator = other._allocator;
_owner = std::exchange (other._owner, false);

Expand Down Expand Up @@ -199,6 +204,14 @@ namespace join

other.suspendAll ();

if (_descriptor != nullptr)
{
_descriptor->~IoDescriptor ();
_allocator->deallocate (_descriptor);
}

_descriptor = std::exchange (other._descriptor, nullptr);

if (_owner)
{
delete _allocator;
Expand Down Expand Up @@ -229,6 +242,12 @@ namespace join
{
close ();

if (_descriptor != nullptr)
{
_descriptor->~IoDescriptor ();
_allocator->deallocate (_descriptor);
}

if (_owner)
{
delete _allocator;
Expand Down Expand Up @@ -359,7 +378,7 @@ namespace join
}

wait->waitHandler = std::move (handler);
wait->op = IoOperation::makePoll (descriptor ().handle (), wantRead ? POLLIN : POLLOUT, this);
wait->op = IoOperation::makePoll (descriptor (), wantRead ? POLLIN : POLLOUT, this);
wait->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = _allocator->getIndex (wait);
Expand Down Expand Up @@ -405,7 +424,7 @@ namespace join
}

wait->waitHandler = std::move (handler);
wait->op = IoOperation::makePollMulti (descriptor ().handle (), wantRead ? POLLIN : POLLOUT, this);
wait->op = IoOperation::makePollMulti (descriptor (), wantRead ? POLLIN : POLLOUT, this);
wait->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = _allocator->getIndex (wait);
Expand Down Expand Up @@ -635,6 +654,17 @@ namespace join
return nullptr; // LCOV_EXCL_LINE
}

if (JOIN_UNLIKELY (_descriptor == nullptr))
{
void* slot = ops->allocate (sizeof (IoDescriptor));
if (JOIN_UNLIKELY (slot == nullptr))
{
return nullptr;
}

_descriptor = new (slot) IoDescriptor ();
}

void* chunk = ops->allocate (sizeof (Op));
if (JOIN_UNLIKELY (chunk == nullptr))
{
Expand Down Expand Up @@ -749,12 +779,12 @@ namespace join
*/
IoDescriptor& descriptor () noexcept
{
if (_descriptor.handle () != _socket.handle ())
if (_descriptor->handle () != _socket.handle ())
{
_descriptor.handle (_socket.handle ());
_descriptor->handle (_socket.handle ());
}

return _descriptor;
return *_descriptor;
}

/**
Expand Down Expand Up @@ -786,7 +816,7 @@ namespace join
Socket _socket;

/// socket descriptor.
IoDescriptor _descriptor;
IoDescriptor* _descriptor = nullptr;

/// operations allocator.
Allocator* _allocator = nullptr;
Expand All @@ -795,7 +825,7 @@ namespace join
bool _owner = false;

/// operations in flight.
std::array<std::atomic<IoOperation*>, _opCount> _ops{};
std::array<std::atomic<IoOperation*>, _slotCount> _ops{};
};
}

Expand Down
30 changes: 14 additions & 16 deletions core/include/join/async_stream_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace join
this->_socket._state = Socket::Connecting;
this->_socket._remote = endpoint;
connect->connectHandler = std::move (handler);
connect->op = IoOperation::makeConnect (this->descriptor ().handle (), this->_socket._remote.addr (),
connect->op = IoOperation::makeConnect (this->descriptor (), this->_socket._remote.addr (),
this->_socket._remote.length (), this);
connect->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

Expand Down Expand Up @@ -274,8 +274,8 @@ namespace join

disconnect->disconnectHandler = std::move (handler);
disconnect->deadline = deadline;
disconnect->op = IoOperation::makeRead (this->descriptor ().handle (), disconnect->buffer,
sizeof (disconnect->buffer), this);
disconnect->op =
IoOperation::makeRead (this->descriptor (), disconnect->buffer, sizeof (disconnect->buffer), this);
disconnect->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

if (this->_proactor->submit (disconnect->op, deadline, true, false) == -1)
Expand Down Expand Up @@ -352,7 +352,7 @@ namespace join
}

read->readHandler = std::move (handler);
read->op = IoOperation::makeRecv (this->descriptor ().handle (), data, maxSize, 0, this, link);
read->op = IoOperation::makeRecv (this->descriptor (), data, maxSize, 0, this, link);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -435,7 +435,7 @@ namespace join
read->iov.iov_base = data;
read->iov.iov_len = size;
read->deadline = deadline;
read->op = IoOperation::makeRecv (this->descriptor ().handle (), data, size, MSG_WAITALL, this, link);
read->op = IoOperation::makeRecv (this->descriptor (), data, size, MSG_WAITALL, this, link);
read->op.state.store (IoOperation::State::Submitted, std::memory_order_release);

size_t index = this->_allocator->getIndex (read);
Expand Down Expand Up @@ -519,18 +519,16 @@ namespace join
{
if (this->_proactor->fixedBuffer ().owns (data, size))
{
write->op =
IoOperation::makeSendZc (this->descriptor ().handle (), data, size, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSendZc (this->descriptor (), data, size, MSG_NOSIGNAL, this, link);
}
else
{
write->op =
IoOperation::makeSend (this->descriptor ().handle (), data, size, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSend (this->descriptor (), data, size, MSG_NOSIGNAL, this, link);
}
}
else
{
write->op = IoOperation::makeSend (this->descriptor ().handle (), data, size, MSG_NOSIGNAL, this, link);
write->op = IoOperation::makeSend (this->descriptor (), data, size, MSG_NOSIGNAL, this, link);
}

write->op.state.store (IoOperation::State::Submitted, std::memory_order_release);
Expand Down Expand Up @@ -620,19 +618,19 @@ namespace join
{
if (this->_proactor->fixedBuffer ().owns (data, size))
{
write->op = IoOperation::makeSendZc (this->descriptor ().handle (), data, size,
MSG_NOSIGNAL | MSG_WAITALL, this, link);
write->op = IoOperation::makeSendZc (this->descriptor (), data, size, MSG_NOSIGNAL | MSG_WAITALL,
this, link);
}
else
{
write->op = IoOperation::makeSend (this->descriptor ().handle (), data, size,
MSG_NOSIGNAL | MSG_WAITALL, this, link);
write->op =
IoOperation::makeSend (this->descriptor (), data, size, MSG_NOSIGNAL | MSG_WAITALL, this, link);
}
}
else
{
write->op = IoOperation::makeSend (this->descriptor ().handle (), data, size,
MSG_NOSIGNAL | MSG_WAITALL, this, link);
write->op =
IoOperation::makeSend (this->descriptor (), data, size, MSG_NOSIGNAL | MSG_WAITALL, this, link);
}

write->op.state.store (IoOperation::State::Submitted, std::memory_order_release);
Expand Down
Loading