Skip to content

Commit

Permalink
Fix #323: Avoid infinite sem-speed-test
Browse files Browse the repository at this point in the history
1. Add a hard limit to the execution (work) counter for the
sem-speed-test worker threads.  With this they will self-exit
eventually even if the root task never gets CPU time to stop them.

2. Set the priority of the worker tasks to be logically lower, so
it is less likely to preempt the main task.

Note that the logical priority is inverted from the numeric value.
  • Loading branch information
jphickey authored and skliper committed Dec 30, 2019
1 parent cb259b9 commit e8a1db7
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/tests/sem-speed-test/sem-speed-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@
#include "uttest.h"
#include "utbsp.h"

/*
* Note the worker priority must be lower than that of
* the executive (init) task. Otherwise, the SemRun()
* function may may never get CPU time to stop the test.
*/
#define SEMTEST_TASK_PRIORITY 150

/*
* A limit for the maximum amount
* of iterations that this test will
* perform. This prevents the test
* from running infinitely in case the
* time-based stop routine does not
* work correctly. See note above
* about priority requirements.
*/
#define SEMTEST_WORK_LIMIT 10000000


/* Define setup and test functions for UT assert */
void SemSetup(void);
void SemRun(void);
Expand All @@ -54,8 +73,6 @@ void SemRun(void);
*/
#define SEMOP(op) SEMCALL(Count,op)

#define TASK_PRIORITY 50

uint32 task_1_id;
uint32 task_1_work;

Expand All @@ -72,7 +89,7 @@ void task_1(void)
OS_printf("Starting task 1\n");
OS_TaskRegister();

while(1)
while(task_1_work < SEMTEST_WORK_LIMIT)
{
status = SEMOP(Take)(sem_id_1);
if ( status != OS_SUCCESS )
Expand All @@ -99,7 +116,7 @@ void task_2(void)
OS_printf("Starting task 2\n");
OS_TaskRegister();

while(1)
while(task_2_work < SEMTEST_WORK_LIMIT)
{
status = SEMOP(Take)(sem_id_2);
if ( status != OS_SUCCESS )
Expand Down Expand Up @@ -150,10 +167,10 @@ void SemSetup(void)
/*
** Create the tasks
*/
status = OS_TaskCreate( &task_1_id, "Task 1", task_1, NULL, 4096, TASK_PRIORITY, 0);
status = OS_TaskCreate( &task_1_id, "Task 1", task_1, NULL, 4096, SEMTEST_TASK_PRIORITY, 0);
UtAssert_True(status == OS_SUCCESS, "Task 1 create Id=%u Rc=%d", (unsigned int)task_1_id, (int)status);

status = OS_TaskCreate( &task_2_id, "Task 2", task_2, NULL, 4096, TASK_PRIORITY, 0);
status = OS_TaskCreate( &task_2_id, "Task 2", task_2, NULL, 4096, SEMTEST_TASK_PRIORITY, 0);
UtAssert_True(status == OS_SUCCESS, "Task 2 create Id=%u Rc=%d", (unsigned int)task_2_id, (int)status);

/* A small delay just to allow the tasks
Expand All @@ -173,6 +190,9 @@ void SemRun(void)

/*
** Delete resources
**
** NOTE: if the work limit was reached, the
** OS_TaskDelete calls may return non-success.
*/
status = OS_TaskDelete( task_1_id );
UtAssert_True(status == OS_SUCCESS, "Task 1 delete Rc=%d", (int)status);
Expand Down

0 comments on commit e8a1db7

Please sign in to comment.