(* interrupts.sml *) (* Copyright (C) 2006 Alley Stoughton This file is part of crypto, a cryptogram encoder/decoder. See the file COPYING.txt for copying and usage restrictions *) structure Interrupts :> INTERRUPTS = struct structure Cont = SMLofNJ.Cont (* first-class continuations *) structure Sigs = Signals (* signals *) (* record of interrupt signaling *) val checkRef = ref false fun ignore f = let val act = Sigs.setHandler(Sigs.sigINT, Sigs.IGNORE) val y = f() handle ex => (Sigs.setHandler(Sigs.sigINT, act); raise ex) in Sigs.setHandler(Sigs.sigINT, act); y end fun track f = let (* val handler : Sigs.signal * int * unit Cont.cont -> unit Cont.cont *) fun handler(_, _, k) = (checkRef := true; k) val _ = Sigs.setHandler(Sigs.sigINT, Sigs.HANDLER handler) val y = f() handle ex => (Sigs.setHandler(Sigs.sigINT, Sigs.IGNORE); checkRef := false; raise ex) in Sigs.setHandler(Sigs.sigINT, Sigs.IGNORE); checkRef := false; y end fun protect f = let val _ = Sigs.maskSignals(Sigs.MASK[Sigs.sigINT]) val y = f() handle ex => (Sigs.unmaskSignals(Sigs.MASK[Sigs.sigINT]); raise ex) in Sigs.unmaskSignals(Sigs.MASK[Sigs.sigINT]); y end fun check() = protect(fn () => let val status = !checkRef val _ = checkRef := false in status end) end;