1999-12-16 04:25:11 -08:00
|
|
|
(*************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml LablTk library *)
|
|
|
|
(* *)
|
|
|
|
(* Francois Rouaix, Francois Pessaux and Jun Furuse *)
|
|
|
|
(* projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* Jacques Garrigue, Kyoto University RIMS *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1999 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique and Kyoto University. All rights reserved. *)
|
|
|
|
(* This file is distributed under the terms of the GNU Library *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* General Public License, with the special exception on linking *)
|
|
|
|
(* described in file ../../../LICENSE. *)
|
1999-12-16 04:25:11 -08:00
|
|
|
(* *)
|
|
|
|
(*************************************************************************)
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Timers *)
|
2001-09-06 01:52:32 -07:00
|
|
|
open Support
|
1999-11-30 06:59:39 -08:00
|
|
|
open Protocol
|
|
|
|
|
|
|
|
type tkTimer = int
|
|
|
|
|
|
|
|
external internal_add_timer : int -> cbid -> tkTimer
|
|
|
|
= "camltk_add_timer"
|
|
|
|
external internal_rem_timer : tkTimer -> unit
|
|
|
|
= "camltk_rem_timer"
|
|
|
|
|
|
|
|
type t = tkTimer * cbid (* the token and the cb id *)
|
|
|
|
|
|
|
|
(* A timer is used only once, so we must clean the callback table *)
|
2000-04-11 20:43:25 -07:00
|
|
|
let add ~ms ~callback =
|
1999-11-30 06:59:39 -08:00
|
|
|
let id = new_function_id () in
|
|
|
|
let wrapped _ =
|
|
|
|
clear_callback id; (* do it first in case f raises exception *)
|
2000-04-11 20:43:25 -07:00
|
|
|
callback() in
|
2001-09-06 01:52:32 -07:00
|
|
|
Hashtbl'.add callback_naming_table ~key:id ~data:wrapped;
|
1999-11-30 06:59:39 -08:00
|
|
|
if !Protocol.debug then begin
|
|
|
|
prerr_cbid id; prerr_endline " for timer"
|
|
|
|
end;
|
2000-04-11 20:43:25 -07:00
|
|
|
let t = internal_add_timer ms id in
|
1999-11-30 06:59:39 -08:00
|
|
|
t,id
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let set ~ms ~callback = ignore (add ~ms ~callback);;
|
2000-02-15 02:12:37 -08:00
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* If the timer has never been used, there is a small space leak in
|
|
|
|
the C heap, where a copy of id has been stored *)
|
|
|
|
let remove (tkTimer, id) =
|
|
|
|
internal_rem_timer tkTimer;
|
|
|
|
clear_callback id
|
|
|
|
|