[MLton] Int.fmt thread unsafe? (YUCK)

Henry Cejtin henry.cejtin@sbcglobal.net
Mon, 13 Mar 2006 15:53:11 -0600


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.

I suppose in the Int*.fromString cases and friends, the loop  may  keep  that
from happening.

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  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