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