2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-10 07:51:27 +02:00
|
|
|
* apps/testing/ostest/prioinherit.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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_SIM
|
|
|
|
# include <nuttx/arch.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ostest.h"
|
|
|
|
|
2017-01-24 16:15:42 +01:00
|
|
|
#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_PTHREAD)
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-02 22:06:11 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_SEM_PREALLOCHOLDERS
|
|
|
|
# define CONFIG_SEM_PREALLOCHOLDERS 0
|
|
|
|
#endif
|
2012-08-26 23:35:14 +02:00
|
|
|
|
2012-08-28 16:40:12 +02:00
|
|
|
/* If resources were configured for lots of holders, then run 3 low priority
|
|
|
|
* threads. Otherwise, just one.
|
|
|
|
*/
|
|
|
|
|
2012-08-26 23:35:14 +02:00
|
|
|
#if CONFIG_SEM_PREALLOCHOLDERS > 3
|
|
|
|
# define NLOWPRI_THREADS 3
|
|
|
|
#else
|
2012-08-28 16:40:12 +02:00
|
|
|
# define NLOWPRI_THREADS 1
|
2012-08-26 23:35:14 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_SEM_NNESTPRIO
|
|
|
|
# define CONFIG_SEM_NNESTPRIO 0
|
|
|
|
#endif
|
2012-08-26 23:35:14 +02:00
|
|
|
|
2012-08-28 16:40:12 +02:00
|
|
|
/* Where resources configured for lots of waiters? If so then run 3 high
|
|
|
|
* priority threads. Otherwise, just one.
|
|
|
|
*/
|
|
|
|
|
2012-08-26 23:35:14 +02:00
|
|
|
#if CONFIG_SEM_NNESTPRIO > 3
|
|
|
|
# define NHIGHPRI_THREADS 3
|
|
|
|
#else
|
2012-08-28 16:40:12 +02:00
|
|
|
# define NHIGHPRI_THREADS 1
|
2012-08-26 23:35:14 +02:00
|
|
|
#endif
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
enum thstate_e
|
|
|
|
{
|
|
|
|
NOTSTARTED = 0,
|
|
|
|
RUNNING,
|
|
|
|
WAITING,
|
|
|
|
DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
static sem_t g_sem;
|
|
|
|
static volatile enum thstate_e g_middlestate;
|
|
|
|
static volatile enum thstate_e g_highstate[NHIGHPRI_THREADS];
|
|
|
|
static volatile enum thstate_e g_lowstate[NLOWPRI_THREADS];
|
|
|
|
static int g_highpri;
|
|
|
|
static int g_medpri;
|
|
|
|
static int g_lowpri;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nhighpri_waiting
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nhighpri_waiting(void)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
int n = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++)
|
|
|
|
{
|
|
|
|
if (g_highstate[i] == WAITING)
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nhighpri_running
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int nhighpri_running(void)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
int n = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++)
|
|
|
|
{
|
|
|
|
if (g_highstate[i] != DONE)
|
|
|
|
{
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: highpri_thread
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *highpri_thread(void *parameter)
|
|
|
|
{
|
2016-10-26 15:34:30 +02:00
|
|
|
int threadno = (int)((intptr_t)parameter);
|
2011-03-20 19:18:19 +01:00
|
|
|
int ret;
|
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
g_highstate[threadno - 1] = RUNNING;
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
printf("highpri_thread-%d: Started\n", threadno);
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
printf("highpri_thread-%d: Calling sem_wait()\n", threadno);
|
2020-04-30 07:33:22 +02:00
|
|
|
g_highstate[threadno - 1] = WAITING;
|
2011-03-20 19:18:19 +01:00
|
|
|
ret = sem_wait(&g_sem);
|
2020-04-30 07:33:22 +02:00
|
|
|
g_highstate[threadno - 1] = DONE;
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("highpri_thread-%d: sem_take failed: %d\n", threadno, ret);
|
|
|
|
}
|
|
|
|
else if (g_middlestate == RUNNING)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("highpri_thread-%d: SUCCESS midpri_thread is still running!\n",
|
|
|
|
threadno);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("highpri_thread-%d: ERROR -- "
|
|
|
|
"midpri_thread has already exited!\n",
|
|
|
|
threadno);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&g_sem);
|
|
|
|
printf("highpri_thread-%d: Okay... I'm done!\n", threadno);
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: hog_cpu
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void hog_cpu(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_ARCH_SIM
|
|
|
|
/* The simulator doesn't have any mechanism to do asynchronous pre-emption
|
2015-04-23 22:04:43 +02:00
|
|
|
* (basically because it doesn't have any interrupts/asynchronous events).
|
2011-03-20 19:18:19 +01:00
|
|
|
* The simulator does "fake" a timer interrupt in up_idle() -- the idle
|
2020-04-30 07:33:22 +02:00
|
|
|
* thread that only executes when nothing else is running. In the
|
|
|
|
* simulator, we cannot suspend the middle priority task, or we wouldn't
|
|
|
|
* have the test that we want. So, we have no option but to pump the fake
|
|
|
|
* clock here by calling up_idle(). Sigh!
|
2011-03-20 19:18:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
up_idle();
|
|
|
|
#else
|
|
|
|
/* On real platforms with a real timer interrupt, we really can hog the
|
|
|
|
* CPU. When the sleep() goes off in priority_inheritance(), it will
|
|
|
|
* wake up and start the high priority thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
volatile int i;
|
|
|
|
for (i = 0; i < INT_MAX; i++);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: medpri_thread
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *medpri_thread(void *parameter)
|
|
|
|
{
|
|
|
|
printf("medpri_thread: Started ... I won't let go of the CPU!\n");
|
|
|
|
g_middlestate = RUNNING;
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* The following loop will completely block lowpri_thread from running.
|
|
|
|
* UNLESS priority inheritance is working. In that case, its priority
|
|
|
|
* will be boosted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (nhighpri_running() > 0)
|
|
|
|
{
|
|
|
|
hog_cpu();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("medpri_thread: Okay... I'm done!\n");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
g_middlestate = DONE;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: lowpri_thread
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *lowpri_thread(void *parameter)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
void *retval = (void *) - 1;
|
2011-03-20 19:18:19 +01:00
|
|
|
struct sched_param sparam;
|
2016-10-26 15:34:30 +02:00
|
|
|
int threadno = (int)((intptr_t)parameter);
|
2011-03-20 19:18:19 +01:00
|
|
|
int expected;
|
|
|
|
int count;
|
|
|
|
int policy;
|
|
|
|
int ret;
|
2020-04-30 06:52:25 +02:00
|
|
|
int nwaiting = 0;
|
2011-03-20 19:18:19 +01:00
|
|
|
int i;
|
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
g_lowstate[threadno - 1] = RUNNING;
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("lowpri_thread-%d: Started\n", threadno);
|
|
|
|
|
|
|
|
ret = pthread_getschedparam(pthread_self(), &policy, &sparam);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: ERROR pthread_getschedparam failed: %d\n",
|
|
|
|
threadno, ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: initial priority: %d\n", threadno,
|
|
|
|
sparam.sched_priority);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (sparam.sched_priority != g_lowpri)
|
|
|
|
{
|
|
|
|
printf(" ERROR should have been %d\n", g_lowpri);
|
2014-04-14 00:24:28 +02:00
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
g_lowstate[threadno - 1] = WAITING;
|
2011-03-20 19:18:19 +01:00
|
|
|
ret = sem_wait(&g_sem);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
printf("lowpri_thread-%d: sem_take failed: %d\n", threadno, ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Hang on to the thread until the middle priority thread runs */
|
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
while (g_middlestate == NOTSTARTED &&
|
|
|
|
nhighpri_waiting() < NHIGHPRI_THREADS)
|
2011-03-20 19:18:19 +01:00
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: Waiting for the midle pri task to run\n",
|
|
|
|
threadno);
|
2011-03-20 19:18:19 +01:00
|
|
|
printf(" g_middlestate: %d\n", (int)g_middlestate);
|
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++)
|
|
|
|
{
|
|
|
|
printf(" g_highstate[%d]: %d\n", i, (int)g_highstate[i]);
|
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf(" I still have a count on the semaphore\n");
|
|
|
|
sem_enumholders(&g_sem);
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
/* Account for all of the semaphore counts. At any given time if there
|
|
|
|
* are 'n' running high prioity tasks, then the semaphore count should
|
|
|
|
* be '-n'
|
2011-03-20 19:18:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock(); /* Needs to be atomic */
|
|
|
|
ret = sem_getvalue(&g_sem, &count);
|
|
|
|
nwaiting = nhighpri_waiting();
|
|
|
|
sched_unlock();
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: ERROR sem_getvalue failed: %d\n",
|
|
|
|
threadno, errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: Sem count: %d, No. highpri thread: %d\n",
|
|
|
|
threadno, count, nwaiting);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* The middle priority task is running, let go of the semaphore */
|
|
|
|
|
|
|
|
if (g_middlestate == RUNNING && nwaiting == -count)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
/* Good.. the middle priority task is still running and the counts
|
|
|
|
* are okay.
|
|
|
|
*/
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
retval = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
/* If the sem count is positive, then there all of the higher
|
|
|
|
* priority threads should have already completed.
|
2011-03-20 19:18:19 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: %s the middle priority task has already"
|
|
|
|
" exitted!\n",
|
|
|
|
threadno, count >= 0 ? "SUCCESS" : "ERROR");
|
|
|
|
printf(" g_middlestate: %d sem count=%d\n",
|
|
|
|
(int)g_middlestate, count);
|
2011-03-20 19:18:19 +01:00
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf(" g_highstate[%d]: %d\n",
|
|
|
|
i, (int)g_highstate[i]);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_getschedparam(pthread_self(), &policy, &sparam);
|
|
|
|
sem_enumholders(&g_sem);
|
|
|
|
sem_post(&g_sem);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: ERROR pthread_getschedparam failed: %d\n",
|
|
|
|
threadno, ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nwaiting > 0)
|
|
|
|
{
|
|
|
|
expected = g_highpri;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expected = g_lowpri;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("lowpri_thread-%d: %s priority before sem_post: %d\n",
|
|
|
|
threadno,
|
|
|
|
sparam.sched_priority != expected ? "ERROR" : "SUCCESS",
|
|
|
|
sparam.sched_priority);
|
|
|
|
|
|
|
|
if (sparam.sched_priority != expected)
|
|
|
|
{
|
|
|
|
printf(" ERROR should have been %d\n", expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_getschedparam(pthread_self(), &policy, &sparam);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("lowpri_thread-%d: ERROR pthread_getschedparam failed: %d\n",
|
|
|
|
threadno, ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("lowpri_thread-%d: %s final priority: %d\n",
|
|
|
|
threadno,
|
|
|
|
sparam.sched_priority != g_lowpri ? "ERROR" : "SUCCESS",
|
|
|
|
sparam.sched_priority);
|
|
|
|
|
|
|
|
if (sparam.sched_priority != g_lowpri)
|
|
|
|
{
|
|
|
|
printf(" ERROR should have been %d\n", g_lowpri);
|
2014-04-14 00:24:28 +02:00
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
sem_enumholders(&g_sem);
|
|
|
|
|
|
|
|
printf("lowpri_thread-%d: Okay... I'm done!\n", threadno);
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2020-04-30 07:33:22 +02:00
|
|
|
g_lowstate[threadno - 1] = DONE;
|
2014-04-14 00:24:28 +02:00
|
|
|
return retval;
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2017-01-24 16:15:42 +01:00
|
|
|
#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_PTHREAD */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: priority_inheritance
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void priority_inheritance(void)
|
|
|
|
{
|
2017-01-24 16:15:42 +01:00
|
|
|
#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_PTHREAD)
|
2011-03-20 19:18:19 +01:00
|
|
|
pthread_t lowpri[NLOWPRI_THREADS];
|
|
|
|
pthread_t medpri;
|
|
|
|
pthread_t highpri[NHIGHPRI_THREADS];
|
|
|
|
pthread_addr_t result;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
struct sched_param sparam;
|
|
|
|
int my_pri;
|
|
|
|
int status;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("priority_inheritance: Started\n");
|
|
|
|
|
|
|
|
g_middlestate = NOTSTARTED;
|
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++) g_highstate[i] = NOTSTARTED;
|
|
|
|
for (i = 0; i < NLOWPRI_THREADS; i++) g_lowstate[i] = NOTSTARTED;
|
|
|
|
|
|
|
|
status = sched_getparam (getpid(), &sparam);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
printf("priority_inheritance: sched_getparam failed\n");
|
|
|
|
sparam.sched_priority = PTHREAD_DEFAULT_PRIORITY;
|
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
my_pri = sparam.sched_priority;
|
|
|
|
|
|
|
|
g_highpri = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
g_lowpri = sched_get_priority_min(SCHED_FIFO);
|
|
|
|
g_medpri = my_pri - 1;
|
|
|
|
|
|
|
|
sem_init(&g_sem, 0, NLOWPRI_THREADS);
|
|
|
|
dump_nfreeholders("priority_inheritance:");
|
|
|
|
|
|
|
|
/* Start the low priority threads */
|
|
|
|
|
|
|
|
for (i = 0; i < NLOWPRI_THREADS; i++)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
int threadno = i + 1;
|
|
|
|
printf("priority_inheritance: "
|
|
|
|
"Starting lowpri_thread-%d (of %d) at %d\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
threadno, NLOWPRI_THREADS, g_lowpri);
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_attr_init failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
sparam.sched_priority = g_lowpri;
|
2020-04-30 07:33:22 +02:00
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_attr_setschedparam failed, status=%d\n",
|
2016-10-26 15:34:30 +02:00
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"Set lowpri_thread-%d priority to %d\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
threadno, sparam.sched_priority);
|
|
|
|
}
|
|
|
|
|
2016-10-26 15:34:30 +02:00
|
|
|
status = pthread_create(&lowpri[i], &attr, lowpri_thread,
|
2020-04-30 07:33:22 +02:00
|
|
|
(void *)((uintptr_t)threadno));
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_create failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("priority_inheritance: Waiting...\n");
|
|
|
|
sleep(2);
|
|
|
|
dump_nfreeholders("priority_inheritance:");
|
|
|
|
|
|
|
|
/* Start the medium priority thread */
|
|
|
|
|
|
|
|
printf("priority_inheritance: Starting medpri_thread at %d\n", g_medpri);
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_attr_init failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sparam.sched_priority = g_medpri;
|
2020-04-30 07:33:22 +02:00
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_attr_setschedparam failed, status=%d\n",
|
2016-10-26 15:34:30 +02:00
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-26 15:34:30 +02:00
|
|
|
printf("priority_inheritance: Set medpri_thread priority to %d\n",
|
|
|
|
sparam.sched_priority);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2020-04-30 07:33:22 +02:00
|
|
|
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
status = pthread_create(&medpri, &attr, medpri_thread, NULL);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: pthread_create failed, status=%d\n",
|
|
|
|
status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("priority_inheritance: Waiting...\n");
|
|
|
|
sleep(1);
|
|
|
|
dump_nfreeholders("priority_inheritance:");
|
|
|
|
|
|
|
|
/* Start the high priority threads */
|
|
|
|
|
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
int threadno = i + 1;
|
|
|
|
printf("priority_inheritance: "
|
|
|
|
"Starting highpri_thread-%d (of %d) at %d\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
threadno, NHIGHPRI_THREADS, g_highpri);
|
|
|
|
status = pthread_attr_init(&attr);
|
|
|
|
if (status != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_attr_init failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sparam.sched_priority = g_highpri - i;
|
2020-04-30 07:33:22 +02:00
|
|
|
status = pthread_attr_setschedparam(&attr, &sparam);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != OK)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_attr_setschedparam failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"Set highpri_thread-%d priority to %d\n",
|
2011-03-20 19:18:19 +01:00
|
|
|
threadno, sparam.sched_priority);
|
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
|
2016-10-26 15:34:30 +02:00
|
|
|
status = pthread_create(&highpri[i], &attr, highpri_thread,
|
2020-04-30 07:33:22 +02:00
|
|
|
(void *)((uintptr_t)threadno));
|
2011-03-20 19:18:19 +01:00
|
|
|
if (status != 0)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"pthread_create failed, status=%d\n", status);
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
dump_nfreeholders("priority_inheritance:");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Wait for all thread instances to complete */
|
|
|
|
|
|
|
|
for (i = 0; i < NHIGHPRI_THREADS; i++)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"Waiting for highpri_thread-%d to complete\n", i + 1);
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_join(highpri[i], &result);
|
2011-03-20 19:18:19 +01:00
|
|
|
dump_nfreeholders("priority_inheritance:");
|
2016-10-26 15:34:30 +02:00
|
|
|
}
|
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
printf("priority_inheritance: Waiting for medpri_thread to complete\n");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_join(medpri, &result);
|
2011-03-20 19:18:19 +01:00
|
|
|
dump_nfreeholders("priority_inheritance:");
|
2016-10-26 15:34:30 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
for (i = 0; i < NLOWPRI_THREADS; i++)
|
|
|
|
{
|
2020-04-30 07:33:22 +02:00
|
|
|
printf("priority_inheritance: "
|
|
|
|
"Waiting for lowpri_thread-%d to complete\n", i + 1);
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_join(lowpri[i], &result);
|
2011-03-20 19:18:19 +01:00
|
|
|
dump_nfreeholders("priority_inheritance:");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("priority_inheritance: Finished\n");
|
|
|
|
sem_destroy(&g_sem);
|
|
|
|
dump_nfreeholders("priority_inheritance:");
|
2011-05-28 23:42:18 +02:00
|
|
|
FFLUSH();
|
2017-01-24 16:15:42 +01:00
|
|
|
#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_PTHREAD */
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|