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