1.1 --- a/tests/dstest_pipe_client.cc Sat May 15 23:38:39 2021 +0200
1.2 +++ b/tests/dstest_pipe_client.cc Tue May 18 00:55:20 2021 +0200
1.3 @@ -22,6 +22,8 @@
1.4 #include <l4/re/env.h>
1.5 #include <l4/sys/err.h>
1.6
1.7 +#include <thread>
1.8 +
1.9 #include <stdio.h>
1.10 #include <string.h>
1.11 #include <stdlib.h>
1.12 @@ -32,8 +34,59 @@
1.13
1.14
1.15
1.16 +/* Minimum pipe region size in pages. */
1.17 +
1.18 const unsigned int PIPE_PAGES = 2;
1.19
1.20 +/* Use the writer to fill the pipe with data. */
1.21 +
1.22 +static void write_pipe(file_t *writer)
1.23 +{
1.24 + offset_t size = 600;
1.25 + char buffer[size];
1.26 +
1.27 + for (int loop = 0; loop < 3; loop++)
1.28 + {
1.29 + for (int region = 0; region < 26; region++)
1.30 + {
1.31 + memset(buffer, (int) 'a' + region, size);
1.32 +
1.33 + offset_t nwritten = client_write(writer, buffer, size);
1.34 +
1.35 + printf("Written %ld/%ld in #%d of %d/%d to pipe...\n", nwritten, size, region, loop, 2);
1.36 + }
1.37 + }
1.38 +
1.39 + /* Flush to make the final output available. */
1.40 +
1.41 + client_flush(writer);
1.42 +}
1.43 +
1.44 +/* Use the reader to obtain data from the pipe. */
1.45 +
1.46 +static void read_pipe(file_t *reader)
1.47 +{
1.48 + offset_t size = 600;
1.49 + char buffer[size];
1.50 + offset_t nread;
1.51 +
1.52 + do
1.53 + {
1.54 + nread = client_read(reader, buffer, size);
1.55 +
1.56 + printf("Read %ld/%ld from pipe...\n", nread, size);
1.57 +
1.58 + for (offset_t i = 0; i < nread; i += 60)
1.59 + {
1.60 + fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout);
1.61 + fputs("\n", stdout);
1.62 + }
1.63 + }
1.64 + while (nread);
1.65 +
1.66 + printf("Data shown.\n");
1.67 +}
1.68 +
1.69 int main(void)
1.70 {
1.71 /* Obtain access to the filesystem. */
1.72 @@ -51,63 +104,23 @@
1.73 return 1;
1.74 }
1.75
1.76 - /* Use the writer to fill the pipe with data. */
1.77 -
1.78 - offset_t size = 600;
1.79 - char buffer[size];
1.80 - int region = 0;
1.81 -
1.82 - for (int loop = 0; loop < 3; loop++)
1.83 - {
1.84 - while (1)
1.85 - {
1.86 - printf("Writing %ld to pipe...\n", size);
1.87 -
1.88 - memset(buffer, (int) 'a' + region, size);
1.89 -
1.90 - offset_t nwritten = client_write(&writer, buffer, size);
1.91 -
1.92 - printf("Written %ld to pipe...\n", nwritten);
1.93 -
1.94 - for (offset_t i = 0; i < nwritten; i += 60)
1.95 - {
1.96 - fwrite(buffer + i, sizeof(char), nwritten - i > 60 ? 60 : nwritten - i, stdout);
1.97 - fputs("\n", stdout);
1.98 - }
1.99 + /* Make the reader and writer blocking to permit synchronisation. */
1.100
1.101 - if (!nwritten)
1.102 - break;
1.103 -
1.104 - region++;
1.105 -
1.106 - if (region == 26)
1.107 - region = 0;
1.108 - }
1.109 -
1.110 - /* Use the reader to obtain data from the pipe. */
1.111 -
1.112 - offset_t nread;
1.113 -
1.114 - do
1.115 - {
1.116 - printf("Reading %ld from pipe...\n", size);
1.117 -
1.118 - nread = client_read(&reader, buffer, size);
1.119 -
1.120 - printf("Read %ld from pipe...\n", nread);
1.121 -
1.122 - for (offset_t i = 0; i < nread; i += 60)
1.123 - {
1.124 - fwrite(buffer + i, sizeof(char), nread - i > 60 ? 60 : nread - i, stdout);
1.125 - fputs("\n", stdout);
1.126 - }
1.127 - }
1.128 - while (nread);
1.129 + if ((err = client_set_blocking(&reader, true)) || (err = client_set_blocking(&writer, true)))
1.130 + {
1.131 + printf("Could not set as blocking: %s\n", l4sys_errtostr(err));
1.132 + return 1;
1.133 }
1.134
1.135 - printf("Data shown.\n");
1.136 + /* Schedule reader and writer threads. */
1.137 +
1.138 + std::thread *activities[2];
1.139
1.140 - return 0;
1.141 + activities[0] = new std::thread(read_pipe, &reader);
1.142 + activities[1] = new std::thread(write_pipe, &writer);
1.143 +
1.144 + for (int i = 0; i < 2; i++)
1.145 + activities[i]->join();
1.146 }
1.147
1.148 // vim: tabstop=2 expandtab shiftwidth=2