• 沒有找到結果。

Thread Queue

在文檔中 多執行緒Java處理器設計 (頁 73-77)

第四章 Java 處理器 Temporal Multithreading 架構

4.3. Thread Queue

當 Thread Manager Unit 維護 2 個以上的 thread 執行資訊時,必須建立一個機制能對

64

所有 threads 做排班,以決定每個 thread 取得 Bytecode Execution Engine 執行權的先後順 序,在此我們採用的排班演算法是循環分時排程(Round-Robin scheduling),因此執行時 間被切成一個個時間片段(Time slice),使每個 thread 能被分配到相同時間公平地輪流執 行。為此我們在 Thread Manager Unit 中設計了 Thread Queue 元件,這個元件中存在著 一組由 registers 所組成的環狀佇列(Circular Queue)(如圖 48)來輔助排班機制,並且維護 兩個指標 Ready pointer(RP)與 Tail pointer(TP)。Ready pointer 指向 Circular Queue 中某個 欄位內容,代表下個可取得 Bytecode Execution Engine 執行權的 ready thread 的 ID number;而 Ready pointer 所指的前一欄位內容則是 current thread 的 ID number;Tail Pointer 同樣會指向 Circular Queue 中某個欄位內容,這表示當產生 new thread 時需要存 入 new thread ID 的欄位、或是當執行 context-switching 時需要存入 previous thread ID 的 欄位。

先前的 Thread Manager Unit 架構[1]並未考慮 thread state 的情況,因為在多核心 JAIP 環境下每個 JAIP 僅有一個 thread 執行指令,所以不管這個 thread 是在 ready state 或是 waiting state,Thread Manager Unit 得不會有任何行為,Thread Queue 也不會被啟動。在 我們的設計下若要把同步機制的電路完整地加入到 Thread Manager Unit,使得多核心 JAIP 處理器下每個 JAIP 能維護 2 個以上的 threads,則 Thread Manager Unit 必須判斷目 前維護的 threads 相關資料之中那些是 waiting thread,並且避免 waiting thread 從 Thread Queue 再次被載入到 Bytecode Execution Engine 執行指令。其中一種做法是新增一個 waiting thread queue 存放所有 waiting threads 的 ID number,但是此作法不適用於硬體設 計上,因為隨著 waiting thread 數量增加,儲存與搜尋 waiting thread ID 的電路資源勢必 會隨著增加。

因此我們提出的修改如下:Thread Queue 只用來儲存 ready thread 的 ID number,當 Thread Controller 狀態為 ContextSwitch 的時候,如果 current thread 在 waiting state,則 Thread Controller 不會將 current thread ID 寫入到 Thread Queue 中 TP 指向的位置。如果 這個 JAIP 的某個 thread 從 waiting state 轉換到 ready state,則由 Thread Controller 修改 Thread Control Block 內對應的 thread state,以及把這個 thread ID 寫到 Thread Queue 之

65

中 TP 指向的位置。

Thread Queue 的操作以圖 48為例,當 new thread 被產生時,Thread Controller 的 new_thread_flag=1 (圖 42)並且直接把 register new_thread_id 的數值分配給 new thread,

在此假設 new thread ID 為 6。並且將 new thread ID 寫到 Thread Queue 的 Tail pointer 所 指向的欄位,如圖 48(a)與(b),最後累加 Tail pointer 的值往前指一個位置。

66

如果 current thread 執行時間到達一個片段時,則 Thread Controller 狀態進入 ContextSwitch 切換 Bytecode Execution Engine 讀取 current thread 的執行資訊,在此假設 ready thread ID 為 2、current thread ID 為 1,且 current thread 在 ready state,此時把 current thread ID 寫入到 Tail pointer 所指向的位置,Tail pointer 與 Ready pointer 都分別累加一往 前指一個位置,如圖 48(b)與(c),此時代表在這次 context-switching 執行完成後 current thread ID 為 2,而下次執行 context-switching 時,ID number 為 3 的 ready thread 即將可 以取得 Bytecode Execution Engine 執行權。

如果 current thread 執行時間到達一個片段並且 current thread 在 waiting state,則 Thread Controller 狀態進入 ContextSwitch,假設 ready thread ID 為 3、current thread ID 為 2,根據 section 3-3-2 的說明,Thread Controller 的內部 register running_thread_state 把 值傳給 prev_thread_state,此時 prev_thread_state=10 表示 current thread 在 waiting state,

Thread Queue 不需要將 current thread ID 寫到 TP(Tail Pointer)指向的位置、不需要累加 TP 的值,指向 Circular Queue 的下個位置,如圖 48(c)與(d)。當 Thread Controller 的狀 態為 ContexstSwitch 會累加 RP(Ready Pointer)的值,表示 current thread 的 ID 為 3、下個 ready thread 的 ID 為 4。

考慮目前 ready thread ID 為 4、current thread ID 為 3,假設其他 JAIP 的 current thread 釋放 lock object(e.g. 執行 monitorexit 指令)並且把 lock 權限轉移到這個 JAIP 之中 ID number 為 2 的 waiting thread。此時 DCC2JAIP_info 的值代表下一個 lock 擁有者的 threadID,首先 ICCU 的 ICCU_monExit_nxtOwner=1,Thread Controller 把 DCC2JAIP_info 的值寫入 Thread Queue 之中 TP 指向的位置,最後累加 TP 的值往前指一個位置,如圖 48(d)與(e)。

當 current thread 以終止其執行時,如圖 48(e)與(f),在此假設執行終止的 thread 其 ID number 為 4。由於此 thread 已經終止,Thread Controller 的 thread_terminate=1 並且其 狀態進入 ContextSwitch,此時不需要將 previous thread 寫入 Thread Queue,而是直接累 加 Ready pointer 使它往前指一個位置。使 ID number 等於 3 的 ready thread 能直接進入 Bytecode Execution Engine 執行指令。

67

在文檔中 多執行緒Java處理器設計 (頁 73-77)