From 6883f9a04fe922015ef91769e8ab3b5ef5a78a17 Mon Sep 17 00:00:00 2001 From: Vladimir Polin Date: Tue, 23 Dec 2025 21:43:15 -0800 Subject: [PATCH] Fix for redundant thread contention in CsdNextMessage --- src/conv-core/convcore.C | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/conv-core/convcore.C b/src/conv-core/convcore.C index 40d0fb33c3..bebde9317a 100644 --- a/src/conv-core/convcore.C +++ b/src/conv-core/convcore.C @@ -1766,14 +1766,18 @@ void *CsdNextMessage(CsdSchedulerState_t *s) { /*#warning "CsdNextMessage: CMK_NODE_QUEUE_AVAILABLE" */ if (NULL!=(msg=CmiGetNonLocalNodeQ())) return msg; #if !CMK_NO_MSG_PRIOS - if(CmiTryLock(s->nodeLock) == 0) { - if (!CqsEmpty(s->nodeQ) - && CqsPrioGT(CqsGetPriority(s->schedQ), - CqsGetPriority(s->nodeQ))) { - CqsDequeue(s->nodeQ,(void **)&msg); + /* Check the node queue lock-free if we have anything to work with. */ + /* This significantly reduces redundant thread contention in CmiTryLock(). */ + if (!CqsEmpty(s->nodeQ)) { + if (CmiTryLock(s->nodeLock) == 0) { + if (!CqsEmpty(s->nodeQ) + && CqsPrioGT(CqsGetPriority(s->schedQ), + CqsGetPriority(s->nodeQ))) { + CqsDequeue(s->nodeQ,(void **)&msg); + } + CmiUnlock(s->nodeLock); + if (msg!=NULL) return msg; } - CmiUnlock(s->nodeLock); - if (msg!=NULL) return msg; } #endif #endif