Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #1487

Re: Question about inter-thread stack references

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.compilers
Subject Re: Question about inter-thread stack references
Date 2015-01-18 17:12 +0000
Organization Aioe.org NNTP Server
Message-ID <15-01-030@comp.compilers> (permalink)
References <15-01-015@comp.compilers>

Show all headers | View raw


On 2015-01-16, Ivan Godard <ivan@ootbcomp.com> wrote:
> If a process has two or more threads running in it, what are the rules
> regarding one thread referencing memory in a different thread's stack?

In what system?

In all the systems I'm familiar with, threads run in one address
space, and accessing all valid objects is fair game regardless of
their storage class or thread affiliation.

If we are talking in C terms, then if in some thread, the following
block is executing:

  { some_type_t x; ... }

then while that block continues to execute, the object x is valid.

Anything in the address space which has a pointer to the object can
use that object.

When the block terminates, then the pointer becomes indeterminate.

Secondly, if this x is accessed by multiple threads, then the usual concurrency
considerations apply. Unless x is some atomically accessed type, you need some
kind of mutex, and so forth.

Consider that this sort of stack access is absolutely essential in
various kinds of core multithreading API's. For instance, send a message
to another thread and wait for a reply:

   message_type_t request, reply;

   request_fill(&request, ...);

   if (send_message(other_thread, &request, &reply) == SEND_OKAY) {
     /* process reply */
   }

How this fictitious API works is that the request and reply objects
(local to the thread, on its stack) are registered into some global
message-queuing data structure. The thread which calls send_message is
then blocked. Because it is blocked, those objects remain valid: the
suspension prevents it from terminating the statement block.  While
the sender is blocked, the other thread can access the request, and
fill in the reply. Then when the sender is unblocked, everything is
cool: it finds the repy.  The suspend and resume discipline also
ensures proper synchronization. The send_message thread does not
access the reply prior to the reply being produced, and the receiver
of course won't access the request before that is enqueued and the
sender thread is being put to sleep.

> In the usual way that paging hardware works both stacks can be made
> visible to both threads, but interstack visibility could also be
> prevented (assuming that stacks occupied whole pages).

How paging hardware works is that preventing inter-stack visibility
using paging would be so expensive, that it would be making a mockery
of the concept of threading.  The processor would have to swap page
tables and flush translation caches when switching thread contexts.

Some special support for protecting stacks could make it possible.
For instance stacks could be segments, and segments could have some
security word in their descriptor which is cheaply checked against the
current security descriptor of the current thread.

> I vaguely (so vague that I can't find citations) remember that on some,
> possibly legacy, hardware it was physically impossible to do
> inter-thread stack accesses; stacks were strictly per-thread and
> private. I also vaguely recall that some languages (or thread packages)
> banned the practice even if it were physically possible.

Could be, but that's a pretty dumb approach which is why those systems
ended up as legacy. Well, it's not a dumb approach, but it's just not
threading, really.

One way to emulate that scenario is simply to use multiple processes
(e.g. with fork() on Unix), and use explicit memory sharing (mmap,
SysV) to have a global area which is common, and mapped to exactly the
same address in every process, too. Then you have private stacks.

Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Question about inter-thread stack references Ivan Godard <ivan@ootbcomp.com> - 2015-01-15 23:54 -0800
  Re: Question about inter-thread stack references Louis Krupp <lkrupp@nospam.pssw.com.invalid> - 2015-01-16 22:28 -0700
    Re: Question about inter-thread stack references glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-17 18:46 +0000
  Re: Question about inter-thread stack references George Neuner <gneuner2@comcast.net> - 2015-01-18 01:58 -0500
    Re: Question about inter-thread stack references glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-18 21:47 +0000
      Re: Question about inter-thread stack references glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-18 23:24 +0000
  Re: Question about inter-thread stack references Stefan Monnier <monnier@iro.umontreal.ca> - 2015-01-18 08:35 -0500
  Re: Question about inter-thread stack references Kaz Kylheku <kaz@kylheku.com> - 2015-01-18 17:12 +0000
  Re: Question about inter-thread stack references Ivan Godard <ivan@ootbcomp.com> - 2015-01-18 12:49 -0800
  Re: Question about inter-thread stack references jgk@panix.com (Joe keane) - 2015-01-25 09:27 +0000
    Re: Question about inter-thread stack references Kaz Kylheku <kaz@kylheku.com> - 2015-01-25 18:46 +0000
      Re: Question about inter-thread stack references jgk@panix.com (Joe keane) - 2015-01-27 02:53 +0000

csiph-web