1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libfsserver/lib/pages/page_queue_partitioned.cc Thu Apr 15 23:15:17 2021 +0200
1.3 @@ -0,0 +1,133 @@
1.4 +/*
1.5 + * A page queue retaining two internal collections of memory pages.
1.6 + *
1.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include "page_queue_partitioned.h"
1.26 +
1.27 +
1.28 +
1.29 +/* Discard all queued flexpages. */
1.30 +
1.31 +void PageQueuePartitioned::close(Memory *memory)
1.32 +{
1.33 + discard(_available, memory);
1.34 + discard(_issued, memory);
1.35 +}
1.36 +
1.37 +/* Keep waiting for a potential queue non-empty condition.
1.38 + Then, attempt to pop an entry from the queue. */
1.39 +
1.40 +void PageQueuePartitioned::pop(PageOwner **owner, Flexpage **flexpage)
1.41 +{
1.42 + std::unique_lock<std::mutex> guard(_lock);
1.43 + QueueEntry entry;
1.44 +
1.45 + while (1)
1.46 + {
1.47 + if (_pop(&entry))
1.48 + {
1.49 + *owner = entry.owner;
1.50 + *flexpage = entry.flexpage;
1.51 + return;
1.52 + }
1.53 + else
1.54 + _counter.wait(guard);
1.55 + }
1.56 +}
1.57 +
1.58 +/* Check the available pages queue for entries, returning false if no entries
1.59 + are available, returning true and providing the details if an entry can be
1.60 + removed from the front of the queue. */
1.61 +
1.62 +bool PageQueuePartitioned::_pop(QueueEntry *entry)
1.63 +{
1.64 + if (_available.empty())
1.65 + return false;
1.66 +
1.67 + *entry = _available.front();
1.68 + _available.pop_front();
1.69 +
1.70 + return true;
1.71 +}
1.72 +
1.73 +/* Push an entry for the given owner and flexpage to the appropriate queue. */
1.74 +
1.75 +void PageQueuePartitioned::push(PageOwner *owner, Flexpage *flexpage)
1.76 +{
1.77 + std::lock_guard<std::mutex> guard(_lock);
1.78 +
1.79 + /* Record the entry and a position reference for the flexpage. */
1.80 +
1.81 + Queue *queue;
1.82 + Positions *positions = NULL;
1.83 +
1.84 + if (owner == NULL)
1.85 + queue = &_available;
1.86 + else
1.87 + {
1.88 + queue = &_issued;
1.89 + positions = &_positions;
1.90 + }
1.91 +
1.92 + queue->push_back((QueueEntry) {flexpage, owner});
1.93 +
1.94 + if (positions != NULL)
1.95 + {
1.96 + Queue::iterator last = queue->end();
1.97 + last--;
1.98 + positions->insert(Position(flexpage, last));
1.99 + }
1.100 +
1.101 + _counter.notify_one();
1.102 +}
1.103 +
1.104 +/* Push an entry to the front of the appropriate queue. */
1.105 +
1.106 +void PageQueuePartitioned::push_front(PageOwner *owner, Flexpage *flexpage)
1.107 +{
1.108 + std::lock_guard<std::mutex> guard(_lock);
1.109 +
1.110 + Queue *queue;
1.111 + Positions *positions = NULL;
1.112 +
1.113 + if (owner == NULL)
1.114 + queue = &_available;
1.115 + else
1.116 + {
1.117 + queue = &_issued;
1.118 + positions = &_positions;
1.119 + }
1.120 +
1.121 + queue->push_back((QueueEntry) {flexpage, owner});
1.122 +
1.123 + if (positions != NULL)
1.124 + positions->insert(Position(flexpage, queue->begin()));
1.125 +
1.126 + _counter.notify_one();
1.127 +}
1.128 +
1.129 +/* Remove an entry for the given owner and flexpage from the queue. */
1.130 +
1.131 +bool PageQueuePartitioned::remove(PageOwner *owner, Flexpage *flexpage)
1.132 +{
1.133 + return PageQueue::remove(_issued, _positions, owner, flexpage);
1.134 +}
1.135 +
1.136 +// vim: tabstop=4 expandtab shiftwidth=4