Skip to content

Commit e1f990f

Browse files
committed
Code review suggestions
Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
1 parent 32a5ae6 commit e1f990f

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

tasks.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3819,7 +3819,30 @@ void vTaskSuspendAll( void )
38193819
#if ( configNUMBER_OF_CORES == 1 )
38203820
{
38213821
/* A critical section is not required as the variable is of type
3822-
* BaseType_t. */
3822+
* BaseType_t. Each task maintains its own context, and a context switch
3823+
* cannot occur if the variable is non zero. So, as long as the writing
3824+
* from the register back into the memory is atomic, it is not a
3825+
* problem.
3826+
*
3827+
* Consider the following scenario, which starts with
3828+
* uxSchedulerSuspended at zero.
3829+
*
3830+
* 1. load uxSchedulerSuspended into register.
3831+
* 2. Now a context switch causes another task to run, and the other
3832+
* task uses the same variable. The other task will see the variable
3833+
* as zero because the variable has not yet been updated by the
3834+
* original task. Eventually the original task runs again. **That can
3835+
* only happen when uxSchedulerSuspended is once again zero**. When
3836+
* the original task runs again, the contents of the CPU registers
3837+
* are restored to exactly how they were when it was switched out -
3838+
* therefore the value it read into the register still matches the
3839+
* value of the uxSchedulerSuspended variable.
3840+
*
3841+
* 3. increment register.
3842+
* 4. store register into uxSchedulerSuspended. The value restored to
3843+
* uxSchedulerSuspended will be the correct value of 1, even though
3844+
* the variable was used by other tasks in the mean time.
3845+
*/
38233846

38243847
/* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
38253848
* do not otherwise exhibit real time behaviour. */

0 commit comments

Comments
 (0)