* [Caml-list] transactions?
@ 2004-05-02 3:35 skaller
2004-05-02 13:36 ` Benjamin Geer
0 siblings, 1 reply; 5+ messages in thread
From: skaller @ 2004-05-02 3:35 UTC (permalink / raw)
To: The Caml Trade
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)?
So can write for example:
begin_trx();
if Hashtbl.mem h k then () else Hashtbl.add h k v;
end_trx()
and know the table won't get changed by another thread
between the lookup and insertion?
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
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] 5+ messages in thread
* Re: [Caml-list] transactions?
2004-05-02 3:35 [Caml-list] transactions? skaller
@ 2004-05-02 13:36 ` Benjamin Geer
2004-05-02 14:24 ` skaller
2004-05-02 14:59 ` Benjamin Geer
0 siblings, 2 replies; 5+ messages in thread
From: Benjamin Geer @ 2004-05-02 13:36 UTC (permalink / raw)
To: skaller; +Cc: The Caml Trade
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] transactions?
2004-05-02 13:36 ` Benjamin Geer
@ 2004-05-02 14:24 ` skaller
2004-05-02 14:59 ` Benjamin Geer
1 sibling, 0 replies; 5+ messages in thread
From: skaller @ 2004-05-02 14:24 UTC (permalink / raw)
To: Benjamin Geer; +Cc: The Caml Trade
On Sun, 2004-05-02 at 23:36, Benjamin Geer wrote:
> 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.
Ah. OK. Thanks!
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
-------------------
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] 5+ messages in thread
* Re: [Caml-list] transactions?
2004-05-02 13:36 ` Benjamin Geer
2004-05-02 14:24 ` skaller
@ 2004-05-02 14:59 ` Benjamin Geer
2004-05-02 15:51 ` [Caml-list] Camlp4 docs Martin Jambon
1 sibling, 1 reply; 5+ messages in thread
From: Benjamin Geer @ 2004-05-02 14:59 UTC (permalink / raw)
To: skaller; +Cc: The Caml Trade
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Caml-list] Camlp4 docs
2004-05-02 14:59 ` Benjamin Geer
@ 2004-05-02 15:51 ` Martin Jambon
0 siblings, 0 replies; 5+ messages in thread
From: Martin Jambon @ 2004-05-02 15:51 UTC (permalink / raw)
To: Benjamin Geer; +Cc: skaller, The Caml Trade
On Sun, 2 May 2004, Benjamin Geer wrote:
> 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].
...
> [1] http://caml.inria.fr/camlp4/manual/manual002.html#toc1
But where is the documentation for defining macros?
The only source of information I found while searching for this a few
days ago was the comments in the source file pa_macro.ml.
Martin
-------------------
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] 5+ messages in thread
end of thread, other threads:[~2004-05-02 15:51 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-02 3:35 [Caml-list] transactions? skaller
2004-05-02 13:36 ` Benjamin Geer
2004-05-02 14:24 ` skaller
2004-05-02 14:59 ` Benjamin Geer
2004-05-02 15:51 ` [Caml-list] Camlp4 docs Martin Jambon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox