paul@6 | 1 | #include "page_queue.h" |
paul@6 | 2 | |
paul@6 | 3 | /* Keep waiting for a potential queue non-empty condition. |
paul@6 | 4 | Then, attempt to pop an entry from the queue. */ |
paul@6 | 5 | |
paul@6 | 6 | void PageQueue::pop(PageOwner **owner, Flexpage **flexpage) |
paul@6 | 7 | { |
paul@6 | 8 | std::unique_lock<std::mutex> guard(_lock); |
paul@6 | 9 | QueueEntry entry; |
paul@6 | 10 | |
paul@6 | 11 | while (1) |
paul@6 | 12 | { |
paul@6 | 13 | if (_pop(&entry)) |
paul@6 | 14 | { |
paul@6 | 15 | *owner = entry.owner; |
paul@6 | 16 | *flexpage = entry.flexpage; |
paul@6 | 17 | return; |
paul@6 | 18 | } |
paul@6 | 19 | else |
paul@6 | 20 | _counter.wait(guard); |
paul@6 | 21 | } |
paul@6 | 22 | } |
paul@6 | 23 | |
paul@6 | 24 | /* Check the queue for entries, returning false if no entries are available, |
paul@6 | 25 | returning true and providing the details if an entry can be removed from the |
paul@6 | 26 | front of the queue. */ |
paul@6 | 27 | |
paul@6 | 28 | bool PageQueue::_pop(QueueEntry *entry) |
paul@6 | 29 | { |
paul@6 | 30 | if (_queue.empty()) |
paul@6 | 31 | return false; |
paul@6 | 32 | |
paul@6 | 33 | *entry = _queue.front(); |
paul@6 | 34 | _queue.pop_front(); |
paul@6 | 35 | |
paul@6 | 36 | /* Remove any position reference for the flexpage. */ |
paul@6 | 37 | |
paul@6 | 38 | Positions::iterator position = _positions.find(entry->flexpage); |
paul@6 | 39 | |
paul@6 | 40 | if (position != _positions.end()) |
paul@6 | 41 | _positions.erase(position); |
paul@6 | 42 | |
paul@6 | 43 | return true; |
paul@6 | 44 | } |
paul@6 | 45 | |
paul@6 | 46 | /* Push an entry for the given owner and flexpage to the queue. */ |
paul@6 | 47 | |
paul@6 | 48 | void PageQueue::push(PageOwner *owner, Flexpage *flexpage) |
paul@6 | 49 | { |
paul@6 | 50 | std::lock_guard<std::mutex> guard(_lock); |
paul@6 | 51 | |
paul@6 | 52 | /* Record the entry and a position reference for the flexpage. */ |
paul@6 | 53 | |
paul@6 | 54 | _queue.push_back((QueueEntry) {flexpage, owner}); |
paul@6 | 55 | |
paul@6 | 56 | Queue::iterator last = _queue.end(); |
paul@6 | 57 | last--; |
paul@6 | 58 | _positions.insert(Position(flexpage, last)); |
paul@6 | 59 | |
paul@6 | 60 | _counter.notify_one(); |
paul@6 | 61 | } |
paul@6 | 62 | |
paul@6 | 63 | /* Push an entry to the front of the queue. */ |
paul@6 | 64 | |
paul@6 | 65 | void PageQueue::push_front(PageOwner *owner, Flexpage *flexpage) |
paul@6 | 66 | { |
paul@6 | 67 | std::lock_guard<std::mutex> guard(_lock); |
paul@6 | 68 | |
paul@6 | 69 | _queue.push_back((QueueEntry) {flexpage, owner}); |
paul@6 | 70 | _positions.insert(Position(flexpage, _queue.begin())); |
paul@6 | 71 | |
paul@6 | 72 | _counter.notify_one(); |
paul@6 | 73 | } |
paul@6 | 74 | |
paul@6 | 75 | /* Remove an entry for the given owner and flexpage from the queue. */ |
paul@6 | 76 | |
paul@6 | 77 | bool PageQueue::remove(PageOwner *owner, Flexpage *flexpage) |
paul@6 | 78 | { |
paul@6 | 79 | std::lock_guard<std::mutex> guard(_lock); |
paul@6 | 80 | |
paul@6 | 81 | Positions::iterator position = _positions.find(flexpage); |
paul@6 | 82 | |
paul@6 | 83 | if (position == _positions.end()) |
paul@6 | 84 | return false; |
paul@6 | 85 | |
paul@6 | 86 | /* The found owner may be different from the requesting owner or even NULL |
paul@6 | 87 | if another owner has acquired and then purged its pages. Such a purged |
paul@6 | 88 | flexpage is not immediately usable, however. */ |
paul@6 | 89 | |
paul@6 | 90 | Queue::iterator entry = position->second; |
paul@6 | 91 | |
paul@6 | 92 | if ((entry->owner == NULL) || (entry->owner != owner)) |
paul@6 | 93 | return false; |
paul@6 | 94 | |
paul@6 | 95 | _queue.erase(entry); |
paul@6 | 96 | _positions.erase(position); |
paul@6 | 97 | |
paul@6 | 98 | return true; |
paul@6 | 99 | } |
paul@6 | 100 | |
paul@6 | 101 | // vim: tabstop=4 expandtab shiftwidth=4 |