Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/util/safeio.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/util/safeio.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 459 - (view) (download)

1 : blume 345 (*
2 :     * Guarding IO against file descriptor leakage...
3 :     *
4 :     * Copyright (c) 1998 by Lucent Bell Laboratories
5 :     *
6 :     * author: Matthias Blume (blume@cs.princeton.edu)
7 :     *)
8 :     signature SAFEIO = sig
9 :    
10 : blume 459 (* the cleanup function is being told whether it is called because
11 :     * of an interrupt *)
12 : blume 345 val perform :
13 :     { openIt : unit -> 'a,
14 :     closeIt : 'a -> unit,
15 :     work : 'a -> 'b,
16 : blume 459 cleanup : bool -> unit } -> 'b
17 : blume 345 end
18 :    
19 :     structure SafeIO :> SAFEIO = struct
20 :    
21 :     structure S = Signals
22 :    
23 :     fun perform { openIt, closeIt, work, cleanup } = let
24 :     val oh = S.inqHandler S.sigINT
25 :     val intMask = S.MASK [S.sigINT]
26 :     val _ = S.maskSignals intMask
27 :     val s = openIt ()
28 :     handle e => (S.unmaskSignals intMask;
29 : blume 459 cleanup false;
30 : blume 345 raise e)
31 :     fun reset () = (closeIt s; ignore (S.setHandler (S.sigINT, oh)))
32 :     fun handler arg =
33 :     (reset ();
34 : blume 459 cleanup true;
35 : blume 345 case oh of
36 :     S.HANDLER h => h arg
37 :     | _ => OS.Process.exit OS.Process.failure)
38 :     in
39 :     (S.overrideHandler (S.sigINT, S.HANDLER handler);
40 :     S.unmaskSignals intMask;
41 : blume 459 (work s handle e => (reset (); cleanup false; raise e))
42 : blume 345 before reset ())
43 :     end
44 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0