From 71c9ff0d916ebe81bb9c7d0f50545d1364ce551c Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 15 May 2021 21:31:45 -0500 Subject: [PATCH] Elide the wait if the queue head is already non-null --- src/LinkedBlockingQueue.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/LinkedBlockingQueue.c b/src/LinkedBlockingQueue.c index 4d8ab26..04a8734 100644 --- a/src/LinkedBlockingQueue.c +++ b/src/LinkedBlockingQueue.c @@ -184,9 +184,14 @@ int LbqWaitForQueueElement(PLINKED_BLOCKING_QUEUE queueHead, void** data) { } for (;;) { - err = PltWaitForEvent(&queueHead->containsDataEvent); - if (err != PLT_WAIT_SUCCESS) { - return LBQ_INTERRUPTED; + // We can also avoid a syscall if the head pointer is non-NULL. + // It's not safe to dereference the value due to potential memory + // ordering hazards, but we can use its presence to elide the wait. + if (queueHead->head == NULL) { + err = PltWaitForEvent(&queueHead->containsDataEvent); + if (err != PLT_WAIT_SUCCESS) { + return LBQ_INTERRUPTED; + } } if (queueHead->shutdown) {