paul@93 | 1 | /* |
paul@93 | 2 | * A page queue whose users take turns to access pages. |
paul@93 | 3 | * |
paul@93 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@70 | 22 | #include "page_queue_shared.h" |
paul@6 | 23 | |
paul@74 | 24 | |
paul@74 | 25 | |
paul@74 | 26 | /* Discard all queued flexpages. */ |
paul@74 | 27 | |
paul@74 | 28 | void PageQueueShared::close(Memory *memory) |
paul@74 | 29 | { |
paul@74 | 30 | discard(_queue, memory); |
paul@74 | 31 | } |
paul@74 | 32 | |
paul@6 | 33 | /* Keep waiting for a potential queue non-empty condition. |
paul@6 | 34 | Then, attempt to pop an entry from the queue. */ |
paul@6 | 35 | |
paul@70 | 36 | void PageQueueShared::pop(PageOwner **owner, Flexpage **flexpage) |
paul@6 | 37 | { |
paul@6 | 38 | std::unique_lock<std::mutex> guard(_lock); |
paul@6 | 39 | QueueEntry entry; |
paul@6 | 40 | |
paul@6 | 41 | while (1) |
paul@6 | 42 | { |
paul@6 | 43 | if (_pop(&entry)) |
paul@6 | 44 | { |
paul@6 | 45 | *owner = entry.owner; |
paul@6 | 46 | *flexpage = entry.flexpage; |
paul@6 | 47 | return; |
paul@6 | 48 | } |
paul@6 | 49 | else |
paul@6 | 50 | _counter.wait(guard); |
paul@6 | 51 | } |
paul@6 | 52 | } |
paul@6 | 53 | |
paul@6 | 54 | /* Check the queue for entries, returning false if no entries are available, |
paul@6 | 55 | returning true and providing the details if an entry can be removed from the |
paul@6 | 56 | front of the queue. */ |
paul@6 | 57 | |
paul@70 | 58 | bool PageQueueShared::_pop(QueueEntry *entry) |
paul@6 | 59 | { |
paul@6 | 60 | if (_queue.empty()) |
paul@6 | 61 | return false; |
paul@6 | 62 | |
paul@6 | 63 | *entry = _queue.front(); |
paul@6 | 64 | _queue.pop_front(); |
paul@6 | 65 | |
paul@6 | 66 | /* Remove any position reference for the flexpage. */ |
paul@6 | 67 | |
paul@6 | 68 | Positions::iterator position = _positions.find(entry->flexpage); |
paul@6 | 69 | |
paul@6 | 70 | if (position != _positions.end()) |
paul@6 | 71 | _positions.erase(position); |
paul@6 | 72 | |
paul@6 | 73 | return true; |
paul@6 | 74 | } |
paul@6 | 75 | |
paul@6 | 76 | /* Push an entry for the given owner and flexpage to the queue. */ |
paul@6 | 77 | |
paul@70 | 78 | void PageQueueShared::push(PageOwner *owner, Flexpage *flexpage) |
paul@6 | 79 | { |
paul@6 | 80 | std::lock_guard<std::mutex> guard(_lock); |
paul@6 | 81 | |
paul@6 | 82 | /* Record the entry and a position reference for the flexpage. */ |
paul@6 | 83 | |
paul@6 | 84 | _queue.push_back((QueueEntry) {flexpage, owner}); |
paul@6 | 85 | |
paul@6 | 86 | Queue::iterator last = _queue.end(); |
paul@6 | 87 | last--; |
paul@6 | 88 | _positions.insert(Position(flexpage, last)); |
paul@6 | 89 | |
paul@6 | 90 | _counter.notify_one(); |
paul@6 | 91 | } |
paul@6 | 92 | |
paul@6 | 93 | /* Push an entry to the front of the queue. */ |
paul@6 | 94 | |
paul@70 | 95 | void PageQueueShared::push_front(PageOwner *owner, Flexpage *flexpage) |
paul@6 | 96 | { |
paul@6 | 97 | std::lock_guard<std::mutex> guard(_lock); |
paul@6 | 98 | |
paul@6 | 99 | _queue.push_back((QueueEntry) {flexpage, owner}); |
paul@6 | 100 | _positions.insert(Position(flexpage, _queue.begin())); |
paul@6 | 101 | |
paul@6 | 102 | _counter.notify_one(); |
paul@6 | 103 | } |
paul@6 | 104 | |
paul@6 | 105 | /* Remove an entry for the given owner and flexpage from the queue. */ |
paul@6 | 106 | |
paul@70 | 107 | bool PageQueueShared::remove(PageOwner *owner, Flexpage *flexpage) |
paul@6 | 108 | { |
paul@74 | 109 | return PageQueue::remove(_queue, _positions, owner, flexpage); |
paul@6 | 110 | } |
paul@6 | 111 | |
paul@6 | 112 | // vim: tabstop=4 expandtab shiftwidth=4 |