[英]test&set and test&test&set LOCK implementations in ASM for C
在ASMx86(x86架構)程序集中搜索一些測試和設置,測試和測試並設置LOCK實現,以便在我的C代碼中使用。 我不希望在C中實現,而是簡單的程序集。
請指出一些有用的。
提前致謝!
您可以在IA32 x86下輕松實現測試和設置
//eax = pointer on 32 bit lock variable
//Variable must be 4 byte aligned
//edx = bit test and set number from 0..31
lock bts dword ptr [eax], edx
setnc al //al is 1 if bts instruction was successful
並且你有一個簡單的循環測試和設置實現
//eax = pointer on 32 bit lock variable
//Variable must be 4 byte aligned
//edx = bit test and set number from 0..31
@wait:
pause //CPU hint for waiting in loop
lock bts dword ptr [eax], edx
jc @wait //waiting in loop!!!
請記住,在循環中等待將凍結應用程序線程,因此它也很聰明,也可以限制最長的等待循環時間。
根據體系結構,您可以在單個指令中執行此操作,也可以通過禁用中斷來執行此操作。
80386及更高版本的兼容架構具有bts
指令,該指令將根據進位標志中的測試結果進行原子測試和設置。 以下是如何使用PPC指令實現互斥鎖的一個很好的解釋。
其他需要類似的東西:
cli ;; Clear interrupts flag.
move r0, r1 ;; Copy the value into r0.
ori r1, 1 ;; Set the bit in r1 (r1 holds the value to test-and-set.)
sti ;; Re-enable interrupts.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.