Skip to content

Commit

Permalink
reindent in containers.thread
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Jan 24, 2017
1 parent 59208fd commit 9a46b45
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 228 deletions.
100 changes: 50 additions & 50 deletions src/threads/CCBlockingQueue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ let with_lock_ q f =
let push q x =
with_lock_ q
(fun () ->
while q.size = q.capacity do
Condition.wait q.cond q.lock
done;
assert (q.size < q.capacity);
Queue.push x q.q;
(* if there are blocked receivers, awake one of them *)
incr_size_ q;
Condition.broadcast q.cond)
while q.size = q.capacity do
Condition.wait q.cond q.lock
done;
assert (q.size < q.capacity);
Queue.push x q.q;
(* if there are blocked receivers, awake one of them *)
incr_size_ q;
Condition.broadcast q.cond)

let take q =
with_lock_ q
(fun () ->
while q.size = 0 do
Condition.wait q.cond q.lock
done;
let x = Queue.take q.q in
(* if there are blocked senders, awake one of them *)
decr_size_ q;
Condition.broadcast q.cond;
x)
while q.size = 0 do
Condition.wait q.cond q.lock
done;
let x = Queue.take q.q in
(* if there are blocked senders, awake one of them *)
decr_size_ q;
Condition.broadcast q.cond;
x)

(*$R
let q = create 1 in
Expand All @@ -83,22 +83,22 @@ let push_list q l =
| [] -> l
| _::_ when q.size = q.capacity -> l (* no room remaining *)
| x :: tl ->
Queue.push x q.q;
incr_size_ q;
push_ q tl
Queue.push x q.q;
incr_size_ q;
push_ q tl
in
(* push chunks of [l] in [q] until [l] is empty *)
let rec aux q l = match l with
| [] -> ()
| _::_ ->
| [] -> ()
| _::_ ->
let l = with_lock_ q
(fun () ->
while q.size = q.capacity do
Condition.wait q.cond q.lock
done;
let l = push_ q l in
Condition.broadcast q.cond;
l)
(fun () ->
while q.size = q.capacity do
Condition.wait q.cond q.lock
done;
let l = push_ q l in
Condition.broadcast q.cond;
l)
in
aux q l
in aux q l
Expand All @@ -118,14 +118,14 @@ let take_list q n =
if n=0 then List.rev acc
else
let acc, n = with_lock_ q
(fun () ->
while q.size = 0 do
Condition.wait q.cond q.lock
done;
let acc, n = pop_ acc q n in
Condition.broadcast q.cond;
acc, n
)
(fun () ->
while q.size = 0 do
Condition.wait q.cond q.lock
done;
let acc, n = pop_ acc q n in
Condition.broadcast q.cond;
acc, n
)
in
aux acc q n
in
Expand Down Expand Up @@ -163,28 +163,28 @@ let take_list q n =
let try_take q =
with_lock_ q
(fun () ->
if q.size = 0 then None
else (
decr_size_ q;
Some (Queue.take q.q)
))
if q.size = 0 then None
else (
decr_size_ q;
Some (Queue.take q.q)
))

let try_push q x =
with_lock_ q
(fun () ->
if q.size = q.capacity then false
else (
incr_size_ q;
Queue.push x q.q;
Condition.signal q.cond;
true
))
if q.size = q.capacity then false
else (
incr_size_ q;
Queue.push x q.q;
Condition.signal q.cond;
true
))

let peek q =
with_lock_ q
(fun () ->
try Some (Queue.peek q.q)
with Queue.Empty -> None)
try Some (Queue.peek q.q)
with Queue.Empty -> None)

let size q = with_lock_ q (fun () -> q.size)

Expand Down
12 changes: 6 additions & 6 deletions src/threads/CCLock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ let update l f =

(*$T
let l = create 5 in update l (fun x->x+1); get l = 6
*)
*)

let update_map l f =
with_lock l
(fun x ->
let x', y = f x in
l.content <- x';
y)
let x', y = f x in
l.content <- x';
y)

(*$T
let l = create 5 in update_map l (fun x->x+1, string_of_int x) = "5" && get l = 6
*)
*)

let get l =
Mutex.lock l.mutex;
Expand Down Expand Up @@ -135,7 +135,7 @@ let decr l = update l Pervasives.pred
(*$T
let l = create 0 in incr l ; get l = 1
let l = create 0 in decr l ; get l = ~-1
*)
*)

let incr_then_get l =
Mutex.lock l.mutex;
Expand Down
Loading

0 comments on commit 9a46b45

Please sign in to comment.