···629629 && Desc.equal desc t.desc
630630 ;;
631631632632- let status s =
632632+ (* To be called when the mutex has already been acquired *)
633633+ let status_no_mutex s =
633634 match s.status with
634635 | Some s -> s
635636 | None ->
636637 let st = Desc.status s.desc in
637638 s.status <- Some st;
638639 st
640640+ ;;
641641+642642+ let status m s =
643643+ Mutex.lock m;
644644+ let st = status_no_mutex s in
645645+ Mutex.unlock m;
646646+ st
639647 ;;
640648641649 module Table = Hashtbl.Make (struct
+2-1
lib/automata.mli
···102102 val dummy : t
103103 val create : Category.t -> expr -> t
104104 val idx : t -> Idx.t
105105- val status : t -> Status.t
105105+ val status_no_mutex : t -> Status.t
106106+ val status : Mutex.t -> t -> Status.t
106107 val to_dyn : t -> Dyn.t
107108108109 module Table : Hashtbl.S with type key = t
+46-24
lib/compile.ml
···105105 ; (* States of the deterministic automata *)
106106 group_names : (string * int) list
107107 ; (* Named groups in the regular expression *)
108108- group_count : int (* Number of groups in the regular expression *)
108108+ group_count : int
109109+ ; (* Number of groups in the regular expression *)
110110+ mutex : Mutex.t
109111 }
110112111113let pp_re ch re = Automata.pp ch re.initial
···169171 | Not_found ->
170172 let st =
171173 let break_state =
172172- match Automata.State.status desc with
174174+ match Automata.State.status_no_mutex desc with
173175 | Running -> false
174176 | Failed | Match _ -> true
175177 in
···193195194196let validate re (s : string) ~pos st =
195197 let color = Color_map.Table.get re.colors s.[pos] in
198198+ Mutex.lock re.mutex;
196199 let st' =
197200 let desc' =
198201 let cat = category re ~color in
···200203 in
201204 find_state re desc'
202205 in
203203- State.set_transition st ~color st'
206206+ State.set_transition st ~color st';
207207+ Mutex.unlock re.mutex
204208;;
205209206206-let next colors st s pos =
207207- State.follow_transition st ~color:(Color_map.Table.get colors (String.unsafe_get s pos))
210210+let next mutex colors st s pos =
211211+ Mutex.lock mutex;
212212+ let res =
213213+ State.follow_transition st ~color:(Color_map.Table.get colors (String.unsafe_get s pos))
214214+ in
215215+ Mutex.unlock mutex;
216216+ res
208217;;
209218210219let rec loop re ~colors ~positions s ~pos ~last st0 st =
211220 if pos < last
212221 then (
213213- let st' = next colors st s pos in
222222+ let st' = next re.mutex colors st s pos in
214223 let idx = (State.get_info st').idx in
215224 if Idx.is_idx idx
216225 then
···236245let rec loop_no_mark re ~colors s ~pos ~last st0 st =
237246 if pos < last
238247 then (
239239- let st' = next colors st s pos in
248248+ let st' = next re.mutex colors st s pos in
240249 let idx = (State.get_info st').idx in
241250 if Idx.is_idx idx
242251 then loop_no_mark re ~colors s ~pos:(pos + 1) ~last st' st'
···250259;;
251260252261let final re st cat =
253253- try List.assq cat st.final with
254254- | Not_found ->
255255- let st' = delta re cat ~color:Cset.null_char st in
256256- let res = Automata.State.idx st', Automata.State.status st' in
257257- st.final <- (cat, res) :: st.final;
258258- res
262262+ Mutex.lock re.mutex;
263263+ let res =
264264+ try List.assq cat st.final with
265265+ | Not_found ->
266266+ let st' = delta re cat ~color:Cset.null_char st in
267267+ let res = Automata.State.idx st', Automata.State.status_no_mutex st' in
268268+ st.final <- (cat, res) :: st.final;
269269+ res
270270+ in
271271+ Mutex.unlock re.mutex;
272272+ res
259273;;
260274261275let find_initial_state re cat =
262262- try List.assq cat re.initial_states with
263263- | Not_found ->
264264- let st = find_state re (Automata.State.create cat re.initial) in
265265- re.initial_states <- (cat, st) :: re.initial_states;
266266- st
276276+ Mutex.lock re.mutex;
277277+ let res =
278278+ try List.assq cat re.initial_states with
279279+ | Not_found ->
280280+ let st = find_state re (Automata.State.create cat re.initial) in
281281+ re.initial_states <- (cat, st) :: re.initial_states;
282282+ st
283283+ in
284284+ Mutex.unlock re.mutex;
285285+ res
267286;;
268287269288let get_color re (s : string) pos =
···295314 else (
296315 (* Unknown *)
297316 let color = re.lnl in
317317+ Mutex.lock re.mutex;
298318 let st' =
299319 let desc =
300320 let cat = category re ~color in
···304324 find_state re desc
305325 in
306326 State.set_transition st ~color st';
327327+ Mutex.unlock re.mutex;
307328 handle_last_newline re positions ~pos st ~groups)
308329;;
309330···360381 in
361382 let state_info = State.get_info st in
362383 if Idx.is_break state_info.idx || (partial && not groups)
363363- then Automata.State.status state_info.desc
384384+ then Automata.State.status re.mutex state_info.desc
364385 else if partial && groups
365386 then (
366366- match Automata.State.status state_info.desc with
387387+ match Automata.State.status re.mutex state_info.desc with
367388 | (Match _ | Failed) as status -> status
368389 | Running ->
369390 (* This could be because it's still not fully matched, or it
···401422 let info = State.get_info state in
402423 if Idx.is_break info.idx
403424 &&
404404- match Automata.State.status info.desc with
425425+ match Automata.State.status t.re.mutex info.desc with
405426 | Failed -> true
406427 | Match _ | Running -> false
407428 then No_match
···472493 let rec loop re ~abs_pos ~colors ~positions s ~pos ~last st0 st =
473494 if pos < last
474495 then (
475475- let st' = next colors st s pos in
496496+ let st' = next re.mutex colors st s pos in
476497 let idx = (State.get_info st').idx in
477498 if Idx.is_idx idx
478499 then
···504525 let info = State.get_info state in
505526 if Idx.is_break info.idx
506527 &&
507507- match Automata.State.status info.desc with
528528+ match Automata.State.status t.re.mutex info.desc with
508529 | Failed -> true
509530 | Match _ | Running -> false
510531 then No_match
···533554 State.get_info state
534555 in
535556 match
536536- match Automata.State.status info.desc with
557557+ match Automata.State.status t.re.mutex info.desc with
537558 | (Match _ | Failed) as s -> s
538559 | Running ->
539560 let idx, res =
···597618 ; states = Automata.State.Table.create 97
598619 ; group_names
599620 ; group_count
621621+ ; mutex = Mutex.create ()
600622 }
601623;;
602624
+1-1
lib_test/expect/test_automata.ml
···3737 if n > 0
3838 then (
3939 print_dyn (State.to_dyn d);
4040- match State.status d with
4040+ match State.status_no_mutex d with
4141 | Failed -> Format.printf "> failed@."
4242 | Match _ -> Format.printf "> matched@."
4343 | Running ->