Chinese translated version of Documentation/process/volatile-considered-harmful.rst
If you have any comment or update to the content, please contact the
original document maintainer directly. However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help. Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.
Maintainer: Jonathan Corbet corbet@lwn.net
Chinese maintainer: Bryan Wu bryan.wu@analog.com
Documentation/process/volatile-considered-harmful.rst çä¸æç¿»è¯
å¦ææ³è¯è®ºææ´æ°æ¬æçå
容ï¼è¯·ç´æ¥èç³»åææ¡£çç»´æ¤è
ãå¦æä½ ä½¿ç¨è±æ
交æµæå°é¾çè¯ï¼ä¹å¯ä»¥åä¸æçç»´æ¤è
æ±å©ãå¦ææ¬ç¿»è¯æ´æ°ä¸åæ¶æè
ç¿»
è¯åå¨é®é¢ï¼è¯·èç³»ä¸æçç»´æ¤è
ã
è±æçç»´æ¤è
ï¼ Jonathan Corbet corbet@lwn.net
ä¸æçç»´æ¤è
ï¼ ä¼é¹ Bryan Wu bryan.wu@analog.com
ä¸æçç¿»è¯è
ï¼ ä¼é¹ Bryan Wu bryan.wu@analog.com
ä¸æçæ ¡è¯è
ï¼ å¼ æ±è¾ Eugene Teo eugeneteo@kernel.sg
æ¨ç Dave Young hidave.darkstar@gmail.com
以ä¸ä¸ºæ£æ
为ä»ä¹ä¸åºè¯¥ä½¿ç¨âvolatileâç±»å
Cç¨åºåé常认为volatile表示æ个åéå¯ä»¥å¨å½åæ§è¡ç线ç¨ä¹å¤è¢«æ¹åï¼å æ¤ï¼å¨å
æ ¸
ä¸ç¨å°å
±äº«æ°æ®ç»ææ¶ï¼å¸¸å¸¸ä¼æCç¨åºåå欢使ç¨volatileè¿ç±»åéãæ¢å¥è¯è¯´ï¼ä»ä»¬ç»
常ä¼ævolatileç±»åçææç§ç®æçåååéï¼å½ç¶å®ä»¬ä¸æ¯ãå¨å
æ ¸ä¸ä½¿ç¨volatileå
ä¹æ»æ¯é误çï¼æ¬ææ¡£å°è§£é为ä»ä¹è¿æ ·ã
ç解volatileçå
³é®æ¯ç¥éå®çç®çæ¯ç¨æ¥æ¶é¤ä¼åï¼å®é
ä¸å¾å°æ人çæ£éè¦è¿æ ·çåº
ç¨ãå¨å
æ ¸ä¸ï¼ç¨åºåå¿
é¡»é²æ¢æå¤ç并å访é®ç ´åå
±äº«çæ°æ®ç»æï¼è¿å
¶å®æ¯ä¸ä¸ªå®å
¨
ä¸åçä»»å¡ãç¨æ¥é²æ¢æå¤å¹¶å访é®çä¿æ¤æªæ½ï¼å¯ä»¥æ´å é«æçé¿å
大å¤æ°ä¼åç¸å
³ç
é®é¢ã
åvolatileä¸æ ·ï¼å
æ ¸æä¾äºå¾å¤åè¯æ¥ä¿è¯å¹¶å访é®æ¶çæ°æ®å®å
¨ï¼èªæé, äºæ¥é,å
åå±éççï¼ï¼åæ ·å¯ä»¥é²æ¢æå¤çä¼åãå¦æå¯ä»¥æ£ç¡®ä½¿ç¨è¿äºå
æ ¸åè¯ï¼é£ä¹å°±æ²¡æ
å¿
è¦å使ç¨volatileãå¦æä»ç¶å¿
须使ç¨volatileï¼é£ä¹å ä¹å¯ä»¥è¯å®å¨ä»£ç çæå¤æä¸
个bugãå¨æ£ç¡®è®¾è®¡çå
æ ¸ä»£ç ä¸ï¼volatileè½å¸¦æ¥çä»
ä»
æ¯ä½¿äºæ
åæ
¢ã
æèä¸ä¸è¿æ®µå ¸åçå æ ¸ä»£ç ï¼
spin_lock(&the_lock);
do_something_on(&shared_data);
do_something_else_with(&shared_data);
spin_unlock(&the_lock);
å¦æææç代ç é½éµå¾ªå éè§åï¼å½ææthe_lockçæ¶åï¼ä¸å¯è½æå¤çæ¹åshared_dataç
å¼ãä»»ä½å¯è½è®¿é®è¯¥æ°æ®çå
¶ä»ä»£ç é½ä¼å¨è¿ä¸ªéä¸çå¾
ãèªæéåè¯è·å
åå±éä¸æ ·ââ å®
们æ¾å¼çç¨æ¥ä¹¦åæè¿æ · ââ æå³çæ°æ®è®¿é®ä¸ä¼è·¨è¶å®ä»¬è被ä¼åãæ以æ¬æ¥ç¼è¯å¨è®¤ä¸º
å®ç¥éå¨shared_dataéé¢å°æä»ä¹ï¼ä½æ¯å 为spin_lock()è°ç¨è·å
åå±éä¸æ ·ï¼ä¼å¼ºå¶ç¼
è¯å¨å¿è®°å®æç¥éçä¸åãé£ä¹å¨è®¿é®è¿äºæ°æ®æ¶ä¸ä¼æä¼åçé®é¢ã
å¦æshared_data被声å为volatileï¼éæä½å°ä»ç¶æ¯å¿
é¡»çãå°±ç®æ们ç¥é没æå
¶ä»äººæ£å¨
使ç¨å®ï¼ç¼è¯å¨ä¹å°è¢«é»æ¢ä¼å对临çåºå
shared_dataç访é®ãå¨éææçåæ¶ï¼
shared_dataä¸æ¯volatileçãå¨å¤çå
±äº«æ°æ®çæ¶åï¼éå½çéæä½å¯ä»¥ä¸åéè¦
volatile ââ 并ä¸æ¯ææ½å¨å±å®³çã
volatileçåå¨ç±»åæåæ¯ä¸ºé£äºå
åæ å°çI/Oå¯åå¨èå®ä¹ãå¨å
æ ¸éï¼å¯åå¨è®¿é®ä¹åº
该被éä¿æ¤ï¼ä½æ¯äººä»¬ä¹ä¸å¸æç¼è¯å¨âä¼åâ临çåºå
çå¯åå¨è®¿é®ãå
æ ¸éI/Oçå
å访é®
æ¯éè¿è®¿é®å½æ°å®æçï¼ä¸èµæéè¿æé对I/Oå
åçç´æ¥è®¿é®ï¼å¹¶ä¸ä¸æ¯å¨ææä½ç³»æ¶æä¸
é½è½å·¥ä½ãé£äºè®¿é®å½æ°æ£æ¯ä¸ºäºé²æ¢æå¤ä¼åèåçï¼å æ¤ï¼å说ä¸æ¬¡ï¼volatileç±»åä¸
æ¯å¿
éçã
å¦ä¸ç§å¼èµ·ç¨æ·å¯è½ä½¿ç¨volatileçæ
åµæ¯å½å¤çå¨æ£å¿ççå¾
ä¸ä¸ªåéçå¼ãæ£ç¡®æ§è¡ä¸
个å¿çå¾
çæ¹æ³æ¯ï¼
while (my_variable != what_i_want)
cpu_relax();
cpu_relax()è°ç¨ä¼éä½CPUçè½éæ¶èæè
让ä½äºè¶
线ç¨åå¤çå¨ï¼å®ä¹ä½ä¸ºå
åå±éä¸æ ·åº
ç°ï¼æ以ï¼åä¸æ¬¡ï¼volatileä¸æ¯å¿
éçãå½ç¶ï¼å¿çå¾
ä¸å¼å§å°±æ¯ä¸ç§å常è§çåæ³ã
å¨å æ ¸ä¸ï¼ä¸äºç¨å°çæ åµä¸volatileä»ç¶æ¯ææä¹çï¼
å¨ä¸äºä½ç³»æ¶æçç³»ç»ä¸ï¼å 许ç´æ¥çI/0å å访é®ï¼é£ä¹åé¢æå°ç访é®å½æ°å¯ä»¥ä½¿ç¨
volatileãåºæ¬ä¸ï¼æ¯ä¸ä¸ªè®¿é®å½æ°è°ç¨å®èªå·±é½æ¯ä¸ä¸ªå°ç临çåºå并ä¸ä¿è¯äºæç §
ç¨åºåææçé£æ ·åç访é®æä½ãæäºä¼æ¹åå åçå èæ±ç¼ä»£ç è½ç¶æ²¡æä»ä¹å ¶ä»ææ¾çéä½ç¨ï¼ä½æ¯æ被GCCå é¤çå¯
è½æ§ãå¨æ±ç¼å£°æä¸å ä¸volatileå ³é®åå¯ä»¥é²æ¢è¿ç§å é¤æä½ãJiffiesåéæ¯ä¸ç§ç¹æ®æ åµï¼è½ç¶æ¯æ¬¡å¼ç¨å®çæ¶åé½å¯ä»¥æä¸åçå¼ï¼ä½è¯»jiffies
åéæ¶ä¸éè¦ä»»ä½ç¹æ®çå éä¿æ¤ãæ以jiffiesåéå¯ä»¥ä½¿ç¨volatileï¼ä½æ¯ä¸èµæ
å ¶ä»è·jiffiesç¸åç±»ååé使ç¨volatileãJiffies被认为æ¯ä¸ç§âæè ¢çéçç©”
ï¼Linusçè¯ï¼å 为解å³è¿ä¸ªé®é¢æ¯ä¿æç°ç¶è¦éº»ç¦çå¤ãç±äºæäºI/0设å¤å¯è½ä¼ä¿®æ¹è¿ç»ä¸è´çå å,æ以ææ¶,æåè¿ç»ä¸è´å åçæ°æ®ç»æ
çæééè¦æ£ç¡®ç使ç¨volatileãç½ç»éé å¨ä½¿ç¨çç¯ç¶ç¼ååºæ£æ¯è¿ç±»æ å½¢çä¸ä¸ªä¾
åï¼å ¶ä¸éé å¨ç¨æ¹åæéæ¥è¡¨ç¤ºåªäºæ述符已ç»å¤çè¿äºã
对äºå¤§å¤ä»£ç ï¼ä¸è¿°å ç§å¯ä»¥ä½¿ç¨volatileçæ
åµé½ä¸éç¨ãæ以ï¼ä½¿ç¨volatileæ¯ä¸ç§
bug并ä¸éè¦å¯¹è¿æ ·ç代ç é¢å¤ä»ç»æ£æ¥ãé£äºè¯å¾ä½¿ç¨volatileçå¼å人åéè¦éä¸æ¥æ³æ³
ä»ä»¬çæ£æ³å®ç°çæ¯ä»ä¹ã
é常欢è¿å é¤volatileåéçè¡¥ä¸ ï¼ åªè¦è¯æè¿äºè¡¥ä¸å®æ´çèèäºå¹¶åé®é¢ã
注é
[1] http://lwn.net/Articles/233481/
[2] http://lwn.net/Articles/233482/
è´è°¢
æåç±Randy Dunlapæ¨å¨å¹¶ä½åæ¥ç 究
ç±Jonathan Corbetæ°å
åèSatyam Sharmaï¼Johannes Stezenbachï¼Jesper Juhlï¼Heikki Orsilaï¼
H. Peter Anvinï¼Philipp HahnåStefan Richterçæè§æ¹åäºæ¬æ¡£ã