paul@65 | 1 | /* |
paul@65 | 2 | * Test pipe operations. |
paul@65 | 3 | * |
paul@65 | 4 | * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@65 | 5 | * |
paul@65 | 6 | * This program is free software; you can redistribute it and/or |
paul@65 | 7 | * modify it under the terms of the GNU General Public License as |
paul@65 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@65 | 9 | * the License, or (at your option) any later version. |
paul@65 | 10 | * |
paul@65 | 11 | * This program is distributed in the hope that it will be useful, |
paul@65 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@65 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@65 | 14 | * GNU General Public License for more details. |
paul@65 | 15 | * |
paul@65 | 16 | * You should have received a copy of the GNU General Public License |
paul@65 | 17 | * along with this program; if not, write to the Free Software |
paul@65 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@65 | 19 | * Boston, MA 02110-1301, USA |
paul@65 | 20 | */ |
paul@65 | 21 | |
paul@65 | 22 | #include <l4/re/env.h> |
paul@65 | 23 | #include <l4/sys/err.h> |
paul@65 | 24 | |
paul@117 | 25 | #include <thread> |
paul@117 | 26 | |
paul@65 | 27 | #include <stdio.h> |
paul@65 | 28 | #include <string.h> |
paul@65 | 29 | #include <stdlib.h> |
paul@65 | 30 | |
paul@108 | 31 | #include <fsclient/client.h> |
paul@94 | 32 | #include <fsclient/file.h> |
paul@94 | 33 | #include <mem/memory_utils.h> |
paul@65 | 34 | |
paul@65 | 35 | |
paul@65 | 36 | |
paul@117 | 37 | /* Minimum pipe region size in pages. */ |
paul@117 | 38 | |
paul@66 | 39 | const unsigned int PIPE_PAGES = 2; |
paul@66 | 40 | |
paul@117 | 41 | /* Use the writer to fill the pipe with data. */ |
paul@117 | 42 | |
paul@117 | 43 | static void write_pipe(file_t *writer) |
paul@117 | 44 | { |
paul@117 | 45 | offset_t size = 600; |
paul@117 | 46 | char buffer[size]; |
paul@117 | 47 | |
paul@117 | 48 | for (int loop = 0; loop < 3; loop++) |
paul@117 | 49 | { |
paul@117 | 50 | for (int region = 0; region < 26; region++) |
paul@117 | 51 | { |
paul@117 | 52 | memset(buffer, (int) 'a' + region, size); |
paul@117 | 53 | |
paul@117 | 54 | offset_t nwritten = client_write(writer, buffer, size); |
paul@117 | 55 | |
paul@117 | 56 | printf("Written %ld/%ld in #%d of %d/%d to pipe...\n", nwritten, size, region, loop, 2); |
paul@117 | 57 | } |
paul@117 | 58 | } |
paul@117 | 59 | |
paul@117 | 60 | /* Flush to make the final output available. */ |
paul@117 | 61 | |
paul@117 | 62 | client_flush(writer); |
paul@117 | 63 | } |
paul@117 | 64 | |
paul@117 | 65 | /* Use the reader to obtain data from the pipe. */ |
paul@117 | 66 | |
paul@117 | 67 | static void read_pipe(file_t *reader) |
paul@117 | 68 | { |
paul@117 | 69 | offset_t size = 600; |
paul@117 | 70 | char buffer[size]; |
paul@117 | 71 | offset_t nread; |
paul@117 | 72 | |
paul@117 | 73 | do |
paul@117 | 74 | { |
paul@117 | 75 | nread = client_read(reader, buffer, size); |
paul@117 | 76 | |
paul@117 | 77 | printf("Read %ld/%ld from pipe...\n", nread, size); |
paul@117 | 78 | |
paul@117 | 79 | for (offset_t i = 0; i < nread; i += 60) |
paul@117 | 80 | { |
paul@117 | 81 | fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout); |
paul@117 | 82 | fputs("\n", stdout); |
paul@117 | 83 | } |
paul@117 | 84 | } |
paul@117 | 85 | while (nread); |
paul@117 | 86 | |
paul@117 | 87 | printf("Data shown.\n"); |
paul@117 | 88 | } |
paul@117 | 89 | |
paul@65 | 90 | int main(void) |
paul@65 | 91 | { |
paul@65 | 92 | /* Obtain access to the filesystem. */ |
paul@65 | 93 | |
paul@65 | 94 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@65 | 95 | |
paul@65 | 96 | /* Invoke the open method to receive the file reference. */ |
paul@65 | 97 | |
paul@65 | 98 | file_t reader, writer; |
paul@66 | 99 | long err = pipe_open(page(PIPE_PAGES), &reader, &writer, server); |
paul@65 | 100 | |
paul@65 | 101 | if (err) |
paul@65 | 102 | { |
paul@65 | 103 | printf("Could not obtain pipe: %s\n", l4sys_errtostr(err)); |
paul@65 | 104 | return 1; |
paul@65 | 105 | } |
paul@65 | 106 | |
paul@117 | 107 | /* Make the reader and writer blocking to permit synchronisation. */ |
paul@65 | 108 | |
paul@122 | 109 | if ((err = client_set_blocking(&reader, NOTIFY_CONTENT_AVAILABLE)) || |
paul@122 | 110 | (err = client_set_blocking(&writer, NOTIFY_SPACE_AVAILABLE))) |
paul@117 | 111 | { |
paul@117 | 112 | printf("Could not set as blocking: %s\n", l4sys_errtostr(err)); |
paul@117 | 113 | return 1; |
paul@65 | 114 | } |
paul@65 | 115 | |
paul@117 | 116 | /* Schedule reader and writer threads. */ |
paul@117 | 117 | |
paul@117 | 118 | std::thread *activities[2]; |
paul@65 | 119 | |
paul@117 | 120 | activities[0] = new std::thread(read_pipe, &reader); |
paul@117 | 121 | activities[1] = new std::thread(write_pipe, &writer); |
paul@117 | 122 | |
paul@117 | 123 | for (int i = 0; i < 2; i++) |
paul@117 | 124 | activities[i]->join(); |
paul@65 | 125 | } |
paul@65 | 126 | |
paul@65 | 127 | // vim: tabstop=2 expandtab shiftwidth=2 |