new register allocator and calling convention

Stephen Weeks sweeks@wasabi.epr.com
Fri, 3 Dec 1999 13:30:07 -0800 (PST)


> What is the advantage of Thread.copyCurrent stashing it some where instead of
> just returning the copy?

Because it's hard to return values from calls to the runtime system.
It's just easier to have all runtime system functions be of type unit
-> unit.

> Note, all of this would really be busted if you
> had preemptive scheduling (since the one magic place could be overwritten
> by another thread doing a call/cc before the other call/cc guy had a chance to
> save it away.

You're right in principle, but because thread switches only can
happen at limit check points and there is no limit check point
between the copyCurrent and the saved, I believe nothing bad can
happen.

fun 'a callcc(f: 'a cont -> 'a): 'a =
   let
      val r: 'a option ref = ref NONE
      val () = Thread.copyCurrent()
   in case !r of
      NONE => let val t = Thread.saved()
	      in f(fn v => (r := SOME v
			    ; Thread.switchTo(Thread.copy t)))
	      end
    | SOME v => (r := NONE (* for space safety *)
		 ; Thread.clearSaved()
		 ; v)
   end