1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/page_queue_shared.cc Sat Mar 27 00:55:05 2021 +0100
1.3 @@ -0,0 +1,101 @@
1.4 +#include "page_queue_shared.h"
1.5 +
1.6 +/* Keep waiting for a potential queue non-empty condition.
1.7 + Then, attempt to pop an entry from the queue. */
1.8 +
1.9 +void PageQueueShared::pop(PageOwner **owner, Flexpage **flexpage)
1.10 +{
1.11 + std::unique_lock<std::mutex> guard(_lock);
1.12 + QueueEntry entry;
1.13 +
1.14 + while (1)
1.15 + {
1.16 + if (_pop(&entry))
1.17 + {
1.18 + *owner = entry.owner;
1.19 + *flexpage = entry.flexpage;
1.20 + return;
1.21 + }
1.22 + else
1.23 + _counter.wait(guard);
1.24 + }
1.25 +}
1.26 +
1.27 +/* Check the queue for entries, returning false if no entries are available,
1.28 + returning true and providing the details if an entry can be removed from the
1.29 + front of the queue. */
1.30 +
1.31 +bool PageQueueShared::_pop(QueueEntry *entry)
1.32 +{
1.33 + if (_queue.empty())
1.34 + return false;
1.35 +
1.36 + *entry = _queue.front();
1.37 + _queue.pop_front();
1.38 +
1.39 + /* Remove any position reference for the flexpage. */
1.40 +
1.41 + Positions::iterator position = _positions.find(entry->flexpage);
1.42 +
1.43 + if (position != _positions.end())
1.44 + _positions.erase(position);
1.45 +
1.46 + return true;
1.47 +}
1.48 +
1.49 +/* Push an entry for the given owner and flexpage to the queue. */
1.50 +
1.51 +void PageQueueShared::push(PageOwner *owner, Flexpage *flexpage)
1.52 +{
1.53 + std::lock_guard<std::mutex> guard(_lock);
1.54 +
1.55 + /* Record the entry and a position reference for the flexpage. */
1.56 +
1.57 + _queue.push_back((QueueEntry) {flexpage, owner});
1.58 +
1.59 + Queue::iterator last = _queue.end();
1.60 + last--;
1.61 + _positions.insert(Position(flexpage, last));
1.62 +
1.63 + _counter.notify_one();
1.64 +}
1.65 +
1.66 +/* Push an entry to the front of the queue. */
1.67 +
1.68 +void PageQueueShared::push_front(PageOwner *owner, Flexpage *flexpage)
1.69 +{
1.70 + std::lock_guard<std::mutex> guard(_lock);
1.71 +
1.72 + _queue.push_back((QueueEntry) {flexpage, owner});
1.73 + _positions.insert(Position(flexpage, _queue.begin()));
1.74 +
1.75 + _counter.notify_one();
1.76 +}
1.77 +
1.78 +/* Remove an entry for the given owner and flexpage from the queue. */
1.79 +
1.80 +bool PageQueueShared::remove(PageOwner *owner, Flexpage *flexpage)
1.81 +{
1.82 + std::lock_guard<std::mutex> guard(_lock);
1.83 +
1.84 + Positions::iterator position = _positions.find(flexpage);
1.85 +
1.86 + if (position == _positions.end())
1.87 + return false;
1.88 +
1.89 + /* The found owner may be different from the requesting owner or even NULL
1.90 + if another owner has acquired and then purged its pages. Such a purged
1.91 + flexpage is not immediately usable, however. */
1.92 +
1.93 + Queue::iterator entry = position->second;
1.94 +
1.95 + if ((entry->owner == NULL) || (entry->owner != owner))
1.96 + return false;
1.97 +
1.98 + _queue.erase(entry);
1.99 + _positions.erase(position);
1.100 +
1.101 + return true;
1.102 +}
1.103 +
1.104 +// vim: tabstop=4 expandtab shiftwidth=4