examples: clean warning of pipe

transfer_test.c:66:12: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
   int fd = (int)pvarg;
            ^
transfer_test.c: In function 'transfer_writer':
transfer_test.c:122:12: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
   int fd = (int)pvarg;
            ^
transfer_test.c: In function 'transfer_test':
transfer_test.c:169:58: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
   ret = pthread_create(&readerid, NULL, transfer_reader, (pthread_addr_t)fdin);
                                                          ^
transfer_test.c:179:58: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
   ret = pthread_create(&writerid, NULL, transfer_writer, (pthread_addr_t)fdout);
                                                          ^
transfer_test.c:202:13: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
       ret = (int)value;
             ^
transfer_test.c:216:13: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
       tmp = (int)value;
             ^

Signed-off-by: nietingting <nietingting@xiaomi.com>
This commit is contained in:
nietingting 2022-10-11 16:26:41 +08:00 committed by Xiang Xiao
parent 7fcba8a119
commit 6e3b402cf4
2 changed files with 109 additions and 44 deletions

View File

@ -57,6 +57,7 @@
static void *null_writer(pthread_addr_t pvarg)
{
int fd;
void *p;
/* Wait a bit */
@ -69,9 +70,11 @@ static void *null_writer(pthread_addr_t pvarg)
fd = open(FIFO_PATH2, O_WRONLY);
if (fd < 0)
{
fprintf(stderr, "null_writer: Failed to open FIFO %s for writing, errno=%d\n",
fprintf(stderr, \
"null_writer: Failed to open FIFO %s for writing, errno=%d\n",
FIFO_PATH2, errno);
return (void*)1;
p = (void *) 1;
return p;
}
/* Wait a bit more */
@ -86,10 +89,12 @@ static void *null_writer(pthread_addr_t pvarg)
{
fprintf(stderr, "null_writer: close failed: %d\n", errno);
}
sleep(5);
printf("null_writer: Returning success\n");
return (void*)0;
p = (void *) 0;
return p;
}
/****************************************************************************
@ -108,13 +113,16 @@ int interlock_test(void)
ssize_t nbytes;
int fd;
int ret;
void *p;
/* Create a FIFO */
ret = mkfifo(FIFO_PATH2, 0666);
if (ret < 0)
{
fprintf(stderr, "interlock_test: mkfifo failed with errno=%d\n", errno);
fprintf(stderr, \
"interlock_test: mkfifo failed with errno=%d\n", \
errno);
return 1;
}
@ -124,40 +132,49 @@ int interlock_test(void)
ret = pthread_create(&writerid, NULL, null_writer, (pthread_addr_t)NULL);
if (ret != 0)
{
fprintf(stderr, "interlock_test: Failed to create null_writer thread, error=%d\n", ret);
fprintf(stderr, \
"interlock_test: Failed to create null_writer thread, \
error=%d\n", ret);
ret = 2;
goto errout_with_fifo;
}
/* Open one end of the FIFO for reading. This open call should block until the
* null_writer thread opens the other end of the FIFO for writing.
/* Open one end of the FIFO for reading.
* This open call should block until the
* null_writer thread opens the other
* end of the FIFO for writing.
*/
printf("interlock_test: Opening FIFO for read access\n");
fd = open(FIFO_PATH2, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "interlock_test: Failed to open FIFO %s for reading, errno=%d\n",
fprintf(stderr, \
"interlock_test: Failed to open FIFO %s for reading, \
errno=%d\n",
FIFO_PATH2, errno);
ret = 3;
goto errout_with_thread;
}
/* Attempt to read one byte from the FIFO. This should return end-of-file because
/* Attempt to read one byte from the FIFO.
* This should return end-of-file because
* the null_writer closes the FIFO without writing anything.
*/
printf("interlock_test: Reading from %s\n", FIFO_PATH2);
nbytes = read(fd, data, 16);
if (nbytes < 0 )
if (nbytes < 0)
{
fprintf(stderr, "interlock_test: read failed, errno=%d\n", errno);
fprintf(stderr, \
"interlock_test: read failed, errno=%d\n", errno);
ret = 4;
goto errout_with_file;
}
else if (ret != 0)
{
fprintf(stderr, "interlock_test: Read %ld bytes of data -- aborting: %d\n",
fprintf(stderr, \
"interlock_test: Read %ld bytes of data -- aborting: %d\n",
(long)nbytes, errno);
ret = 5;
goto errout_with_file;
@ -177,14 +194,16 @@ int interlock_test(void)
ret = pthread_join(writerid, &value);
if (ret != 0)
{
fprintf(stderr, "interlock_test: pthread_join failed, error=%d\n", ret);
fprintf(stderr, \
"interlock_test: pthread_join failed, error=%d\n", ret);
ret = 6;
goto errout_with_fifo;
}
else
{
printf("interlock_test: writer returned %d\n", (int)value);
if (value != (void*)0)
printf("interlock_test: writer returned %p\n", value);
p = (void *) 0;
if (value != p)
{
ret = 7;
goto errout_with_fifo;
@ -192,6 +211,7 @@ int interlock_test(void)
}
/* unlink(FIFO_PATH2); */
printf("interlock_test: Returning success\n");
return 0;
@ -200,11 +220,14 @@ errout_with_file:
{
fprintf(stderr, "interlock_test: close failed: %d\n", errno);
}
errout_with_thread:
pthread_detach(writerid);
pthread_cancel(writerid);
errout_with_fifo:
/* unlink(FIFO_PATH2); */
printf("interlock_test: Returning %d\n", ret);
return ret;
}

View File

@ -63,53 +63,76 @@
static void *transfer_reader(pthread_addr_t pvarg)
{
char buffer[READ_SIZE];
int fd = (int)pvarg;
int fd = (intptr_t)pvarg;
int ret;
int nbytes;
int value;
int ndx;
void *p;
printf("transfer_reader: started\n");
for (nbytes = 0, value = 0; nbytes < NREAD_BYTES;)
for (nbytes = 0, value = 0; nbytes < NREAD_BYTES; )
{
ret = read(fd, buffer, READ_SIZE);
if (ret < 0 )
if (ret < 0)
{
fprintf(stderr, "transfer_reader: read failed, errno=%d\n", errno);
return (void*)1;
fprintf(stderr, \
"transfer_reader: read failed, errno=%d\n", \
errno);
p = (void *) 1;
return p;
}
else if (ret == 0)
{
if (nbytes < NREAD_BYTES)
{
fprintf(stderr, "transfer_reader: Too few bytes read -- aborting: %d\n", nbytes);
return (void*)2;
fprintf(stderr, \
"transfer_reader: Too few bytes read -- aborting: %d\n", \
nbytes);
p = (void *) 2;
return p;
}
break;
break;
}
for (ndx = 0; ndx < ret; ndx++)
{
if (value >= WRITE_SIZE)
{
value = 0;
}
if (buffer[ndx] != value)
{
fprintf(stderr, "transfer_reader: Byte %d, expected %d, found %d\n",
nbytes + ndx, value, buffer[ndx]);
return (void*)3;
fprintf(stderr, \
"transfer_reader: Byte %d, expected %d, found %d\n",
nbytes + ndx, value, buffer[ndx]);
p = (void *) 3;
return p;
}
value++;
}
nbytes += ret;
if (nbytes > NREAD_BYTES)
{
fprintf(stderr, "transfer_reader: Too many bytes read -- aborting: %d\n", nbytes);
return (void*)4;
fprintf(stderr, \
"transfer_reader: Too many bytes read -- aborting: %d\n", \
nbytes);
p = (void *) 4;
return p;
}
}
printf("transfer_reader: %d bytes read\n", nbytes);
return (void*)0;
p = (void *) 0;
return p;
}
/****************************************************************************
@ -119,9 +142,10 @@ static void *transfer_reader(pthread_addr_t pvarg)
static void *transfer_writer(pthread_addr_t pvarg)
{
char buffer[WRITE_SIZE];
int fd = (int)pvarg;
int fd = (intptr_t)pvarg;
int ret;
int i;
void *p;
printf("transfer_writer: started\n");
for (i = 0; i < WRITE_SIZE; i++)
@ -132,19 +156,25 @@ static void *transfer_writer(pthread_addr_t pvarg)
for (i = 0; i < NWRITES; i++)
{
ret = write(fd, buffer, WRITE_SIZE);
if (ret < 0 )
if (ret < 0)
{
fprintf(stderr, "transfer_writer: write failed, errno=%d\n", errno);
return (void*)1;
fprintf(stderr, \
"transfer_writer: write failed, errno=%d\n", errno);
p = (void *) 1;
return p;
}
else if (ret != WRITE_SIZE)
{
fprintf(stderr, "transfer_writer: Unexpected write size=%d\n", ret);
return (void*)2;
fprintf(stderr, \
"transfer_writer: Unexpected write size=%d\n", ret);
p = (void *) 2;
return p;
}
}
printf("transfer_writer: %d bytes written\n", NWRITE_BYTES);
return (void*)0;
p = (void *) 0;
return p;
}
/****************************************************************************
@ -165,27 +195,37 @@ int transfer_test(int fdin, int fdout)
/* Start transfer_reader thread */
printf("transfer_test: Starting transfer_reader thread\n");
ret = pthread_create(&readerid, NULL, transfer_reader, (pthread_addr_t)fdin);
printf("transfer_test: \
Starting transfer_reader thread\n");
ret = pthread_create(&readerid, NULL, \
transfer_reader, (void *)(intptr_t)fdin);
if (ret != 0)
{
fprintf(stderr, "transfer_test: Failed to create transfer_reader thread, error=%d\n", ret);
fprintf(stderr, \
"transfer_test: Failed to create transfer_reader thread, \
error=%d\n", ret);
return 1;
}
/* Start transfer_writer thread */
printf("transfer_test: Starting transfer_writer thread\n");
ret = pthread_create(&writerid, NULL, transfer_writer, (pthread_addr_t)fdout);
ret = pthread_create(&writerid, \
NULL, transfer_writer, (void *)(intptr_t)fdout);
if (ret != 0)
{
fprintf(stderr, "transfer_test: Failed to create transfer_writer thread, error=%d\n", ret);
fprintf(stderr, \
"transfer_test: Failed to create transfer_writer thread, \
error=%d\n", ret);
pthread_detach(readerid);
ret = pthread_cancel(readerid);
if (ret != 0)
{
fprintf(stderr, "transfer_test: Failed to cancel transfer_reader thread, error=%d\n", ret);
fprintf(stderr, \
"transfer_test: Failed to cancel transfer_reader thread, \
error=%d\n", ret);
}
return 2;
}
@ -199,7 +239,7 @@ int transfer_test(int fdin, int fdout)
}
else
{
ret = (int)value;
ret = (intptr_t)value;
printf("transfer_test: transfer_writer returned %d\n", ret);
}
@ -213,7 +253,7 @@ int transfer_test(int fdin, int fdout)
}
else
{
tmp = (int)value;
tmp = (intptr_t)value;
printf("transfer_test: transfer_reader returned %d\n", tmp);
}
@ -221,6 +261,8 @@ int transfer_test(int fdin, int fdout)
{
ret = tmp;
}
printf("transfer_test: returning %d\n", ret);
return ret;
}