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