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 15:59:16 +0100	[thread overview]
Message-ID: <40950CC4.6050907@socialtools.net> (raw)
In-Reply-To: <4094F956.3060206@socialtools.net>

Benjamin Geer 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.

Actually I've just realised that pa_ifdef.cmo seems to be deprecated 
(it's not mentioned in the camlp4 docs anymore), but you can use 
pa_macro.cmo[1].  Here's a revised version:

------------------------------------------------------------------------
(* Implements try-with-finally. *)
let try_with_finally ~(f : unit -> 'a) ~(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 call_in_mutex ~f ~(mutex : Mutex.t) =
     Mutex.lock mutex;
     try_with_finally ~f ~finally:(function () -> Mutex.unlock mutex) END ;;

(* If threads are enabled, creates a mutex, otherwise returns unit. *)
let create_optional_mutex () =
   (IFDEF THREADS THEN
      Mutex.create ()
    ELSE
      () END) ;;

(* This function's arguments are a function to be called, and a value
    returned by create_optional_mutex.  If threads are enabled, it
    calls the function in a mutex.  If threads are disabled, it
    just calls the function. *)
let call_in_optional_mutex ~f ~mutex =
   (IFDEF THREADS THEN
      call_in_mutex ~f ~mutex
    ELSE
      f () END) ;;
------------------------------------------------------------------------

Note that you have to take care of defining the THREADS symbol.

Ben

[1] http://caml.inria.fr/camlp4/manual/manual002.html#toc1

-------------------
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


  parent reply	other threads:[~2004-05-02 14:59 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
2004-05-02 14:24   ` skaller
2004-05-02 14:59   ` Benjamin Geer [this message]
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=40950CC4.6050907@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).