* [Caml-list] feature wish: warmup in compiled code (symetrical of at_exit)
@ 2003-02-13 23:03 Basile STARYNKEVITCH
0 siblings, 0 replies; only message in thread
From: Basile STARYNKEVITCH @ 2003-02-13 23:03 UTC (permalink / raw)
To: caml-list
Dear All
I have a simple wish for next Ocaml, and partly implemented it.
It would be to provide a function [at_warm_up] in pervasives which is given a
warming function and register it to be executed after all modules have been
loaded; and a function [do_warm_up] which when explicitly called
execute all the warming functions (not yet executed). And do_warm_up
should be implicitly called by the system after loading of all user
supplied (and library linked) modules.
For example with Caml files
################==> a.mli <==
(* file a.mli *)
val afun : unit -> unit
(* eof a.mli *)
################==> b.mli <==
(* file b.mli *)
val bfun: unit -> unit
(* eof b.mli *)
################==> a.ml <==
(* file a.ml *)
print_endline "in a.ml";;
let afun () = print_endline "in a.ml afun";;
at_warm_up ~descr: "inside a" (fun () -> print_endline "a.ml warmup");;
(* eof a.ml *)
################==> b.ml <==
(* file b.ml *)
print_endline "in b.ml before do_warm_up";
do_warm_up ~prelude: (fun n -> Printf.printf "prelude of %S\n" n) ();;
let bfun () = print_endline "in b.ml bfun";;
at_warm_up ~descr: "inside b warmup" (fun () -> print_endline "b.ml warmup");;
print_endline "at end of b.ml";;
(* eof b.ml *)
################==> c.ml <==
(* file c.ml *)
print_endline "in c.ml";;
A.afun ();;
B.bfun ();;
print_endline "end c.ml";;
(* eof c.ml *)
################################################################
Then I compile with (using my patched version of Ocaml)
ocamlc=/usr/local/bin/ocamlc
$ocamlc -c a.mli
$ocamlc -c b.mli
$ocamlc -c a.ml
$ocamlc -c b.ml
$ocamlc -c c.ml
$ocamlc -o _tryabc a.cmo b.cmo c.cmo
################################################################
Then I would like that
./_tryabc
produces
in a.ml
in b.ml before do_warm_up
prelude of "inside a"
a.ml warmup
at end of b.ml
in c.ml
in a.ml afun
in b.ml bfun
end c.ml
b.ml warmup
in pervasives.mli I added
################################################################
(** {6 Warmup support} *)
val at_warm_up : ?descr: string -> (unit -> unit) -> unit
(** [at_warm_up descr f] registers the given function to be called at
program warm up, when all object files has been loaded and their
code executed; The optional [descr] is a descriptive string *)
val do_warm_up : ?prelude: (string -> unit) -> ?exhandler: (exn -> unit) -> unit -> unit
(** [do_warm_up ~prelude: p ~exhandler: exh ()] where both [prelude]
and [exhandler] are optional execute all the warming functions
registered with [at_warm_up] in ``first in, first out'' order
and clear the list of warming functions; so user code can call do_warm_up
several times; *)
################################################################
In pervasives.ml I added at end
################################################################
let warmup_list = ref []
let at_warm_up ?(descr="") f =
warmup_list := (descr, f) :: !warmup_list;;
let do_warm_up ?prelude ?(exhandler= raise) () =
let rec doall =
function
(descr, f) :: rest ->
begin
try
begin
match prelude with None -> () | Some p -> p descr
end;
f ();
doall rest
with ex -> begin exhandler ex; doall rest end
end
| [] -> ()
in
let wl = !warmup_list in
let rec revloop l r = (* a list reversal *)
match l with [] -> r | h::t -> revloop t (h::r)
in
warmup_list := [];
doall (revloop wl []);;
################################################################
The same above code should also go into the
extralib/threads/pervasives.ml also
The only thing that is missing is to incorporate into driver/main.ml
(and optmain.ml) the stuff which links after all user supplied (and
library linked) modules - so the compiler generates the equivalent of
the compiled form of a file _end.ml containing only
do_warm_up ();;
and adds the _end.cmo after all user & library stuff.
Actually this _end.ml could contain something like
let curd = ref "" in
Pervasives.do_warm_up
~prelude: (fun d -> curd := d)
~exhandler:
(fun ex -> eprintf "Uncaught exception: %s in warmup %S\n"
(to_string ex)
!curd;
flush stderr)
()
Actually, when coding multi-threaded stuff I find that such a feature
(which I feel symetrical to at_exit) very useful; in particular, this
enables to start the threads after having done any useful
initialisation.
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net
alias: basile<at>tunes<dot>org
8, rue de la Faïencerie, 92340 Bourg La Reine, France
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2003-02-13 23:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-13 23:03 [Caml-list] feature wish: warmup in compiled code (symetrical of at_exit) Basile STARYNKEVITCH
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox