9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] stack reclamation on Unix using clone, rfork_thread, etc.
@ 2004-06-04 15:33 Richard C Bilson
  2004-06-04 15:53 ` Russ Cox
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Richard C Bilson @ 2004-06-04 15:33 UTC (permalink / raw)
  To: 9fans

> From 9fans-bounces+rcbilson=plg.uwaterloo.ca@cse.psu.edu Fri Jun  4 11:19:57 2004
> 
> I would associate each stack segment to an 'address space' structure
> shared by each shared-memory process.
> When the reference counter of this structure goes to zero, I would
> free all the segment 'associated' to this structure (and free the structure 
> :) ).

Yes, but what thread does the free?  You can't free your own stack, and
how does any other thread know to do it for you?  This is a pain, and a
pain that I have felt myself.

Of course, it's easy if there is some kind of termination
synchronization, and the thread that waits is responsible for cleaning
up after the thread that finishes.  But I assume that Russ is trying to
solve the problem in the general case.

As far as I know, the new pthreads library on Linux uses a technique
very similar to the one that Russ describes, with the additional
optimization of re-using stacks if possible.  In fact, I'm not sure if
they ever free stacks -- just keep them around in case a new thread
needs one.

The old linuxthreads library used a "manager thread" to do the reaping
(among other things), which isn't particularly nice or efficient.


^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: [9fans] stack reclamation on Unix using clone, rfork_thread, etc.
@ 2004-06-04 16:44 Richard C Bilson
  0 siblings, 0 replies; 15+ messages in thread
From: Richard C Bilson @ 2004-06-04 16:44 UTC (permalink / raw)
  To: 9fans

> From: Philippe Anel <xigh@free.fr> Fri Jun  4 12:23:49 2004
> 
> void
> exit_thread()
>   {
>          struct segment * s;
> 
>          lock(&addrspace);
>          for (s = addrspace.tofree; s; s = s->next)
>                  free(s->addr);
>          if (--refcount) {
>                  s = find_segment_from_id(get_my_thread_id());
>                  list_remove(s);
>                  list_add(tofree, s);
>          }
>          unlock(&addrspace);
> }
> 
> ------------------------------------------------------------------------------
> 
> It would free the stack of the previous exited thread.

This isn't actually so different from what Russ describes -- you're just
keeping a separate free list, rather than setting a per-thread flag in
some global list of all threads.

Note that you have to be very careful about race conditions -- your
stack could disappear immediately after the call to unlock, which means
that you'd better be done all you need to do at that point, except for
calling _exit (and even that is problematic).


^ permalink raw reply	[flat|nested] 15+ messages in thread
* [9fans] stack reclamation on Unix using clone, rfork_thread, etc.
@ 2004-06-04 14:39 Russ Cox
  2004-06-04 15:20 ` Philippe Anel
  2004-06-04 18:30 ` boyd, rounin
  0 siblings, 2 replies; 15+ messages in thread
From: Russ Cox @ 2004-06-04 14:39 UTC (permalink / raw)
  To: 9fans

Anyone have any idea how one is supposed to arrange
to free the stack on which a shared-memory proc is 
running?

In my current implementation, I have a routine called
stackalloc() that keeps a list of all stacks ever allocated
along with the pids they were allocated for, and every Nth
time it gets called, it scans the list freeing the stacks that
correspond to processes that no longer exist.

Damn TLB-friendly operating systems.

Russ


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2004-06-04 19:02 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-04 15:33 [9fans] stack reclamation on Unix using clone, rfork_thread, etc Richard C Bilson
2004-06-04 15:53 ` Russ Cox
2004-06-04 18:43   ` boyd, rounin
2004-06-04 16:22 ` Philippe Anel
2004-06-04 16:24   ` Philippe Anel
2004-06-04 16:38   ` Russ Cox
2004-06-04 16:43     ` Charles Forsyth
2004-06-04 19:02   ` boyd, rounin
2004-06-04 18:35 ` boyd, rounin
  -- strict thread matches above, loose matches on Subject: below --
2004-06-04 16:44 Richard C Bilson
2004-06-04 14:39 Russ Cox
2004-06-04 15:20 ` Philippe Anel
2004-06-04 15:23   ` Philippe Anel
2004-06-04 18:30 ` boyd, rounin
2004-06-04 18:38   ` Russ Cox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).