Skip to content

Commit

Permalink
Add notify
Browse files Browse the repository at this point in the history
  • Loading branch information
mjp41 committed Jun 20, 2024
1 parent b8f4f10 commit d1f5f53
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/snmalloc/ds/combininglock.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <atomic>
#include <functional>

#define SNMALLOC_USE_NOTIFY

namespace snmalloc
{
class CombineLockNode;
Expand Down Expand Up @@ -52,6 +54,15 @@ namespace snmalloc
// Stores the C++ lambda associated with this node in the queue.
void (*f_raw)(CombineLockNode*);

void set_status(LockStatus s)
{
status.store(s, std::memory_order_release);
#ifdef SNMALLOC_USE_NOTIFY
status.notify_one();
#endif
}


public:
constexpr CombineLockNode(void (*f)(CombineLockNode*)) : f_raw(f) {}

Expand All @@ -66,9 +77,12 @@ namespace snmalloc
prev->next = this;

// Wait to for predecessor to complete
#ifdef SNMALLOC_USE_NOTIFY
status.wait(LockStatus::WAITING);
#else
while (status.load() == LockStatus::WAITING)
Aal::pause();

#endif
// Determine if another thread completed our work.
if (status.load() == LockStatus::DONE)
return;
Expand All @@ -92,7 +106,7 @@ namespace snmalloc
{
// Signal this work was completed and move on to
// next item.
curr->status = LockStatus::DONE;
curr->set_status(LockStatus::DONE);
curr = n;
continue;
}
Expand All @@ -104,7 +118,7 @@ namespace snmalloc
{
// Queue was successfully closed.
// Notify last element the work was completed.
curr->status = LockStatus::DONE;
curr->set_status(LockStatus::DONE);
return;
}

Expand All @@ -115,12 +129,12 @@ namespace snmalloc

// As we had to wait, give the job to the next thread
// to carry on performing the work.
curr->next.load()->status = LockStatus::READY;
curr->next.load()->set_status(LockStatus::READY);

// Notify the thread that we completed its work.
// Note that this needs to be done last, as we can't read
// curr->next after setting curr->status
curr->status = LockStatus::DONE;
curr->set_status(LockStatus::DONE);
return;
}
}
Expand Down

0 comments on commit d1f5f53

Please sign in to comment.