2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2018-12-12 23:37:38 +01:00
|
|
|
* apps/testing/ostest/mqueue.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +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-10 07:51:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-10 07:51:27 +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
|
|
|
*
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Included Files
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <assert.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <ctype.h>
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <errno.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <mqueue.h>
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <pthread.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
#include <sched.h>
|
2023-03-16 03:35:15 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#include "ostest.h"
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Private Definitions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#define TEST_MESSAGE "This is a test and only a test"
|
|
|
|
#if defined(SDCC) || defined(__ZILOG__)
|
2020-12-31 17:49:00 +01:00
|
|
|
/* Cannot use strlen in array size */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2017-01-24 16:15:42 +01:00
|
|
|
# define TEST_MSGLEN (31)
|
2011-03-20 19:18:19 +01:00
|
|
|
#else
|
2020-12-31 17:49:00 +01:00
|
|
|
/* Message length is the size of the message plus the null terminator */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2023-08-28 05:23:11 +02:00
|
|
|
# define TEST_MSGLEN sizeof(TEST_MESSAGE)
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define TEST_SEND_NMSGS (10)
|
2017-01-24 16:15:42 +01:00
|
|
|
#define TEST_RECEIVE_NMSGS (11)
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#define HALF_SECOND_USEC_USEC 500000L
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2016-02-22 01:15:00 +01:00
|
|
|
* Private Data
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
static mqd_t g_send_mqfd;
|
2013-02-06 01:06:35 +01:00
|
|
|
static mqd_t g_recv_mqfd;
|
2013-02-05 20:50:37 +01:00
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
static void *sender_thread(void *arg)
|
|
|
|
{
|
|
|
|
char msg_buffer[TEST_MSGLEN];
|
|
|
|
struct mq_attr attr;
|
|
|
|
int status = 0;
|
|
|
|
int nerrors = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("sender_thread: Starting\n");
|
|
|
|
|
|
|
|
/* Fill in attributes for message queue */
|
|
|
|
|
|
|
|
attr.mq_maxmsg = 20;
|
|
|
|
attr.mq_msgsize = TEST_MSGLEN;
|
|
|
|
attr.mq_flags = 0;
|
|
|
|
|
|
|
|
/* Set the flags for the open of the queue.
|
|
|
|
* Make it a blocking open on the queue, meaning it will block if
|
|
|
|
* this process tries to send to the queue and the queue is full.
|
|
|
|
*
|
|
|
|
* O_CREAT - the queue will get created if it does not already exist.
|
|
|
|
* O_WRONLY - we are only planning to write to the queue.
|
|
|
|
*
|
|
|
|
* Open the queue, and create it if the receiving process hasn't
|
|
|
|
* already created it.
|
|
|
|
*/
|
|
|
|
|
2020-12-31 17:49:00 +01:00
|
|
|
g_send_mqfd = mq_open("mqueue", O_WRONLY | O_CREAT, 0666, &attr);
|
2014-09-30 00:03:56 +02:00
|
|
|
if (g_send_mqfd == (mqd_t)-1)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2023-01-31 10:10:59 +01:00
|
|
|
printf("sender_thread: ERROR mq_open failed, errno=%d\n", errno);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-05 20:50:37 +01:00
|
|
|
pthread_exit((pthread_addr_t)1);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in a test message buffer to send */
|
|
|
|
|
|
|
|
memcpy(msg_buffer, TEST_MESSAGE, TEST_MSGLEN);
|
|
|
|
|
|
|
|
/* Perform the send TEST_SEND_NMSGS times */
|
|
|
|
|
|
|
|
for (i = 0; i < TEST_SEND_NMSGS; i++)
|
|
|
|
{
|
2013-02-05 20:50:37 +01:00
|
|
|
status = mq_send(g_send_mqfd, msg_buffer, TEST_MSGLEN, 42);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status < 0)
|
|
|
|
{
|
2023-01-31 10:10:59 +01:00
|
|
|
printf("sender_thread: ERROR mq_send failure=%d on msg %d, "
|
|
|
|
"errno=%d\n",
|
|
|
|
status, i, errno);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("sender_thread: mq_send succeeded on msg %d\n", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the queue and return success */
|
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
if (mq_close(g_send_mqfd) < 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2023-01-31 10:10:59 +01:00
|
|
|
printf("sender_thread: ERROR mq_close failed, errno=%d\n", errno);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2013-02-06 01:06:35 +01:00
|
|
|
else
|
|
|
|
{
|
2020-12-31 17:35:07 +01:00
|
|
|
g_send_mqfd = 0;
|
2013-02-06 01:06:35 +01:00
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("sender_thread: returning nerrors=%d\n", nerrors);
|
2015-12-30 23:29:21 +01:00
|
|
|
return (pthread_addr_t)((uintptr_t)nerrors);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *receiver_thread(void *arg)
|
|
|
|
{
|
|
|
|
char msg_buffer[TEST_MSGLEN];
|
|
|
|
struct mq_attr attr;
|
|
|
|
int nbytes;
|
|
|
|
int nerrors = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("receiver_thread: Starting\n");
|
|
|
|
|
|
|
|
/* Fill in attributes for message queue */
|
|
|
|
|
|
|
|
attr.mq_maxmsg = 20;
|
|
|
|
attr.mq_msgsize = TEST_MSGLEN;
|
|
|
|
attr.mq_flags = 0;
|
|
|
|
|
|
|
|
/* Set the flags for the open of the queue.
|
|
|
|
* Make it a blocking open on the queue, meaning it will block if
|
|
|
|
* this task tries to read from the queue when the queue is empty
|
|
|
|
*
|
|
|
|
* O_CREAT - the queue will get created if it does not already exist.
|
|
|
|
* O_RDONLY - we are only planning to read from the queue.
|
|
|
|
*
|
|
|
|
* Open the queue, and create it if the sending process hasn't
|
|
|
|
* already created it.
|
|
|
|
*/
|
|
|
|
|
2020-12-31 17:49:00 +01:00
|
|
|
g_recv_mqfd = mq_open("mqueue", O_RDONLY | O_CREAT, 0666, &attr);
|
2021-01-03 13:13:39 +01:00
|
|
|
if (g_recv_mqfd == (mqd_t)-1)
|
2020-12-31 17:49:00 +01:00
|
|
|
{
|
2023-01-31 10:10:59 +01:00
|
|
|
printf("receiver_thread: ERROR mq_open failed, errno=%d\n", errno);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2020-12-31 17:49:00 +01:00
|
|
|
pthread_exit((pthread_addr_t)1);
|
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-12-31 17:49:00 +01:00
|
|
|
/* Perform the receive TEST_RECEIVE_NMSGS times */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2020-12-31 17:49:00 +01:00
|
|
|
for (i = 0; i < TEST_RECEIVE_NMSGS; i++)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
memset(msg_buffer, 0xaa, TEST_MSGLEN);
|
2013-02-06 01:06:35 +01:00
|
|
|
nbytes = mq_receive(g_recv_mqfd, msg_buffer, TEST_MSGLEN, 0);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* mq_receive failed. If the error is because of EINTR then
|
|
|
|
* it is not a failure.
|
|
|
|
*/
|
|
|
|
|
2011-04-03 16:26:05 +02:00
|
|
|
if (errno != EINTR)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("receiver_thread: ERROR mq_receive failure on msg %d, "
|
|
|
|
"errno=%d\n", i, errno);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-26 17:24:43 +01:00
|
|
|
printf("receiver_thread: mq_receive interrupted!\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nbytes != TEST_MSGLEN)
|
|
|
|
{
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("receiver_thread: "
|
2022-10-15 16:36:29 +02:00
|
|
|
"ERROR mq_receive return bad size %d on msg %d\n",
|
2020-12-31 17:49:00 +01:00
|
|
|
nbytes, i);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
else if (memcmp(TEST_MESSAGE, msg_buffer, nbytes) != 0)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("receiver_thread: "
|
|
|
|
"mq_receive returned corrupt message on msg %d\n", i);
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("receiver_thread: i Expected Received\n");
|
|
|
|
|
2020-12-31 17:49:00 +01:00
|
|
|
for (j = 0; j < TEST_MSGLEN - 1; j++)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
if (isprint(msg_buffer[j]))
|
|
|
|
{
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("receiver_thread: "
|
|
|
|
"%2d %02x (%c) %02x (%c)\n",
|
|
|
|
j, TEST_MESSAGE[j], TEST_MESSAGE[j],
|
|
|
|
msg_buffer[j], msg_buffer[j]);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("receiver_thread: "
|
|
|
|
"%2d %02x (%c) %02x\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j]);
|
|
|
|
}
|
|
|
|
}
|
2013-02-05 20:50:37 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("receiver_thread: %2d 00 %02x\n",
|
|
|
|
j, msg_buffer[j]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("receiver_thread: mq_receive succeeded on msg %d\n", i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the queue and return success */
|
|
|
|
|
2013-02-06 01:06:35 +01:00
|
|
|
if (mq_close(g_recv_mqfd) < 0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
|
|
|
printf("receiver_thread: ERROR mq_close failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
nerrors++;
|
|
|
|
}
|
2013-02-06 01:06:35 +01:00
|
|
|
else
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2021-01-03 13:13:39 +01:00
|
|
|
g_recv_mqfd = 0;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("receiver_thread: returning nerrors=%d\n", nerrors);
|
2015-12-30 23:29:21 +01:00
|
|
|
pthread_exit((pthread_addr_t)((uintptr_t)nerrors));
|
|
|
|
return (pthread_addr_t)((uintptr_t)nerrors);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void mqueue_test(void)
|
|
|
|
{
|
|
|
|
pthread_t sender;
|
|
|
|
pthread_t receiver;
|
|
|
|
void *result;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
struct sched_param sparam;
|
2013-02-05 20:50:37 +01:00
|
|
|
FAR void *expected;
|
2011-03-20 19:18:19 +01:00
|
|
|
int prio_min;
|
|
|
|
int prio_max;
|
|
|
|
int prio_mid;
|
|
|
|
int status;
|
|
|
|
|
2013-02-06 01:06:35 +01:00
|
|
|
/* Reset globals for the beginning of the test */
|
|
|
|
|
2020-12-31 17:35:07 +01:00
|
|
|
g_send_mqfd = 0;
|
|
|
|
g_recv_mqfd = 0;
|
2013-02-06 01:06:35 +01:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Start the sending thread at higher priority */
|
|
|
|
|
|
|
|
printf("mqueue_test: Starting receiver\n");
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: ERROR pthread_attr_init failed, status=%d\n",
|
2020-12-31 17:49:00 +01:00
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_attr_setstacksize(&attr, STACKSIZE);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_attr_setstacksize failed, status=%d\n",
|
2020-12-31 17:49:00 +01:00
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prio_min = sched_get_priority_min(SCHED_FIFO);
|
|
|
|
prio_max = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
prio_mid = (prio_min + prio_max) / 2;
|
|
|
|
|
|
|
|
sparam.sched_priority = prio_mid;
|
2020-12-31 17:49:00 +01:00
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_attr_setschedparam failed, status=%d\n",
|
2020-12-31 17:49:00 +01:00
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("mqueue_test: Set receiver priority to %d\n",
|
|
|
|
sparam.sched_priority);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_create(&receiver, &attr, receiver_thread, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_create failed, status=%d\n", status);
|
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the sending thread at lower priority */
|
|
|
|
|
|
|
|
printf("mqueue_test: Starting sender\n");
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_attr_init failed, status=%d\n", status);
|
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_attr_setstacksize(&attr, STACKSIZE);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_attr_setstacksize failed, status=%d\n",
|
2015-12-30 23:29:21 +01:00
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sparam.sched_priority = (prio_min + prio_mid) / 2;
|
2020-12-31 17:49:00 +01:00
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_attr_setschedparam failed, status=%d\n",
|
2015-12-30 23:29:21 +01:00
|
|
|
status);
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-30 23:29:21 +01:00
|
|
|
printf("mqueue_test: Set sender thread priority to %d\n",
|
|
|
|
sparam.sched_priority);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_create(&sender, &attr, sender_thread, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2022-10-15 16:36:29 +02:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR pthread_create failed, status=%d\n", status);
|
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("mqueue_test: Waiting for sender to complete\n");
|
|
|
|
pthread_join(sender, &result);
|
2015-12-30 23:29:21 +01:00
|
|
|
if (result != (FAR void *)0)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2015-12-30 23:29:21 +01:00
|
|
|
printf("mqueue_test: ERROR sender thread exited with %d errors\n",
|
|
|
|
(int)((intptr_t)result));
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wake up the receiver thread with a signal */
|
|
|
|
|
|
|
|
printf("mqueue_test: Killing receiver\n");
|
2020-07-07 02:48:49 +02:00
|
|
|
pthread_kill(receiver, SIGUSR1);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Wait a bit to see if the thread exits on its own */
|
|
|
|
|
|
|
|
usleep(HALF_SECOND_USEC_USEC);
|
|
|
|
|
|
|
|
/* Then cancel the thread and see if it did */
|
|
|
|
|
|
|
|
printf("mqueue_test: Canceling receiver\n");
|
2013-02-05 20:50:37 +01:00
|
|
|
|
|
|
|
expected = PTHREAD_CANCELED;
|
2011-03-20 19:18:19 +01:00
|
|
|
status = pthread_cancel(receiver);
|
|
|
|
if (status == ESRCH)
|
|
|
|
{
|
|
|
|
printf("mqueue_test: receiver has already terminated\n");
|
2013-02-05 20:50:37 +01:00
|
|
|
expected = (FAR void *)0;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2013-02-05 20:50:37 +01:00
|
|
|
/* Check the result. If the pthread was canceled, PTHREAD_CANCELED is the
|
|
|
|
* correct result. Zero might be returned if the thread ran to completion
|
|
|
|
* before it was canceled.
|
|
|
|
*/
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
pthread_join(receiver, &result);
|
2013-02-05 20:50:37 +01:00
|
|
|
if (result != expected)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2020-12-31 17:49:00 +01:00
|
|
|
printf("mqueue_test: "
|
|
|
|
"ERROR receiver thread should have exited with %p\n",
|
2013-02-05 20:50:37 +01:00
|
|
|
expected);
|
|
|
|
printf(" ERROR Instead exited with nerrors=%d\n",
|
2015-12-30 23:29:21 +01:00
|
|
|
(int)((intptr_t)result));
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-05 20:50:37 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 21:40:03 +02:00
|
|
|
/* Message queues are global resources and persist for the life the
|
2020-12-31 17:49:00 +01:00
|
|
|
* task group. The message queue opened by the sender_thread must be
|
|
|
|
* closed since the sender pthread may have been canceled and may have
|
|
|
|
* left the message queue open.
|
2013-02-05 20:50:37 +01:00
|
|
|
*/
|
|
|
|
|
2013-02-06 16:43:28 +01:00
|
|
|
if (result == PTHREAD_CANCELED && g_recv_mqfd)
|
2013-02-05 20:50:37 +01:00
|
|
|
{
|
2013-02-06 16:43:28 +01:00
|
|
|
if (mq_close(g_recv_mqfd) < 0)
|
2013-02-06 01:06:35 +01:00
|
|
|
{
|
|
|
|
printf("mqueue_test: ERROR mq_close failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-06 01:06:35 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-06 16:43:28 +01:00
|
|
|
else if (result != PTHREAD_CANCELED && g_recv_mqfd)
|
2013-02-06 01:06:35 +01:00
|
|
|
{
|
|
|
|
printf("mqueue_test: ERROR send mqd_t left open\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-06 16:43:28 +01:00
|
|
|
if (mq_close(g_recv_mqfd) < 0)
|
2013-02-06 01:06:35 +01:00
|
|
|
{
|
|
|
|
printf("mqueue_test: ERROR mq_close failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-06 01:06:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the receive queue is closed as well */
|
|
|
|
|
2013-02-06 16:43:28 +01:00
|
|
|
if (g_send_mqfd)
|
2013-02-06 01:06:35 +01:00
|
|
|
{
|
2014-09-30 00:03:56 +02:00
|
|
|
printf("mqueue_test: ERROR receiver mqd_t left open\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-06 16:43:28 +01:00
|
|
|
if (mq_close(g_send_mqfd) < 0)
|
2013-02-06 01:06:35 +01:00
|
|
|
{
|
|
|
|
printf("sender_thread: ERROR mq_close failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2013-02-06 01:06:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the message queue */
|
|
|
|
|
|
|
|
if (mq_unlink("mqueue") < 0)
|
|
|
|
{
|
|
|
|
printf("mqueue_test: ERROR mq_unlink failed\n");
|
2022-10-15 16:36:29 +02:00
|
|
|
ASSERT(false);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|