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