Skip to content

Commit

Permalink
WIP: Timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
polytypic committed Jun 7, 2023
1 parent d144446 commit 11050ca
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ is distributed under the [ISC license](LICENSE.md).
- [A transactional lock-free queue](#a-transactional-lock-free-queue)
- [Composing transactions](#composing-transactions)
- [Blocking transactions](#blocking-transactions)
- [Timeouts](#timeouts)
- [A transactional lock-free leftist heap](#a-transactional-lock-free-leftist-heap)
- [Programming with transactional data structures](#programming-with-transactional-data-structures)
- [The dining philosophers problem](#the-dining-philosophers-problem)
Expand Down Expand Up @@ -552,6 +553,61 @@ The retry mechanism essentially allows a transaction to wait for an arbitrary
condition and can function as a fairly expressive communication and
synchronization mechanism.

#### Timeouts

```ocaml
# #require "domain-local-timeout"
```

```ocaml
# #thread
```

```ocaml
# Domain_local_timeout.set_system (module Thread) (module Unix)
- : unit = ()
```

```ocaml
type timeout = {
passed : 'x.xt:'x Xt.t -> bool;
cancel : unit -> unit;
}
```

```ocaml
# let timeoutf seconds =
let passed = Loc.make false in
let cancel =
Domain_local_timeout.set_timeoutf seconds @@ fun () ->
Loc.set passed true
in
{ passed = Xt.get passed;
cancel = (cancel :> unit -> unit) }
val timeoutf : float -> timeout = <fun>
```

```ocaml
# let exit_if ~xt { passed; cancel } =
if passed ~xt then
raise Exit;
Xt.post_commit ~xt cancel;
Retry.later ()
val exit_if : xt:'a Xt.t -> timeout -> 'b = <fun>
```

```ocaml
# let an_empty_stack = stack () in
let timeout = timeoutf 0.1 in
Xt.commit { tx =
Xt.first [
pop an_empty_stack;
exit_if timeout;
]
}
Exception: Stdlib.Exit.
```

#### A transactional lock-free leftist heap

Let's implement something a bit more complicated,
Expand Down
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
(depends
(ocaml (>= 5.0))
(domain-local-await (>= 0.1.0))
domain-local-timeout
(mdx (and (>= 1.10.0) :with-test))))
(package (name kcas_data)
(synopsis "Compositional lock-free data structures and primitives for communication and synchronization")
Expand Down
4 changes: 4 additions & 0 deletions kcas.opam
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ depends: [
"dune" {>= "3.3"}
"ocaml" {>= "5.0"}
"domain-local-await" {>= "0.1.0"}
"domain-local-timeout"
"mdx" {>= "1.10.0" & with-test}
"odoc" {with-doc}
]
Expand All @@ -29,3 +30,6 @@ build: [
]
]
dev-repo: "git+https://github.com/ocaml-multicore/kcas.git"
pin-depends: [
[ "domain-local-timeout.dev" "git+https://github.com/ocaml-multicore/domain-local-timeout#3c90d77a6b6f7ca77c21ec7be9f803ca2cadb138" ]
]
3 changes: 3 additions & 0 deletions kcas.opam.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pin-depends: [
[ "domain-local-timeout.dev" "git+https://github.com/ocaml-multicore/domain-local-timeout#3c90d77a6b6f7ca77c21ec7be9f803ca2cadb138" ]
]

0 comments on commit 11050ca

Please sign in to comment.