Track writer request id
This commit is contained in:
parent
f2cf82bccd
commit
68d6c44e06
2 changed files with 17 additions and 11 deletions
|
|
@ -66,7 +66,9 @@ nano::store::write_guard nano::store::write_queue::wait (writer writer)
|
|||
bool nano::store::write_queue::contains (writer writer) const
|
||||
{
|
||||
nano::lock_guard<nano::mutex> guard{ mutex };
|
||||
return std::find (queue.cbegin (), queue.cend (), writer) != queue.cend ();
|
||||
return std::any_of (queue.cbegin (), queue.cend (), [writer] (auto const & item) {
|
||||
return item.first == writer;
|
||||
});
|
||||
}
|
||||
|
||||
void nano::store::write_queue::pop ()
|
||||
|
|
@ -83,17 +85,19 @@ void nano::store::write_queue::acquire (writer writer)
|
|||
{
|
||||
nano::unique_lock<nano::mutex> lock{ mutex };
|
||||
|
||||
// There should be no duplicates in the queue
|
||||
debug_assert (std::none_of (queue.cbegin (), queue.cend (), [writer] (auto const & item) { return item == writer; }));
|
||||
// There should be no duplicates in the queue (exception is testing)
|
||||
debug_assert (std::none_of (queue.cbegin (), queue.cend (), [writer] (auto const & item) {
|
||||
return item.first == writer;
|
||||
})
|
||||
|| writer == writer::testing);
|
||||
|
||||
auto const id = next++;
|
||||
|
||||
// Add writer to the end of the queue if it's not already waiting
|
||||
auto exists = std::find (queue.cbegin (), queue.cend (), writer) != queue.cend ();
|
||||
if (!exists)
|
||||
{
|
||||
queue.push_back (writer);
|
||||
}
|
||||
queue.push_back ({ writer, id });
|
||||
|
||||
condition.wait (lock, [&] () { return queue.front () == writer; });
|
||||
// Wait until we are at the front of the queue
|
||||
condition.wait (lock, [&] () { return queue.front ().second == id; });
|
||||
}
|
||||
|
||||
void nano::store::write_queue::release (writer writer)
|
||||
|
|
@ -101,7 +105,7 @@ void nano::store::write_queue::release (writer writer)
|
|||
{
|
||||
nano::lock_guard<nano::mutex> guard{ mutex };
|
||||
release_assert (!queue.empty ());
|
||||
release_assert (queue.front () == writer);
|
||||
release_assert (queue.front ().first == writer);
|
||||
queue.pop_front ();
|
||||
}
|
||||
condition.notify_all ();
|
||||
|
|
|
|||
|
|
@ -70,7 +70,9 @@ private:
|
|||
void release (writer writer);
|
||||
|
||||
private:
|
||||
std::deque<writer> queue;
|
||||
uint64_t next{ 0 };
|
||||
using entry = std::pair<writer, uint64_t>; // uint64_t is a unique id for each write_guard
|
||||
std::deque<entry> queue;
|
||||
mutable nano::mutex mutex;
|
||||
nano::condition_variable condition;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue