2017-03-26 22:45:48 +02:00
|
|
|
/****************************************************************************
|
2021-06-10 07:51:27 +02:00
|
|
|
* apps/testing/ostest/robust.c
|
2017-03-26 22:45:48 +02: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
|
2017-03-26 22:45:48 +02:00
|
|
|
*
|
2021-06-10 07:51:27 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-26 22:45:48 +02: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.
|
2017-03-26 22:45:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "ostest.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static pthread_mutex_t g_robust_mutex;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR void *robust_waiter(FAR void *parameter)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Take the mutex */
|
|
|
|
|
|
|
|
printf("robust_waiter: Taking mutex\n");
|
|
|
|
status = pthread_mutex_lock(&g_robust_mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("thread_waiter: ERROR: pthread_mutex_lock failed, status=%d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_waiter: ERROR: pthread_mutex_lock failed, status=%d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-27 15:46:13 +01:00
|
|
|
printf("robust_waiter: Exiting with mutex\n");
|
2017-03-26 22:45:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void robust_test(void)
|
|
|
|
{
|
2017-03-27 14:55:51 +02:00
|
|
|
pthread_attr_t pattr;
|
2017-03-27 16:58:23 +02:00
|
|
|
pthread_mutexattr_t mattr;
|
2017-03-26 22:45:48 +02:00
|
|
|
pthread_t waiter;
|
|
|
|
void *result;
|
|
|
|
int nerrors = 0;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Initialize the mutex */
|
|
|
|
|
|
|
|
printf("robust_test: Initializing mutex\n");
|
2017-03-27 14:55:51 +02:00
|
|
|
|
|
|
|
status = pthread_mutexattr_init(&mattr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: "
|
|
|
|
"pthread_mutexattr_init failed, status=%d\n",
|
2017-03-27 14:55:51 +02:00
|
|
|
status);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: "
|
|
|
|
"pthread_mutexattr_setrobust failed, status=%d\n",
|
2017-03-27 14:55:51 +02:00
|
|
|
status);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_mutex_init(&g_robust_mutex, &mattr);
|
2017-03-26 22:45:48 +02:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("robust_test: ERROR: pthread_mutex_init failed, status=%d\n",
|
|
|
|
status);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up pthread attributes */
|
|
|
|
|
2017-03-27 14:55:51 +02:00
|
|
|
printf("robust_test: Starting thread\n");
|
|
|
|
|
|
|
|
status = pthread_attr_init(&pattr);
|
2017-03-26 22:45:48 +02:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("robust_test: ERROR: pthread_attr_init failed, status=%d\n",
|
|
|
|
status);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
2017-03-27 14:55:51 +02:00
|
|
|
status = pthread_attr_setstacksize(&pattr, STACKSIZE);
|
2017-03-26 22:45:48 +02:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: "
|
|
|
|
"pthread_attr_setstacksize failed, status=%d\n",
|
2017-03-26 22:45:48 +02:00
|
|
|
status);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the robust waiter thread. It will take the mutex, sleep for two
|
|
|
|
* seconds, and exit holding the mutex.
|
|
|
|
*/
|
|
|
|
|
2017-03-27 14:55:51 +02:00
|
|
|
status = pthread_create(&waiter, &pattr, robust_waiter, NULL);
|
2017-03-26 22:45:48 +02:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: "
|
|
|
|
"pthread_create failed, status=%d\n", status);
|
2017-03-26 22:45:48 +02:00
|
|
|
printf(" ERROR: Terminating test\n");
|
|
|
|
nerrors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait one second.. the robust waiter should still be waiting */
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
/* Now try to take the mutex held by the robust waiter. This should wait
|
|
|
|
* one second there fail with EOWNERDEAD.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status = pthread_mutex_lock(&g_robust_mutex);
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
printf("robust_test: ERROR: pthread_mutex_lock succeeded\n");
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
else if (status != EOWNERDEAD)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: pthread_mutex_lock failed with %d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
printf(" ERROR: expected %d (EOWNERDEAD)\n", EOWNERDEAD);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
2021-06-10 11:56:16 +02:00
|
|
|
/* Try again,
|
|
|
|
* this should return immediately, still failing with EOWNERDEAD
|
|
|
|
*/
|
2017-03-26 22:45:48 +02:00
|
|
|
|
|
|
|
printf("robust_test: Take the lock again\n");
|
|
|
|
status = pthread_mutex_lock(&g_robust_mutex);
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
printf("robust_test: ERROR: pthread_mutex_lock succeeded\n");
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
else if (status != EOWNERDEAD)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: pthread_mutex_lock failed with %d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
printf(" ERROR: expected %d (EOWNERDEAD)\n", EOWNERDEAD);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the mutex consistent and try again. It should succeed this time. */
|
|
|
|
|
|
|
|
printf("robust_test: Make the mutex consistent again.\n");
|
|
|
|
status = pthread_mutex_consistent(&g_robust_mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: pthread_mutex_consistent failed: %d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("robust_test: Take the lock again\n");
|
|
|
|
status = pthread_mutex_lock(&g_robust_mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: pthread_mutex_lock failed with: %d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then join to the thread to pick up the result (if we don't do this we
|
|
|
|
* will have a memory leak!)
|
|
|
|
*/
|
|
|
|
|
|
|
|
printf("robust_test: Joining\n");
|
|
|
|
status = pthread_join(waiter, &result);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("robust_test: ERROR: pthread_join failed, status=%d\n", status);
|
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("robust_test: waiter exited with result=%p\n", result);
|
|
|
|
if (result != NULL)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: expected result=%p\n",
|
|
|
|
PTHREAD_CANCELED);
|
2017-03-26 22:45:48 +02:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-22 19:52:12 +01:00
|
|
|
/* Release and destroy the mutex then return success */
|
2017-03-26 22:45:48 +02:00
|
|
|
|
|
|
|
status = pthread_mutex_unlock(&g_robust_mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: pthread_mutex_unlock failed, status=%d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_mutex_destroy(&g_robust_mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("robust_test: ERROR: pthread_mutex_unlock failed, status=%d\n",
|
|
|
|
status);
|
2017-03-26 22:45:48 +02:00
|
|
|
nerrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("robust_test: Test complete with nerrors=%d\n", nerrors);
|
|
|
|
}
|