ENع
→Priority inversion
الدرس 42 · Bare metal versus an RTOS

Priority inversion and priority inheritance

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

A preemptive RTOS always runs the most urgent ready task, until a lock gets in the way. Three FreeRTOS tasks on one core share an I2C bus: motor (priority 3, with a deadline), radio (2) and log (1). Watch the timeline as log holds the lock, motor waits for it, and radio, which needs no lock at all, runs in between: motor ends 5 ms late, the bug that reset Mars Pathfinder in 1997. Then replay it with a FreeRTOS mutex, whose priority inheritance keeps the wait short. Semaphores, locks and real-time scheduling in general are taught in the Operating Systems course; the FreeRTOS mutex API itself has its own lesson in this course.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 42 · تفاعلي
01 / 11

Three tasks, one CPU

One core runs three FreeRTOS tasks. motor has priority 3, radio 2, log 1; a bigger number is more urgent. The scheduler always runs the most urgent task that is ready. Each tick is 1 ms.

priority   bigger number = more urgent (0 is the idle task)
  motor  3   drives the motor; must finish 6 ms after it wakes
  radio  2   sends a radio packet; never touches the bus
  log    1   reads a sensor on the I2C bus for the log
rule       run the highest-priority task that is ready
tick       1 ms  (configTICK_RATE_HZ 1000)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Three tasks, one CPU

    One core runs three FreeRTOS tasks. motor has priority 3, radio 2, log 1; a bigger number is more urgent. The scheduler always runs the most urgent task that is ready. Each tick is 1 ms.

    priority   bigger number = more urgent (0 is the idle task)
      motor  3   drives the motor; must finish 6 ms after it wakes
      radio  2   sends a radio packet; never touches the bus
      log    1   reads a sensor on the I2C bus for the log
    rule       run the highest-priority task that is ready
    tick       1 ms  (configTICK_RATE_HZ 1000)
  2. log takes the lock

    At 0 ms only log is ready, so it runs. It calls xSemaphoreTake and gets the lock, then starts a 3 ms read on the I2C bus. Our lock is a binary semaphore: a lock with no extra rules.

    t = 0 ms    log runs (only ready task)
    line 2     xSemaphoreTake(lock, LIMIT)  ->  lock is free: log holds it
    line 3     readSensor()  needs 3 ms on the bus
    lock       lock = xSemaphoreCreateBinary();
  3. motor preempts log

    At 1 ms motor wakes. 3 beats 1, so the scheduler stops log at once and runs motor. That is preemption. log keeps the lock while it waits: it is 1 ms into its 3 ms read.

    t = 1 ms    motor wakes: priority 3 > 1
               log -> Ready (still holds the lock)
               motor -> Running
    deadline   motor must finish by 7 ms
  4. motor waits for the lock

    At 2 ms motor needs the bus and calls xSemaphoreTake, but log holds the lock. motor is blocked, so log runs again. A short wait like this is normal: at most log's 3 ms read.

    t = 2 ms    motor: xSemaphoreTake(lock)  ->  held by log
               motor -> Blocked (waits for the lock)
               log -> Running: the only ready task
    bounded    worst wait = log's critical section, 3 ms
  5. radio jumps the queue

    At 3 ms radio wakes. 2 beats log's 1, so radio runs. Now motor waits for log, and log waits for radio. A task that needs no lock is delaying the most urgent one. This is priority inversion.

    t = 3 ms    radio wakes: priority 2 > 1
               radio -> Running
               log -> Ready (holds the lock, cannot run)
               motor -> Blocked on the lock
    in effect  motor (3) is waiting for radio (2)
  6. The deadline passes

    radio runs for 6 ms, and motor's deadline at 7 ms goes by. Nothing limits this wait: a longer radio job, or more medium tasks, would stretch it further. That is called unbounded priority inversion.

    radio      runs 3 -> 9 ms (6 ms)
    deadline   7 ms  ->  missed
    unbounded  the wait depends on every task between H and L
    Mars Pathfinder, 1997: the same pattern, a low weather task,
               a medium radio task and a high bus task
  7. motor ends 5 ms late

    radio finishes, log ends its read and gives the lock at 10 ms. motor finally takes it and finishes at 12 ms: 5 ms late. On Mars Pathfinder in 1997, a watchdog saw such a late task and reset the whole computer.

    t = 10 ms   log: xSemaphoreGive(lock)  ->  motor gets it
    t = 12 ms   motor done
    late       12 - 7 = 5 ms past the deadline
    timeline   LHLMMMMMMLHH
  8. Replay with a mutex

    Same tasks, but the lock is now a FreeRTOS mutex. At 2 ms motor blocks on it as before. This time FreeRTOS lifts log to motor's priority, 3, while log holds the lock. That is priority inheritance.

    line 1     xSemaphoreCreateMutex(): a mutex inherits, a binary semaphore does not
    t = 2 ms    motor blocks on the mutex held by log
               log's priority 1 -> 3 (inherited from motor)
    needs      configUSE_MUTEXES 1
  9. radio has to wait

    At 3 ms radio wakes again, but 2 no longer beats log, which now runs at 3. log keeps the CPU and finishes its read at 4 ms. motor's wait is now bounded: only log's short read.

    t = 3 ms    radio wakes: priority 2 < log's inherited 3
               radio -> Ready (waits)
               log keeps running its read
    t = 4 ms    log's read is done
  10. Give back, drop back

    log gives the mutex at 4 ms and drops back to priority 1. motor takes it and finishes at 6 ms, 1 ms before its deadline. Then radio runs. Same 12 ms of work, in a better order.

    t = 4 ms    log: xSemaphoreGive(lock)  ->  priority back to 1
               motor takes the mutex, done at 6 ms (deadline 7)
               radio runs 6 -> 12 ms
    binary     LHLMMMMMMLHH   motor done at 12
    mutex      LHLLHHMMMMMM   motor done at 6
  11. Rules for shared locks

    Use a mutex, not a binary semaphore, to guard a shared part. Keep the code between take and give short. Give a timeout to xSemaphoreTake. Never take a mutex inside an interrupt. Inheritance limits the wait; it does not remove it.

    guard with   xSemaphoreCreateMutex()
    keep short   the code between take and give
    time out     xSemaphoreTake(lock, LIMIT), not forever
    no ISRs      mutexes cannot be used in interrupts
    bounded      inheritance limits the wait, it does not remove it

اختبار يتاح بعد الخطوة الأخيرة.