paul@93 | 1 | /* |
paul@93 | 2 | * A pipe opener resource. |
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@94 | 22 | #include <mem/memory_incremental.h> |
paul@94 | 23 | |
paul@65 | 24 | #include "pipe_opener_resource.h" |
paul@65 | 25 | #include "pipe_opener_server.h" |
paul@65 | 26 | #include "pipe_pager.h" |
paul@65 | 27 | #include "resource_server.h" |
paul@65 | 28 | |
paul@65 | 29 | |
paul@65 | 30 | |
paul@65 | 31 | /* Support for providing access to pipes. */ |
paul@65 | 32 | |
paul@70 | 33 | PipeOpenerResource::PipeOpenerResource(Memory *memory) |
paul@70 | 34 | : _memory(memory) |
paul@70 | 35 | { |
paul@70 | 36 | } |
paul@70 | 37 | |
paul@65 | 38 | int PipeOpenerResource::expected_items() |
paul@65 | 39 | { |
paul@65 | 40 | return PipeOpener_expected_items; |
paul@65 | 41 | } |
paul@65 | 42 | |
paul@65 | 43 | ipc_server_handler_type PipeOpenerResource::handler() |
paul@65 | 44 | { |
paul@65 | 45 | return (ipc_server_handler_type) handle_PipeOpener; |
paul@65 | 46 | } |
paul@65 | 47 | |
paul@65 | 48 | |
paul@65 | 49 | |
paul@65 | 50 | /* Pipe opener interface methods. */ |
paul@65 | 51 | |
paul@66 | 52 | long PipeOpenerResource::pipe(offset_t size, l4_cap_idx_t *reader, l4_cap_idx_t *writer) |
paul@65 | 53 | { |
paul@65 | 54 | /* Both endpoints will employ a common paging coordinator. */ |
paul@65 | 55 | |
paul@70 | 56 | PipePaging *paging = new PipePaging(_memory, size); |
paul@65 | 57 | |
paul@65 | 58 | /* Each endpoint will have its own pager. */ |
paul@65 | 59 | |
paul@65 | 60 | /* NOTE: Failure to open an endpoint should invalidate both, plus the |
paul@65 | 61 | paging object. Also, any active server thread would need to be |
paul@65 | 62 | cancelled. */ |
paul@65 | 63 | |
paul@65 | 64 | return open_endpoint(paging, false, reader) || open_endpoint(paging, true, writer); |
paul@65 | 65 | } |
paul@65 | 66 | |
paul@66 | 67 | long PipeOpenerResource::open_endpoint(PipePaging *paging, bool writing, l4_cap_idx_t *endpoint) |
paul@65 | 68 | { |
paul@66 | 69 | PipePager *pager = new PipePager(paging, writing); |
paul@65 | 70 | |
paul@65 | 71 | /* Start the endpoint server in a new thread. |
paul@65 | 72 | If the thread does not start, the resource should be finalised. */ |
paul@65 | 73 | |
paul@65 | 74 | ResourceServer server(pager); |
paul@65 | 75 | long err = server.start_thread(); |
paul@65 | 76 | |
paul@65 | 77 | /* Return the server capability to the caller. */ |
paul@65 | 78 | |
paul@65 | 79 | if (!err) |
paul@65 | 80 | *endpoint = server.config()->server; |
paul@65 | 81 | |
paul@65 | 82 | return err; |
paul@65 | 83 | } |
paul@65 | 84 | |
paul@65 | 85 | // vim: tabstop=4 expandtab shiftwidth=4 |