2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2021-06-10 07:51:27 +02:00
|
|
|
* apps/testing/ostest/timedwait.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 <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#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
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Private Data
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
static pthread_mutex_t mutex;
|
|
|
|
static pthread_cond_t cond;
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Private Functions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
static void *thread_waiter(void *parameter)
|
|
|
|
{
|
2011-04-27 01:11:02 +02:00
|
|
|
struct timespec ts;
|
2011-03-20 19:18:19 +01:00
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Take the mutex */
|
|
|
|
|
|
|
|
printf("thread_waiter: Taking mutex\n");
|
|
|
|
status = pthread_mutex_lock(&mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("thread_waiter: ERROR pthread_mutex_lock failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("thread_waiter: Starting 5 second wait for condition\n");
|
|
|
|
|
2011-04-27 01:11:02 +02:00
|
|
|
status = clock_gettime(CLOCK_REALTIME, &ts);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("thread_waiter: ERROR clock_gettime failed\n");
|
|
|
|
}
|
2021-06-10 11:56:16 +02:00
|
|
|
|
2011-04-27 01:11:02 +02:00
|
|
|
ts.tv_sec += 5;
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* The wait -- no-one is ever going to awaken us */
|
|
|
|
|
2011-04-27 01:11:02 +02:00
|
|
|
status = pthread_cond_timedwait(&cond, &mutex, &ts);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
if (status == ETIMEDOUT)
|
|
|
|
{
|
|
|
|
printf("thread_waiter: pthread_cond_timedwait timed out\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("thread_waiter: "
|
|
|
|
"ERROR pthread_cond_timedwait failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("thread_waiter: ERROR "
|
|
|
|
"pthread_cond_timedwait returned without timeout, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the mutex */
|
|
|
|
|
|
|
|
printf("thread_waiter: Releasing mutex\n");
|
|
|
|
status = pthread_mutex_unlock(&mutex);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("thread_waiter: ERROR pthread_mutex_unlock failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("thread_waiter: Exit with status 0x12345678\n");
|
|
|
|
pthread_exit((pthread_addr_t)0x12345678);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:33:30 +02:00
|
|
|
/****************************************************************************
|
2011-03-20 19:18:19 +01:00
|
|
|
* Public Definitions
|
2015-10-03 01:33:30 +02:00
|
|
|
****************************************************************************/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
void timedwait_test(void)
|
|
|
|
{
|
|
|
|
pthread_t waiter;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
struct sched_param sparam;
|
|
|
|
void *result;
|
|
|
|
int prio_max;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Initialize the mutex */
|
|
|
|
|
|
|
|
printf("thread_waiter: Initializing mutex\n");
|
|
|
|
status = pthread_mutex_init(&mutex, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("timedwait_test: ERROR pthread_mutex_init failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the condition variable */
|
|
|
|
|
|
|
|
printf("timedwait_test: Initializing cond\n");
|
|
|
|
status = pthread_cond_init(&cond, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("timedwait_test: ERROR pthread_condinit failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the waiter thread at higher priority */
|
|
|
|
|
|
|
|
printf("timedwait_test: Starting waiter\n");
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("timedwait_test: pthread_attr_init failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prio_max = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
status = sched_getparam (getpid(), &sparam);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("timedwait_test: sched_getparam failed\n");
|
|
|
|
sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
sparam.sched_priority = (prio_max + sparam.sched_priority) / 2;
|
2021-06-10 11:56:16 +02:00
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("timedwait_test: "
|
|
|
|
"pthread_attr_setschedparam failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("timedwait_test: Set thread 2 priority to %d\n",
|
|
|
|
sparam.sched_priority);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_create(&waiter, &attr, thread_waiter, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("timedwait_test: pthread_create failed, status=%d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("timedwait_test: Joining\n");
|
|
|
|
FFLUSH();
|
|
|
|
status = pthread_join(waiter, &result);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2021-06-10 11:56:16 +02:00
|
|
|
printf("timedwait_test: ERROR pthread_join failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("timedwait_test: waiter exited with result=%p\n", result);
|
|
|
|
}
|
|
|
|
}
|