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

  Рет қаралды 41,157

Quantum Leaps, LLC

Quantum Leaps, LLC

8 жыл бұрын

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-machine.com/quickstart
GitHub repository for projects for this video course:
github.com/QuantumLeaps/moder...
Transcript of this lesson:
www.state-machine.com/course/...
Race Condition explanations:
en.wikipedia.org/wiki/Race_co...
stackoverflow.com/questions/34...
Music credits:
The background music comes from:
www.bensound.com/royalty-free...

Пікірлер: 63
@lksdlkd
@lksdlkd 3 жыл бұрын
I'm really afraid of race condition bugs
@StateMachineCOM
@StateMachineCOM 3 жыл бұрын
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 жыл бұрын
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 жыл бұрын
This is such a beautiful explanation! The shared resource and separate gpio bit thing makes a lot more sense now :)
@GLAProducer
@GLAProducer 8 жыл бұрын
Thank you so much for this video, it is the greatest lesson in ARM in the whole KZfaq.
@saikirangattu2924
@saikirangattu2924 6 жыл бұрын
These tutorials are a GEM. God bless you Sir.
@nolandsman5649
@nolandsman5649 7 жыл бұрын
Hi Miro, very eagerly waiting for your next lesson...
@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!
@Carlos.USA88
@Carlos.USA88 7 жыл бұрын
can't wait for the next videos. Thank you !!
@jhanvisaraswat6976
@jhanvisaraswat6976 13 күн бұрын
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 12 күн бұрын
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 7 жыл бұрын
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?
@joseluisconesa9187
@joseluisconesa9187 7 жыл бұрын
Great series!!. Thanks for the effort.
@Aemilindore
@Aemilindore 8 жыл бұрын
excellent video! love your series!
@qqryqq123
@qqryqq123 7 жыл бұрын
any chance for next lessons any time soon? Perhaps covering peripherials (uart, spi, timers etc)...?
@0zmax
@0zmax 8 жыл бұрын
Amazing video ! Thank you
@krish2nasa
@krish2nasa 7 жыл бұрын
Excellent Thank you very much Samek
@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).
@endwalkvideos8562
@endwalkvideos8562 6 жыл бұрын
waiting for more videos about ARM, what do u think bout Keil ?
@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?
@deboxonline
@deboxonline 6 ай бұрын
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
@parasbhanot
@parasbhanot 7 жыл бұрын
can you please make video about DMA ?
@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 жыл бұрын
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 жыл бұрын
very informative..
@brankomiletic9444
@brankomiletic9444 8 жыл бұрын
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 жыл бұрын
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 жыл бұрын
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.
@JeanJonethal
@JeanJonethal 8 жыл бұрын
Some processors offer set and reset registers to provide atomic gpio output modification.
@StateMachineCOM
@StateMachineCOM 8 жыл бұрын
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".
@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
@matthewperry3549
@matthewperry3549 7 жыл бұрын
very good vedio,thx!
@Dhairyasd
@Dhairyasd 3 жыл бұрын
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 3 жыл бұрын
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
@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
@mertaliturk650
@mertaliturk650 5 ай бұрын
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 5 ай бұрын
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 5 ай бұрын
@@StateMachineCOM thank you for return
@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 жыл бұрын
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 (kzfaq.info/get/bejne/ocmgiaxkrtC5hYE.html ), which expands on the problems caused by sharing resources and mutual exclusion mechanisms in the context of an RTOS. --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 жыл бұрын
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
@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
@juniorlucival
@juniorlucival 9 ай бұрын
Bit-banding wouldn't eliminate this problem?
@StateMachineCOM
@StateMachineCOM 9 ай бұрын
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 kzfaq.info/get/bejne/preja9mhnLCzg58.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
@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 жыл бұрын
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 жыл бұрын
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.
@Ilovecrossfit
@Ilovecrossfit 6 жыл бұрын
Excellent course, please please do not add music.
@ThinhTran-yu9sn
@ThinhTran-yu9sn 7 жыл бұрын
Lesson 21 please
@StateMachineCOM
@StateMachineCOM 7 жыл бұрын
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 жыл бұрын
With the background music, it is hard to concentrate
@Bits32
@Bits32 7 жыл бұрын
Miro, Next lesson please ..
@StateMachineCOM
@StateMachineCOM 7 жыл бұрын
The next lesson is forthcoming. Stay tuned... --MMS
@jagmohannanaware
@jagmohannanaware 3 жыл бұрын
Great Video. But can you please switch off that background music please?
@AliNormukhamedov
@AliNormukhamedov Жыл бұрын
all very nice but annoying music in the background .....
@sheetalarya4094
@sheetalarya4094 8 жыл бұрын
Excellent video but exceptionally irritating background music. Who studies with music? Please remove music
@windward2818
@windward2818 2 жыл бұрын
Read-modify-write ARM = non-ATOMIC.
#21 Foreground-Background Architecture ("Superloop")
18:37
Quantum Leaps, LLC
Рет қаралды 42 М.
Race Conditions in C# .NET Core
18:58
Raw Coding
Рет қаралды 9 М.
ЧУТЬ НЕ УТОНУЛ #shorts
00:27
Паша Осадчий
Рет қаралды 6 МЛН
39kgのガリガリが踊る絵文字ダンス/39kg boney emoji dance#dance #ダンス #にんげんっていいな
00:16
💀Skeleton Ninja🥷【にんげんっていいなチャンネル】
Рет қаралды 7 МЛН
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 59 МЛН
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
#16 Interrupts Part-1: What are interrupts, and how they work
15:58
Quantum Leaps, LLC
Рет қаралды 78 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
What are Race Conditions?
13:09
CodeVault
Рет қаралды 88 М.
Embedded Software Engineering Interview Questions & Answers
10:24
Greidi Ajalik
Рет қаралды 54 М.
37C3 -  Breaking "DRM" in Polish trains
1:01:46
media.ccc.de
Рет қаралды 431 М.
WHY IS THE STACK SO FAST?
13:46
Core Dumped
Рет қаралды 142 М.
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 5 МЛН
EXEED VX 2024: Не өзгерді?
9:06
Oljas Oqas
Рет қаралды 46 М.
Как бесплатно замутить игровой ноутбук
1:00
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 183 М.
Телефон-електрошокер
0:43
RICARDO 2.0
Рет қаралды 1,3 МЛН
Сколько реально стоит ПК Величайшего?
0:37
Это Xiaomi Su7 Max 🤯 #xiaomi #su7max
1:01
Tynalieff Shorts
Рет қаралды 1,1 МЛН