From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA26999; Sun, 2 May 2004 15:36:26 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA27004 for ; Sun, 2 May 2004 15:36:25 +0200 (MET DST) Received: from rabelais.socialtools.net (rabelais.socialtools.net [81.2.94.243]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i42DaNEV007088 for ; Sun, 2 May 2004 15:36:24 +0200 Received: by rabelais.socialtools.net (Postfix, from userid 108) id 76EC6232DF; Sun, 2 May 2004 14:36:23 +0100 (BST) Received: from socialtools.net (chaucer.socialtools.net [81.2.94.242]) by rabelais.socialtools.net (Postfix) with ESMTP id 63E70232DD; Sun, 2 May 2004 14:36:22 +0100 (BST) Message-ID: <4094F956.3060206@socialtools.net> Date: Sun, 02 May 2004 14:36:22 +0100 From: Benjamin Geer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113 X-Accept-Language: en-gb, en, fr, it MIME-Version: 1.0 To: skaller@users.sourceforge.net Cc: The Caml Trade Subject: Re: [Caml-list] transactions? References: <1083468950.20722.189.camel@pelican.wigram> In-Reply-To: <1083468950.20722.189.camel@pelican.wigram> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on rabelais.socialtools.net X-Spam-Status: No, hits=-4.9 required=5.0 tests=BAYES_00 autolearn=ham version=2.63 X-Miltered: at nez-perce with ID 4094F957.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 threads:01 compiles:01 camlp:01 threads:01 unlocking:01 hash:01 hash:01 hashtbl:01 hashtbl:01 ifdef:01 ifdef:01 implements:01 cmo:01 ocaml:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk skaller wrote: > Is there an Ocaml eqivalent of begin/end blocking section? > I.e. something that sets the global lock if threads > are enabled (but still compiles if they're not)? You can use camlp4's pa_ifdef.cmo to create a global mutex only if threads are enabled. The utility functions below take care of this, as well as unlocking the mutex if an exception is thrown. --------------------------------------------------------------------- (* Implements try-with-finally. *) let run_with_finally ~f ~(finally: unit -> unit) = let res = try f () with e -> finally (); raise e in finally (); res ;; (* Locks a mutex, calls a function, then unlocks the mutex. The mutex is also unlocked if the function throws an exception. *) ifdef THREADS then let run_in_mutex ~f ~(mutex : Mutex.t) = Mutex.lock mutex; run_with_finally ~f ~finally:(function () -> Mutex.unlock mutex) ;; (* If threads are enabled, creates a mutex, otherwise returns unit. *) let create_optional_mutex () = (ifdef THREADS then Mutex.create () else ()) ;; (* Calls a function, using the supplied mutex if threads are enabled. *) let run_in_optional_mutex ~f ~mutex = (ifdef THREADS then run_in_mutex ~f ~mutex else f ()) ;; --------------------------------------------------------------------- Example usage: --------------------------------------------------------------------- let hash_mutex = create_optional_mutex () ;; let add_to_hash h k v = run_in_optional_mutex ~f:(function () -> if Hashtbl.mem h k then () else Hashtbl.add h k v) ~mutex:hash_mutex ;; --------------------------------------------------------------------- Ben ------------------- 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