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

Поділитися
Вставка
  • Опубліковано 18 січ 2025

КОМЕНТАРІ • 65

  • @lksdlkd
    @lksdlkd 3 роки тому +4

    I'm really afraid of race condition bugs

    • @StateMachineCOM
      @StateMachineCOM  3 роки тому +15

      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 UA-cam.

  • @saikirangattu2924
    @saikirangattu2924 7 років тому +1

    These tutorials are a GEM. God bless you Sir.

  • @04mfjoyce
    @04mfjoyce 3 роки тому

    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 8 років тому +2

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

  • @jhanvisaraswat6976
    @jhanvisaraswat6976 6 місяців тому +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  6 місяців тому

      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

  • @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?

  • @qqryqq123
    @qqryqq123 7 років тому +1

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

  • @0zmax
    @0zmax 8 років тому +3

    Amazing video ! Thank you

  • @joseluisconesa9187
    @joseluisconesa9187 8 років тому +1

    Great series!!. Thanks for the effort.

  • @krish2nasa
    @krish2nasa 8 років тому +1

    Excellent Thank you very much Samek

  • @Aemilindore
    @Aemilindore 8 років тому +2

    excellent video! love your series!

  • @deboxonline
    @deboxonline Рік тому

    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

  • @endwalkvideos8562
    @endwalkvideos8562 7 років тому

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

  • @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".

  • @parasbhanot
    @parasbhanot 8 років тому +1

    can you please make video about DMA ?

  • @metaldownm
    @metaldownm 6 років тому

    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).

  • @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.

  • @kile9584
    @kile9584 6 років тому

    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  6 років тому +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

  • @sagarr7775
    @sagarr7775 8 років тому +1

    very informative..

  • @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

  • @MSKY2113
    @MSKY2113 3 місяці тому

    You still have a bug in the final code - you should put both lines withing CS as opposed each line separately! As the same race condition can still happen if right after enable inbetween the interrupt occurs!

  • @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

  • @juniorlucival
    @juniorlucival Рік тому

    Bit-banding wouldn't eliminate this problem?

    • @StateMachineCOM
      @StateMachineCOM  Рік тому

      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 ua-cam.com/video/pQs8vp7JOSk/v-deo.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

  • @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?

  • @matthewperry3549
    @matthewperry3549 7 років тому

    very good vedio,thx!

  • @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.

  • @chiragpanchal1510
    @chiragpanchal1510 5 років тому

    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  5 років тому +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.

  • @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 (ua-cam.com/video/kcpVI3IjUUM/v-deo.html ), which expands on the problems caused by sharing resources and mutual exclusion mechanisms in the context of an RTOS. --MMS

  • @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

  • @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

  • @gauravminocha9335
    @gauravminocha9335 8 років тому +4

    With the background music, it is hard to concentrate

  • @Bits32
    @Bits32 8 років тому +1

    Miro, Next lesson please ..

    • @StateMachineCOM
      @StateMachineCOM  8 років тому +6

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

  • @mertaliturk650
    @mertaliturk650 11 місяців тому

    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  11 місяців тому +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 11 місяців тому

      @@StateMachineCOM thank you for return

  • @sheetalarya4094
    @sheetalarya4094 8 років тому +7

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

  • @jagmohannanaware
    @jagmohannanaware 4 роки тому

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

  • @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

  • @AliNormukhamedov
    @AliNormukhamedov Рік тому

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

  • @windward2818
    @windward2818 3 роки тому

    Read-modify-write ARM = non-ATOMIC.