2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/pipe/transfer_test.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-03-08 19:57:51 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "pipe.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-proecessor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#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: transfer_reader
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *transfer_reader(pthread_addr_t pvarg)
|
|
|
|
{
|
|
|
|
char buffer[READ_SIZE];
|
2022-10-11 10:26:41 +02:00
|
|
|
int fd = (intptr_t)pvarg;
|
2011-03-20 19:18:19 +01:00
|
|
|
int ret;
|
|
|
|
int nbytes;
|
|
|
|
int value;
|
|
|
|
int ndx;
|
|
|
|
|
|
|
|
printf("transfer_reader: started\n");
|
2022-10-11 10:26:41 +02:00
|
|
|
for (nbytes = 0, value = 0; nbytes < NREAD_BYTES; )
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
ret = read(fd, buffer, READ_SIZE);
|
2022-10-11 10:26:41 +02:00
|
|
|
if (ret < 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_reader: read failed, errno=%d\n",
|
|
|
|
errno);
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
return (void *)(uintptr_t)1;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
if (nbytes < NREAD_BYTES)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_reader: Too few bytes read. Aborting: %d\n",
|
|
|
|
nbytes);
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
return (void *)(uintptr_t)2;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
break;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
for (ndx = 0; ndx < ret; ndx++)
|
|
|
|
{
|
|
|
|
if (value >= WRITE_SIZE)
|
|
|
|
{
|
|
|
|
value = 0;
|
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
if (buffer[ndx] != value)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_reader: Byte %d, expected %d, found %d\n",
|
|
|
|
nbytes + ndx, value, buffer[ndx]);
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
return (void *)(uintptr_t)3;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
value++;
|
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
nbytes += ret;
|
|
|
|
if (nbytes > NREAD_BYTES)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_reader: Too many bytes read. Aborting: %d\n",
|
|
|
|
nbytes);
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
return (void *)(uintptr_t)4;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("transfer_reader: %d bytes read\n", nbytes);
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
return NULL;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: transfer_writer
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *transfer_writer(pthread_addr_t pvarg)
|
|
|
|
{
|
|
|
|
char buffer[WRITE_SIZE];
|
2022-10-11 10:26:41 +02:00
|
|
|
int fd = (intptr_t)pvarg;
|
2011-03-20 19:18:19 +01:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("transfer_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);
|
2022-10-11 10:26:41 +02:00
|
|
|
if (ret < 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_writer: write failed, errno=%d\n", errno);
|
2022-10-08 10:31:21 +02:00
|
|
|
return (void *)(uintptr_t)1;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else if (ret != WRITE_SIZE)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_writer: Unexpected write size=%d\n", ret);
|
2022-10-08 10:31:21 +02:00
|
|
|
return (void *)(uintptr_t)2;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("transfer_writer: %d bytes written\n", NWRITE_BYTES);
|
2022-10-08 10:31:21 +02:00
|
|
|
return NULL;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: transfer_test
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int transfer_test(int fdin, int fdout)
|
|
|
|
{
|
|
|
|
pthread_t readerid;
|
|
|
|
pthread_t writerid;
|
|
|
|
void *value;
|
|
|
|
int tmp;
|
|
|
|
int ret;
|
|
|
|
|
2023-03-27 00:19:48 +02:00
|
|
|
printf("transfer_test: fdin=%d fdout=%d\n", fdin, fdout);
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Start transfer_reader thread */
|
|
|
|
|
2023-03-27 00:19:48 +02:00
|
|
|
printf("transfer_test: Starting transfer_reader thread\n");
|
2022-10-11 10:26:41 +02:00
|
|
|
ret = pthread_create(&readerid, NULL, \
|
|
|
|
transfer_reader, (void *)(intptr_t)fdin);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_test: Failed to create transfer_reader thread,"
|
|
|
|
"error=%d\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start transfer_writer thread */
|
|
|
|
|
|
|
|
printf("transfer_test: Starting transfer_writer thread\n");
|
2022-10-11 10:26:41 +02:00
|
|
|
ret = pthread_create(&writerid, \
|
|
|
|
NULL, transfer_writer, (void *)(intptr_t)fdout);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_test: Failed to create transfer_writer thread,"
|
|
|
|
"error=%d\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
pthread_detach(readerid);
|
|
|
|
ret = pthread_cancel(readerid);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
fprintf(stderr, \
|
2023-03-27 00:19:48 +02:00
|
|
|
"transfer_test: Failed to cancel transfer_reader thread,"
|
|
|
|
"error=%d\n", ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for transfer_writer thread to complete */
|
|
|
|
|
|
|
|
printf("transfer_test: Waiting for transfer_writer thread\n");
|
|
|
|
ret = pthread_join(writerid, &value);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "transfer_test: pthread_join failed, error=%d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
ret = (intptr_t)value;
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("transfer_test: transfer_writer returned %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for transfer_reader thread to complete */
|
|
|
|
|
|
|
|
printf("transfer_test: Waiting for transfer_reader thread\n");
|
|
|
|
tmp = pthread_join(readerid, &value);
|
|
|
|
if (tmp != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "transfer_test: pthread_join failed, error=%d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-11 10:26:41 +02:00
|
|
|
tmp = (intptr_t)value;
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("transfer_test: transfer_reader returned %d\n", tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
ret = tmp;
|
|
|
|
}
|
2022-10-11 10:26:41 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("transfer_test: returning %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|