thread-specific storage

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:thread-specific storage

Post by Pype.Clicker »

Brendan wrote: Hi,
Pype.Clicker wrote:I suppose if i'm to use that trick, i'd rather tune the switching code so that the very page (or table) that contains thread-local variables will be replaced (but still not touching CR3 to avoid full TLB flush) as needed ...
Does your scheduler schedule processes or threads, and how big will your TLS areas be?
I schedule threads. That is, if a switch keeps in the same process, then CR3 is kept untouched, so that the new thread can benefits of already-present-data in TLB cache and can execute faster. When a switch happens to go to another process, then CR3 is modified (implicitly, by hardware task switching, iirc).

There's something similar to your 'priority classes' too (indirectly and untested since i have no multi-cpu machine at hand), by the amount of TSS a process is allotted. E.g. if a process owns only one TSS, then it cannot have two threads running simultaneously, even if the machine has 16 CPUs. If the process owns 4 TSSes, then it can use up to 4/16 CPUs simultaneously. Yet you _could_ start several processes, each with their own set of TSS, all working together (and optionally sharing memory) as the web server you described.

However, usually, webserver writers want threads not only to take advantage of multiple CPUs, but mainly to compensate the I/O latency and keep the CPU busy while there is disk access or network delays. That suggests to have more threads than CPUs and as low overhead for thread switching within the same app. as possible ...
I schedule threads, and for my OS I recommend using the TLS areas for as much as possible - process space should only be used for the executable file, and any data that is shared by all threads (which should be almost nothing if good OOP practices are used).
And i keep thinking that your model (as much thread-local state as possible, "process" area is mainly for code and things that _must_ be shared) looks much more like pre-forked multiprocess with shared memory model than it looks like the usual "multithreat" model.

Not meaning that it's silly to do it ... it's just not exactly what threads have been looking like for the past 10 years ;)
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:thread-specific storage

Post by Candy »

Pype.Clicker wrote: .. the usual "multithreat" model.
I'm hoping that's a typo.
User avatar
kataklinger
Member
Member
Posts: 381
Joined: Fri Nov 04, 2005 12:00 am
Location: Serbia

Re:thread-specific storage

Post by kataklinger »

I was thinking early this morning, and if you have one TSS per CPU you can use TR as index:

Code: Select all

SYS_CODE_SEL equ 0x08
SYS_DATA_SEL equ 0x10
.....
TSS_CPU0 equ 0x??
TSS_CPU1 equ TSS_CPU0 + 0x8
TSS_CPU2 equ TSS_CPU1 + 0x8
....
TSS_CPU16 equ TSS_CPU15 + 0x8
....

Code: Select all

str ax
shl ax,3
sub ax,TSS_CPU0>>3
tom1000000

Re:thread-specific storage

Post by tom1000000 »

Hello,

I know that Windows has Thread Local Storage as part of its API.

Does Linux / Unix have an equivalent?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:thread-specific storage

Post by Pype.Clicker »

tom1000000 wrote: I know that Windows has Thread Local Storage as part of its API.
Does Linux / Unix have an equivalent?
Posix threads offer TLS for quite a long time now.
For the more recent ABI, probably available only in more recent kernels; see above:
gcc-3.3.info.gz redirects the curious reader to http://people.redhat.com/drepper/tls.pdf, for those who wished to know
Post Reply