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