From mboxrd@z Thu Jan 1 00:00:00 1970 Message-Id: <6.1.1.1.0.20040604175442.021d8420@pop.free.fr> Date: Fri, 4 Jun 2004 18:22:32 +0200 To: Fans of the OS Plan 9 from Bell Labs <9fans@cse.psu.edu> From: Philippe Anel Subject: Re: [9fans] stack reclamation on Unix using clone, rfork_thread, etc. In-Reply-To: <200406041533.i54FXNm29967@plg2.math.uwaterloo.ca> References: <200406041533.i54FXNm29967@plg2.math.uwaterloo.ca> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Topicbox-Message-UUID: 951a0890-eacd-11e9-9e20-41e7f4b1d025 >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. Ok. What do you think then about the following pseudo-code: ------------------------------------------------------------------------------ struct segment { struct segment * next; struct segment * prev; void * addr; int len; int threadid; }; struct addr_space { Lock; int refcount; struct segment * inuse; struct segment * tofree; } addrspace; 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. Philippe,