以下是關于volatile關鍵的外文描述:
By declaring an object volatile, the compiler is informed that the value of the object can change beyond the compiler’s control. The compiler must also assume that any accesses can have side effects—thus all accesses to the volatile object must be preserved.
There are three main reasons for declaring an object volatile:
Shared access; the object is shared between several tasks in a multitasking environment
Trigger access; as for a memory-mapped SFR where the fact that an access occurshas an effect
Modified access; where the contents of the object can change in ways not known tothe compiler.
1、Shared access
the object is shared between several tasks in a multitasking environment。
當同一全局變量在多個線程之間被共享時,有可能會出現(xiàn)同步錯誤,編譯器可能會將訪問該全局變量的代碼優(yōu)化為訪問某個寄存器,而不會再次訪問相應的內(nèi)存,導致程序運行錯誤。
測試代碼如下:
staticstructrt_threadv_thread1;
staticcharv_thread1_stack[8192];
staticstructrt_threadv_thread2;
staticcharv_thread2_stack[8192];
staticintflag;
staticintcount;
staticvoidrt_init_thread1_entry(void*parameter)
{
while(1)
{
rt_thread_mdelay(300);
flag=1;
rt_thread_mdelay(300);
flag=0;
if(count++>10)
{
rt_kprintf("thread1 exit.\n");
flag=1;
return;
}
}
}
staticvoidrt_init_thread2_entry(void*parameter)
{
while(1)
{
while(flag==0);
rt_kprintf("thread2 running.\n");
rt_thread_mdelay(100);
if(count++>10)
{
rt_kprintf("thread2 exit.\n");
return;
}
}
}
intvolatile_test()
{
rt_err_tresult=RT_EOK;
result=rt_thread_init(&v_thread1,"vth1",
rt_init_thread1_entry,
RT_NULL,
v_thread1_stack,sizeof(v_thread1_stack),
RT_THREAD_PRIORITY_MAX/3-1,20);
if(result==RT_EOK)
rt_thread_startup(&v_thread1);
result=rt_thread_init(&v_thread2,"vth2",
rt_init_thread2_entry,
RT_NULL,
v_thread2_stack,sizeof(v_thread2_stack),
RT_THREAD_PRIORITY_MAX/3,20);
if(result==RT_EOK)
rt_thread_startup(&v_thread2);
return0;
}
MSH_CMD_EXPORT(volatile_test,runvolatile_test);
上面的測試代碼在 O0 優(yōu)化時正常運行,打印結果如下:
msh />volatile_test
thread2 running.
msh />thread2 running.
thread2 running.
thread2 running.
thread2 running.
thread2 running.
thread2 running.
thread2 running.
thread2 running.
thread2 exit.
thread1 exit.
但是如果開啟 O3 優(yōu)化,則打印結果如下:
msh />volatile_test
thread1 exit.
也就是說 thread2 永遠得不到運行,那么原因是什么呢,請看下圖的反匯編,語句
while(flag==0);
被優(yōu)化成了如下匯編:
00108b4c: ldr r3, [r4, #+288] # 第一次讀取 flag 的實際值到 r3
00108b50: cmp r3,#0 # 對比 r3 的值是否為 0
00108b54: bne +0 ; # 如果不為 0 則跳轉
00108b58: b -8 ; # 再次跳轉回 cmp 語句繼續(xù)循環(huán)
也就是說,整個程序被翻譯成,只讀取一次 flag 的實際值,后續(xù)一直使用 r3 寄存器中的值來進行對比,而第一次讀取到的 r3 值為零,因此 while 的條件將永遠成立,thread2 永遠也得不到執(zhí)行。
2、Trigger access
as for a memory-mapped SFR(特殊功能寄存器)where the fact that an access occurs has an effect。
當讀取類似串口設備的數(shù)據(jù)寄存器時,一定要加上 volatile,因為該地址寄存器中的數(shù)值可能會發(fā)生改變,如果不加 volatile,可能會發(fā)現(xiàn)讀取的數(shù)據(jù)是錯誤的。
3、Modified access
where the contents of the object can change in ways not known to the compiler.
對象的內(nèi)容可能會被以編譯器不清楚的方式被修改,例如在內(nèi)核態(tài)與用戶態(tài)的程序在不同的虛擬地址訪問同一塊物理內(nèi)存,此時如果不加上 volatile,則外部的修改無法被感知到,造成程序錯誤。
關于優(yōu)化錯誤
如果系統(tǒng)在低優(yōu)化等級能正常運行,但是在高優(yōu)化的情況下的無法正常運行,首先懷疑兩個方面:
是否是一些關鍵操作沒有添加 volatile
是否是有內(nèi)存寫穿(因為不同的優(yōu)化等級改變了內(nèi)存排布導致寫穿位置發(fā)生改變)
4、如何避免關鍵操作被優(yōu)化
情況一
如果發(fā)現(xiàn)加上了printf打印,或者調用了某個外部函數(shù),系統(tǒng)就正常運行了,也要懷疑是否出現(xiàn)了變量訪問被優(yōu)化的情況,因為如果加上了外部函數(shù)(非本文件中的函數(shù)或其他庫中的函數(shù))調用,則編譯器無法確定被引用的變量是否被外部函數(shù)所改變,因而會自動從原有地址重新讀取該變量的值。
如果修改上面的測試代碼,在 while 循環(huán)中加入rt_kprintf打印如下:
while(flag==0)
{
rt_kprintf("5\n");
}
則程序仍然正常運行,原因就是編譯器不知道rt_kprintf函數(shù)是否會修改 flag 變量,因此編譯器會嘗試每次都重新讀取flag的值。
情況二
還可以使用另外一種方式來解決這個問題,如下:
while(flag==0)
{
asmvolatile("":::"memory");
}
If our instruction modifies memory in an unpredictable fashion, add "memory" to the list of clobbered registers. This will cause GCC to not keep memory values cached in registers across the assembler instruction. We also have to add thevolatile keywordif the memory affected is not listed in the inputs or outputs of the asm.
這將會告訴編譯器,經(jīng)過一些指令后,memory 中的數(shù)據(jù)已經(jīng)發(fā)生了變化,GCC 將不會再使用寄存器作為數(shù)據(jù)的緩存。因此再次使用這些數(shù)據(jù)時,會從內(nèi)存中重新嘗試讀取。使用關鍵字 volatile 也可以達到同樣的效果。
以下描述摘自《GCC-Inline-Assembly-HOWTO》:
Some instructions clobber some hardware registers. We have to list those registers in the clobber-list, ie the field after the third ’:’ in the asm function. This is to inform gcc that we will use and modify them ourselves. So gcc will not assume that the values it loads into these registers will be valid. We shoudn’t list the input and output registers in this list. Because, gcc knows that "asm" uses them (because they are specified explicitly as constraints). If the instructions use any other registers, implicitly or explicitly (and the registers are not present either in input or in the output constraint list), then those registers have to be specified in the clobbered list.
If our instruction can alter the condition code register, we have to add "cc" to the list of clobbered registers.
4、結論
關于 volatile 關鍵字,最重要的是要認識到一點,即是否在編譯器清楚的范圍之外,所操作的變量有可能被改變,如果有這種可能性,則一定要添加上 volatile 關鍵字,以避免這種錯誤。
歸根結底,是要確定代碼在真實運行的狀態(tài)下,當其訪問某個變量時,是否真正地從這個變量所在的地址重新讀取該變量的值,而不是直接使用上次存儲在某個寄存器中的值。
-
寄存器
+關注
關注
31文章
5590瀏覽量
129092 -
代碼
+關注
關注
30文章
4942瀏覽量
73160 -
RT-Thread
+關注
關注
32文章
1542瀏覽量
44294
發(fā)布評論請先 登錄
【原創(chuàng)精選】RT-Thread征文精選技術文章合集
RT-Thread Smart已正式上線,源代碼可下載,采用Apache License 2.0
記錄——基于 RT-Thread 實現(xiàn) USB 虛擬串口
RT-Thread全球技術大會:螢石研發(fā)團隊使用RT-Thread的技術挑戰(zhàn)
RT-Thread全球技術大會:RT-Thread Smart更好的兼容Linux生態(tài)
RT-Thread全球技術大會:Kconfig在RT-Thread中的工作機制
RT-Thread全球技術大會:RT-Thread對POSIX的實現(xiàn)情況介紹
RT-Thread全球技術大會:在RT-Thread中使用棧幀來調試程序
RT-Thread 編程風格
使用RT-Thread Studio進行智能家居終端的設計

調試 RT-Thread 時,我的代碼怎么“被優(yōu)化”了? | 技術集結
評論