caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Benjamin Geer <ben@socialtools.net>
To: skaller@users.sourceforge.net
Cc: The Caml Trade <caml-list@inria.fr>
Subject: Re: [Caml-list] transactions?
Date: Sun, 02 May 2004 14:36:22 +0100	[thread overview]
Message-ID: <4094F956.3060206@socialtools.net> (raw)
In-Reply-To: <1083468950.20722.189.camel@pelican.wigram>

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


  reply	other threads:[~2004-05-02 13:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-02  3:35 skaller
2004-05-02 13:36 ` Benjamin Geer [this message]
2004-05-02 14:24   ` skaller
2004-05-02 14:59   ` Benjamin Geer
2004-05-02 15:51     ` [Caml-list] Camlp4 docs Martin Jambon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4094F956.3060206@socialtools.net \
    --to=ben@socialtools.net \
    --cc=caml-list@inria.fr \
    --cc=skaller@users.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).