<html><head><base href="x-msg://97/"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">What are the expectations in the cvsup child regarding the threads it inherits?<div><div><br class="Apple-interchange-newline">
</div>
<br><div><div>On 17 Mar 2010, at 18:05, Jay K wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div class="hmmessage" style="font-size: 10pt; font-family: Verdana; ">Tony, I don't know.<br>Here is some "argument', but I'm not sure.<br> <br> <br>Adding threads does something different. Such threads would share mutation to global state.<br>I'm not a big fan of this model, but fork lets you establish some perhaps expensive to establish state, then share it cheaply among a bunch of future threads/processes, that may make their own local modifications to it. One would have to read the cvsup code a bunch to determine what it actually does and requires.<br> <br>I do suspect there is a general solution. Leaving anyone who uses platform specific functions to fend for themselves seems a bit unfair. Which functions to we abtract away in m3ore vs. which do we leave<br>people to use on their own? And does that list change much in time? Well, infinity isn't possible either, granted. And we've only seen one program so far that cares, we shouldn't spend too much just for one program.<br> <br> <br>There may be a smaller related fix, where m3core internally uses atfork, but doesn't expose ForkAll to the client. I know cvsup has the dispatcher thread that it expects to be inherited by children, however all it does with it is queue a request to it to shut itself down. In that way, ForkAll is a waste -- it recreates a thread, only so the client can shut it down. I can pursue that more.<br> <br> <br> - Jay<br><br> <br><hr id="stopSpelling">From:<span class="Apple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>Date: Wed, 17 Mar 2010 14:30:47 -0400<br>To:<span class="Apple-converted-space"> </span><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a><br>CC:<span class="Apple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>Subject: Re: [M3devel] fork/cvsup<br><br>I don't think there is a "general" solution to this that should be applied to the thread library.  Modula-3 does not mandate any support for fork!  It is not part of the language.  If a program relies on platform-specific interfaces then it must be the one to handle situations arising from the problem.  Why does cvsup need to fork in the first place?  Surely it can simply add threads to handle clients as they arrive?<div><br><div><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><div style="word-wrap: break-word; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><div><font class="ecxApple-style-span" color="#0000ff"><font class="ecxApple-style-span" face="Gill Sans"><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; color: rgb(0, 0, 255); "><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; color: rgb(0, 0, 255); ">Antony Hosking</span></span></font></font><font class="ecxApple-style-span" face="Gill Sans"><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; "><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; "><span class="ecxApple-converted-space"> </span>|<span class="ecxApple-converted-space"> </span></span></span><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; "><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; ">Associate Professor</span></span><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; "><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; "> | Computer Science | Purdue University</span></span></font></div><div><font class="ecxApple-style-span" face="GillSans-Light"><span class="ecxApple-style-span" style="font-family: GillSans-Light; ">305 N. University Street | West Lafayette | IN 47907 | USA</span></font></div><div><font class="ecxApple-style-span" color="#0000ff" face="Gill Sans"><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; color: rgb(0, 0, 255); "><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; color: rgb(0, 0, 255); ">Office</span></span></font><font class="ecxApple-style-span" face="GillSans-Light"><span class="ecxApple-style-span" style="font-family: GillSans-Light; "><span class="ecxApple-style-span" style="font-family: GillSans-Light; "> +1 765 494 6001 |<span class="ecxApple-converted-space"> </span></span></span></font><font class="ecxApple-style-span" color="#0000ff" face="Gill Sans"><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; color: rgb(0, 0, 255); "><span class="ecxApple-style-span" style="font-family: 'Gill Sans'; color: rgb(0, 0, 255); ">Mobile</span></span></font><font class="ecxApple-style-span" face="GillSans-Light"><span class="ecxApple-style-span" style="font-family: GillSans-Light; "><span class="ecxApple-style-span" style="font-family: GillSans-Light; "><span class="ecxApple-converted-space"> </span>+1 765 427 5484</span></span></font></div><div><font class="ecxApple-style-span" face="GillSans-Light"><br class="ecxkhtml-block-placeholder"></font></div></span></span></span></span></span></span></span><br class="ecxApple-interchange-newline"></span></div></span></span><br class="ecxApple-interchange-newline"></div><br><div><div>On 17 Mar 2010, at 14:13, Jay K wrote:</div><br class="ecxApple-interchange-newline"><blockquote><span class="ecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal medium/normal Helvetica; white-space: normal; letter-spacing: normal; word-spacing: 0px; "><div class="ecxhmmessage" style="font-family: Verdana; font-size: 10pt; ">---<br>bad news:<br>It doesn't completely work. It works a bunch of times in a row, like 9, then hangs.<br>Restart manually. Works again. Around 9 times. Then hangs again.<br>That is on Linux/x86 and Solaris/sparc.<br>Doesn't work at all on Mac/amd64, just hangs.<br> <br>---<br>sketch:<br>m3core uses pthread_atfork to selectively reinitialize<br>  Mainly to only have one thread.<br> <br> <br>common Thread.PThreadAtFork is provided for others to do the same<br>  It is deliberately in a portable interface.<br> <br> <br>Thread.ReforkThreadAfterProcessFork<br>  Is provided for users to restart threads from their child AtFork hander.<br>  This is used by the allocator/collector.<br><br> <br>Thread.ForkProcessAndAllThreads()<br>  Is used by "lazy" clients who want to restart all their threads<br>  but didn't keep track of them. The runtime can do it for them.<br> <br> <br>This allows for "fork + do work" folks do call or not call ForkProcessAndAllThreads<br>or not, depending on if they need their threads restarted.<br>The runtime takes care of its threads either way.<br> <br> <br>---<br>What'd I'd written up:<br> <br>attached works typically 9 times on Linux and Solaris<br>before server hangs again.<br><br> <br>No improvement on Darwin, just hangs.<br>Can't see much in debuggers for some reason.<br> <br><br>There is extra allowance in the m3core change such<br> that users of fork + do work (as opposed to fork + exec)<br> may or may not call ForkAll, depending on if they<br> feel a need for their own threads to be recreated,<br> and if they've kept track of how to recreate them,<br> or just rely on the runtime to know all the threads.<br> <br><br>There are three runtime threads that are sometimes<br>created in the parent, and if so, recreated in the child.<br>background collector, foreground collector, weak ref thread<br> <br> <br>I'll try to poke at it some more.<br> <br><br>I'm not sure what is the best way to suspend all threads.<br>I tried a few differnt ways.<br>  SuspendOthers<br>  LockHeap<br>  pthread_mutex_lock<br>  various combinations<br><br> <br>It is deliberate that pthread specific code is in common/Thread.i3.<br>That way code can be portable, at least among the two Posix thread implementations.<br> <br><br> - Jay<br><br><br> <br><hr id="ecxstopSpelling">From:<span class="ecxApple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>Date: Wed, 17 Mar 2010 14:01:31 -0400<br>To:<span class="ecxApple-converted-space"> </span><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a><br>CC:<span class="ecxApple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>Subject: Re: [M3devel] fork/cvsup<br><br>Can you sketch the approach you've taken?<div><br><br><div><div>On 17 Mar 2010, at 11:39, Jay K wrote:</div><br class="ecxecxApple-interchange-newline"><blockquote><span class="ecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal medium/normal Helvetica; white-space: normal; letter-spacing: normal; word-spacing: 0px; "><div class="ecxecxhmmessage" style="font-family: Verdana; font-size: 10pt; ">I have something working on Solaris now.<br>More details after testing on Linux and Darwin.<br> <br> - Jay<br> <br><hr id="ecxecxstopSpelling">From:<span class="ecxecxApple-converted-space"> </span><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a><br>To:<span class="ecxecxApple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>Date: Wed, 17 Mar 2010 14:01:15 +0000<br>CC:<span class="ecxecxApple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>Subject: Re: [M3devel] fork/cvsup<br><br>Exec what?<br>You'd have to change the code to carefully reach the same place.<br> <br> - Jay<br> <br><hr id="ecxecxecxstopSpelling">Subject: Re: [M3devel] fork/cvsup<br>From:<span class="ecxecxApple-converted-space"> </span><a href="mailto:hosking@cs.purdue.edu">hosking@cs.purdue.edu</a><br>Date: Wed, 17 Mar 2010 09:28:14 -0400<br>CC:<span class="ecxecxApple-converted-space"> </span><a href="mailto:m3devel@elegosoft.com">m3devel@elegosoft.com</a><br>To:<span class="ecxecxApple-converted-space"> </span><a href="mailto:jay.krell@cornell.edu">jay.krell@cornell.edu</a><br><br><div><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><div style="word-wrap: break-word; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal 12px/normal Helvetica; white-space: normal; letter-spacing: normal; color: rgb(0, 0, 0); word-spacing: 0px; "><div><span class="ecxecxecxecxApple-style-span" style="font-size: medium; "><font class="ecxecxecxecxApple-style-span" color="#0000ff" face="'Gill Sans'">Why not just exec in the child?</font></span></div></span></span></span></span></span></span></span></span></div></span></span></div><br><div><div>On 17 Mar 2010, at 03:47, Jay K wrote:</div><br class="ecxecxecxecxApple-interchange-newline"><blockquote><span class="ecxecxecxecxApple-style-span" style="text-transform: none; text-indent: 0px; border-collapse: separate; font: normal normal normal medium/normal Helvetica; white-space: normal; letter-spacing: normal; word-spacing: 0px; "><div class="ecxecxecxecxhmmessage" style="font-family: Verdana; font-size: 10pt; "><a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man2/fork.2.html">http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man2/fork.2.html</a><br> <br><font face=""></font> <br><font face="">There are limits to what you can do in the child process.  To be totally safe you should restrict your-<font class="ecxecxecxecxwhiteout" color="#ffffff" size="1">self yourself</font><br>     self to only executing async-signal safe operations until such time as one of the exec functions is<br>     called.  All APIs, including global data symbols, in any framework or library should be assumed to be<br>     unsafe after a<span class="ecxecxecxecxApple-converted-space"> </span><b>fork</b>() unless explicitly documented to be safe or async-signal safe.  If you need to use<br>     these frameworks in the child process, you must exec.  In this situation it is reasonable to exec your-<font class="ecxecxecxecxwhiteout" color="#ffffff" size="1">self. yourself.</font><br>     self.<br><br></font><font face=""></font> <br><font face=""><a href="http://www.opengroup.org/onlinepubs/000095399/functions/fork.html">http://www.opengroup.org/onlinepubs/000095399/functions/fork.html</a></font><br><font face=""></font> <br><font face="">Consequently, to avoid errors, the child process may only execute async-signal-safe operations until such time as one of the<i><a href="http://www.opengroup.org/onlinepubs/000095399/functions/exec.html">exec</a></i><span class="ecxecxecxecxApple-converted-space"> </span>functions is called.<span class="ecxecxecxecxApple-converted-space"> </span><sup>[<a>THR</a>]</sup><span class="ecxecxecxecxApple-converted-space"> </span><img border="0" alt="[Option Start]" src="http://www.opengroup.org/onlinepubs/000095399/images/opt-start.gif"><span class="ecxecxecxecxApple-converted-space"> </span> Fork handlers may be established by means of the<span class="ecxecxecxecxApple-converted-space"> </span><a href="http://www.opengroup.org/onlinepubs/000095399/functions/pthread_atfork.html"><i>pthread_atfork</i>()</a><span class="ecxecxecxecxApple-converted-space"> </span>function in order to maintain application invariants across<span class="ecxecxecxecxApple-converted-space"> </span><i>fork</i>() calls.<span class="ecxecxecxecxApple-converted-space"> </span><img border="0" alt="[Option End]" src="http://www.opengroup.org/onlinepubs/000095399/images/opt-end.gif"><br> <br> <br>I've run through a few theories so far.<br>Current thinking is related to what Tony said:<br> use pthread_atfork:<span class="ecxecxecxecxApple-converted-space"> </span><br>   in prepare, stopworld<span class="ecxecxecxecxApple-converted-space"> </span><br>   in parent, resumeworld<span class="ecxecxecxecxApple-converted-space"> </span><br>   You don't want the child to be mid-gc for example, on another thread. Or mid-anything.<br>   in child, reinitialize -- current thread is the only thread<br> <br> <br>Also in the cvsup code, ShutDown should just call DoShutDown immediately.<br>I did that, without m3core changes, and it hits an error in the pthread code, signaling a nonexistant thread.<br>pthread_atfork/child should address that -- child shouldn't retain a record of all the threads in the parent.<br> <br> <br>I don't have a theory as to why user threads work.<br> <br> <br>I experimented with malloc vs. static alloc vs. sbrk vs. mmap(private) vs. mmap(shared).<br>I was expecting more cases to act like mmap(shared), but none did, only it.<br> <br> <br>I experimented with having mutexes and condition variables be initialize up front instead of on-demand.<br>Via changing cvsup to lock/unlock or broadcast immediately upon creating them.<br>On the theory that might let them work across process.<br>That didn't make a difference.<br> <br> <br> - Jay<br></font></div></span></blockquote></div><br></div></span><br class="ecxecxApple-interchange-newline"></blockquote></div><br></div><span><m3core_atfork.txt></span><span><cvsup_forkall.txt></span></div></span></blockquote></div><br></div></div></span><br class="Apple-interchange-newline"></blockquote></div><br></div></body></html>