Shared variable read from low priority thread in preemptive scheduling

52 views Asked by At

Single core embedded system

Priority based scheduling

Thread 2(T2) - High priority

Thread 1(T1) - Low priority

Single producer(T2) and single consumer (T1)

Requirement:

  1. Data flow from T2 to T1 through shared circular buffer. Data flow possible in one direction i.e. T2 to T1.

  2. Between threads planning to use circular buffer as shared memory to post received packets from T2 to T1.

  3. T2 will enqueue and T1 will dequeue the packets from circular buffer.

  4. And planning to add enqueue index and dequeue index pointer for circular buffer as volatile(consider it won't solve critical section problem).

  5. T2 will update the Enqueue index pointer in circular buffer and T2 will update the Dequeue index pointer in circular buffer.

  6. T1 thread will check Enqueue and dequeue index are equal before start to dequeue the packet from circular buffer.

Query:

  • while T1 thread(low priority) trying to read the enqueue index, let's say in that instance task switched to high priority thread T2 and it will update the enqueue index variable?
  • In this case again task switched to T1, whether variable 'enqueue index' read will give old value or INVALID value?
  • My understanding is, for my requirement T2->T1 direction only possible so here critical section issue will not come in preemptive scheduling

Please give your views

1

There are 1 answers

0
MSalters On

In standard C (and you don't specify an embedded dialect), reading an "enqueue index" or any other shared variable does not force thread scheduling. Hence, thread T1 probably continues to run. It reads the last value written by T2. If there is no value written yet, that's the regular C reading of uninitialized memory, i.e. Undefined Behavior. (Not "INVALID value")

I've got no idea why you mention a "critical section issue".