Skip to content

Commit 7db0e87

Browse files
chinglee-iotSkptak
andauthored
Update taskYIELD_IF_USING_PREEMPTION macro (#769)
* Add taskYIELD_TASK_CORE_IF_USING_PREEMPTION and taskYIELD_ANY_CORE_IF_USING_PREEMPTION to align task yield behavior for single core and SMP. --------- Co-authored-by: Soren Ptak <[email protected]>
1 parent 26c48de commit 7db0e87

File tree

1 file changed

+63
-88
lines changed

1 file changed

+63
-88
lines changed

tasks.c

Lines changed: 63 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,49 @@
5858
#include <stdio.h>
5959
#endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
6060

61-
#if ( configNUMBER_OF_CORES == 1 )
62-
#if ( configUSE_PREEMPTION == 0 )
61+
#if ( configUSE_PREEMPTION == 0 )
6362

6463
/* If the cooperative scheduler is being used then a yield should not be
6564
* performed just because a higher priority task has been woken. */
66-
#define taskYIELD_IF_USING_PREEMPTION()
67-
#else
68-
#define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
69-
#endif
70-
#endif /* if ( configNUMBER_OF_CORES == 1 ) */
65+
#define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66+
#define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
67+
#else
68+
69+
#if ( configNUMBER_OF_CORES == 1 )
70+
71+
/* This macro requests the running task pxTCB to yield. In single core
72+
* scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73+
* can be used to request the task running on core 0 to yield. Therefore, pxTCB
74+
* is not used in this macro. */
75+
#define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
76+
do { \
77+
( void ) ( pxTCB ); \
78+
portYIELD_WITHIN_API(); \
79+
} while( 0 )
80+
81+
#define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
82+
do { \
83+
if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
84+
{ \
85+
portYIELD_WITHIN_API(); \
86+
} \
87+
else \
88+
{ \
89+
mtCOVERAGE_TEST_MARKER(); \
90+
} \
91+
} while( 0 )
92+
93+
#else /* if ( configNUMBER_OF_CORES == 1 ) */
94+
95+
/* Yield the core on which this task is running. */
96+
#define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
97+
98+
/* Yield for the task if a running task has priority lower than this task. */
99+
#define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
100+
101+
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
102+
103+
#endif /* if ( configUSE_PREEMPTION == 0 ) */
71104

72105
/* Values that can be assigned to the ucNotifyState member of the TCB. */
73106
#define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
@@ -1776,14 +1809,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
17761809
{
17771810
/* If the created task is of a higher priority than the current task
17781811
* then it should run now. */
1779-
if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1780-
{
1781-
taskYIELD_IF_USING_PREEMPTION();
1782-
}
1783-
else
1784-
{
1785-
mtCOVERAGE_TEST_MARKER();
1786-
}
1812+
taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
17871813
}
17881814
else
17891815
{
@@ -1859,9 +1885,7 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
18591885
/* If the created task is of a higher priority than another
18601886
* currently running task and preemption is on then it should
18611887
* run now. */
1862-
#if ( configUSE_PREEMPTION == 1 )
1863-
prvYieldForTask( pxNewTCB );
1864-
#endif
1888+
taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
18651889
}
18661890
else
18671891
{
@@ -2525,37 +2549,27 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
25252549
#endif
25262550
}
25272551

2528-
#if ( configNUMBER_OF_CORES == 1 )
2552+
if( xYieldRequired != pdFALSE )
25292553
{
2530-
if( xYieldRequired != pdFALSE )
2531-
{
2532-
taskYIELD_IF_USING_PREEMPTION();
2533-
}
2534-
else
2535-
{
2536-
mtCOVERAGE_TEST_MARKER();
2537-
}
2554+
/* The running task priority is set down. Request the task to yield. */
2555+
taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
25382556
}
2539-
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
2557+
else
25402558
{
2541-
#if ( configUSE_PREEMPTION == 1 )
2542-
{
2543-
if( xYieldRequired != pdFALSE )
2544-
{
2545-
prvYieldCore( pxTCB->xTaskRunState );
2546-
}
2547-
else if( xYieldForTask != pdFALSE )
2559+
#if ( configNUMBER_OF_CORES > 1 )
2560+
if( xYieldForTask != pdFALSE )
25482561
{
2549-
prvYieldForTask( pxTCB );
2562+
/* The priority of the task is being raised. If a running
2563+
* task has priority lower than this task, it should yield
2564+
* for this task. */
2565+
taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
25502566
}
25512567
else
2552-
{
2553-
mtCOVERAGE_TEST_MARKER();
2554-
}
2568+
#endif /* if ( configNUMBER_OF_CORES > 1 ) */
2569+
{
2570+
mtCOVERAGE_TEST_MARKER();
25552571
}
2556-
#endif /* #if ( configUSE_PREEMPTION == 1 ) */
25572572
}
2558-
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
25592573

25602574
/* Remove compiler warning about unused variables when the port
25612575
* optimised task selection is not being used. */
@@ -2939,30 +2953,10 @@ static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
29392953
( void ) uxListRemove( &( pxTCB->xStateListItem ) );
29402954
prvAddTaskToReadyList( pxTCB );
29412955

2942-
#if ( configNUMBER_OF_CORES == 1 )
2943-
{
2944-
/* A higher priority task may have just been resumed. */
2945-
if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2946-
{
2947-
/* This yield may not cause the task just resumed to run,
2948-
* but will leave the lists in the correct state for the
2949-
* next yield. */
2950-
taskYIELD_IF_USING_PREEMPTION();
2951-
}
2952-
else
2953-
{
2954-
mtCOVERAGE_TEST_MARKER();
2955-
}
2956-
}
2957-
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
2958-
{
2959-
#if ( configUSE_PREEMPTION == 1 )
2960-
{
2961-
prvYieldForTask( pxTCB );
2962-
}
2963-
#endif /* #if ( configUSE_PREEMPTION == 1 ) */
2964-
}
2965-
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2956+
/* This yield may not cause the task just resumed to run,
2957+
* but will leave the lists in the correct state for the
2958+
* next yield. */
2959+
taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
29662960
}
29672961
else
29682962
{
@@ -3618,7 +3612,7 @@ BaseType_t xTaskResumeAll( void )
36183612

36193613
#if ( configNUMBER_OF_CORES == 1 )
36203614
{
3621-
taskYIELD_IF_USING_PREEMPTION();
3615+
taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
36223616
}
36233617
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
36243618
}
@@ -7063,28 +7057,9 @@ TickType_t uxTaskResetEventItemValue( void )
70637057
}
70647058
#endif
70657059

7066-
#if ( configNUMBER_OF_CORES == 1 )
7067-
{
7068-
if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7069-
{
7070-
/* The notified task has a priority above the currently
7071-
* executing task so a yield is required. */
7072-
taskYIELD_IF_USING_PREEMPTION();
7073-
}
7074-
else
7075-
{
7076-
mtCOVERAGE_TEST_MARKER();
7077-
}
7078-
}
7079-
#else /* #if ( configNUMBER_OF_CORES == 1 ) */
7080-
{
7081-
#if ( configUSE_PREEMPTION == 1 )
7082-
{
7083-
prvYieldForTask( pxTCB );
7084-
}
7085-
#endif
7086-
}
7087-
#endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7060+
/* Check if the notified task has a priority above the currently
7061+
* executing task. */
7062+
taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
70887063
}
70897064
else
70907065
{

0 commit comments

Comments
 (0)