Skip to content

Commit

Permalink
Upon epoll notification for reads and writes to a socket, queue read …
Browse files Browse the repository at this point in the history
…work and process write work in same thread
  • Loading branch information
kouvel committed Apr 23, 2020
1 parent 27992a1 commit 46dc10d
Showing 1 changed file with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,9 @@ public bool StartAsyncOperation(SocketAsyncContext context, TOperation operation
}
}

// Called on the epoll thread whenever we receive an epoll notification.
public void HandleEvent(SocketAsyncContext context)
/// <returns>An operation that should be processed, or null if no further processing is required</returns>
public AsyncOperation? HandleEvent(SocketAsyncContext context)
{
AsyncOperation op;
using (Lock())
{
Trace(context, $"Enter");
Expand All @@ -843,34 +842,29 @@ public void HandleEvent(SocketAsyncContext context)
Debug.Assert(_tail == null, "State == Ready but queue is not empty!");
_sequenceNumber++;
Trace(context, $"Exit (previously ready)");
return;
return null;

case QueueState.Waiting:
Debug.Assert(_tail != null, "State == Waiting but queue is empty!");
_state = QueueState.Processing;
op = _tail.Next;
// Break out and release lock
break;
return _tail.Next;

case QueueState.Processing:
Debug.Assert(_tail != null, "State == Processing but queue is empty!");
_sequenceNumber++;
Trace(context, $"Exit (currently processing)");
return;
return null;

case QueueState.Stopped:
Debug.Assert(_tail == null);
Trace(context, $"Exit (stopped)");
return;
return null;

default:
Environment.FailFast("unexpected queue state");
return;
return null;
}
}

// Dispatch the op so we can try to process it.
op.Dispatch(inlineAsync: true);
}

internal void ProcessAsyncOperation(TOperation op)
Expand Down Expand Up @@ -1959,14 +1953,18 @@ public unsafe void HandleEvents(Interop.Sys.SocketEvents events)
events |= Interop.Sys.SocketEvents.Read | Interop.Sys.SocketEvents.Write;
}

if ((events & Interop.Sys.SocketEvents.Read) != 0)
AsyncOperation? receiveOperation =
(events & Interop.Sys.SocketEvents.Read) != 0 ? _receiveQueue.HandleEvent(this) : null;
AsyncOperation? sendOperation =
(events & Interop.Sys.SocketEvents.Write) != 0 ? _sendQueue.HandleEvent(this) : null;
if (sendOperation == null)
{
_receiveQueue.HandleEvent(this);
receiveOperation?.Dispatch(inlineAsync: true);
}

if ((events & Interop.Sys.SocketEvents.Write) != 0)
else
{
_sendQueue.HandleEvent(this);
receiveOperation?.Dispatch(inlineAsync: false);
sendOperation.Dispatch(inlineAsync: true);
}
}

Expand Down

0 comments on commit 46dc10d

Please sign in to comment.