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