camlex/camlyacc + threads problem

Olivier Bouyssou (bouyssou@yeti.didac-mip.fr)
Wed, 10 Sep 1997 10:17:24 +0200

Message-Id: <199709100817.KAA05734@yeti.didac-mip.fr>
To: caml-list@inria.fr
Subject: camlex/camlyacc + threads problem
Date: Wed, 10 Sep 1997 10:17:24 +0200
From: Olivier Bouyssou <bouyssou@yeti.didac-mip.fr>

English translation below.

J'ai un probleme lorsque j'utilise une fonction de parsing dans plusieurs
threads

Voici l'exemple qui parse une liste de crenaux horaires :

Le lexer :

--
{
open Horloge_parser
} 
rule token = parse
   [' ' '\t' '\n']          { token lexbuf }
|  ['0'-'9']['0'-'9']       { INT(int_of_string(Lexing.lexeme lexbuf) }
|  ['0'-'9']                { INT(int_of_string(Lexing.lexeme lexbuf) }
|  ':'                      { PP }
|  '-'                      { TIRET }
|  eof                      { EOF }
--

Le parser :

--
%{
exception Incorrect_minute
exception Incorrect_hour
let test_minute m = if m < 0 || m > 59 then raise Incorrect_minute
let test_heure  h = if h < 0 || h > 23 then raise Incorrect_hour
%}
%token <int> INT
%token PP
%token TIRET
%token EOF
%start main
%type <(int * int) list> main
%%
main:
  EOF { [] }
| liste_intervalle EOF { $1 };

liste_intervalle: intervalle { [$1] } | liste_intervalle intervalle { $2::$1 };

intervalle: heure TIRET heure { ($1,$3) };

heure: INT { test_heure $1; $1*60 } | INT PP INT { test_heure $1; test_minute $3; $1*60+$3}; --

Et le programme qui utilise le parser :

--
let mutex = Mutex.create () 
  
let task () =
  while true 
  do
    let lexbuf = Lexing.from_string "0-23:59" in
    Mutex.lock mutex ;
    Horloge_parser.main Horloge_lexer.token lexbuf ;
    Mutex.unlock mutex ;
    if (Random.int 100) = 10 then ThreadUnix.sleep 1 ;
  done 
    
let main () =
  for i = 0 to 100 do Thread.create task () done ; 
  while true do ThreadUnix.sleep 1 done ;;

main () ;;

--

Le probleme est que sans le mutex j'obtient des Parse_errors.

----------------------------------------------------------------------------- English summary :

I've a problem when I use a parser in multiple threads. If the calls to the parser are not enclosed by a mutex, there are parse errors. ( See example in french version )

-----------------------------------------------------------------------------

-- 
Olivier Bouyssou (F1NXH), Didactheque Regionale          bouyssou@didac-mip.fr
Universite Paul Sabatier 118, route de Narbonne           31062 TOULOUSE Cedex
Tel : +33.5.61.55.65.74                                Fax : +33.5.61.55.65.71