Skip to content

Commit 3171fc9

Browse files
committed
Merge remote-tracking branch 'origin/pr/55' into pr/55
2 parents 093e2ae + f93070a commit 3171fc9

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

lib/postgresql.ml

Lines changed: 28 additions & 6 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"
@@ -955,9 +954,11 @@ module Connection (Mutex : Mutex) = struct
955954
else Gc.finalise Stub.finish my_conn
956955
in
957956
let conn_mtx = Mutex.create () in
958-
let finishing = ref false in
957+
let cancel_mtx = Mutex.create () in
958+
let finished = ref false in
959+
(* bool becomes true after deallocation *)
959960
let check_null () =
960-
if !finishing || Stub.conn_isnull my_conn then
961+
if !finished then
961962
failwith "Postgresql.check_null: connection already finished"
962963
in
963964
let wrap_conn f =
@@ -969,11 +970,32 @@ module Connection (Mutex : Mutex) = struct
969970
(* Check again in case the world has changed *)
970971
f my_conn)
971972
in
973+
let wrap_cancel f =
974+
protectx
975+
~f:(fun _ ->
976+
Mutex.lock cancel_mtx;
977+
check_null ();
978+
(* Check again in case the world has changed *)
979+
f my_conn)
980+
~finally:(fun _ -> Mutex.unlock cancel_mtx)
981+
in
982+
let wrap_both f =
983+
protectx
984+
~f:(fun _ ->
985+
Mutex.lock conn_mtx;
986+
Mutex.lock cancel_mtx;
987+
check_null ();
988+
(* Check again in case the world has changed *)
989+
f my_conn)
990+
~finally:(fun _ ->
991+
Mutex.unlock cancel_mtx;
992+
Mutex.unlock conn_mtx)
993+
in
972994
let signal_error conn =
973995
raise (Error (Connection_failure (Stub.error_message conn)))
974996
in
975997
let request_cancel () =
976-
wrap_conn (fun _ ->
998+
wrap_cancel (fun _ ->
977999
match Stub.request_cancel my_conn with
9781000
| None -> ()
9791001
| Some err -> raise (Error (Cancel_failure err)))
@@ -999,9 +1021,9 @@ module Connection (Mutex : Mutex) = struct
9991021

10001022
object (self (* Main routines *))
10011023
method finish =
1002-
wrap_conn (fun c ->
1024+
wrap_both (fun c ->
10031025
Stub.finish c;
1004-
finishing := true)
1026+
finished := true)
10051027

10061028
method try_reset =
10071029
wrap_conn (fun conn ->

lib/postgresql_stubs.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,6 @@ static inline void np_decr_refcount(np_callback *c) {
266266
#define get_cancel_obj(v) ((PGcancel *)Field(v, 2))
267267
#define set_cancel_obj(v, cancel) (Field(v, 2) = (value)cancel)
268268

269-
CAMLprim value PQconn_isnull(value v_conn) {
270-
return Val_bool((get_conn(v_conn)) ? 0 : 1);
271-
}
272-
273269
static inline void free_conn(value v_conn) {
274270
PGconn *conn = get_conn(v_conn);
275271
if (conn) {

0 commit comments

Comments
 (0)