UNIX Socket FAQ

You are not logged in.

#1 2008-02-25 01:31 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,187

Re: Private pages possible for threads?

Just wondering, does anyone know a way for a thread to create a memory page
locally, which isn't shared with other threads? That is, a chunk of memory that
can't be accessed by other threads, but can by the current one, while other memory
is shared as usual.

This on x86 Linux. Kernel modifications are fine, as I just want to know if it's possible
at all, without doing too crazy stuff.

Offline

#2 2008-02-25 09:44 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Private pages possible for threads?

I'm not certain what you are asking for... ?

Memory can be allocated within any thread... and as long as it is done 'locally' for the thread and the memory address is not exposed... no one is going to be able to purposely access it...

On the other hand...

Are you looking for something like the pthread_key_create / pthread_setspecific / pthread_getspecific calls ?

On the off chance its the latter - since you didn't specify OS... the same functionality is available under Windows with the TlsAlloc, TlsGetValue and TlsSetValue calls...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2008-02-25 01:21 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,736
Website

Re: Private pages possible for threads?

mlampkin wrote:

since you didn't specify OS


i3839 wrote:

This on x86 Linux.

That sounds specific enough to me... ;-)  Besides, given how much we berate others
here who ask about Windoze programming (at the "UNIX Socket FAQ" web page!),
do you really think i3839 is crazy enough to be talking about Windoze coding?? ;-)

In addition to the pthread_{get,set}specific() stuff, there's also the TLS (Thread Local
Storage) stuff in recent GCC/glibc...  Basically, you just use the "__thread" keyword
when declaring a variable, it every thread gets its own private copy...  Eg: "__thread int i;"...

Or, do you really want all other threads to be totally prevented from possibly accessing
the memory in any way at all, even if they were to obtain a pointer to it?  I'm not
sure if that'd be possible, since they all share a single address space, and if the
memory is readable/writable by one thread, I'd think it necessarily must be by all
others, as well... *shrug*

Offline

#4 2008-02-25 04:10 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,187

Re: Private pages possible for threads?

RobSeace;23910 wrote:

Or, do you really want all other threads to be totally prevented from possibly accessing
the memory in any way at all, even if they were to obtain a pointer to it?  I'm not
sure if that'd be possible, since they all share a single address space, and if the
memory is readable/writable by one thread, I'd think it necessarily must be by all
others, as well... *shrug*

Yeah, that. And it's plain impossible. :-)
(At least, when ignoring ugly segment hacks.)

Offline

#5 2008-02-25 05:54 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Private pages possible for threads?

Thats really bizarre... I re-read it twice and the bottom of his message didn't display when I read / went to reply to it earlier today...

Either my browser, the forum ( db ) or my brain must have suffered a hiccup of some sort...

Anyway...

Here is a REALLY convoluted set of things you could do that would get you part of the way towards what you want...

Retrieve the default stack size for threads on the system... then during thread creation explicitly set the stack address and size ( default + 'hidden' memory pages desired ) via use of attributes configured with the pthread_attr_setstackaddr and pthread_attr_setstacksize calls...

An alternative for the stack size attribute would be to create a guard zone via the guard size attribute i.e. pthread_attr_setguardsize... and while any stack manipulations / overruns by the stack pointer would cause a fault ( catchable via a SIGSEGV etc. )... you could still access the space directly as 'hidden' memory...

I realize this STILL wouldn't completely protect / hide the memory in question... and that other threads could still potentially access it... but in this case it would be akin to other threads in the program accessing each other's stack space directly...

?!


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2008-02-25 06:08 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: Private pages possible for threads?

Kinda off topic...

But actually I wasn't aware of the __thread stuff... I knew it had been talked about once upon a time but didn't think it had actually been implemented... but after looking it ( and the history ) up I realize they went ahead with their cunning plan...

Btw - I it looks like the implementation was pushed by RedHat and their lead threading / lib guy Drepper... among others... under the premise that the pthread model was too hard / complicated ( ? )

The strange part is that while it MAY make the code 'less complicated' if you are writing for code to be compiled on gcc using a supported platform... it looks like the use of that attribute will make the code that much harder to understand / convoluted when designing cross platform / compiler / lib versions... ?!

Oh well...


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#7 2008-02-25 10:57 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,736
Website

Re: Private pages possible for threads?

Yeah, I'm really not sure what's so bloody difficult about pthread_{set,get}specific()...
I use it a few places to essentially give me thread-specific static variables...  I really
don't recall it being mind-meltingly complex or anything... ;-)  I think the only tricky
thing might be possibly needing to use pthread_once() to create the key initially, if
you don't do it prior to creating any other threads anyway...  I've never actually
bothered to switch to "__thread", and probably won't bother... *shrug*

Offline

Board footer

Powered by FluxBB