summaryrefslogtreecommitdiff
path: root/nuttx/examples/pipe/pipe_main.c
blob: eadad056292a264cfe749ad657e4ba189a1d101f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************
 * pipe_main.c
 *
 *   Copyright (C) 2008 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

/****************************************************************************
 * Definitions
 ****************************************************************************/

#ifndef CONFIG_EXAMPLES_FIFO_PATH
# define CONFIG_EXAMPLES_FIFO_PATH "/tmp/testfifo"
#endif

#define MAX_BYTE      13

#define WRITE_SIZE    MAX_BYTE
#define NWRITES       1400
#define NWRITE_BYTES (NWRITES * WRITE_SIZE)

#define READ_SIZE    (2*MAX_BYTE)
#define NREADS       (NWRITES / 2)
#define NREAD_BYTES   NWRITE_BYTES

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: reader
 ****************************************************************************/

static void *reader(pthread_addr_t pvarg)
{
  char buffer[READ_SIZE];
  int fd = (int)pvarg;
  int ret;
  int nbytes;
  int value;
  int ndx;

  printf("reader: started\n");
  for (nbytes = 0, value = 0; nbytes < NREAD_BYTES;)
    {
      ret = read(fd, buffer, READ_SIZE);
      if (ret < 0 )
        {
           fprintf(stderr, "reader: read failed, errno=%d\n", errno);
           return (void*)1;
        }
      else if (ret == 0)
        {
          if (nbytes < NREAD_BYTES)
            {
              fprintf(stderr, "reader: Too few bytes read -- aborting: %d\n", nbytes);
              return (void*)2;
            }
          break;
        }
      for (ndx = 0; ndx < ret; ndx++)
        {
          if (value >= WRITE_SIZE)
            {
              value = 0;
            }
          if (buffer[ndx] != value)
            {
              fprintf(stderr, "reader: Byte %d, expected %d, found %d\n",
                      nbytes + ndx, value, buffer[ndx]);
              return (void*)3;
            }
          value++;
        }
      nbytes += ret;
      if (nbytes > NREAD_BYTES)
        {
          fprintf(stderr, "reader: Too many bytes read -- aborting: %d\n", nbytes);
          return (void*)4;
        }
    }
  printf("reader: %d bytes read\n", nbytes);
  return (void*)0;
}

/****************************************************************************
 * Name: writer
 ****************************************************************************/

static void *writer(pthread_addr_t pvarg)
{
  char buffer[WRITE_SIZE];
  int fd = (int)pvarg;
  int ret;
  int i;

  printf("writer: started\n");
  for (i = 0; i < WRITE_SIZE; i++)
    {
      buffer[i] = i;
    }

  for (i = 0; i < NWRITES; i++)
    {
      ret = write(fd, buffer, WRITE_SIZE);
      if (ret < 0 )
        {
           fprintf(stderr, "writer: write failed, errno=%d\n", errno);
           return (void*)1;
        }
      else if (ret != WRITE_SIZE)
        {
           fprintf(stderr, "writer: Unexpected write size=%d\n", ret);
           return (void*)2;
        }
    }
  printf("writer: %d bytes written\n", NWRITE_BYTES);
  return (void*)0;
}

/****************************************************************************
 * Name: perform_test
 ****************************************************************************/

static int perform_test(int fdin, int fdout)
{
  pthread_t readerid;
  pthread_t writerid;
  void *value;
  int tmp;
  int ret;

  /* Start reader thread */

  printf("perform_test: Starting reader thread\n");
  ret = pthread_create(&readerid, NULL, reader, (pthread_addr_t)fdin);
  if (ret != 0)
    {
      fprintf(stderr, "perform_test: Failed to create reader thread, error=%d\n", ret);
      return -1;
    }

  /* Start writer thread */

  printf("perform_test: Starting writer thread\n");
  ret = pthread_create(&writerid, NULL, writer, (pthread_addr_t)fdout);
  if (ret != 0)
    {
      fprintf(stderr, "perform_test: Failed to create writer thread, error=%d\n", ret);
      ret = pthread_cancel(readerid);
      if (ret != 0)
        {
          fprintf(stderr, "perform_test: Failed to cancel reader thread, error=%d\n", ret);
        }
      return -1;
    }

  /* Wait for writer thread to complete */

  printf("perform_test: Waiting for writer thread\n");
  ret = pthread_join(writerid, &value);
  if (ret != 0)
    {
      fprintf(stderr, "perform_test: pthread_join failed, error=%d\n", ret);
    }
  else
    {
      ret = (int)value;
      printf("perform_test: writer returned %d\n", ret);
    }

  /* Wait for reader thread to complete */

  printf("perform_test: Waiting for reader thread\n");
  tmp = pthread_join(readerid, &value);
  if (tmp != 0)
    {
      fprintf(stderr, "perform_test: pthread_join failed, error=%d\n", ret);
    }
  else
    {
      tmp = (int)value;
      printf("perform_test: reader returned %d\n", tmp);
    }

  if (ret == 0)
    {
      ret = tmp;
    }
  printf("perform_test: returning %d\n", ret);
  return ret;
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: user_initialize
 ****************************************************************************/

void user_initialize(void)
{
}

/****************************************************************************
 * Name: user_start
 ****************************************************************************/

int user_start(int argc, char *argv[])
{
  int filedes[2];
  int ret;

  /* Test FIFO logic */

  printf("user_start: Performing FIFO test\n");
  ret = mkfifo(CONFIG_EXAMPLES_FIFO_PATH, 0666);
  if (ret < 0)
    {
      fprintf(stderr, "user_start: mkfifo failed with errno=%d\n", errno);
      return 1;
    }

  /* Open open end of the FIFO for reading and the other end for writing.  NOTE:
   * the following would not work on most FIFO implementations because the attempt
   * to open just one end of the FIFO would block.  The NuttX FIFOs do not block.
   */

  filedes[1] = open(CONFIG_EXAMPLES_FIFO_PATH, O_WRONLY);
  if (filedes[1] < 0)
    {
      fprintf(stderr, "user_start: Failed to open FIFO %s for writing, errno=%d\n",
              CONFIG_EXAMPLES_FIFO_PATH, errno);
      close(filedes[0]);
      return 3;
    }

  filedes[0] = open(CONFIG_EXAMPLES_FIFO_PATH, O_RDONLY);
  if (filedes[0] < 0)
    {
      fprintf(stderr, "user_start: Failed to open FIFO %s for reading, errno=%d\n",
              CONFIG_EXAMPLES_FIFO_PATH, errno);
      return 2;
    }

  /* Then perform the test using those file descriptors */

  ret = perform_test(filedes[0], filedes[1]);
  close(filedes[0]);
  close(filedes[1]);
  unlink(CONFIG_EXAMPLES_FIFO_PATH);
  if (ret != 0)
    {
      fprintf(stderr, "user_start: FIFO test FAILED\n");
      return 4;
    }
  printf("user_start: FIFO test PASSED\n");

  /* Test PIPE logic */

  printf("user_start: Performing pipe test\n");
  ret = pipe(filedes);
  if (ret < 0)
    {
      fprintf(stderr, "user_start: pipe failed with errno=%d\n", errno);
      return 1;
    }

  /* Then perform the test using those file descriptors */

  ret = perform_test(filedes[0], filedes[1]);
  close(filedes[0]);
  close(filedes[1]);
  unlink(CONFIG_EXAMPLES_FIFO_PATH);
  if (ret != 0)
    {
      fprintf(stderr, "user_start: PIPE test FAILED\n");
      return 4;
    }
  printf("user_start: PIPE test PASSED\n");

  fflush(stdout);
  return 0;
}