{"id":2041,"date":"2019-11-23T09:44:35","date_gmt":"2019-11-23T01:44:35","guid":{"rendered":"https:\/\/coderbee.net\/?p=2041"},"modified":"2019-11-23T09:44:35","modified_gmt":"2019-11-23T01:44:35","slug":"cas-volatile-%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/jvm\/20191123\/2041","title":{"rendered":"CAS\/volatile \u539f\u7406"},"content":{"rendered":"<h1>1. \u6307\u4ee4\u7684 lock \u524d\u7f00<\/h1>\n<p>intel\u624b\u518c\u5bf9 lock \u524d\u7f00\u7684\u8bf4\u660e\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u786e\u4fdd\u88ab\u4fee\u9970\u6307\u4ee4\u6267\u884c\u7684\u539f\u5b50\u6027\u3002<\/li>\n<li>\u7981\u6b62\u8be5\u6307\u4ee4\u4e0e\u524d\u9762\u548c\u540e\u9762\u7684\u8bfb\u5199\u6307\u4ee4\u91cd\u6392\u5e8f\u3002<\/li>\n<li>\u6307\u4ee4\u6267\u884c\u5b8c\u540e\u628a\u5199\u7f13\u51b2\u533a\u7684<strong>\u6240\u6709\u6570\u636e<\/strong>\u5237\u65b0\u5230\u5185\u5b58\u4e2d\u3002(\u8fd9\u6837\u8fd9\u4e2a\u6307\u4ee4\u4e4b\u524d\u7684\u5176\u4ed6\u4fee\u6539\u5bf9\u6240\u6709\u5904\u7406\u5668\u53ef\u89c1\u3002)<\/li>\n<\/ol>\n<p>\u5728 Pentium \u53ca\u4e4b\u524d\u7684\u5904\u7406\u5668\u4e2d\uff0c\u5e26\u6709 lock \u524d\u7f00\u7684\u6307\u4ee4\u5728\u6267\u884c\u671f\u95f4\u4f1a\u58f0\u8a00 <code>LOCK#<\/code> \u4fe1\u53f7\u4ee5\u9501\u4f4f\u603b\u7ebf\uff0c\u4f7f\u5f97\u5176\u5b83\u5904\u7406\u5668\u6682\u65f6\u65e0\u6cd5\u901a\u8fc7\u603b\u7ebf\u8bbf\u95ee\u5185\u5b58\uff0c\u5f88\u663e\u7136\uff0c\u8fd9\u4e2a\u5f00\u9500\u5f88\u5927\u3002\u5728\u65b0\u7684\u5904\u7406\u5668\u4e2d\uff0cIntel \u4f7f\u7528\u7f13\u5b58\u9501\u5b9a\u6765\u4fdd\u8bc1\u6307\u4ee4\u6267\u884c\u7684\u539f\u5b50\u6027\uff0c\u7f13\u5b58\u9501\u5b9a\u5c06\u5927\u5927\u964d\u4f4e lock \u524d\u7f00\u6307\u4ee4\u7684\u6267\u884c\u5f00\u9500\u3002\u5982\u679c\u8bbf\u95ee\u7684\u5185\u5b58\u533a\u57df\u5df2\u7ecf\u7f13\u5b58\u5728\u5904\u7406\u5668\u5185\u90e8\uff0c\u5219\u4e0d\u4f1a\u58f0\u8a00 <code>LOCK#<\/code> \u4fe1\u53f7\u3002\u76f8\u53cd\u5730\uff0c\u5b83\u4f1a\u9501\u5b9a\u8fd9\u5757\u5185\u5b58\u533a\u57df\u7684\u7f13\u5b58\u5e76\u56de\u5199\u5230\u5185\u5b58\uff0c\u5e76\u4f7f\u7528\u7f13\u5b58\u4e00\u81f4\u6027\u673a\u5236\u6765\u786e\u4fdd\u4fee\u6539\u7684\u539f\u5b50\u6027\uff0c<strong>\u7f13\u5b58\u4e00\u81f4\u6027\u673a\u5236\u4f1a\u963b\u6b62\u540c\u65f6\u4fee\u6539\u88ab\u4e24\u4e2a\u4ee5\u4e0a\u5904\u7406\u5668\u7f13\u5b58\u7684\u5185\u5b58\u533a\u57df\u6570\u636e<\/strong>\u3002<\/p>\n<p><!--more--><\/p>\n<h1>2. CAS<\/h1>\n<p>Intel \u5904\u7406\u5668\u63d0\u4f9b\u4e86 <code>cmpxchg<\/code> \u6307\u4ee4\u6765\u652f\u6301 Compare-And-Swap \u64cd\u4f5c\u3002<\/p>\n<p>JVM \u6839\u636e\u5f53\u524d\u7cfb\u7edf\u662f\u5426\u4e3a\u591a\u6838\u5904\u7406\u5668\u51b3\u5b9a\u662f\u5426\u4e3a <code>cmpxchg<\/code> \u6307\u4ee4\u6dfb\u52a0 lock \u524d\u7f00\u3002<br \/>\n1. \u5982\u679c\u662f\u591a\u5904\u7406\u5668\uff0c\u4e3a <code>cmpxchg<\/code> \u6307\u4ee4\u6dfb\u52a0 lock \u524d\u7f00\u3002<br \/>\n2. \u53cd\u4e4b\uff0c\u5c31\u7701\u7565 lock \u524d\u7f00\u3002\uff08\u5355\u5904\u7406\u5668\u4f1a\u4e0d\u9700\u8981 lock \u524d\u7f00\u63d0\u4f9b\u7684\u5185\u5b58\u5c4f\u969c\u6548\u679c\uff09<\/p>\n<pre><code class=\"java\">public final native boolean compareAndSwapInt(Object paramObject, long paramLong, int paramInt1, int paramInt2);\n<\/code><\/pre>\n<p><code>Unsafe.compareAndSwapInt<\/code> \u65b9\u6cd5\u5728 X86 \u5904\u7406\u5668\u4e0b\u7684\u6e90\u4ee3\u7801\uff1a<br \/>\n<code>c++<br \/>\ninline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) {<br \/>\n    int mp = os::isMP(); \/\/\u5224\u65ad\u662f\u5426\u662f\u591a\u5904\u7406\u5668<br \/>\n    _asm {<br \/>\n        mov edx, dest<br \/>\n        mov ecx, exchange_value<br \/>\n        mov eax, compare_value<br \/>\n        LOCK_IF_MP(mp)<br \/>\n        cmpxchg dword ptr [edx], ecx<br \/>\n    }<br \/>\n}<\/code><br \/>\n<code>LOCK_IF_MP(mp)<\/code> \u51b3\u5b9a\u662f\u5426\u8f93\u51fa lock \u524d\u7f00\u3002<\/p>\n<h1>3. volatile<\/h1>\n<p>\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u88ab\u58f0\u660e\u6210 volatile\uff0cJava \u5185\u5b58\u6a21\u578b\u4fdd\u8bc1\u6240\u6709\u7ebf\u7a0b\u770b\u5230\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<p>volatile \u4e0d\u4f1a\u5f15\u8d77\u7ebf\u7a0b\u4e0a\u4e0b\u6587\u7684\u5207\u6362\u548c\u8c03\u5ea6\u3002<\/p>\n<table>\n<tr>\n<td>Java \u4ee3\u7801\uff1a<\/td>\n<td>instance = new Singleton();\/\/instance \u662f volatile \u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">\u6c47\u7f16\u4ee3\u7801\uff1a<\/td>\n<td>0x01a3de1d: movb \\$0x0,0x1104800(%esi); <\/td>\n<\/tr>\n<tr>\n<td>0x01a3de24: lock addl \\$0x0,(%esp);<\/td>\n<\/tr>\n<\/table>\n<p>\u5bf9\u6709 volatile \u4fee\u9970\u7684\u53d8\u91cf\u8fdb\u884c\u5199\u64cd\u4f5c\u65f6\uff0c\u4f1a\u8ffd\u52a0\u4e00\u6761 lock \u524d\u7f00\u7684\u6c47\u7f16\u6307\u4ee4\u3002<\/p>\n<h1>4. \u53c2\u8003\u8d44\u6599<\/h1>\n<ul>\n<li><a href=\"https:\/\/www.infoq.cn\/article\/ftf-java-volatile\">\u804a\u804a\u5e76\u53d1\uff08\u4e00\uff09\u2014\u2014\u6df1\u5165\u5206\u6790 Volatile \u7684\u5b9e\u73b0\u539f\u7406<\/a><\/li>\n<li><a href=\"https:\/\/www.jianshu.com\/p\/7fadaaa38e33\">\u8c08\u8c08Java\u4e2d\u7684CAS<\/a><\/li>\n<\/ul>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6307\u4ee4\u7684 lock \u524d\u7f00 intel\u624b\u518c\u5bf9 lock \u524d\u7f00\u7684\u8bf4\u660e\u5982\u4e0b\uff1a \u786e\u4fdd &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/jvm\/20191123\/2041\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96],"tags":[335,336,334],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2041"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2041"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2041\/revisions"}],"predecessor-version":[{"id":2043,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2041\/revisions\/2043"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}