Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method for user programs to be notified when a worker thread is started or stopped #1126

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/core/registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ MsQuicRegistrationOpen(
Registration,
WorkerThreadFlags,
Registration->NoPartitioning ? 1 : MsQuicLib.PartitionCount,
Config != NULL ? Config->WorkerStateHandler : NULL,
&Registration->WorkerPool);
if (QUIC_FAILED(Status)) {
goto Error;
Expand Down
13 changes: 12 additions & 1 deletion src/core/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ QuicWorkerInitialize(
_In_opt_ const void* Owner,
_In_ uint16_t ThreadFlags,
_In_ uint16_t IdealProcessor,
_In_opt_ QUIC_WORKER_CALLBACK_HANDLER WorkerStateHandler,
_Inout_ QUIC_WORKER* Worker
)
{
Expand All @@ -55,6 +56,7 @@ QuicWorkerInitialize(

Worker->Enabled = TRUE;
Worker->IdealProcessor = IdealProcessor;
Worker->WorkerStateHandler = WorkerStateHandler;
QuicDispatchLockInitialize(&Worker->Lock);
QuicEventInitialize(&Worker->Ready, FALSE, FALSE);
QuicListInitializeHead(&Worker->Connections);
Expand Down Expand Up @@ -557,6 +559,10 @@ QUIC_THREAD_CALLBACK(QuicWorkerThread, Context)
"[wrkr][%p] Start",
Worker);

if (Worker->WorkerStateHandler != NULL) {
Worker->WorkerStateHandler(QUIC_WORKER_STARTED);
}

//
// TODO - Review how often QuicTimeUs64() is called in the thread. Perhaps
// we can get it down to once per loop, passing the value along.
Expand Down Expand Up @@ -678,6 +684,10 @@ QUIC_THREAD_CALLBACK(QuicWorkerThread, Context)
}
QuicPerfCounterAdd(QUIC_PERF_COUNTER_WORK_OPER_QUEUE_DEPTH, Dequeue);

if (Worker->WorkerStateHandler != NULL) {
Worker->WorkerStateHandler(QUIC_WORKER_STOPPED);
}

QuicTraceEvent(
WorkerStop,
"[wrkr][%p] Stop",
Expand All @@ -691,6 +701,7 @@ QuicWorkerPoolInitialize(
_In_opt_ const void* Owner,
_In_ uint16_t ThreadFlags,
_In_ uint16_t WorkerCount,
_In_opt_ QUIC_WORKER_CALLBACK_HANDLER WorkerStateHandler,
_Out_ QUIC_WORKER_POOL** NewWorkerPool
)
{
Expand Down Expand Up @@ -718,7 +729,7 @@ QuicWorkerPoolInitialize(
//

for (uint16_t i = 0; i < WorkerCount; i++) {
Status = QuicWorkerInitialize(Owner, ThreadFlags, i, &WorkerPool->Workers[i]);
Status = QuicWorkerInitialize(Owner, ThreadFlags, i, WorkerStateHandler, &WorkerPool->Workers[i]);
if (QUIC_FAILED(Status)) {
for (uint16_t j = 0; j < i; j++) {
QuicWorkerUninitialize(&WorkerPool->Workers[j]);
Expand Down
3 changes: 3 additions & 0 deletions src/core/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ typedef struct QUIC_CACHEALIGN QUIC_WORKER {
QUIC_POOL StatelessContextPool; // QUIC_STATELESS_CONTEXT
QUIC_POOL OperPool; // QUIC_OPERATION

QUIC_WORKER_CALLBACK_HANDLER WorkerStateHandler;

} QUIC_WORKER;

//
Expand Down Expand Up @@ -123,6 +125,7 @@ QuicWorkerPoolInitialize(
_In_opt_ const void* Owner,
_In_ uint16_t ThreadFlags,
_In_ uint16_t WorkerCount,
_In_opt_ QUIC_WORKER_CALLBACK_HANDLER WorkerStateHandler,
_Out_ QUIC_WORKER_POOL** WorkerPool
);

Expand Down
21 changes: 21 additions & 0 deletions src/inc/msquic.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,31 @@ typedef enum QUIC_DATAGRAM_SEND_STATE {
#define QUIC_DATAGRAM_SEND_STATE_IS_FINAL(State) \
((State) >= QUIC_DATAGRAM_SEND_LOST_DISCARDED)

//
// Event type for worker thread starting and stopping.
//
typedef enum QUIC_WORKER_EVENT_TYPE {
QUIC_WORKER_STARTED = 0,
QUIC_WORKER_STOPPED = 1
} QUIC_WORKER_EVENT_TYPE;

//
// Callback handler for worker thread starting and stopping.
//
typedef
_IRQL_requires_max_(PASSIVE_LEVEL)
_Function_class_(QUIC_WORKER_CALLBACK)
void
(QUIC_API QUIC_WORKER_CALLBACK)(
_In_ QUIC_WORKER_EVENT_TYPE EventType
);

typedef QUIC_WORKER_CALLBACK *QUIC_WORKER_CALLBACK_HANDLER;

typedef struct QUIC_REGISTRATION_CONFIG { // All fields may be NULL/zero.
const char* AppName;
QUIC_EXECUTION_PROFILE ExecutionProfile;
QUIC_WORKER_CALLBACK_HANDLER WorkerStateHandler;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can make this non-breaking by putting it behind a "custom" execution profile flag. Though, still I don't believe it should be architected this way.

} QUIC_REGISTRATION_CONFIG;

typedef
Expand Down