2014年5月2日 星期五

TLS 小筆記

Thread Local Storage的實作需要compiler, linker, 甚至OS的幫忙才能達成 (所以ELF也需要改)


TLS的實作分成4種model
1. General Dynamic Model可以處理所有狀況
2. Local Dynamic Model的限制在於 TLS variable需要declare在同個file scope (extern thread_local), 在variable >=2的情況下會有賺頭. 這時存取變數只需要透過 get_tls_address(module , 0) 拿取一次base後 (tlsoffset指的地方), 加上offset就能存取各個變數了
3. 忘惹自己查
4. 忘惹自己查

TLS會在以下三種狀況下allocation
1. program start
2. thread create
3.  如果thread_local declare在.so file, 當.so load好後只會create table (如下圖的dtv t4), 直到TLS variable被access的時後, 才會做allocation

http://docs.oracle.com/cd/E19683-01/817-3677/chapter8-1/index.html

ps. TLS varible的address是透過 thread pointer + module id  + offset 算出的
ps. 每個thread有一個table
dtv: dynamic thread vector
TCB: thread control block