-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
113a08b
commit 22e4151
Showing
6 changed files
with
106 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
(* SPDX-License-Identifier: AGPL-3.0-or-later *) | ||
(* Copyright © 2021-2024 OCamlPro *) | ||
(* Written by the Owi programmers *) | ||
|
||
type ('get, 'write) t = | ||
{ mutex : Mutex.t | ||
; cond : Condition.t | ||
; getter : unit -> 'get option | ||
; writter : 'write -> Condition.t -> unit | ||
; mutable pledges : int | ||
; mutable closed : bool | ||
} | ||
|
||
let init getter writter = | ||
{ mutex = Mutex.create () | ||
; cond = Condition.create () | ||
; getter | ||
; writter | ||
; pledges = 0 | ||
; closed = false | ||
} | ||
|
||
let get synchro pledge = | ||
let rec inner_loop synchro pledge = | ||
match synchro.getter () with | ||
| None when synchro.pledges = 0 || synchro.closed -> | ||
Condition.broadcast synchro.cond; | ||
None | ||
| None -> | ||
Condition.wait synchro.cond synchro.mutex; | ||
inner_loop synchro pledge | ||
| Some _ as v -> | ||
if pledge then synchro.pledges <- synchro.pledges + 1; | ||
v | ||
in | ||
Mutex.protect synchro.mutex (fun () -> inner_loop synchro pledge) | ||
|
||
let write v { writter; cond; mutex; _ } = | ||
Mutex.protect mutex (fun () -> writter v cond) | ||
|
||
let make_pledge synchro = | ||
Mutex.lock synchro.mutex; | ||
synchro.pledges <- synchro.pledges + 1; | ||
Mutex.unlock synchro.mutex | ||
|
||
let end_pledge synchro = | ||
Mutex.lock synchro.mutex; | ||
synchro.pledges <- synchro.pledges - 1; | ||
Condition.broadcast synchro.cond; | ||
Mutex.unlock synchro.mutex | ||
|
||
let fail q = | ||
Mutex.lock q.mutex; | ||
q.closed <- true; | ||
Condition.broadcast q.cond; | ||
Mutex.unlock q.mutex | ||
|
||
let rec work_while f q = | ||
match get q true with | ||
| None -> () | ||
| Some v -> | ||
let () = f v (fun v -> write v q) in | ||
end_pledge q; | ||
work_while f q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
type (!'get, !'write) t | ||
|
||
val init : | ||
(unit -> 'get option) -> ('write -> Condition.t -> unit) -> ('get, 'write) t | ||
|
||
val get : ('get, 'write) t -> bool -> 'get option | ||
|
||
val write : 'write -> ('get, 'write) t -> unit | ||
|
||
val make_pledge : ('get, 'write) t -> unit | ||
|
||
val end_pledge : ('get, 'write) t -> unit | ||
|
||
val fail : ('get, 'write) t -> unit | ||
|
||
val work_while : ('get -> ('write -> unit) -> unit) -> ('get, 'write) t -> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,7 @@ | |
stack | ||
string_map | ||
syntax | ||
synchronizer | ||
text | ||
text_lexer | ||
text_parser | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters