{"id":1421,"date":"2012-03-11T10:20:19","date_gmt":"2012-03-10T16:20:19","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1421"},"modified":"2012-03-14T11:21:44","modified_gmt":"2012-03-14T02:21:44","slug":"xv6%e3%82%bd%e3%83%bc%e3%82%b9%e3%82%b3%e3%83%bc%e3%83%89%e3%83%aa%e3%83%bc%e3%83%87%e3%82%a3%e3%83%b3%e3%82%b0-%e3%81%9d%e3%81%ae35-2","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1421","title":{"rendered":"[xv6 #35] Chapter 3 &#8211; Locking &#8211; Code: Locks"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e45\u301c46\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>xv6\u306f\u3001spinlock\u69cb\u9020\u4f53\u3068\u3057\u3066\u30ed\u30c3\u30af\u3092\u8868\u73fe\u3059\u308b\u3002<br \/>\n\u305d\u306e\u69cb\u9020\u4f53\u306e\u306a\u304b\u3067\u91cd\u8981\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u306flocked\u3067\u3042\u308a\u3001\u30ed\u30c3\u30af\u53ef\u80fd\u306a\u3068\u304d\u306b\u30bc\u30ed\u3068\u306a\u308a\u3001\u3059\u3067\u306b\u30ed\u30c3\u30af\u3055\u308c\u3066\u3044\u308b\u3068\u304d\u306b\u30bc\u30ed\u4ee5\u5916\u306e\u5024\u3068\u306a\u308b\u3002<br \/>\n\u7406\u8ad6\u4e0a\u3001xv6\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void\nacquire(struct spinlock *lk)\n{\n  for(;;) {\n    if(!lk-&gt;locked) {\n      lk-&gt;locked = 1;\n      break;\n    }\n  }\n}<\/pre>\n<p>\u6b8b\u5ff5\u306a\u304c\u3089\u3001\u3053\u306e\u5b9f\u88c5\u306f\u3001\u6700\u8fd1\u306e\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30c3\u30b5\u4e0a\u3067\u306f\u6392\u4ed6\u3092\u4fdd\u8a3c\u3057\u306a\u3044\u3002<br \/>\n\uff12\u3064\uff08\u3082\u3057\u304f\u306f\u305d\u308c\u4ee5\u4e0a\u306e\uff09CPU\u304c\u540c\u6642\u306bif\u6587\u306b\u5230\u9054\u3057\u3001locked\u304c\u30bc\u30ed\u306a\u306e\u3092\u898b\u3066\u3001\u305d\u3057\u3066\u4e21\u65b9\u3068\u3082if\u6587\u306e\u4e2d\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30ed\u30c3\u30af\u3092\u3064\u304b\u3080\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<br \/>\n\u3053\u306e\u6642\u70b9\u3067\u30012\u3064\u306e\u5225\u306eCPU\u304c\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3001\u6392\u4ed6\u7684\u7279\u6027\u3092\u7834\u3063\u3066\u3057\u307e\u3046\u3002<br \/>\n\u7af6\u5408\u72b6\u614b\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u5f79\u306b\u7acb\u3064\u3069\u3053\u308d\u304b\u3001\u3053\u306eacquire\u306e\u5b9f\u88c5\u306f\u305d\u308c\u81ea\u8eab\u304c\u7af6\u5408\u72b6\u614b\u3092\u6301\u3064\u3002<br \/>\n\u3053\u306e\u554f\u984c\u306f\u3001locked\u306e\u30c1\u30a7\u30c3\u30af\u3068locked\u3078\u306e\u4ee3\u5165\uff08if(!lk-&gt;locked) \u3068 lk-&gt;locked = 1;\u306e\u884c\uff09\u304c\u5225\u306e\u6587\u3067\u884c\u308f\u308c\u3066\u3044\u308b\u3068\u3053\u308d\u306b\u3042\u308b\u3002<br \/>\n\u4e0a\u8a18\u306e\u30eb\u30fc\u30c1\u30f3\u3092\u6b63\u3059\u305f\u3081\u3001locked\u306e\u30c1\u30a7\u30c3\u30af\u3068locked\u3078\u306e\u4ee3\u5165\u306f\u3001\u3072\u3068\u3064\u306e\u30a2\u30c8\u30df\u30c3\u30af\u306a\u51e6\u7406\uff08\u3053\u308c\u4ee5\u4e0a\u5206\u5272\u3067\u304d\u306a\u3044\uff09\u3067\u5b9f\u884c\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>spinlock.h<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Mutual exclusion lock.\nstruct spinlock {\n  uint locked;       \/\/ Is the lock held?\n  \n  \/\/ For debugging:\n  char *name;        \/\/ Name of lock.\n  struct cpu *cpu;   \/\/ The cpu holding the lock.\n  uint pcs&#x5B;10];      \/\/ The call stack (an array of program counters)\n                     \/\/ that locked the lock.\n};<\/pre>\n<p>\u305d\u308c\u30892\u3064\u306e\u884c\u3092\u30a2\u30c8\u30df\u30c3\u30af\u306b\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001xv6\u306fxchg\u3068\u3044\u3046386\u306e\u7279\u6b8a\u306a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u547d\u4ee4\u306b\u983c\u308b\u3002<br \/>\n\u3072\u3068\u3064\u306e\u30a2\u30c8\u30df\u30c3\u30af\u306a\u64cd\u4f5c\u3067\u3001xchg\u306f\u3001\u30e1\u30e2\u30ea\u4e0a\u306e1\u30ef\u30fc\u30c9\uff08\u3053\u3053\u3067\u306f\u591a\u520632\u30d3\u30c3\u30c8\u304b\u306a\uff09\u3068\u30ec\u30b8\u30b9\u30bf\u306e\u5185\u5bb9\u3092\u5165\u308c\u66ff\u3048\u308b\u3002<br \/>\n\u5b9f\u969b\u306eacquire\u95a2\u6570\u306f\u3001\u30eb\u30fc\u30d7\u5185\u3067\u3053\u306exchg\u547d\u4ee4\u3092\u7e70\u308a\u8fd4\u3059\u3002<br \/>\n\u30eb\u30fc\u30d7\u5185\u3067\u6bce\u56delk-&gt;locked\u3092\u8aad\u307f\u53d6\u308a\u306a\u304c\u3089\u3001\u30a2\u30c8\u30df\u30c3\u30af\u306b\u305d\u308c\u306b1\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\uff08acquire\u95a2\u6570\u306ewhile\u6587\u306e\u3068\u3053\u308d\uff09<br \/>\n\u3059\u3067\u306b\u30ed\u30c3\u30af\u304c\u78ba\u4fdd\u3055\u308c\u3066\u3044\u305f\u3089\u3001lk-&gt;locked\u306f\u3059\u3067\u306b1\u306b\u306a\u3063\u3066\u3044\u308b\u3060\u308d\u3046\u3002<br \/>\n\u305d\u3057\u3066xchg\u306f1\u3092\u8fd4\u3057\u3001\u30eb\u30fc\u30d7\u306f\u7d99\u7d9a\u3055\u308c\u308b\u3002<br \/>\nxchg\u304c0\u3092\u8fd4\u3057\u305f\u5834\u5408\u3001acquire\u306f\u30ed\u30c3\u30af\u306e\u7372\u5f97\u306b\u6210\u529f\u3057\u305f\u72b6\u614b\u3067\u3042\u308a\uff080\u3060\u3063\u305flocked\u304c1\u306b\u306a\u3063\u305f\u72b6\u614b\uff09\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u30eb\u30fc\u30d7\u306f\u7d42\u308f\u308b\u3002<br \/>\n\u4e00\u5ea6\u30ed\u30c3\u30af\u304c\u78ba\u4fdd\u3055\u308c\u305f\u3089\u3001acquire\u306f\u3001\u30c7\u30d0\u30c3\u30b0\u306e\u305f\u3081\u3001\u30ed\u30c3\u30af\u3092\u78ba\u4fdd\u3057\u305fCPU\u3068\u30b9\u30bf\u30c3\u30af\u30c8\u30ec\u30fc\u30b9\u3092\u8a18\u9332\u3059\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u304c\u3001\u30ed\u30c3\u30af\u3092\u78ba\u4fdd\u3057\u3066\u304a\u304d\u306a\u304c\u3089\u89e3\u653e\u3059\u308b\u306e\u3092\u5fd8\u308c\u305f\u3068\u304d\u3001\u3053\u306e\u60c5\u5831\u306f\u3001\u539f\u56e0\u3092\u7279\u5b9a\u3059\u308b\u306e\u306b\u5f79\u7acb\u3064\u3002<br \/>\n\u305d\u308c\u3089\u30c7\u30d0\u30c3\u30b0\u60c5\u5831\u7528\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u3001\u30ed\u30c3\u30af\u306b\u3088\u3063\u3066\u4fdd\u8b77\u3055\u308c\u3001\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3066\u308b\u3068\u304d\u3060\u3051\u5909\u66f4\u51fa\u6765\u308b\u3002<\/p>\n<p>x86.h\u306exchg\u30a4\u30f3\u30e9\u30a4\u30f3\u95a2\u6570\uff08xchg\u547d\u4ee4\u3092\u30e9\u30c3\u30d7\u3057\u3066\u3044\u308b\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static inline uint\nxchg(volatile uint *addr, uint newval)\n{\n  uint result;\n  \n  \/\/ The + in \"+m\" denotes a read-modify-write operand.\n  asm volatile(\"lock; xchgl %0, %1\" :\n               \"+m\" (*addr), \"=a\" (result) :\n               \"1\" (newval) :\n               \"cc\");\n  return result;\n}<\/pre>\n<p>spinlock.c\u306eacquire\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Acquire the lock.\n\/\/ Loops (spins) until the lock is acquired.\n\/\/ Holding a lock for a long time may cause\n\/\/ other CPUs to waste time spinning to acquire it.\nvoid\nacquire(struct spinlock *lk)\n{\n  pushcli(); \/\/ disable interrupts to avoid deadlock.\n  if(holding(lk))\n    panic(&quot;acquire&quot;);\n\n  \/\/ The xchg is atomic.\n  \/\/ It also serializes, so that reads after acquire are not\n  \/\/ reordered before it. \n  while(xchg(&amp;lk-&gt;locked, 1) != 0)\n    ;\n\n  \/\/ Record info about lock acquisition for debugging.\n  lk-&gt;cpu = cpu;\n  getcallerpcs(&amp;lk, lk-&gt;pcs);\n}<\/pre>\n<p>release\u95a2\u6570\u306f\u3001acquire\u306e\u5bfe\u306b\u306a\u308b\u3082\u306e\u3067\u3042\u308a\u3001\u30c7\u30d0\u30c3\u30b0\u60c5\u5831\u7528\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30af\u30ea\u30a2\u3057\u3001\u305d\u3057\u3066\u30ed\u30c3\u30af\u3092\u89e3\u653e\u3059\u308b\u3002<\/p>\n<p>spinlock.c\u306erelease\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Release the lock.\nvoid\nrelease(struct spinlock *lk)\n{\n  if(!holding(lk))\n    panic(&quot;release&quot;);\n\n  lk-&gt;pcs&#x5B;0] = 0;\n  lk-&gt;cpu = 0;\n\n  \/\/ The xchg serializes, so that reads before release are \n  \/\/ not reordered after it.  The 1996 PentiumPro manual (Volume 3,\n  \/\/ 7.2) says reads can be carried out speculatively and in\n  \/\/ any order, which implies we need to serialize here.\n  \/\/ But the 2007 Intel 64 Architecture Memory Ordering White\n  \/\/ Paper says that Intel 64 and IA-32 will not move a load\n  \/\/ after a store. So lock-&gt;locked = 0 would work here.\n  \/\/ The xchg being asm volatile ensures gcc emits it after\n  \/\/ the above assignments (and after the critical section).\n  xchg(&amp;lk-&gt;locked, 0);\n\n  popcli();\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u8a73\u7d30\u306a\u30ed\u30c3\u30af\u306e\u5b9f\u88c5\u306b\u3064\u3044\u3066\u3067\u3059\u3002<\/p>\n<p>xchg\u547d\u4ee4\u306e\u3068\u3053\u308d\u304c\u5206\u304b\u308a\u3065\u3089\u3044\u3067\u3059\u304c\u3001lk-&gt;locked\u304c1\u3060\u3063\u305f\u5834\u5408\uff08\u3059\u3067\u306b\u4ed6\u3067\u30ed\u30c3\u30af\u304c\u7372\u5f97\u3055\u308c\u3066\u3044\u308b\u5834\u5408\uff09\u3001\u50241\u3068\u4ea4\u63db\u3057\u305f\u7d50\u679c\u3001\u4ea4\u63db\u76f4\u524d\u306elk-&gt;locked\u306e\u5024\u3067\u3042\u308b1\u304c\u8fd4\u3063\u3066\u304d\u3066\u3001while\u6587\u306e\u6761\u4ef6\u90e8\u304c\u771f\u3068\u306a\u308a\u3001\u30eb\u30fc\u30d7\u304c\u7d9a\u304f\uff08\u30ed\u30c3\u30af\u304c\u89e3\u653e\u3055\u308c\u308b\u307e\u3067\u5f85\u3064\uff09\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nlk-&gt;locked\u304c0\u3060\u3063\u305f\u5834\u5408\uff08\u30ed\u30c3\u30af\u304c\u7372\u5f97\u53ef\u80fd\u306a\u5834\u5408\uff09\u3001\u50241\u3068\u4ea4\u63db\u3057\u305f\u7d50\u679c\u3001\u4ea4\u63db\u76f4\u524d\u306elk-&gt;locked\u306e\u5024\u3067\u3042\u308b0\u304c\u8fd4\u3063\u3066\u304d\u3066\u3001while\u6587\u306e\u6761\u4ef6\u90e8\u304c\u507d\u3068\u306a\u308a\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u3001\u51e6\u7406\u3092\u7d9a\u884c\u3067\u304d\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u30b9\u30d4\u30f3\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066\u306f<a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E3%82%B9%E3%83%94%E3%83%B3%E3%83%AD%E3%83%83%E3%82%AF\" target=\"_blank\">\u30b9\u30d4\u30f3\u30ed\u30c3\u30af &#8211; Wikipedia<\/a>\u306b\u3082\u3046\u5c11\u3057\u8a73\u3057\u304f\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>release\u95a2\u6570\u5185\u306b\u9577\u3081\u306e\u30b3\u30e1\u30f3\u30c8\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u591a\u5206Wikipedia\u306e\u30b9\u30d4\u30f3\u30ed\u30c3\u30af\u306e\u30da\u30fc\u30b8\u306e\u6700\u9069\u5316\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u66f8\u304b\u308c\u3066\u308b\u3088\u3046\u306a\u6700\u9069\u5316\u51fa\u6765\u308b\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u306e\u3067\u3001\u305d\u306e\u307e\u307exchg\u3092\u4f7f\u3063\u3066\u307e\u3059\u3088\u3068\u3044\u3046\u4e8b\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e45\u301c46\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>xv6\u306f\u3001spinlock\u69cb\u9020\u4f53\u3068\u3057\u3066\u30ed\u30c3\u30af\u3092\u8868\u73fe\u3059\u308b\u3002<br \/>\n\u305d\u306e\u69cb\u9020\u4f53\u306e\u306a\u304b\u3067\u91cd\u8981\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u306flocked\u3067\u3042\u308a\u3001\u30ed\u30c3\u30af\u53ef\u80fd\u306a\u3068\u304d\u306b\u30bc\u30ed\u3068\u306a\u308a\u3001\u3059\u3067\u306b\u30ed\u30c3\u30af\u3055\u308c\u3066\u3044\u308b\u3068\u304d\u306b\u30bc\u30ed\u4ee5\u5916\u306e\u5024\u3068\u306a\u308b\u3002<br \/>\n\u7406\u8ad6\u4e0a\u3001xv6\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void\nacquire(struct spinlock *lk)\n{\n  for(;;) {\n    if(!lk-&gt;locked) {\n      lk-&gt;locked = 1;\n      break;\n    }\n  }\n}<\/pre>\n<p>\u6b8b\u5ff5\u306a\u304c\u3089\u3001\u3053\u306e\u5b9f\u88c5\u306f\u3001\u6700\u8fd1\u306e\u30de\u30eb\u30c1\u30d7\u30ed\u30bb\u30c3\u30b5\u4e0a\u3067\u306f\u6392\u4ed6\u3092\u4fdd\u8a3c\u3057\u306a\u3044\u3002<br \/>\n\uff12\u3064\uff08\u3082\u3057\u304f\u306f\u305d\u308c\u4ee5\u4e0a\u306e\uff09CPU\u304c\u540c\u6642\u306bif\u6587\u306b\u5230\u9054\u3057\u3001locked\u304c\u30bc\u30ed\u306a\u306e\u3092\u898b\u3066\u3001\u305d\u3057\u3066\u4e21\u65b9\u3068\u3082if\u6587\u306e\u4e2d\u306e\u30d6\u30ed\u30c3\u30af\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u30ed\u30c3\u30af\u3092\u3064\u304b\u3080\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<br \/>\n\u3053\u306e\u6642\u70b9\u3067\u30012\u3064\u306e\u5225\u306eCPU\u304c\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3001\u6392\u4ed6\u7684\u7279\u6027\u3092\u7834\u3063\u3066\u3057\u307e\u3046\u3002<br \/>\n\u7af6\u5408\u72b6\u614b\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u5f79\u306b\u7acb\u3064\u3069\u3053\u308d\u304b\u3001\u3053\u306eacquire\u306e\u5b9f\u88c5\u306f\u305d\u308c\u81ea\u8eab\u304c\u7af6\u5408\u72b6\u614b\u3092\u6301\u3064\u3002<br \/>\n\u3053\u306e\u554f\u984c\u306f\u3001locked\u306e\u30c1\u30a7\u30c3\u30af\u3068locked\u3078\u306e\u4ee3\u5165\uff08if(!lk-&gt;locked) \u3068 lk-&gt;locked = 1;\u306e\u884c\uff09\u304c\u5225\u306e\u6587\u3067\u884c\u308f\u308c\u3066\u3044\u308b\u3068\u3053\u308d\u306b\u3042\u308b\u3002<br \/>\n\u4e0a\u8a18\u306e\u30eb\u30fc\u30c1\u30f3\u3092\u6b63\u3059\u305f\u3081\u3001locked\u306e\u30c1\u30a7\u30c3\u30af\u3068locked\u3078\u306e\u4ee3\u5165\u306f\u3001\u3072\u3068\u3064\u306e\u30a2\u30c8\u30df\u30c3\u30af\u306a\u51e6\u7406\uff08\u3053\u308c\u4ee5\u4e0a\u5206\u5272\u3067\u304d\u306a\u3044\uff09\u3067\u5b9f\u884c\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<\/p>\n<p>spinlock.h<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Mutual exclusion lock.\nstruct spinlock {\n  uint locked;       \/\/ Is the lock held?\n  \n  \/\/ For debugging:\n  char *name;        \/\/ Name of lock.\n  struct cpu *cpu;   \/\/ The cpu holding the lock.\n  uint pcs&#x5B;10];      \/\/ The call stack (an array of program counters)\n                     \/\/ that locked the lock.\n};<\/pre>\n<p>\u305d\u308c\u30892\u3064\u306e\u884c\u3092\u30a2\u30c8\u30df\u30c3\u30af\u306b\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001xv6\u306fxchg\u3068\u3044\u3046386\u306e\u7279\u6b8a\u306a\u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u547d\u4ee4\u306b\u983c\u308b\u3002<br \/>\n\u3072\u3068\u3064\u306e\u30a2\u30c8\u30df\u30c3\u30af\u306a\u64cd\u4f5c\u3067\u3001xchg\u306f\u3001\u30e1\u30e2\u30ea\u4e0a\u306e1\u30ef\u30fc\u30c9\uff08\u3053\u3053\u3067\u306f\u591a\u520632\u30d3\u30c3\u30c8\u304b\u306a\uff09\u3068\u30ec\u30b8\u30b9\u30bf\u306e\u5185\u5bb9\u3092\u5165\u308c\u66ff\u3048\u308b\u3002<br \/>\n\u5b9f\u969b\u306eacquire\u95a2\u6570\u306f\u3001\u30eb\u30fc\u30d7\u5185\u3067\u3053\u306exchg\u547d\u4ee4\u3092\u7e70\u308a\u8fd4\u3059\u3002<br \/>\n\u30eb\u30fc\u30d7\u5185\u3067\u6bce\u56delk-&gt;locked\u3092\u8aad\u307f\u53d6\u308a\u306a\u304c\u3089\u3001\u30a2\u30c8\u30df\u30c3\u30af\u306b\u305d\u308c\u306b1\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002\uff08acquire\u95a2\u6570\u306ewhile\u6587\u306e\u3068\u3053\u308d\uff09<br \/>\n\u3059\u3067\u306b\u30ed\u30c3\u30af\u304c\u78ba\u4fdd\u3055\u308c\u3066\u3044\u305f\u3089\u3001lk-&gt;locked\u306f\u3059\u3067\u306b1\u306b\u306a\u3063\u3066\u3044\u308b\u3060\u308d\u3046\u3002<br \/>\n\u305d\u3057\u3066xchg\u306f1\u3092\u8fd4\u3057\u3001\u30eb\u30fc\u30d7\u306f\u7d99\u7d9a\u3055\u308c\u308b\u3002<br \/>\nxchg\u304c0\u3092\u8fd4\u3057\u305f\u5834\u5408\u3001acquire\u306f\u30ed\u30c3\u30af\u306e\u7372\u5f97\u306b\u6210\u529f\u3057\u305f\u72b6\u614b\u3067\u3042\u308a\uff080\u3060\u3063\u305flocked\u304c1\u306b\u306a\u3063\u305f\u72b6\u614b\uff09\u3001\u305d\u308c\u306b\u3088\u3063\u3066\u30eb\u30fc\u30d7\u306f\u7d42\u308f\u308b\u3002<br \/>\n\u4e00\u5ea6\u30ed\u30c3\u30af\u304c\u78ba\u4fdd\u3055\u308c\u305f\u3089\u3001acquire\u306f\u3001\u30c7\u30d0\u30c3\u30b0\u306e\u305f\u3081\u3001\u30ed\u30c3\u30af\u3092\u78ba\u4fdd\u3057\u305fCPU\u3068\u30b9\u30bf\u30c3\u30af\u30c8\u30ec\u30fc\u30b9\u3092\u8a18\u9332\u3059\u308b\u3002<br \/>\n\u30d7\u30ed\u30bb\u30b9\u304c\u3001\u30ed\u30c3\u30af\u3092\u78ba\u4fdd\u3057\u3066\u304a\u304d\u306a\u304c\u3089\u89e3\u653e\u3059\u308b\u306e\u3092\u5fd8\u308c\u305f\u3068\u304d\u3001\u3053\u306e\u60c5\u5831\u306f\u3001\u539f\u56e0\u3092\u7279\u5b9a\u3059\u308b\u306e\u306b\u5f79\u7acb\u3064\u3002<br \/>\n\u305d\u308c\u3089\u30c7\u30d0\u30c3\u30b0\u60c5\u5831\u7528\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u3001\u30ed\u30c3\u30af\u306b\u3088\u3063\u3066\u4fdd\u8b77\u3055\u308c\u3001\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3066\u308b\u3068\u304d\u3060\u3051\u5909\u66f4\u51fa\u6765\u308b\u3002<\/p>\n<p>x86.h\u306exchg\u30a4\u30f3\u30e9\u30a4\u30f3\u95a2\u6570\uff08xchg\u547d\u4ee4\u3092\u30e9\u30c3\u30d7\u3057\u3066\u3044\u308b\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">static inline uint\nxchg(volatile uint *addr, uint newval)\n{\n  uint result;\n  \n  \/\/ The + in \"+m\" denotes a read-modify-write operand.\n  asm volatile(\"lock; xchgl %0, %1\" :\n               \"+m\" (*addr), \"=a\" (result) :\n               \"1\" (newval) :\n               \"cc\");\n  return result;\n}<\/pre>\n<p>spinlock.c\u306eacquire\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Acquire the lock.\n\/\/ Loops (spins) until the lock is acquired.\n\/\/ Holding a lock for a long time may cause\n\/\/ other CPUs to waste time spinning to acquire it.\nvoid\nacquire(struct spinlock *lk)\n{\n  pushcli(); \/\/ disable interrupts to avoid deadlock.\n  if(holding(lk))\n    panic(&quot;acquire&quot;);\n\n  \/\/ The xchg is atomic.\n  \/\/ It also serializes, so that reads after acquire are not\n  \/\/ reordered before it. \n  while(xchg(&amp;lk-&gt;locked, 1) != 0)\n    ;\n\n  \/\/ Record info about lock acquisition for debugging.\n  lk-&gt;cpu = cpu;\n  getcallerpcs(&amp;lk, lk-&gt;pcs);\n}<\/pre>\n<p>release\u95a2\u6570\u306f\u3001acquire\u306e\u5bfe\u306b\u306a\u308b\u3082\u306e\u3067\u3042\u308a\u3001\u30c7\u30d0\u30c3\u30b0\u60c5\u5831\u7528\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u30af\u30ea\u30a2\u3057\u3001\u305d\u3057\u3066\u30ed\u30c3\u30af\u3092\u89e3\u653e\u3059\u308b\u3002<\/p>\n<p>spinlock.c\u306erelease\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Release the lock.\nvoid\nrelease(struct spinlock *lk)\n{\n  if(!holding(lk))\n    panic(&quot;release&quot;);\n\n  lk-&gt;pcs&#x5B;0] = 0;\n  lk-&gt;cpu = 0;\n\n  \/\/ The xchg serializes, so that reads before release are \n  \/\/ not reordered after it.  The 1996 PentiumPro manual (Volume 3,\n  \/\/ 7.2) says reads can be carried out speculatively and in\n  \/\/ any order, which implies we need to serialize here.\n  \/\/ But the 2007 Intel 64 Architecture Memory Ordering White\n  \/\/ Paper says that Intel 64 and IA-32 will not move a load\n  \/\/ after a store. So lock-&gt;locked = 0 would work here.\n  \/\/ The xchg being asm volatile ensures gcc emits it after\n  \/\/ the above assignments (and after the critical section).\n  xchg(&amp;lk-&gt;locked, 0);\n\n  popcli();\n}<\/pre>\n<h3>\u611f\u60f3<\/h3>\n<p>\u8a73\u7d30\u306a\u30ed\u30c3\u30af\u306e\u5b9f\u88c5\u306b\u3064\u3044\u3066\u3067\u3059\u3002<\/p>\n<p>xchg\u547d\u4ee4\u306e\u3068\u3053\u308d\u304c\u5206\u304b\u308a\u3065\u3089\u3044\u3067\u3059\u304c\u3001lk-&gt;locked\u304c1\u3060\u3063\u305f\u5834\u5408\uff08\u3059\u3067\u306b\u4ed6\u3067\u30ed\u30c3\u30af\u304c\u7372\u5f97\u3055\u308c\u3066\u3044\u308b\u5834\u5408\uff09\u3001\u50241\u3068\u4ea4\u63db\u3057\u305f\u7d50\u679c\u3001\u4ea4\u63db\u76f4\u524d\u306elk-&gt;locked\u306e\u5024\u3067\u3042\u308b1\u304c\u8fd4\u3063\u3066\u304d\u3066\u3001while\u6587\u306e\u6761\u4ef6\u90e8\u304c\u771f\u3068\u306a\u308a\u3001\u30eb\u30fc\u30d7\u304c\u7d9a\u304f\uff08\u30ed\u30c3\u30af\u304c\u89e3\u653e\u3055\u308c\u308b\u307e\u3067\u5f85\u3064\uff09\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\nlk-&gt;locked\u304c0\u3060\u3063\u305f\u5834\u5408\uff08\u30ed\u30c3\u30af\u304c\u7372\u5f97\u53ef\u80fd\u306a\u5834\u5408\uff09\u3001\u50241\u3068\u4ea4\u63db\u3057\u305f\u7d50\u679c\u3001\u4ea4\u63db\u76f4\u524d\u306elk-&gt;locked\u306e\u5024\u3067\u3042\u308b0\u304c\u8fd4\u3063\u3066\u304d\u3066\u3001while\u6587\u306e\u6761\u4ef6\u90e8\u304c\u507d\u3068\u306a\u308a\u3001\u30eb\u30fc\u30d7\u3092\u629c\u3051\u3001\u51e6\u7406\u3092\u7d9a\u884c\u3067\u304d\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u30b9\u30d4\u30f3\u30ed\u30c3\u30af\u306b\u3064\u3044\u3066\u306f<a href=\"http:\/\/ja.wikipedia.org\/wiki\/%E3%82%B9%E3%83%94%E3%83%B3%E3%83%AD%E3%83%83%E3%82%AF\" target=\"_blank\">\u30b9\u30d4\u30f3\u30ed\u30c3\u30af &#8211; Wikipedia<\/a>\u306b\u3082\u3046\u5c11\u3057\u8a73\u3057\u304f\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>release\u95a2\u6570\u5185\u306b\u9577\u3081\u306e\u30b3\u30e1\u30f3\u30c8\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u591a\u5206Wikipedia\u306e\u30b9\u30d4\u30f3\u30ed\u30c3\u30af\u306e\u30da\u30fc\u30b8\u306e\u6700\u9069\u5316\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u306b\u66f8\u304b\u308c\u3066\u308b\u3088\u3046\u306a\u6700\u9069\u5316\u51fa\u6765\u308b\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u306e\u3067\u3001\u305d\u306e\u307e\u307exchg\u3092\u4f7f\u3063\u3066\u307e\u3059\u3088\u3068\u3044\u3046\u4e8b\u3060\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[405],"class_list":["post-1421","post","type-post","status-publish","format-standard","hentry","category-tech","tag-xv6"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=1421"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1421\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}