From d8a1b61690862777b137ec182a0d62dcf4ee8db2 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 6 Feb 2013 00:06:35 +0000 Subject: Misc clean-up and bugfixes related to multi-thread group signalling git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5614 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/ostest/cond.c | 13 +++++--- apps/examples/ostest/mqueue.c | 62 ++++++++++++++++++++++++++++-------- apps/examples/ostest/timedmqueue.c | 65 ++++++++++++++++++++++++++++---------- 3 files changed, 105 insertions(+), 35 deletions(-) (limited to 'apps/examples/ostest') diff --git a/apps/examples/ostest/cond.c b/apps/examples/ostest/cond.c index 96468c3e4..35456efc1 100644 --- a/apps/examples/ostest/cond.c +++ b/apps/examples/ostest/cond.c @@ -1,7 +1,7 @@ /*********************************************************************** * cond.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008, 2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -97,6 +97,7 @@ static void *thread_waiter(void *parameter) printf("waiter_thread: ERROR pthread_cond_wait failed, status=%d\n", status); waiter_nerrors++; } + waiter_waits++; } @@ -123,6 +124,7 @@ static void *thread_waiter(void *parameter) waiter_nloops++; } + return NULL; } @@ -288,7 +290,10 @@ void cond_test(void) printf("cond_test: Loops\t%d\t%d\n", waiter_nloops, signaler_nloops); printf("cond_test: Errors\t%d\t%d\n", waiter_nerrors, signaler_nerrors); printf("cond_test:\n"); - printf("cond_test: %d times, waiter did not have to wait for data\n", waiter_nloops - waiter_waits); - printf("cond_test: %d times, data was already available when the signaler run\n", signaler_already); - printf("cond_test: %d times, the waiter was in an unexpected state when the signaler ran\n", signaler_state); + printf("cond_test: %d times, waiter did not have to wait for data\n", + waiter_nloops - waiter_waits); + printf("cond_test: %d times, data was already available when the signaler run\n", + signaler_already); + printf("cond_test: %d times, the waiter was in an unexpected state when the signaler ran\n", + signaler_state); } diff --git a/apps/examples/ostest/mqueue.c b/apps/examples/ostest/mqueue.c index 54e84ca45..0bb3473dc 100644 --- a/apps/examples/ostest/mqueue.c +++ b/apps/examples/ostest/mqueue.c @@ -92,6 +92,7 @@ **************************************************************************/ static mqd_t g_send_mqfd; +static mqd_t g_recv_mqfd; /************************************************************************** * Private Functions @@ -128,7 +129,7 @@ static void *sender_thread(void *arg) * already created it. */ - g_send_mqfd = mq_open("testmq", O_WRONLY|O_CREAT, 0666, &attr); + g_send_mqfd = mq_open("mqueue", O_WRONLY|O_CREAT, 0666, &attr); if (g_send_mqfd < 0) { printf("sender_thread: ERROR mq_open failed\n"); @@ -161,6 +162,10 @@ static void *sender_thread(void *arg) { printf("sender_thread: ERROR mq_close failed\n"); } + else + { + g_send_mqfd = NULL; + } printf("sender_thread: returning nerrors=%d\n", nerrors); return (pthread_addr_t)nerrors; @@ -168,7 +173,6 @@ static void *sender_thread(void *arg) static void *receiver_thread(void *arg) { - mqd_t mqfd; char msg_buffer[TEST_MSGLEN]; struct mq_attr attr; int nbytes; @@ -194,8 +198,8 @@ static void *receiver_thread(void *arg) * already created it. */ - mqfd = mq_open("testmq", O_RDONLY|O_CREAT, 0666, &attr); - if (mqfd < 0) + g_recv_mqfd = mq_open("mqueue", O_RDONLY|O_CREAT, 0666, &attr); + if (g_recv_mqfd < 0) { printf("receiver_thread: ERROR mq_open failed\n"); pthread_exit((pthread_addr_t)1); @@ -206,7 +210,7 @@ static void *receiver_thread(void *arg) for (i = 0; i < TEST_RECEIVE_NMSGS; i++) { memset(msg_buffer, 0xaa, TEST_MSGLEN); - nbytes = mq_receive(mqfd, msg_buffer, TEST_MSGLEN, 0); + nbytes = mq_receive(g_recv_mqfd, msg_buffer, TEST_MSGLEN, 0); if (nbytes < 0) { /* mq_receive failed. If the error is because of EINTR then @@ -260,18 +264,14 @@ static void *receiver_thread(void *arg) /* Close the queue and return success */ - if (mq_close(mqfd) < 0) + if (mq_close(g_recv_mqfd) < 0) { printf("receiver_thread: ERROR mq_close failed\n"); nerrors++; } - - /* Destroy the queue */ - - if (mq_unlink("testmq") < 0) + else { - printf("receiver_thread: ERROR mq_close failed\n"); - nerrors++; + g_recv_mqfd = NULL; } printf("receiver_thread: returning nerrors=%d\n", nerrors); @@ -292,6 +292,11 @@ void mqueue_test(void) int prio_mid; int status; + /* Reset globals for the beginning of the test */ + + g_send_mqfd = NULL; + g_recv_mqfd = NULL; + /* Start the sending thread at higher priority */ printf("mqueue_test: Starting receiver\n"); @@ -410,9 +415,38 @@ void mqueue_test(void) * message queue open. */ - if (mq_close(g_send_mqfd) < 0) + if (result == PTHREAD_CANCELED && g_send_mqfd) { - printf("sender_thread: ERROR mq_close failed\n"); + if (mq_close(g_send_mqfd) < 0) + { + printf("mqueue_test: ERROR mq_close failed\n"); + } + } + else if (result != PTHREAD_CANCELED && g_send_mqfd) + { + printf("mqueue_test: ERROR send mqd_t left open\n"); + if (mq_close(g_send_mqfd) < 0) + { + printf("mqueue_test: ERROR mq_close failed\n"); + } + } + + /* Make sure that the receive queue is closed as well */ + + if (g_recv_mqfd) + { + printf("mqueue_test: ERROR receive mqd_t left open\n"); + if (mq_close(g_recv_mqfd) < 0) + { + printf("sender_thread: ERROR mq_close failed\n"); + } + } + + /* Destroy the message queue */ + + if (mq_unlink("mqueue") < 0) + { + printf("mqueue_test: ERROR mq_unlink failed\n"); } } diff --git a/apps/examples/ostest/timedmqueue.c b/apps/examples/ostest/timedmqueue.c index 6c3269e84..24dd0bc34 100644 --- a/apps/examples/ostest/timedmqueue.c +++ b/apps/examples/ostest/timedmqueue.c @@ -85,6 +85,9 @@ * Private Variables **************************************************************************/ +static mqd_t g_send_mqfd; +static mqd_t g_recv_mqfd; + /************************************************************************** * Private Functions **************************************************************************/ @@ -95,7 +98,6 @@ static void *sender_thread(void *arg) { - mqd_t mqfd; char msg_buffer[TEST_MSGLEN]; struct mq_attr attr; int status = 0; @@ -121,8 +123,8 @@ static void *sender_thread(void *arg) * already created it. */ - mqfd = mq_open("testmq", O_WRONLY|O_CREAT, 0666, &attr); - if (mqfd < 0) + g_send_mqfd = mq_open("timedmq", O_WRONLY|O_CREAT, 0666, &attr); + if (g_send_mqfd < 0) { printf("sender_thread: ERROR mq_open failed\n"); pthread_exit((pthread_addr_t)1); @@ -148,7 +150,7 @@ static void *sender_thread(void *arg) * one should fail with errno == ETIMEDOUT */ - status = mq_timedsend(mqfd, msg_buffer, TEST_MSGLEN, 42, &ts); + status = mq_timedsend(g_send_mqfd, msg_buffer, TEST_MSGLEN, 42, &ts); if (status < 0) { if (i == TEST_SEND_NMSGS-1 && errno == ETIMEDOUT) @@ -177,10 +179,14 @@ static void *sender_thread(void *arg) /* Close the queue and return success */ - if (mq_close(mqfd) < 0) + if (mq_close(g_send_mqfd) < 0) { printf("sender_thread: ERROR mq_close failed\n"); } + else + { + g_send_mqfd = NULL; + } printf("sender_thread: returning nerrors=%d\n", nerrors); FFLUSH(); @@ -189,7 +195,6 @@ static void *sender_thread(void *arg) static void *receiver_thread(void *arg) { - mqd_t mqfd; char msg_buffer[TEST_MSGLEN]; struct mq_attr attr; int nbytes; @@ -215,8 +220,8 @@ static void *receiver_thread(void *arg) * already created it. */ - mqfd = mq_open("testmq", O_RDONLY|O_CREAT, 0666, &attr); - if (mqfd < 0) + g_recv_mqfd = mq_open("timedmq", O_RDONLY|O_CREAT, 0666, &attr); + if (g_recv_mqfd < 0) { printf("receiver_thread: ERROR mq_open failed\n"); pthread_exit((pthread_addr_t)1); @@ -239,7 +244,7 @@ static void *receiver_thread(void *arg) */ memset(msg_buffer, 0xaa, TEST_MSGLEN); - nbytes = mq_timedreceive(mqfd, msg_buffer, TEST_MSGLEN, 0, &ts); + nbytes = mq_timedreceive(g_recv_mqfd, msg_buffer, TEST_MSGLEN, 0, &ts); if (nbytes < 0) { if (i == TEST_SEND_NMSGS-1 && errno == ETIMEDOUT) @@ -293,18 +298,14 @@ static void *receiver_thread(void *arg) /* Close the queue and return success */ - if (mq_close(mqfd) < 0) + if (mq_close(g_recv_mqfd) < 0) { printf("receiver_thread: ERROR mq_close failed\n"); nerrors++; } - - /* Destroy the queue */ - - if (mq_unlink("testmq") < 0) + else { - printf("receiver_thread: ERROR mq_close failed\n"); - nerrors++; + g_recv_mqfd = NULL; } printf("receiver_thread: returning nerrors=%d\n", nerrors); @@ -378,7 +379,37 @@ void timedmqueue_test(void) pthread_join(receiver, &result); if (result != (void*)0) { - printf("timedmqueue_test: ERROR receiver thread exited with %d errors\n", (int)result); + printf("timedmqueue_test: ERROR receiver thread exited with %d errors\n", + (int)result); + } + + /* Make sure that the message queues were properly closed (otherwise, we + * might have problems the next time in the test loop. + */ + + if (g_send_mqfd) + { + printf("timedmqueue_test: ERROR send mqd_t left open\n"); + if (mq_close(g_send_mqfd) < 0) + { + printf("timedmqueue_test: ERROR mq_close failed\n"); + } + } + + if (g_recv_mqfd) + { + printf("timedmqueue_test: ERROR receive mqd_t left open\n"); + if (mq_close(g_recv_mqfd) < 0) + { + printf("timedmqueue_test: ERROR mq_close failed\n"); + } + } + + /* Destroy the message queue */ + + if (mq_unlink("timedmq") < 0) + { + printf("timedmqueue_test: ERROR mq_unlink failed\n"); } printf("timedmqueue_test: Test complete\n"); -- cgit v1.2.3