ããã¯ããªã¼æ§ã®è¨¼æã«ã¤ãã¦
http://www.slideshare.net/kumagi/lock-free-safe?next_slideshow=1
ã¨ãéå»ã«èªåã§æ¸ãã¦ãããªããããã®å½æã®èªåã®èªèãçãã£ãäºãããã®ã§ããã«ä¸åº¦æ¸ãåºãã¦ããã
Lock-Freeã¯ãããã¯ã使ããªãäºãã§ã¯ãªã
STMã®äºããã£ã¦ãã¦Lock-Freeã¨å¼ãã§ãæèã¯ãã£ã±ãããããã©ãSTMã®å®è£ ã«ããã¯ã使ãäºã¯ä¸è¬çã ããããã¯æ£ããå°éç¨èªã§è¨ãæã®Lock-Freeã¨ã¯å¼ã°ãªãã
Lock-Freeã¨ã¯ãã©ããªã¹ã±ã¸ã¥ã¼ãªã³ã°ãçºããããã¨ãã©ããã®æä½ãé²è¡ãããã¨ããé²è¡ä¿è¨¼(Progress Guarantee)ã表ãã¦ããã
ã¹ã±ã¸ã¥ã¼ãªã³ã°ï¼
ãã«ãã³ã¢CPUã§ãã·ã³ã°ã«ã³ã¢CPUã§ããOSã¯å®è¡ä¸ã®ããã°ã©ã ãä»»æã®ã¿ã¤ãã³ã°ã§å¼·å¶çã«ä¸æåæ¢ããã¦ä»ã®ããã°ã©ã ã«CPUãªã½ã¼ã¹ãå²ãå½ã¦ãäºãã§ãã*1ãå®è¡ä¸ã¹ã¬ããããã®OSã«ããCPUã®å¼ãå¥ããã»å²ãä»ãã®äºã並è¡ããã°ã©ã ã®ä¸çã§ã¯ã¹ã±ã¸ã¥ã¼ãªã³ã°ã¨å¼ã¶ã
ã·ã³ã°ã«CPUã³ã¢ã®ç°å¢ã§ã¤ããã£ããCè¨èªãããã§ç¡éã«ã¼ããè¨è¿°ãã¦ãkillã§ããã®ã¯ãã®ä»çµã¿ã®ãé°ã§ããã
æ®éã®æä»ããã¯ã¯æªæããã¹ã±ã¸ã¥ã¼ãªã³ã°ã«å¯¾ãã¦ç¡å
æªæããã¹ã±ã¸ã¥ã¼ãªã³ã°ãã¨ããç¶æ³ã¯ä¸ã®ä¸ã«ã¯æ®éç¡ãããææªã®å ´åãæ³å®ãã¦ã·ã¹ãã ãæºããç¹æ§ã®ä¸éãä¿è¨¼ããå¿
è¦ãããç¶æ³ã¨ããã®ã¯ãã*2ã
ä¾ãã°ããã¹ã¬ãããå
±æè³æºã«å¯¾ããæä»ããã¯Xãç²å¾ããå¾ã«OSããã®ã¹ã¬ãããå¼·å¶çã«ä¸æåæ¢ããã¦ãä»ã®ããã°ã©ã ãã¹ã¬ããã ãã«CPUãªã½ã¼ã¹ãå²ãå½ã¦ç¶ããå ´åãä»ã®ã¹ã¬ããã¯ä½å
ã¯ããã¯ã®CPUãµã¤ã¯ã«ãä¸ããããããæä»ããã¯Xã確ä¿ãããã¾ã¾ã®è³æºã«ã¢ã¯ã»ã¹ãã¦ã¯ãªããªãã®ã§ãå©ç¨å¯è½ãªCPUãµã¤ã¯ã«å
¨ã¦ãããã«æ¨ã¦ãäºã«ãªãããããå°éç¨èªã§ããããã³ã°ã¨å¼ã¶ã
æªæããã¹ã±ã¸ã¥ã¼ãªã³ã°ã«å¯¾ãã¦èæ§ã®ããããã¯ã®ä½¿ãæ¹ä¾
ä»ã®ã¹ã¬ãããæä»ããã¯ãç²å¾ããã¾ã¾ã§ãã£ã¦ãããã°ã©ã ãé²è¡ããã使ãæ¹ã¯ããããã
try_lock()ãç¨ãã¦ãããã¯ãåããã¨ã¯è©¦ã¿ããã©ãããã¯ãåããªãã£ããå¥ã®æ段ãè¬ãããã¨ãããã¿ã¼ã³ã¯ããããã³ã°ãçºçããªãã®ã§Lock-Freeã¨å¼ã¹ãäºããããããã¯ãå©ç¨ãã¦ããã«ãé¢ããããã ãå
¸åçã«ã¯ãã¡ãã´ãå
çã®mallocの話ãããã«è©²å½ããã¢ã«ã´ãªãºã ã®ä¸ä¾ã
Lock-Freeã®é²è¡ä¿è¨¼ã«ã¤ãã¦
ã¹ã±ã¸ã¥ã¼ã©ã®ç«å ´ããLock-Freeã¨ããã®ã解éãç´ãã¨ãã©ããªã¹ã±ã¸ã¥ã¼ãªã³ã°ãè¡ã£ã¦ã絶対ã«é²è¡ãã¦ãã¾ããã¢ã«ã´ãªãºã ã§ãã£ã¦ãLock-Freeã§ãããã¨ã証æããããã«ã¯ãèãããéãææªã®ã¹ã±ã¸ã¥ã¼ãªã³ã°ãè¡ã£ãå ´åã«ãã¢ã«ã´ãªãºã å´ãä½ããã®æä½ãå®ç¾ããã¾ã§ã«å¿ è¦ãªã¯ããã¯æ°ã®ä¸éãç¡éã§ãªããã¨ããäºã示ãã°è¯ãã
ä¾ãã°ã·ã³ãã«ãªLock-Free Stackã®å ´åã
- tåã®ã¹ã¬ããã§å®è¡ãã¦ãã
- ã¹ã±ã¸ã¥ã¼ã©ã¯pushãpopã1ã¤ã§ãå®è¡ãããªãããææªã±ã¼ã¹ã®ã¹ã±ã¸ã¥ã¼ã«ãè¡ããã¨ãã
ã¨ããç¶æ³ã«ã¦
- headãã¤ã³ã¿ã®ã¢ãã¬ã¹Pãèªãã®ã«aã¯ããã¯(æé)
- mallocã«bã¯ããã¯(æé)
- mallocããã¡ã¢ãªé åQk*3ã«å¿ è¦ãªãã¼ã¿ãæ¸ãè¾¼ãã®ã«cã¯ããã¯(æé)
- PâQkã®CASã試ã¿ãã®ã«dã¯ããã¯(æé)
- ã¹ã±ã¸ã¥ã¼ã©ã¯CASã失æããããã®ã§CASå®è¡ã®ç´åã§ä»ã®ã¹ã¬ããã«CPUæéãå²ãå½ã¦ã
- âã®1〜5ã®åä½ãtåã®ã¹ã¬ããå ¨é¨ã®åç¹°ãè¿ã
- ã¹ã±ã¸ã¥ã¼ã©ã¯ã©ã®ã¹ã¬ããã«CPUæéãå²ãå½ã¦ã¦ãPâQkã®CASãå®è¡ãããã¨ãã¦ããã¹ã¬ããã°ãã
- tåã®ã¹ã¬ããã®ãã¡ã©ãããå¿ ãCASæåãããã¹ã±ã¸ã¥ã¼ã©ã¯ t * (a + b + c + d)ã¯ããã¯ã§æäº
- t * (a + b + c + d)ã¯æéãªã®ã§ãã®ã¢ã«ã´ãªãºã ã«ã¯é²è¡ä¿è¨¼ããã
ã¨ããããã«Lock-Freeæ§(=é²è¡ä¿è¨¼)ã証æã§ããã
Wait-Freeã«é¢ãã¦ã¯ã¾ãæ°ãåãããæ¸ãã
*1:å½ç¶OSã®ã¬ãã«ã§ããªã¨ã³ããã£ããªæ©è½ãå®ç¾ãã¦ããå ´åã ããWindowsãLinuxãMacOSããã¾ã©ãã®OSã¯ããã©ã«ãã§ã¯å¤§æµãããå®è£ ãã¦ãããã¹ãã³ã³ã ã¨ãã®æ©è½ã®ãªã¼ãã¼ããããæ°ã«ãã¦ãã³ããªã¨ã³ããã£ããªOSãå©ç¨ããå ´åããããããã詳ãããªã
*2:éè¡ã®ã·ã¹ãã ã ã¨å ¨ã¦ã®æä½ããã£ãã·ã¥ãã¹ããå ´åãæ³å®ãã¦ããã§ãæ§è½ãã¡ããã¨åºãããã«è¡ãåããããªåªåããã¦ããã¨èãããã¨ããã
*3:kã¯ãã®ã¹ã¬ããåºæã®æ°å