Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Resource acquisition is initialization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Quetzalcoatl Bradley <qbradley@b...>
Subject: Re: [Caml-list] Resource acquisition is initialization

On Thursday, December 12, 2002, at 05:15  AM, Xavier Leroy wrote:
> For a mutex:
>
> let synchronize mut action =
>   try
>     Mutex.lock mut;
>     let res = action () in
>     Mutex.unlock mut;
>     res
>   with x ->
>     Mutex.unlock mut;
>     raise x
>
> You get the idea.

For fun I try to use campl4 to add synchronize directive.

usage:   synchronize "expression that evaluates to Mutex.t" in 
"expression"
The value is result of "expression".

  Example:

let lock = Mutex.create ();;
let counter = ref 0;;
let next_sym prefix =
   synchronize lock in
     let current = !counter in
       counter := current + 1;
       prefix ^ (string_of_int current)
;;

synchronize.ml file:

open Pcaml;;

EXTEND
  expr: LEVEL "expr1"
   [[ "synchronize"; e1 = expr; "in"; e2 = expr ->
    <:expr<
     let __synchronize_lock = $e1$ in
     do {
      Mutex.lock __synchronize_lock;
     try
      let __synchronize_result = $e2$ in
      do { Mutex.unlock __synchronize_lock; __synchronize_result }
     with z ->
      do { Mutex.unlock __synchronize_lock; raise z }
     }
    >>
   ]];
END;;
-------------------
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