paul@93 | 1 | /* |
paul@93 | 2 | * A pipe paging coordinator, permitting memory sharing pipe endpoints. |
paul@93 | 3 | * |
paul@544 | 4 | * Copyright (C) 2021, 2023 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@65 | 22 | #pragma once |
paul@65 | 23 | |
paul@544 | 24 | #include <mutex> |
paul@130 | 25 | #include <set> |
paul@130 | 26 | |
paul@133 | 27 | #include <fsserver/notification.h> |
paul@94 | 28 | #include <fsserver/page_mapper.h> |
paul@94 | 29 | #include <fsserver/pages.h> |
paul@94 | 30 | #include <fsserver/pipe_accessor.h> |
paul@65 | 31 | |
paul@93 | 32 | |
paul@93 | 33 | |
paul@65 | 34 | /* Pipe paging support, maintaining the sequence of active regions or sections |
paul@65 | 35 | in a pipe. */ |
paul@65 | 36 | |
paul@133 | 37 | class PipePaging : public NotificationSupport |
paul@65 | 38 | { |
paul@65 | 39 | protected: |
paul@88 | 40 | Memory *_memory; |
paul@72 | 41 | Pages *_pages; |
paul@72 | 42 | PageQueue *_queue; |
paul@544 | 43 | std::mutex _lock; |
paul@66 | 44 | |
paul@66 | 45 | /* Regions acting as files with their own accessors. */ |
paul@66 | 46 | |
paul@72 | 47 | PageMapper *_regions[2]; |
paul@72 | 48 | PipeAccessor _accessors[2]; |
paul@72 | 49 | |
paul@72 | 50 | /* The first region is initially exposed to both reader and writer. */ |
paul@72 | 51 | |
paul@72 | 52 | int _reading = 0, _writing = 0; |
paul@66 | 53 | |
paul@66 | 54 | /* Pipe section/region size. */ |
paul@66 | 55 | |
paul@66 | 56 | offset_t _size; |
paul@65 | 57 | |
paul@67 | 58 | /* Endpoint status. */ |
paul@67 | 59 | |
paul@138 | 60 | unsigned int _active_endpoints = 2; |
paul@67 | 61 | |
paul@118 | 62 | /* Common functionality. */ |
paul@118 | 63 | |
paul@166 | 64 | virtual PageMapper *_add_region(); |
paul@166 | 65 | |
paul@166 | 66 | virtual PageMapper *_next_region(); |
paul@166 | 67 | |
paul@118 | 68 | virtual void discard_region(unsigned int i); |
paul@118 | 69 | |
paul@65 | 70 | public: |
paul@70 | 71 | explicit PipePaging(Memory *memory, offset_t size); |
paul@70 | 72 | |
paul@199 | 73 | virtual ~PipePaging(); |
paul@199 | 74 | |
paul@199 | 75 | virtual unsigned int detach(); |
paul@67 | 76 | |
paul@67 | 77 | virtual offset_t region_size() |
paul@67 | 78 | { return _size; } |
paul@67 | 79 | |
paul@67 | 80 | /* Region management. */ |
paul@67 | 81 | |
paul@72 | 82 | virtual PageMapper *add_region(); |
paul@65 | 83 | |
paul@72 | 84 | virtual PageMapper *current_region(); |
paul@65 | 85 | |
paul@166 | 86 | virtual PageMapper *current_region(bool writing); |
paul@166 | 87 | |
paul@65 | 88 | virtual PageMapper *next_region(); |
paul@114 | 89 | |
paul@114 | 90 | /* Access management. */ |
paul@114 | 91 | |
paul@114 | 92 | virtual int closed(); |
paul@137 | 93 | |
paul@137 | 94 | /* Special values for various arrays. */ |
paul@137 | 95 | |
paul@137 | 96 | enum PipePagingRoles : unsigned int |
paul@137 | 97 | { |
paul@137 | 98 | READER = 0, |
paul@137 | 99 | WRITER = 1, |
paul@137 | 100 | }; |
paul@65 | 101 | }; |
paul@65 | 102 | |
paul@65 | 103 | // vim: tabstop=4 expandtab shiftwidth=4 |