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@123 | 30 | #include <unistd.h> /* sleep */ |
paul@65 | 31 | |
paul@108 | 32 | #include <fsclient/client.h> |
paul@94 | 33 | #include <fsclient/file.h> |
paul@94 | 34 | #include <mem/memory_utils.h> |
paul@65 | 35 | |
paul@65 | 36 | |
paul@65 | 37 | |
paul@117 | 38 | /* Minimum pipe region size in pages. */ |
paul@117 | 39 | |
paul@66 | 40 | const unsigned int PIPE_PAGES = 2; |
paul@66 | 41 | |
paul@117 | 42 | /* Use the writer to fill the pipe with data. */ |
paul@117 | 43 | |
paul@124 | 44 | static void write_pipe(file_t *writer, int number) |
paul@117 | 45 | { |
paul@117 | 46 | offset_t size = 600; |
paul@117 | 47 | char buffer[size]; |
paul@124 | 48 | long err; |
paul@124 | 49 | |
paul@124 | 50 | /* Make writers blocking to permit synchronisation. */ |
paul@124 | 51 | |
paul@124 | 52 | if ((err = client_set_blocking(writer, NOTIFY_SPACE_AVAILABLE))) |
paul@124 | 53 | { |
paul@124 | 54 | printf("Could not set pipe #%d as blocking: %s\n", number, l4sys_errtostr(err)); |
paul@124 | 55 | return; |
paul@124 | 56 | } |
paul@117 | 57 | |
paul@117 | 58 | for (int loop = 0; loop < 3; loop++) |
paul@117 | 59 | { |
paul@117 | 60 | for (int region = 0; region < 26; region++) |
paul@117 | 61 | { |
paul@117 | 62 | memset(buffer, (int) 'a' + region, size); |
paul@117 | 63 | |
paul@117 | 64 | offset_t nwritten = client_write(writer, buffer, size); |
paul@117 | 65 | |
paul@124 | 66 | printf("Written %ld/%ld in #%d of %d/%d to pipe #%d...\n", nwritten, size, region, loop, 2, number); |
paul@117 | 67 | } |
paul@123 | 68 | |
paul@123 | 69 | sleep(1); |
paul@117 | 70 | } |
paul@117 | 71 | |
paul@117 | 72 | /* Flush to make the final output available. */ |
paul@117 | 73 | |
paul@117 | 74 | client_flush(writer); |
paul@123 | 75 | |
paul@123 | 76 | file_close(writer); |
paul@117 | 77 | } |
paul@117 | 78 | |
paul@117 | 79 | /* Use the reader to obtain data from the pipe. */ |
paul@117 | 80 | |
paul@123 | 81 | static void read_pipes(file_t *reader1, file_t *reader2) |
paul@117 | 82 | { |
paul@123 | 83 | offset_t size = 600, totals[] = {0, 0}; |
paul@123 | 84 | bool active[] = {true, true}; |
paul@123 | 85 | int num_active = 2; |
paul@124 | 86 | long err; |
paul@123 | 87 | file_t *reader; |
paul@124 | 88 | |
paul@124 | 89 | /* Register the readers for notification. */ |
paul@124 | 90 | |
paul@124 | 91 | // NOTE: Use the flags to detect initial conditions! |
paul@117 | 92 | |
paul@124 | 93 | if ((err = client_subscribe(reader1, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED)) || |
paul@124 | 94 | (err = client_subscribe(reader2, NOTIFY_CONTENT_AVAILABLE | NOTIFY_PEER_CLOSED))) |
paul@123 | 95 | { |
paul@124 | 96 | printf("Could not subscribe to notifications: %s\n", l4sys_errtostr(err)); |
paul@123 | 97 | return; |
paul@123 | 98 | } |
paul@123 | 99 | |
paul@123 | 100 | while (1) |
paul@117 | 101 | { |
paul@124 | 102 | char buffer[size]; |
paul@124 | 103 | offset_t nread; |
paul@124 | 104 | |
paul@123 | 105 | /* Wait for notification of content. */ |
paul@123 | 106 | |
paul@124 | 107 | err = client_wait_files(&reader); |
paul@123 | 108 | |
paul@123 | 109 | if (err) |
paul@123 | 110 | { |
paul@123 | 111 | printf("Error waiting for notifications: %s\n", l4sys_errtostr(err)); |
paul@123 | 112 | return; |
paul@123 | 113 | } |
paul@123 | 114 | |
paul@123 | 115 | if ((reader != reader1) && (reader != reader2)) |
paul@123 | 116 | { |
paul@123 | 117 | printf("Spurious notification received for %p versus %p, %p.\n", reader, reader1, reader2); |
paul@123 | 118 | continue; |
paul@123 | 119 | } |
paul@123 | 120 | |
paul@117 | 121 | nread = client_read(reader, buffer, size); |
paul@117 | 122 | |
paul@123 | 123 | // NOTE: Should really be testing for the condition somehow. |
paul@117 | 124 | |
paul@123 | 125 | int p = reader == reader1 ? 0 : 1; |
paul@123 | 126 | |
paul@123 | 127 | if (!nread) |
paul@117 | 128 | { |
paul@123 | 129 | if (active[p]) |
paul@123 | 130 | { |
paul@123 | 131 | active[p] = false; |
paul@123 | 132 | num_active--; |
paul@123 | 133 | |
paul@123 | 134 | if (!num_active) |
paul@123 | 135 | break; |
paul@123 | 136 | } |
paul@117 | 137 | } |
paul@123 | 138 | |
paul@124 | 139 | while (nread) |
paul@123 | 140 | { |
paul@123 | 141 | totals[p] += nread; |
paul@123 | 142 | |
paul@124 | 143 | printf("Read %ld/%ld, total %ld, first %c, last %c, from pipe #%d...\n", nread, size, totals[p], *buffer, *(buffer + nread - 1), p + 1); |
paul@123 | 144 | #if 0 |
paul@123 | 145 | for (offset_t i = 0; i < nread; i += 60) |
paul@123 | 146 | { |
paul@123 | 147 | fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout); |
paul@123 | 148 | fputs("\n", stdout); |
paul@123 | 149 | } |
paul@123 | 150 | #endif |
paul@123 | 151 | nread = client_read(reader, buffer, size); |
paul@123 | 152 | } |
paul@117 | 153 | } |
paul@117 | 154 | |
paul@117 | 155 | printf("Data shown.\n"); |
paul@117 | 156 | } |
paul@117 | 157 | |
paul@65 | 158 | int main(void) |
paul@65 | 159 | { |
paul@65 | 160 | /* Obtain access to the filesystem. */ |
paul@65 | 161 | |
paul@65 | 162 | l4_cap_idx_t server = l4re_env_get_cap("server"); |
paul@65 | 163 | |
paul@65 | 164 | /* Invoke the open method to receive the file reference. */ |
paul@65 | 165 | |
paul@123 | 166 | file_t reader1, reader2, writer1, writer2; |
paul@123 | 167 | long err = pipe_open(page(PIPE_PAGES), &reader1, &writer1, server) || |
paul@123 | 168 | pipe_open(page(PIPE_PAGES), &reader2, &writer2, server); |
paul@65 | 169 | |
paul@65 | 170 | if (err) |
paul@65 | 171 | { |
paul@123 | 172 | printf("Could not obtain pipes: %s\n", l4sys_errtostr(err)); |
paul@65 | 173 | return 1; |
paul@65 | 174 | } |
paul@65 | 175 | |
paul@117 | 176 | /* Schedule reader and writer threads. */ |
paul@117 | 177 | |
paul@123 | 178 | std::thread *activities[3]; |
paul@65 | 179 | |
paul@123 | 180 | activities[0] = new std::thread(read_pipes, &reader1, &reader2); |
paul@124 | 181 | activities[1] = new std::thread(write_pipe, &writer1, 1); |
paul@124 | 182 | activities[2] = new std::thread(write_pipe, &writer2, 2); |
paul@117 | 183 | |
paul@123 | 184 | for (int i = 0; i < 3; i++) |
paul@117 | 185 | activities[i]->join(); |
paul@65 | 186 | } |
paul@65 | 187 | |
paul@65 | 188 | // vim: tabstop=2 expandtab shiftwidth=2 |