Skip to content

Commit 7b87154

Browse files
committed
Async: Manually activate write requests with same handle on Posix
1 parent 1eb28c7 commit 7b87154

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Libraries/Async/Internal/AsyncPosix.inl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
637637
{
638638
if (handle == current->handle and (current->flags & Internal::Flag_WatcherSet))
639639
{
640-
current->flags |= Internal::Flag_WatcherSet;
641640
return Result(KernelQueuePosix::startSingleWatcherImmediate(*current->eventLoop, current->handle,
642641
OUTPUT_EVENTS_MASK));
643642
}
@@ -735,15 +734,16 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
735734
SC_ASSERT_RELEASE((async.flags & Internal::Flag_ManualCompletion) == 0);
736735
if (not posixTryWrite(async, totalBytesToSend, offset))
737736
{
738-
// Not all bytes have been written
737+
// Not all bytes have been written, so if descriptor supports watching
738+
// start monitoring it, otherwise just return error
739739
if (watchable)
740740
{
741-
// setup a writable state watcher for this handle
742741
async.flags |= Internal::Flag_WatcherSet;
743742
return Result(setEventWatcher(async, async.handle, OUTPUT_EVENTS_MASK));
744743
}
745744
return Result::Error("Error in posixTryWrite");
746745
}
746+
// Write has finished synchronously so force a manual invocation of its completion
747747
async.flags |= Internal::Flag_ManualCompletion;
748748
return Result(true);
749749
}
@@ -776,6 +776,7 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
776776
static Result posixWriteManualActivateWithSameHandle(T& async, T* current)
777777
{
778778
// Activate all asyncs on the same socket descriptor too
779+
// TODO: This linear search is not great
779780
while (current)
780781
{
781782
if (current->handle == async.handle)
@@ -784,6 +785,7 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
784785
async.flags |= Internal::Flag_ManualCompletion;
785786
async.eventLoop->internal.manualCompletions.queueBack(*current);
786787
}
788+
current = static_cast<T*>(current->next);
787789
}
788790
return Result(true);
789791
}
@@ -808,9 +810,9 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
808810

809811
static Result completeAsync(AsyncSocketSend::Result& result)
810812
{
813+
AsyncSocketSend& async = result.getAsync();
811814
SC_TRY(posixWriteCompleteAsync<AsyncSocketSend>(result, -1));
812-
return posixWriteManualActivateWithSameHandle(result.getAsync(),
813-
result.getAsync().eventLoop->internal.activeSocketSends.front);
815+
return posixWriteManualActivateWithSameHandle(async, async.eventLoop->internal.activeSocketSends.front);
814816
}
815817

816818
//-------------------------------------------------------------------------------------------------------
@@ -947,14 +949,17 @@ struct SC::AsyncEventLoop::Internal::KernelEventsPosix
947949

948950
static Result completeAsync(AsyncFileWrite::Result& result)
949951
{
950-
AsyncFileWrite& async = result.getAsync();
951-
return posixWriteCompleteAsync<AsyncFileWrite>(result, async.useOffset ? static_cast<off_t>(async.offset) : -1);
952+
AsyncFileWrite& async = result.getAsync();
953+
const off_t offset = async.useOffset ? static_cast<off_t>(async.offset) : -1;
954+
SC_TRY(posixWriteCompleteAsync<AsyncFileWrite>(result, offset));
955+
return posixWriteManualActivateWithSameHandle(async, async.eventLoop->internal.activeFileWrites.front);
952956
}
953957

954958
static Result executeOperation(AsyncFileWrite& async, AsyncFileWrite::CompletionData& completionData)
955959
{
956960
const size_t totalBytesToSend = Internal::getSummedSizeOfBuffers(async);
957-
SC_TRY((posixTryWrite(async, totalBytesToSend, async.useOffset ? static_cast<off_t>(async.offset) : -1)));
961+
const off_t offset = async.useOffset ? static_cast<off_t>(async.offset) : -1;
962+
SC_TRY(posixTryWrite(async, totalBytesToSend, offset));
958963
completionData.numBytes = async.totalBytesWritten;
959964
SC_TRY_MSG(completionData.numBytes == totalBytesToSend, "Partial write (disk full or RLIMIT_FSIZE reached)");
960965
return Result(true);

0 commit comments

Comments
 (0)