主題內容: MIT OSE 讀書會: (共90分鐘)
時間: 2012/05/29 19:30 ~ 21:00 (聚會在Mix Coffee &Tea)
報名網址:http://registrano.com/events/345d7c分享者: Ben及與會者
複習上次實題及討論: (40分)
Lab 3 - Part A/B: Page Faults, Breakpoints Exceptions, and System Calls
LEC 5: Process Creation System call, Interrupt, and Exception Handling (handouts:IDT)LEC 6: Virtual Memory Multiprocessors and locking
社群運作的下一步 (20分鐘)
(本次活動會需要使用電腦實際上機練習, 請不要把notebook忘在家裡。)
此次活動已加入TOSSUG行程(請參閱http://www.tossug.org/2012)
(註1:活動時間 7:30pm 開始,6:30pm 開始入場。)
(註2:如包場,參加者需在店消費約200元以下,折抵場地費。)
(註3:活動採分享心得或研究實作展示。)
(註4:活動場地有提供無線網路。)
Gotcha!
> 這裡看到一個實作方式使用 gcc builtins 的函式
> http://stackoverflow.com/questions/6935442/x86-spinlock-using-cmpxchg
> Note that GCC has atomic builtins, so you don't actually need to use inline
> asm to accomplish this:
在 gcc-4.5 之後,對於 atomic builtins 有了較多的硬體架構支援,如 ARMv6+
(ldrex/strex),Android library 原本有若干 atomics macro,在新版 (應該在下個 AOSP 版本)
也改用 gcc builtins
> void spin_lock(int *p)
> {
> while(!__sync_bool_compare_and_swap(p, 0, 1));
> }
>
> void spin_unlock(int volatile *p)
> {
> asm volatile (""); // acts as a memory barrier.
> *p = 0;
> }
memory barrier 還得考慮到 SMP,有對應的硬體指令。
Thanks,
-jserv
*p = 0;}
> *p = 0;
> }
我覺得應該沒問題吧~至少我看過的很多kernl都是這樣寫的直接來看WIKI 的code吧lock: ; The lock variable. 1 = locked, 0 = unlocked.dd 0
spin_lock:mov eax, 1xchg eax, [lock] ; Atomically swap EAX register with lock variable.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^這沒問題,一定是 atomic
test eax, eax^^^^^^^^^^^^^^^^^^^^^^^^^^你指的問題,是說如果在執行這指令時被context switch,然後被修改嗎?可是context switch會保證你的register前後的內容一致阿,所以應該不會有問題才對.
jnz spin_lock
ret
x86 應該可以用 lock bus 來解決 multi-core 的問題。我記得 arm 的 ldrex/strex 也可以解決 multi-core 共競現象。有錯請指教。
Gavin
wayling於 2012年6月1日星期五UTC+8上午9時29分18秒寫道:我覺得應該沒問題吧~至少我看過的很多kernl都是這樣寫的直接來看WIKI 的code吧lock: ; The lock variable. 1 = locked, 0 = unlocked.dd 0Ben: 我定義lock 為全域變數.spin_lock:mov eax, 1xchg eax, [lock] ; Atomically swap EAX register with lock variable.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^這沒問題,一定是 atomic>> yestest eax, eax^^^^^^^^^^^^^^^^^^^^^^^^^^你指的問題,是說如果在執行這指令時被context switch,然後被修改嗎?可是context switch會保證你的register前後的內容一致阿,所以應該不會有問題才對.我們先不談content switch情況,
timing 1. 當有兩顆以上CPU 共用這個lock全域變數時, 其中第一、二顆CPU 同時執行到 xchg 及 test eax, eax 中間(因為這段程式沒有比較 eax 是否為 1, 這時有第三顆cpu即將呼叫 spinunlock,
timing 2. 第三顆 cpu 呼叫spinunlock, eax 設為0, 則第一、二顆CPU 同時拿到鎖, 進入 critical section.