Created
July 19, 2016 08:05
-
-
Save KevinKu/4fbe2c14acc6a027df0deff34371f911 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(::) | |
/* | |
*這邊設計是不斷的try 直到搶到index 如果擔心CPU會空轉的話 可以多個計 | |
*數的變數 設計成大於某數值 讓出CPU | |
*/ | |
{ | |
__sync_synchronize(); | |
/* | |
*這邊需要一個屏障指令是要確保 讀取s是最新的數值 | |
*/ | |
r=s; | |
/* | |
*取得最新可分配的index | |
*/ | |
if( (r-get_last_reader_index()) < Ringbuffer_length ) | |
/* | |
*檢查這index是否會蓋掉reader的job | |
*/ | |
{ | |
if(__sync_bool_compare_and_swap(&s,r,r+1)) | |
/* | |
*writer們開始搶這index拉 | |
*這指令有個特性 成功代表其他失敗 | |
*這樣就確保{}內只有 | |
*一個thread執行 | |
*/ | |
{ | |
__sync_synchronize(); | |
/* | |
*確保其他thread讀取是最新的可分 | |
*配index 所以插入一條屏障指令 | |
*/ | |
return r; | |
} | |
/* | |
*走到這代表是搶這index失敗的writer 所以要重新取 | |
*得最新的可分配index 與重新檢查是否會蓋到read | |
*er的job | |
*/ | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment