Skip to content

Commit a43be1d

Browse files
committed
avoid to go by a C stub to know if the connection was finished
1 parent b4920ad commit a43be1d

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/postgresql.ml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ module Stub = struct
338338
type connection
339339
type result
340340

341-
external conn_isnull : connection -> bool = "PQconn_isnull" [@@noalloc]
342341
external connect : string -> bool -> connection = "PQconnectdb_stub"
343342
external finish : connection -> unit = "PQfinish_stub"
344343
external reset : connection -> unit = "PQreset_stub"
@@ -968,8 +967,9 @@ module Connection (Mutex : Mutex) = struct
968967
in
969968
let conn_mtx = Mutex.create () in
970969
let cancel_mtx = Mutex.create () in
970+
let finished = ref false in (* bool becomes true after deallocation *)
971971
let check_null () =
972-
if Stub.conn_isnull my_conn then
972+
if !finished then
973973
failwith "Postgresql.check_null: connection already finished"
974974
in
975975
let wrap_conn f =
@@ -990,6 +990,18 @@ module Connection (Mutex : Mutex) = struct
990990
f my_conn)
991991
~finally:(fun _ -> Mutex.unlock cancel_mtx)
992992
in
993+
let wrap_both f =
994+
protectx
995+
~f:(fun _ ->
996+
Mutex.lock conn_mtx;
997+
Mutex.lock cancel_mtx;
998+
check_null ();
999+
(* Check again in case the world has changed *)
1000+
f my_conn)
1001+
~finally:(fun _ ->
1002+
Mutex.unlock cancel_mtx;
1003+
Mutex.unlock conn_mtx)
1004+
in
9931005
let signal_error conn =
9941006
raise (Error (Connection_failure (Stub.error_message conn)))
9951007
in
@@ -1019,7 +1031,7 @@ module Connection (Mutex : Mutex) = struct
10191031
in
10201032

10211033
object (self (* Main routines *))
1022-
method finish = wrap_conn (fun c -> Stub.finish c)
1034+
method finish = wrap_both (fun c -> Stub.finish c; finished := true)
10231035

10241036
method try_reset =
10251037
wrap_conn (fun conn ->

lib/postgresql_stubs.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,6 @@ static inline void free_result(value v_res) {
494494
}
495495
}
496496

497-
CAMLprim value PQres_isnull(value v_res) {
498-
return Val_bool(get_res(v_res) ? 0 : 1);
499-
}
500-
501497
static struct custom_operations result_ops = {
502498
"pg_ocaml_result", free_result,
503499
custom_compare_default, custom_hash_default,

0 commit comments

Comments
 (0)