Skip to content

Commit

Permalink
Use a binary semaphore instead of a mutex to protect work queue reads
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed May 27, 2024
1 parent 2f7038c commit 1c937f1
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/ammonite/core/threadManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <atomic>
#include <cstring>
#include <mutex>
#include <semaphore>
#include <thread>

#include "../types.hpp"
Expand All @@ -23,7 +23,7 @@ namespace {

class WorkQueue {
private:
std::mutex readLock;
std::binary_semaphore readSemaphore{1};
Node* nextPopped;
std::atomic<Node*> nextPushed;

Expand Down Expand Up @@ -95,18 +95,18 @@ namespace {

void pop(WorkItem* workItemPtr) {
//Use the most recently popped node to find the next
readLock.lock();
readSemaphore.acquire();

//Copy the data and free the old node, otherwise return if we don't have a new node
Node* currentNode = nextPopped;
if (currentNode->nextNode != nullptr) {
nextPopped = nextPopped->nextNode;
readLock.unlock();
readSemaphore.release();

*workItemPtr = currentNode->workItem;
delete currentNode;
} else {
readLock.unlock();
readSemaphore.release();
workItemPtr->work = nullptr;
}
}
Expand Down

0 comments on commit 1c937f1

Please sign in to comment.