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 367 - (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 :     val perform :
11 :     { openIt : unit -> 'a,
12 :     closeIt : 'a -> unit,
13 :     work : 'a -> 'b,
14 :     cleanup : unit -> unit } -> 'b
15 :     end
16 :    
17 :     structure SafeIO :> SAFEIO = struct
18 :    
19 :     structure S = Signals
20 :    
21 :     fun perform { openIt, closeIt, work, cleanup } = let
22 :     val oh = S.inqHandler S.sigINT
23 :     val intMask = S.MASK [S.sigINT]
24 :     val _ = S.maskSignals intMask
25 :     val s = openIt ()
26 :     handle e => (S.unmaskSignals intMask;
27 :     cleanup ();
28 :     raise e)
29 :     fun reset () = (closeIt s; ignore (S.setHandler (S.sigINT, oh)))
30 :     fun handler arg =
31 :     (reset ();
32 :     cleanup ();
33 :     case oh of
34 :     S.HANDLER h => h arg
35 :     | _ => OS.Process.exit OS.Process.failure)
36 :     in
37 :     (S.overrideHandler (S.sigINT, S.HANDLER handler);
38 :     S.unmaskSignals intMask;
39 :     (work s handle e => (reset (); cleanup (); raise e))
40 :     before reset ())
41 :     end
42 :     end

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