Re: [MLton] globals + threads
Jeffrey Murphy <[email protected]> Fri, 11 Oct 2019 23:31:24 -0400
| Newsgroups | gmane.comp.lang.ml.mlton.devel |
|---|---|
| Message-ID | <[email protected]> |
--===============3965719577683323898== Content-Type: multipart/signed; boundary="Apple-Mail=_F82BE9DD-02F1-476A-AE05-19A007BBD957"; protocol="application/pgp-signature"; micalg=pgp-sha1 --Apple-Mail=_F82BE9DD-02F1-476A-AE05-19A007BBD957 Content-Type: multipart/alternative; boundary="Apple-Mail=_46E976BF-1095-4452-877D-5851112521A8" --Apple-Mail=_46E976BF-1095-4452-877D-5851112521A8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Once again, thanks for this very helpful explanation. Additional = questions: 1) with respect to "but only in the chunk that corresponds to the = initialization of globals=E2=80=9D where in the compiler can i find the = section that generates the chunk that initializes the globals? (i=E2=80=99= ve done some searching but came up empty) 2) why are some globals initialized in initVectors (in the branch of = mlton I am using) and others are initialized inside of a block? thanks again, jeff > On Oct 10, 2019, at 8:41 AM, Matthew Fluet <[email protected]> = wrote: >=20 > On Wed, Oct 9, 2019 at 11:10 PM Jeffrey Murphy <[email protected] = <mailto:[email protected]>> wrote: > Hi, >=20 > Context: an SML application that is 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 posix thread. We are = using the c-codegen. >=20 > 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 potential races I should be concerned about? >=20 >=20 > You should always be concerned about potential races in a = multi-threaded program. ;-) >=20 > 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 = <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 = <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. >=20 > 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. >=20 > The only time the globals are modified is as a consequence of garbage = collection, because the pointed-to object may have moved. >=20 > _______________________________________________ > MLton-devel mailing list > [email protected]; [email protected] > https://lists.sourceforge.net/lists/listinfo/mlton-devel --Apple-Mail=_46E976BF-1095-4452-877D-5851112521A8 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 <html><head><meta http-equiv=3D"Content-Type" content=3D"text/html; = charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;" = class=3D""><div><div dir=3D"auto" style=3D"word-wrap: break-word; = -webkit-nbsp-mode: space; line-break: after-white-space;" class=3D""><font= face=3D"Courier New" size=3D"4" class=3D"">Once again, thanks for this = very helpful explanation. Additional questions:</font><div = class=3D""><font face=3D"Courier New" size=3D"4" class=3D""><br = class=3D""></font></div><div class=3D""><font face=3D"Courier New" = size=3D"4" class=3D"">1) with respect to "but only in the chunk that = corresponds to the initialization of globals=E2=80=9D where in the = compiler can i find the section that generates the chunk that = initializes the globals? (i=E2=80=99ve done some searching but came up = empty)</font></div><div class=3D""><br class=3D""></div><div = class=3D""><font face=3D"Courier New" size=3D"4" class=3D"">2) why are = some globals initialized in initVectors (in the branch of mlton I am = using) and others are initialized inside of a block?</font></div><div = class=3D""><font face=3D"Courier New" size=3D"4" class=3D""><br = class=3D""></font></div><div class=3D""><font face=3D"Courier New" = size=3D"4" class=3D"">thanks again,</font></div><div class=3D""><font = face=3D"Courier New" size=3D"4" class=3D"">jeff</font></div><div = class=3D""><font face=3D"Courier New" size=3D"4" class=3D""><br = class=3D""></font></div><div class=3D""><div class=3D""><div><br = class=3D""><blockquote type=3D"cite" class=3D""><div class=3D"">On Oct = 10, 2019, at 8:41 AM, Matthew Fluet <<a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>> wrote:</div><br = class=3D"Apple-interchange-newline"><div class=3D""><div dir=3D"ltr" = class=3D""><div dir=3D"ltr" class=3D""><div class=3D"gmail_default" = style=3D"font-family:courier new,monospace;font-size:large"><span = style=3D"font-family:Arial,Helvetica,sans-serif;font-size:small" = class=3D"">On Wed, Oct 9, 2019 at 11:10 PM Jeffrey Murphy <<a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>> wrote:</span><br = class=3D""></div></div><div class=3D"gmail_quote"><blockquote = 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: = break-word;" class=3D"">Hi, <div class=3D""><br class=3D""></div><div= class=3D"">Context: an SML application that is 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 posix thread. = We are using the c-codegen.</div><div class=3D""><br class=3D""></div><div= class=3D"">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 potential races I should be concerned = about?</div></div></blockquote><div class=3D""><br class=3D""></div><div = class=3D"gmail_default" style=3D"font-family:"courier = new",monospace;font-size:large"><br class=3D""></div><div = class=3D"gmail_default" style=3D"font-family:"courier = new",monospace;font-size:large">You should 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 class=3D""></div><div = class=3D"gmail_default" style=3D"font-family:"courier = new",monospace;font-size:large">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 <a = href=3D"https://github.com/MLton/mlton/pull/328" = class=3D"">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 <a = href=3D"https://github.com/MLton/mlton/pull/328" = class=3D"">https://github.com/MLton/mlton/pull/328</a>, the global `int = ref` will simply be some static that is a pointer to a mutable integer = object.</div><div class=3D"gmail_default" = style=3D"font-family:"courier = new",monospace;font-size:large"><br class=3D""></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. 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.</div><div class=3D"gmail_default" = style=3D"font-family:"courier = new",monospace;font-size:large"><br class=3D""></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-family:"courier = new",monospace;font-size:large"><br class=3D""></div></div></div> _______________________________________________<br class=3D"">MLton-devel = mailing list<br class=3D""><a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a>; <a = href=3D"mailto:[email protected]" = class=3D"">[email protected]</a><br class=3D""><a = href=3D"https://lists.sourceforge.net/lists/listinfo/mlton-devel" = class=3D"">https://lists.sourceforge.net/lists/listinfo/mlton-devel</a><br= class=3D""></div></blockquote></div><br = class=3D""></div></div></div></div></body></html>= --Apple-Mail=_46E976BF-1095-4452-877D-5851112521A8-- --Apple-Mail=_F82BE9DD-02F1-476A-AE05-19A007BBD957 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP -----BEGIN PGP SIGNATURE----- Comment: GPGTools - http://gpgtools.org iF0EARECAB0WIQQHIhekR6suZqSxyU6vHJ3LwqTBwQUCXaFJDAAKCRCvHJ3LwqTB wXRQAKCu6Oxf78o5sDY0EmKPxiKCQmW+3ACgh4/rZ2bQGvo/gXz7QSFP/4pc5Xc= =5YJd -----END PGP SIGNATURE----- --Apple-Mail=_F82BE9DD-02F1-476A-AE05-19A007BBD957-- --===============3965719577683323898== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline --===============3965719577683323898== 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 --===============3965719577683323898==--