From 648e25bac137accc683f4b82c300070565ba6cb9 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Tue, 14 May 2024 18:03:36 +0800 Subject: [PATCH] examples/pipe: Enhance test by different r/w thread priority By arranging and combining the priorities of read and write threads to cover more usage scenarios. Signed-off-by: Huang Qi --- examples/pipe/pipe.h | 3 +- examples/pipe/pipe_main.c | 70 +++++++++++++++++++++++++++++++---- examples/pipe/transfer_test.c | 32 ++++++++++++++-- 3 files changed, 92 insertions(+), 13 deletions(-) diff --git a/examples/pipe/pipe.h b/examples/pipe/pipe.h index 304fb8e16..14931607a 100644 --- a/examples/pipe/pipe.h +++ b/examples/pipe/pipe.h @@ -48,7 +48,8 @@ * Public Function Prototypes ****************************************************************************/ -extern int transfer_test(int fdin, int fdout); +extern int transfer_test(int fdin, int fdout, + int boost_reader, int boost_writer); extern int interlock_test(void); extern int redirection_test(void); diff --git a/examples/pipe/pipe_main.c b/examples/pipe/pipe_main.c index 697afe5f9..a8e85c696 100644 --- a/examples/pipe/pipe_main.c +++ b/examples/pipe/pipe_main.c @@ -137,7 +137,37 @@ int main(int argc, FAR char *argv[]) /* Then perform the test using those file descriptors */ - ret = transfer_test(fd[0], fd[1]); + ret = transfer_test(fd[0], fd[1], 0, 0); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: FIFO test FAILED (00) (%d)\n", ret); + return 6; + } + + ret = transfer_test(fd[0], fd[1], 1, 0); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: FIFO test FAILED (10) (%d)\n", ret); + return 6; + } + + ret = transfer_test(fd[0], fd[1], 0, 1); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: FIFO test FAILED (01)(%d)\n", ret); + return 6; + } + + ret = transfer_test(fd[0], fd[1], 1, 1); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: FIFO test FAILED (11) (%d)\n", ret); + return 6; + } if (close(fd[0]) != 0) { @@ -196,7 +226,37 @@ int main(int argc, FAR char *argv[]) /* Then perform the test using those file descriptors */ - ret = transfer_test(fd[0], fd[1]); + ret = transfer_test(fd[0], fd[1], 0, 0); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: PIPE test FAILED (00) (%d)\n", ret); + return 9; + } + + ret = transfer_test(fd[0], fd[1], 1, 0); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: PIPE test FAILED (10) (%d)\n", ret); + return 9; + } + + ret = transfer_test(fd[0], fd[1], 0, 1); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: PIPE test FAILED (01) (%d)\n", ret); + return 9; + } + + ret = transfer_test(fd[0], fd[1], 1, 1); + + if (ret != 0) + { + fprintf(stderr, "pipe_main: PIPE test FAILED (11) (%d)\n", ret); + return 9; + } if (close(fd[0]) != 0) { @@ -208,12 +268,6 @@ int main(int argc, FAR char *argv[]) fprintf(stderr, "pipe_main: close failed: %d\n", errno); } - if (ret != 0) - { - fprintf(stderr, "pipe_main: PIPE test FAILED (%d)\n", ret); - return 9; - } - /* Perform the pipe redirection test */ fprintf(stderr, "\npipe_main: Performing redirection test\n"); diff --git a/examples/pipe/transfer_test.c b/examples/pipe/transfer_test.c index fcba4413b..a7ae26d11 100644 --- a/examples/pipe/transfer_test.c +++ b/examples/pipe/transfer_test.c @@ -176,21 +176,34 @@ static void *transfer_writer(pthread_addr_t pvarg) * Name: transfer_test ****************************************************************************/ -int transfer_test(int fdin, int fdout) +int transfer_test(int fdin, int fdout, int boost_reader, int boost_writer) { pthread_t readerid; pthread_t writerid; + pthread_attr_t rattr; + pthread_attr_t wattr; void *value; int tmp; int ret; printf("transfer_test: fdin=%d fdout=%d\n", fdin, fdout); + /* Boost the priority of reader or writer on need */ + + pthread_attr_init(&rattr); + if (boost_reader) + { + rattr.priority += 1; + printf("transfer_test: Boost priority of transfer_reader" + "thread to %d\n", rattr.priority); + } + /* Start transfer_reader thread */ printf("transfer_test: Starting transfer_reader thread\n"); - ret = pthread_create(&readerid, NULL, \ + ret = pthread_create(&readerid, &rattr, transfer_reader, (void *)(intptr_t)fdin); + pthread_attr_destroy(&rattr); if (ret != 0) { fprintf(stderr, \ @@ -199,11 +212,22 @@ int transfer_test(int fdin, int fdout) return 1; } + /* Boost the priority of reader or writer on need */ + + pthread_attr_init(&wattr); + if (boost_writer) + { + wattr.priority += 1; + printf("transfer_test: Boost priority of transfer_writer" + "thread to %d\n", wattr.priority); + } + /* Start transfer_writer thread */ printf("transfer_test: Starting transfer_writer thread\n"); - ret = pthread_create(&writerid, \ - NULL, transfer_writer, (void *)(intptr_t)fdout); + ret = pthread_create(&writerid, &wattr, + transfer_writer, (void *)(intptr_t)fdout); + pthread_attr_destroy(&wattr); if (ret != 0) { fprintf(stderr, \