The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: mathewzhao on September 12, 2008, 07:52:33 AM

Title: How to use Thread Local Storage in programming
Post by: mathewzhao on September 12, 2008, 07:52:33 AM
  I recently learned the concept of TLS(Thread Local Storage),but I don't know how to make use of it in programming(since the book only talks the theory ).
  Could someone give me an example in MASM ?
  Thanks!
Title: Re: How to use Thread Local Storage in programming
Post by: MichaelW on September 18, 2008, 05:10:24 AM
At least at its simplest there really isn't much to using Thread Local Storage (TLS), so instead of a do-nothing example I tried to create something marginally useful. The attachment tests a modified version of the MASM32 nrandom procedure that uses TLS for the seed. The use of a per-thread seed allows the procedure to be called from multiple threads and still produce the same sequences than nrandom would if called from a single thread. If nrandom, with its global seed, were called from multiple, overlapping threads, the generated sequence in each of the threads would likely be very different from the sequence that would be produced from a single thread, and possibly less random.

Considering that per the PSDK the only possible error return for TlsAlloc is TLS_OUT_OF_INDEXES, and that the minimum number of indexes is guaranteed to be at least 64 for all systems, I could see no reason to include error handling.


[attachment deleted by admin]
Title: Re: How to use Thread Local Storage in programming
Post by: BogdanOntanu on September 18, 2008, 07:02:20 AM
Also keep in mind that TLS is only for lazy HLL programmers that do not have access to a function to allocate memory and allocate a memory buffer per thread. TLS does this "automatically" at PE start time but this can be done "by hand" by the programmer very easy and with more power. Hence there is little purpose for using TLS.
Title: Re: How to use Thread Local Storage in programming
Post by: Jimg on May 15, 2009, 12:44:37 AM
I'd like to try this for a project I'm working on.  I don't see how I can use what's in your example, however, Michael.

I'm going to create several threads.  Each thread will call the same procs as needed.  When I'm in one of the called procs, I can get the id of the thread that called, but I can't figure out how to get the tls index that I got by doing a tlsAlloc somewhere else in the thread.

It seems I need a place to store the index for the thread so that I can get the location of the data for the thread, but I need some way to get the location of that place I stored the index, and so it goes around and around.

Is there some api I just can't find, or am I totally misunderstanding what this is for?
Title: Re: How to use Thread Local Storage in programming
Post by: Jimg on May 15, 2009, 01:16:50 AM
Okay, never mind, I get it, it's automatic.  Very confusing.
Title: Re: How to use Thread Local Storage in programming
Post by: MichaelW on May 15, 2009, 03:34:30 AM
What I did was relatively easy because I was able to store the seed directly in the slot for the thread. If I had needed to store more data I would have had to allocate space for the data and store a pointer to the data in the slot. When I was trying to sort this out one of the things I examined was the CRT source files from the PSDK. Many, if not most, of the CRT functions have MT versions that use TLS. See tidtable.c for the functions that are used to setup and access the thread data table.
Title: Re: How to use Thread Local Storage in programming
Post by: Jimg on May 15, 2009, 05:28:44 AM
Exactly what I did.  HeapAlloc and store the address in the tls.  I was just confused because I thought I needed a TlsAlloc for each thread, and that didn't gain me anything.  Now I can download as many files simultaneous as I need.