[MLton] Int.fmt thread unsafe? (YUCK)
Stephen Weeks
MLton@mlton.org
Mon, 13 Mar 2006 14:24:36 -0800
> Given that in MLton a thread context change can only happen at safe
> points, I would think (hope?) that in the case that the more
> procedure is inlined and can't have a safe point in it, the
> atomicBegin and atomicEnd get optimized away.
Nope. We don't do optimization of atomic{Begin,End} any more. It
could be done, but it would take some work to avoid the problems we
encountered several years ago.
> I don't see the advantage of your code over either the original or
> mine. It duplicates the call to f (which is bad) and still tests b
> twice before f is called. (It does save the wind call, but only in
> the uncommon case.)
I agree. I meant to say that the original code that I sent was best,
not the intermediate.
> I would think that a better choice would be to avoid the double test of b on
> the way in, but keep a single call to f.
>
> fun use (T {more, static, staticIsInUse}, f) =
> let
> val () = Primitive.Thread.atomicBegin ()
> val b = ! staticIsInUse
> val d = if b
> then (Primitive.Thread.atomicEnd ()
> ; more ())
> else (staticIsInUse := true
> ; Primitive.Thread.atomicEnd ()
> ; static)
> in DynamicWind.wind (fn () => f d,
> fn () => if b then () else staticIsInUse := false)
> end
Yes, that looks like the best version to me.