{"id":102383,"date":"2019-04-04T07:00:00","date_gmt":"2019-04-04T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102383"},"modified":"2019-06-06T17:40:45","modified_gmt":"2019-06-07T00:40:45","slug":"20190404-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190404-00\/?p=102383","title":{"rendered":"Why do we need atomic operations on the 80386, if it doesn&#8217;t support symmetric multiprocessing anyway?"},"content":{"rendered":"<p>The 80386 processor did not support symmetric multiprocessing, yet we discussed atomic operations when in our overview of the processor. If the processor doesn&#8217;t even support symmetric multiprocessing, why does it matter? <\/p>\n<p>Well, one reason is that the 80386 processor does support <i>asymmetric<\/i> multiprocessing. Floating point operations are performed by a coprocessor, and the main processor and coprocessor are both accessing the same memory. Another source of competing memory access is from hardware devices that are using Direct Memory Access (DMA). <\/p>\n<p>Even within the processor, you have to worry about races, because you might be racing with <i>yourself<\/i>. <\/p>\n<p>The 80386 did not support symmetric multiprocessing, but it did support pre-emptive multitasking, which means that any multi-instruction sequence is at risk of being interrupted, and at the worst possible time. <\/p>\n<pre>\n    ; decrement the variable and check against zero\n    mov     eax, [var]\n    dec     eax\n    mov     [var], eax\n    je      zero\n<\/pre>\n<p>If the threads gets pre-empted between the first and third instructions, then the contents of the variable may be changed by another thread, and the decrement operation becomes non-atomic. To ensure atomicity, you need to force the compiler to generate a single <code>dec<\/code> instruction, and then to test the flags directly from the decrement. <\/p>\n<pre>\n    ; decrement the variable and check against zero\n    dec     [var]\n    jz      zero\n<\/pre>\n<p>There was no way to express this level of detail to compilers of that era, so you had to hide it behind a function call. <\/p>\n<p>And if your operation cannot be expressed in a single instruction, then you&#8217;re out of luck. Increment and compare against 10? Compare and exchange if equal? Nope, you can&#8217;t do those things, at least  <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20040506-00\/?p=39463\">not without some help from the operating system<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might race with yourself.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-102383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You might race with yourself.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=102383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=102383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}