Re: [MLton] globals + threads
Matthew Fluet <[email protected]> Thu, 10 Oct 2019 08:41:32 -0400
| Newsgroups | gmane.comp.lang.ml.mlton.devel |
|---|---|
| Message-ID | <CAMrhFL5h7Jg5bCVim8jaNq-yW0ZHEhY6aY10GMY03OwAjbZbbg@mail.gmail.com> |
--===============8346957081002014880== Content-Type: multipart/alternative; boundary="0000000000004b832705948db92e" --0000000000004b832705948db92e Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Oct 9, 2019 at 11:10 PM Jeffrey Murphy <[email protected]> wrote= : > Hi, > > Context: an SML application that is executing in multiple posix threads. > Similar to how multimlton copythread=E2=80=99s the current thread to a ne= w one and > then executes it in a new posix thread. We are using the c-codegen. > > When thinking about how to handle globals, I noticed that multimlton uses > the same gcstate->globals pointer across posix threads. Is this safe (I= =E2=80=99m > assuming it is)? When looking at the C-codegen output, I noticed that the > G() macro will be on the LHS and so I=E2=80=99m wondering if there are po= tential > races I should be concerned about? > You should always be concerned about potential races in a multi-threaded program. ;-) The globals are values that are in scope for the whole program. Lots of top-level "val x =3D ..." can become globals, although for space-safety, it is limited to values of fixed size. Most object that are made into globals are immutable, so races won't be an issue with them. It is possible for an `int ref` to be made into a global (because the `int` contents are of fixed size, it won't violate space-safety). Prior to https://github.com/MLton/mlton/pull/328, the global `int ref` will simply be some GP(7) that is a pointer to the heap-allocated mutable integer object; after https://github.com/MLton/mlton/pull/328, the global `int ref` will simply be some static that is a pointer to a mutable integer object. In the generated C code, there will be some uses of GP(N) as a LHS, but only in the chunk that corresponds to the initialization of globals. After the GP(N) is initialized, it won't be used as a LHS any more. And, this initialization happens before any other ML thread is launched, so there shouldn't be a problem with multiple ML threads accessing globals. There may well be races after those ML threads have read through a global pointer to a mutable heap object and then try to read/write the heap object, but that's the same issue one would have with non-globalized mutable objects. The only time the globals are modified is as a consequence of garbage collection, because the pointed-to object may have moved. --0000000000004b832705948db92e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div dir=3D"ltr"><div class=3D"gmail_default" style=3D"fon= t-family:courier new,monospace;font-size:large"><span style=3D"font-family:= Arial,Helvetica,sans-serif;font-size:small">On Wed, Oct 9, 2019 at 11:10 PM= Jeffrey Murphy <<a href=3D"mailto:[email protected]">jcmurphy@buffal= o.edu</a>> wrote:</span><br></div></div><div class=3D"gmail_quote"><bloc= kquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:= 1px solid rgb(204,204,204);padding-left:1ex"><div style=3D"overflow-wrap: b= reak-word;">Hi,=C2=A0<div><br></div><div>Context: an SML application that i= s executing in multiple posix threads. Similar to how multimlton copythread= =E2=80=99s the current thread to a new one and then executes it in a new po= six thread. We are using the c-codegen.</div><div><br></div><div>When think= ing about how to handle globals, I noticed that multimlton uses the same gc= state->globals pointer across posix threads. Is this safe (I=E2=80=99m a= ssuming it is)? When looking at the C-codegen output, I noticed that the G(= ) macro will be on the LHS and so I=E2=80=99m wondering if there are potent= ial races I should be concerned about?</div></div></blockquote><div><br></d= iv><div class=3D"gmail_default" style=3D"font-family:"courier new"= ;,monospace;font-size:large"><br></div><div class=3D"gmail_default" style= =3D"font-family:"courier new",monospace;font-size:large">You shou= ld always be concerned about potential races in a multi-threaded program. ;= -)</div><div class=3D"gmail_default" style=3D"font-family:"courier new= ",monospace;font-size:large"><br></div><div class=3D"gmail_default" st= yle=3D"font-family:"courier new",monospace;font-size:large">The g= lobals are values that are in scope for the whole program.=C2=A0 Lots of to= p-level "val x =3D ..." can become globals, although for space-sa= fety, it is limited to values of fixed size.=C2=A0 Most object that are mad= e into globals are immutable, so races won't be an issue with them.=C2= =A0 It is possible for an `int ref` to be made into a global (because the `= int` contents are of fixed size, it won't violate space-safety).=C2=A0 = Prior to <a href=3D"https://github.com/MLton/mlton/pull/328">https://github= .com/MLton/mlton/pull/328</a>, the global `int ref` will simply be some GP(= 7) that is a pointer to the heap-allocated mutable integer object; after=C2= =A0<a href=3D"https://github.com/MLton/mlton/pull/328">https://github.com/M= Lton/mlton/pull/328</a>, the global `int ref` will simply be some static th= at is a pointer to a mutable integer object.</div><div class=3D"gmail_defau= lt" style=3D"font-family:"courier new",monospace;font-size:large"= ><br></div><div class=3D"gmail_default" style=3D"font-family:"courier = new",monospace;font-size:large">In the generated C code, there will be= some uses of GP(N) as a LHS, but only in the chunk that corresponds to the= initialization of globals.=C2=A0 After the GP(N) is initialized, it won= 9;t be used as a LHS any more.=C2=A0 And, this initialization happens befor= e any other ML thread is launched, so there shouldn't be a problem with= multiple ML threads accessing globals.=C2=A0 There may well be races after= those ML threads have read through a global pointer to a mutable heap obje= ct and then try to read/write the heap object, but that's the same issu= e one would have with non-globalized mutable objects.</div><div class=3D"gm= ail_default" style=3D"font-family:"courier new",monospace;font-si= ze:large"><br></div><div class=3D"gmail_default" style=3D"font-family:"= ;courier new",monospace;font-size:large">The only time the globals are= modified is as a consequence of garbage collection, because the pointed-to= object may have moved.</div><div class=3D"gmail_default" style=3D"font-fam= ily:"courier new",monospace;font-size:large"><br></div></div></di= v> --0000000000004b832705948db92e-- --===============8346957081002014880== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============8346957081002014880== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ MLton-devel mailing list [email protected]; [email protected] https://lists.sourceforge.net/lists/listinfo/mlton-devel --===============8346957081002014880==--