Skip to content

Commit ccf7478

Browse files
committed
fixes
1 parent 518fa23 commit ccf7478

8 files changed

Lines changed: 149 additions & 19 deletions

File tree

dune-project

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
sqlite3)
2020
(depends
2121
(containers (and (>= 3.3) (< 4.0)))
22-
(pp_loc (and (>= 1.0) (< 2.0)))
22+
(pp_loc (and (>= 2.0) (< 3.0)))
2323
(iter (>= 1.0))
2424
(ocaml (>= 4.08.0))
2525
sha
@@ -58,7 +58,6 @@
5858
(synopsis "Trustee-powered interactive theorem prover")
5959
(depends
6060
(containers (and (>= 3.0) (< 4.0)))
61-
containers-thread
6261
logs
6362
cmdliner
6463
(ocaml (>= 4.08.0))

src/itp/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
(name trustee_itp)
33
(public_name trustee-itp)
44
(flags :standard -open Trustee_core.Sigs)
5-
(libraries trustee.core trustee.syntax pp_loc containers containers-thread
5+
(libraries trustee.core trustee.syntax pp_loc containers
66
threads.posix logs))

src/itp/lock.ml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
type 'a t = {
2+
mutex: Mutex.t;
3+
mutable content: 'a;
4+
}
5+
6+
let create content = { mutex = Mutex.create (); content }
7+
8+
let with_lock l f =
9+
Mutex.lock l.mutex;
10+
try
11+
let x = f l.content in
12+
Mutex.unlock l.mutex;
13+
x
14+
with exn ->
15+
let bt = Printexc.get_raw_backtrace () in
16+
(match Mutex.unlock l.mutex with
17+
| () -> Printexc.raise_with_backtrace exn bt
18+
| exception _ ->
19+
(* this should only happen if [f …] switched thread *)
20+
Printf.eprintf "mutex.unlock failed during `with_lock` cleanup\n%!";
21+
Printexc.raise_with_backtrace (Fun.Finally_raised exn) bt)
22+
23+
let mutex l = l.mutex
24+
let update l f = with_lock l (fun x -> l.content <- f x)
25+
26+
let update_map l f =
27+
with_lock l (fun x ->
28+
let x', y = f x in
29+
l.content <- x';
30+
y)
31+
32+
let get l =
33+
Mutex.lock l.mutex;
34+
let x = l.content in
35+
Mutex.unlock l.mutex;
36+
x
37+
38+
let set l x =
39+
Mutex.lock l.mutex;
40+
l.content <- x;
41+
Mutex.unlock l.mutex
42+
43+
let exchange l x =
44+
Mutex.lock l.mutex;
45+
let old = l.content in
46+
l.content <- x;
47+
Mutex.unlock l.mutex;
48+
old
49+
50+
module LockRef = struct
51+
type nonrec 'a t = {
52+
l: 'a ref;
53+
mutable usable: bool;
54+
}
55+
56+
let as_ref self = self.l
57+
let get self = !(self.l)
58+
59+
let set self x =
60+
assert self.usable;
61+
self.l := x
62+
end
63+
64+
let with_lock_as_ref l f =
65+
Mutex.lock l.mutex;
66+
let lref = { LockRef.l = ref l.content; usable = true } in
67+
try
68+
let x = f lref in
69+
l.content <- !(lref.l);
70+
lref.usable <- false;
71+
Mutex.unlock l.mutex;
72+
x
73+
with e ->
74+
l.content <- !(lref.l);
75+
lref.usable <- false;
76+
Mutex.unlock l.mutex;
77+
raise e
78+
79+
let pp ppx out self =
80+
let x = get self in
81+
Fmt.fprintf out "(@[lock@ %a@])" ppx x

src/itp/lock.mli

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(** Value protected by a mutex *)
2+
3+
type 'a t
4+
(** A value surrounded with a lock *)
5+
6+
val create : 'a -> 'a t
7+
(** Create a new protected value. *)
8+
9+
val with_lock : 'a t -> ('a -> 'b) -> 'b
10+
(** [with_lock l f] runs [f x] where [x] is the value protected with the lock
11+
[l], in a critical section. If [f x] fails, [with_lock l f] fails too but
12+
the lock is released. *)
13+
14+
val update : 'a t -> ('a -> 'a) -> unit
15+
(** [update l f] replaces the content [x] of [l] with [f x], atomically. *)
16+
17+
val update_map : 'a t -> ('a -> 'a * 'b) -> 'b
18+
(** [update_map l f] computes [x', y = f (get l)], then puts [x'] in [l] and
19+
returns [y]. *)
20+
21+
val mutex : _ t -> Mutex.t
22+
(** Underlying mutex. *)
23+
24+
val get : 'a t -> 'a
25+
(** Atomically get the value in the lock. The value that is returned isn't
26+
protected! *)
27+
28+
val set : 'a t -> 'a -> unit
29+
(** Atomically set the value. *)
30+
31+
val exchange : 'a t -> 'a -> 'a
32+
(** [exchange lock x] atomically sets [lock := x] and returns the previous value
33+
*)
34+
35+
(** Type allowing to manipulate the lock as a reference when one is holding it.
36+
*)
37+
module LockRef : sig
38+
type 'a t
39+
40+
val as_ref : 'a t -> 'a ref
41+
val get : 'a t -> 'a
42+
val set : 'a t -> 'a -> unit
43+
end
44+
45+
val with_lock_as_ref : 'a t -> ('a LockRef.t -> 'b) -> 'b
46+
(** [with_lock_as_ref l f] calls [f] with a reference-like object that allows to
47+
manipulate the value of [l] safely. The object passed to [f] must not escape
48+
the function call. *)
49+
50+
val pp : 'a Fmt.printer -> 'a t Fmt.printer

src/loc/local_loc.ml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,21 @@ let positions ~ctx self =
8888
let off1, off2 = offsets self in
8989
pos_of_offset ~ctx off1, pos_of_offset ~ctx off2
9090

91-
let tr_position ~ctx ~left ~offset : Lexing.position =
91+
let tr_position ~ctx ~left ~offset : Pp_loc.Position.t =
9292
let line, col = tr_offset ctx offset in
93-
{
94-
Lexing.pos_fname = ctx.filename;
95-
pos_lnum = line;
96-
pos_cnum =
97-
(offset
98-
+
99-
if left then
100-
0
101-
else
102-
1);
103-
pos_bol = offset - col;
104-
}
93+
Pp_loc.Position.of_lexing
94+
@@ {
95+
Lexing.pos_fname = ctx.filename;
96+
pos_lnum = line;
97+
pos_cnum =
98+
(offset
99+
+
100+
if left then
101+
0
102+
else
103+
1);
104+
pos_bol = offset - col;
105+
}
105106

106107
let line_cols_ ~ctx self =
107108
let off1, off2 = offsets self in

src/loc/local_loc.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ val make : ctx:ctx -> off1:int -> off2:int -> t
2222
val make_pos : ctx:ctx -> Position.t -> Position.t -> t
2323
val of_lexbuf : ctx:ctx -> Lexing.lexbuf -> t
2424
val of_lex_pos : ctx:ctx -> Lexing.position -> Lexing.position -> t
25-
val tr_position : ctx:ctx -> left:bool -> offset:int -> Lexing.position
25+
val tr_position : ctx:ctx -> left:bool -> offset:int -> Pp_loc.Position.t
2626
val offset_of_pos : ctx:ctx -> Position.t -> int
2727
val pos_of_offset : ctx:ctx -> int -> Position.t
2828
val offsets : t -> int * int

trustee-itp.opam

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ bug-reports: "https://github.com/c-cube/trustee/issues"
99
depends: [
1010
"dune" {>= "2.0"}
1111
"containers" {>= "3.0" & < "4.0"}
12-
"containers-thread"
1312
"logs"
1413
"cmdliner"
1514
"ocaml" {>= "4.08.0"}

trustee.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bug-reports: "https://github.com/c-cube/trustee/issues"
1111
depends: [
1212
"dune" {>= "2.0"}
1313
"containers" {>= "3.3" & < "4.0"}
14-
"pp_loc" {>= "1.0" & < "2.0"}
14+
"pp_loc" {>= "2.0" & < "3.0"}
1515
"iter" {>= "1.0"}
1616
"ocaml" {>= "4.08.0"}
1717
"sha"

0 commit comments

Comments
 (0)