#20 Race Conditions: What are they and how to avoid them?

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • This lesson explains what Race Conditions are, why they are particularly nasty, and how you can avoid them.
    ------
    Resources:
    Companion web page for this video course:
    www.state-mach...
    GitHub repository for projects for this video course:
    github.com/Qua...
    Transcript of this lesson:
    www.state-mach...
    Race Condition explanations:
    en.wikipedia.o...
    stackoverflow.c...
    Music credits:
    The background music comes from:
    www.bensound.c...

Комментарии • 63

  • @lksdlkd
    @lksdlkd 3 года назад +4

    I'm really afraid of race condition bugs

    • @StateMachineCOM
      @StateMachineCOM  3 года назад +14

      I really hope that race conditions scared the hell out of you! The greatest minds in computer science, such as Edsger Dijkstra, recognized very early on how dangerous race conditions can be. Unfortunately, many software developers vastly underestimate the problem and the true costs of preemption. In the later lessons of this course you will see methods to prevent race conditions upfront, *by design*, as opposed to removing them retroactively by applying mutual exclusion. Specifically, you might want to watch lessons 33 and 34 about "event-driven programming" for embedded systems. --MMS

  • @rithmakid
    @rithmakid 8 лет назад +6

    Dr Samek, we are so excited to see you BACK on youtube!!!! Thank You for your time explaining such a complex art.

  • @GalinaMalakhova
    @GalinaMalakhova 4 года назад +6

    This is such a beautiful explanation! The shared resource and separate gpio bit thing makes a lot more sense now :)

  • @GLAProducer
    @GLAProducer 8 лет назад +7

    Thank you so much for this video, it is the greatest lesson in ARM in the whole RUclips.

  • @jhanvisaraswat6976
    @jhanvisaraswat6976 2 месяца назад +1

    I knew how race conditions occur but looking at disassembly view gave a better perspective..
    Sometimes its really hard to spot them or predict that the code will lead to race condition..
    Cant really use locks and semaphore everywhere , so looking at disassembly is really a good idea..

    • @StateMachineCOM
      @StateMachineCOM  2 месяца назад

      That's precisely why I step down to the disassembly level. But the real lesson from this video should be to avoid *sharing* because sharing is at the root of all evil. And actually, the pure race conditions as shown here are just the tip of the iceberg. Even more common are various data races, where some complex data gets changed while preemption happens. After the preemption, the data is partially old and partially new (it's corrupted). Again, without sharing such a frequent problem wouldn't happen. --MMS

  • @saikirangattu2924
    @saikirangattu2924 6 лет назад +1

    These tutorials are a GEM. God bless you Sir.

  • @04mfjoyce
    @04mfjoyce 2 года назад

    Wow, very informative demonstration of how race conditions can arise when using the DATA register (with its necessary read-modify-write sequence) and how the DATA_Bits register eliminates this risk (with its atomic writes). Thank you again!

  • @nolandsman5649
    @nolandsman5649 7 лет назад +2

    Hi Miro, very eagerly waiting for your next lesson...

  • @Carlos.USA88
    @Carlos.USA88 7 лет назад +2

    can't wait for the next videos. Thank you !!

  • @metaldownm
    @metaldownm 5 лет назад

    very well explained..first strategy of mutual exclusion must be used with caution especially while using an RTOS, it should be made sure that the execution time of the critical section is less than the system tick. Another strategy would be, to use semaphores (binary in this case).

  • @0zmax
    @0zmax 8 лет назад +3

    Amazing video ! Thank you

  • @vmueller
    @vmueller 8 лет назад +1

    Quantum Leaps, LLC Thank you for your Tutorials. I am really looking forward to see the RTOS tutorials. Do you know already when you are going to upload the new Tutorials?

  • @krish2nasa
    @krish2nasa 7 лет назад +1

    Excellent Thank you very much Samek

  • @joseluisconesa9187
    @joseluisconesa9187 7 лет назад +1

    Great series!!. Thanks for the effort.

  • @Aemilindore
    @Aemilindore 8 лет назад +2

    excellent video! love your series!

  • @qqryqq123
    @qqryqq123 7 лет назад +1

    any chance for next lessons any time soon? Perhaps covering peripherials (uart, spi, timers etc)...?

  • @deboxonline
    @deboxonline 7 месяцев назад

    Is there any literature on this topic? Race conditions on bare metal microcontroller project, or how to design bare metal software to avoid them. Thank you

  • @JeanJonethal
    @JeanJonethal 8 лет назад +2

    Some processors offer set and reset registers to provide atomic gpio output modification.

    • @StateMachineCOM
      @StateMachineCOM  8 лет назад +1

      Yes, absolutely, there are many different approaches in hardware to alleviate the chance of race conditions. The Tiva GPIO is just one example, but there are others. In fact, the ARM Cortex-M core offers the unique feature called "bit-banding", which allows you to access each individual bit *atomically*. You might learn more about this feature by searching the web for the term "bit-banding".

  • @sagarr7775
    @sagarr7775 8 лет назад +1

    very informative..

  • @brankomiletic9444
    @brankomiletic9444 8 лет назад +1

    I just finished all these lessons. Will you be making lesson 21, 22 and so on or is this it for ARM. Good job on the lessons, they were pact with detail in every sentence you said.
    Can I apply all this if I was to move to the MSP430 launchpad. Yea probably the instruction set is different than arm but this approach you took with arm,.. can it also be applied with MSP430 or is that completely different. Thanks.

    • @StateMachineCOM
      @StateMachineCOM  8 лет назад +1

      The general approach to look at the disassembly and understanding what the code is doing is applicable to any CPU. The details of instruction set and registers will be obviously different, but you should be much better prepared to work it out on your own. In fact, MSP430 is a much simpler machine than ARM and the MSP430 LaunchPad was used in this course in (lesson11 and lesson17).

    • @brankomiletic9444
      @brankomiletic9444 8 лет назад +2

      Thanks a lot. I will get started with MSP430 as soon as I play around more with what you have taught me. Your knowledge is golden.

  • @ianboard544
    @ianboard544 Год назад

    This is very interesting, but I have one question (perhaps dumb): in the while(1) loop in main, you are turning the green led on and then on the next line, turning it off. What is it that paces the loop - ie, why isn't the green led toggling so fast you can't see it? What am I missing?

    • @StateMachineCOM
      @StateMachineCOM  Год назад

      You are right that in this particular example, the wile(1) loop in main() is apparently not delayed between turning the LED on and off. This means that the "blinks" of the LED occur many thousands of times per second and are perceivable to the human eye as a constant glow of the LED. I decided to remove the usual delay (used in all previous lessons) to increase the chance of a race condition, which is the main subject of this lesson. I hope this makes sense to you. --MMS

  • @parasbhanot
    @parasbhanot 7 лет назад +1

    can you please make video about DMA ?

  • @brankomiletic9444
    @brankomiletic9444 8 лет назад

    I know the whole time we were using GCC for the projects. The tiva c examples are all done in a different compiler. If i wanted to use the LCD booster pack they have i would just get the graphics standalone library for the boosterpack LCD which should work in any of the compilers.. right.. and then just take the example they have written with their ti library and change the references from the GPIO perihperal library they used to how you did all the gpio setup in this course... right?

  • @kile9584
    @kile9584 5 лет назад

    HI Miro at the conclusion of this lesson you intended on discussing prioritizing interrups and other ways to disable interrups but your long absence shifted your focus. Can you direct me to text books that can be used along with your lessons please?

    • @StateMachineCOM
      @StateMachineCOM  5 лет назад +2

      Yes, the subject of interrupt prioritization in ARM Cortex-M deserves more attention. Some of this will be discussed in the upcoming lesson 27 (RTOS part-6). For now, I recommend my ARM Community blog post community.arm.com/iot/embedded/b/embedded-blog/posts/cutting-through-the-confusion-with-arm-cortex-m-interrupt-priorities . --MMS

  • @endwalkvideos8562
    @endwalkvideos8562 7 лет назад

    waiting for more videos about ARM, what do u think bout Keil ?

  • @Dhairyasd
    @Dhairyasd 4 года назад

    I had a few questions regarding the race conditions, I understand why they are bad and how they can modify the shared the resources unexpectedly. I don't understand, why isn't the compiler/assembler smart enough to place a "push to stack" instruction after the interrupt is called and a "pop from stack" instruction before the interrupt service routine exits. I remember hearing this in previous videos, that the ARM CPU automatically places the PC, LR, R0 - R4, xPSR and some other registers to stack whenever an interrupt is triggered. (I also remember that the FPU can increase this context switch overhead by a huge extent). I don't understand why isn't the MCU automatically performing this operation, and if the compiler/assembler is aware of this, then why cannot it use a PUSH and POP instruction in the ISR?.

    • @StateMachineCOM
      @StateMachineCOM  4 года назад

      This is the whole point. The compiler *is* already saving and restoring all clobbered registers in the ISRs! But this is *not* enough. The compiler simply *cannot* prevent race conditions by itself. You have to additionally use mutual-exclusion mechanisms explained in this video. --MMS

  • @matthewperry3549
    @matthewperry3549 7 лет назад

    very good vedio,thx!

  • @apoorvamn6233
    @apoorvamn6233 Год назад

    Isn't the GPIO_ABH->DATA register declared volatile? Why then does the main code not read the updated value of the register before modifying it? What's the use of volatile keyword if this does not work then?

    • @StateMachineCOM
      @StateMachineCOM  Год назад

      Hi Apoorva. The "volatile" keyword does NOT protect against race conditions. Here is a good article explaining the real role of "volatile": www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html

  • @bibekkoirala8802
    @bibekkoirala8802 5 лет назад

    Doesn't volatile keyword also help prevent data races to some extent. Let's say a global flag variable ledState is shared between main function and ISR. If I understand correctly, declaring ledState as volatile would force the processor to get it's value from RAM everytime it's accessed and not use the cached value in register.

    • @StateMachineCOM
      @StateMachineCOM  5 лет назад +2

      No, the 'volatile' keyword alone does NOT prevent race conditions, so please don't think that you are off the hook just because you declared a variable 'volatile'. Every variable shared between any concurrent contexts (e.g., ISR and the background loop, or between two ISRs at different priority levels) must be protected by a mutual exclusion mechanism, such as critical section. Please also watch lesson 28 (ruclips.net/video/kcpVI3IjUUM/видео.html ), which expands on the problems caused by sharing resources and mutual exclusion mechanisms in the context of an RTOS. --MMS

  • @mertaliturk650
    @mertaliturk650 6 месяцев назад

    Hi miro , can ı use STM32 F0912 NUCLEO in this course? (i saw you said we can use nucle c031c6 in course but i just gonna barrow it F0 from my friend) , best regards.

    • @StateMachineCOM
      @StateMachineCOM  6 месяцев назад +2

      It does NOT seem possible to use any other STM32 NUCELO than the one explicitly supported. I just tried to plug in STM32 NUCLEO-F0912 and naively load to it project for the C031C6. The uVision debugger reports "Error while accessing a target resource". --MMS

    • @mertaliturk650
      @mertaliturk650 6 месяцев назад

      @@StateMachineCOM thank you for return

  • @juniorlucival
    @juniorlucival 10 месяцев назад

    Bit-banding wouldn't eliminate this problem?

    • @StateMachineCOM
      @StateMachineCOM  10 месяцев назад

      Yes, bit-banding will cure this particular problem (it has been invented precisely for this purpose). Also, using the clever TivaC GPIO design (which I explained in lesson #7 ruclips.net/video/pQs8vp7JOSk/видео.html ) will cure this particular problem. Both solutions work on the same principle of eliminating the *sharing* of the GPIO bits. But I intentionally used the shared version of the GPIO interface to make a larger point: sharing resources among concurrent code is *dangerous* . I hope you don't miss that important message. --MMS

  • @chiragpanchal1510
    @chiragpanchal1510 4 года назад

    one confusion: in example load -> modify -> store .... you made interrupt of systick. then all data of R0 to R3 and PC, LR should saved onto Stack, and after returning old stack should be retrieved. but instead you shown example of race condition where R2 used value which it saved from interrupt routine? ............................... in this case where stack gone ? why it did not store value to stack and get it back ?

    • @StateMachineCOM
      @StateMachineCOM  4 года назад +1

      The stack operations are fine. All registers are saved and then restored correctly. This is NOT the problem. The problem is that data in memory has been copied to registers and that these two copies diverged. Specifically, the SysTick interrupt was triggered after loading the registers from memory but before storing the changed values from registers to memory. Then the interrupt changed the data in memory, but the registers restored from the stack were still holding the old data. This is a rather fundamental problem. The hardware, the stack, the CPU all work correctly and as designed. The issue is that the operations are not atomic with respect to interrupts.
      --MMS

  • @Ilovecrossfit
    @Ilovecrossfit 6 лет назад

    Excellent course, please please do not add music.

  • @gnomishworks
    @gnomishworks 7 лет назад

    Your videos are awesome!
    I have the MSP432 (red) launchpad which I have been trying to use to follow your videos with limited luck. I am struggling a bit with registry differences at the moment.
    www.ti.com/tool/msp-exp432p401r
    Can your startup files in Lesson 19/20 be converted without to much effort from the TIVA to the MSP432? I would like to use them as a starting point to learn CCS7.
    DB

  • @gauravminocha9335
    @gauravminocha9335 8 лет назад +4

    With the background music, it is hard to concentrate

  • @keithevans6450
    @keithevans6450 7 лет назад

    Miro - did lesson 21 recently go offline? "Lesson 21: Foreground Background Systems" Perhaps to make some updates based on newer tools? (I can delete this comment later)

    • @StateMachineCOM
      @StateMachineCOM  7 лет назад +1

      No, lesson #21 is still in the works. The stuff at state-machine.com/quickstart is just a skeleton of the lesson (please disregard for now). This is there, so that I can show in the video how to download the projects and the QP/C framework, which will be needed for the next group of lessons. Hope this makes sense. --MMS

    • @keithevans6450
      @keithevans6450 7 лет назад +1

      OK - thanks for responding. That makes perfect sense. Let me know if you want me to delete this comment. Perhaps somebody else will wonder about it too though.

  • @Bits32
    @Bits32 7 лет назад +1

    Miro, Next lesson please ..

    • @StateMachineCOM
      @StateMachineCOM  7 лет назад +6

      The next lesson is forthcoming. Stay tuned...
      --MMS

  • @sheetalarya4094
    @sheetalarya4094 8 лет назад +7

    Excellent video but exceptionally irritating background music. Who studies with music?
    Please remove music

  • @ThinhTran-yu9sn
    @ThinhTran-yu9sn 7 лет назад

    Lesson 21 please

    • @StateMachineCOM
      @StateMachineCOM  7 лет назад +3

      I'm sorry for the silence, but new lessons are coming (!) By popular demand, I will be talking about RTOS, concurrency and embedded software architecture.
      --MMS

  • @AliNormukhamedov
    @AliNormukhamedov Год назад

    all very nice but annoying music in the background .....

  • @jagmohannanaware
    @jagmohannanaware 3 года назад

    Great Video. But can you please switch off that background music please?

  • @windward2818
    @windward2818 2 года назад

    Read-modify-write ARM = non-ATOMIC.