File: lf_queue.md

package info (click to toggle)
ocaml-eio 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,548 kB
  • sloc: ml: 14,608; ansic: 1,237; makefile: 25
file content (100 lines) | stat: -rw-r--r-- 1,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# A lock-free queue for schedulers

```ocaml
# #require "eio.utils";;
```

```ocaml
module Q = Eio_utils.Lf_queue;;
```

## A basic run

```ocaml
# let q : int Q.t = Q.create ();;
val q : int Q.t = <abstr>
# Q.push q 1;;
- : unit = ()
# Q.push q 2;;
- : unit = ()
# Q.pop q;;
- : int option = Some 1
# Q.pop q;;
- : int option = Some 2
# Q.pop q;;
- : int option = None
# Q.pop q;;
- : int option = None
# Q.push q 3;;
- : unit = ()
# Q.pop q;;
- : int option = Some 3
```

## Closing the queue

```ocaml
# let q : int Q.t = Q.create ();;
val q : int Q.t = <abstr>
# Q.push q 1;;
- : unit = ()
# Q.close q;;
- : unit = ()
# Q.push q 2;;
Exception: Eio_utils__Lf_queue.Closed.
# Q.push_head q 3;;
- : unit = ()
# Q.pop q;;
- : int option = Some 3
# Q.pop q;;
- : int option = Some 1
# Q.pop q;;
Exception: Eio_utils__Lf_queue.Closed.
# Q.push_head q 4;;
Exception: Eio_utils__Lf_queue.Closed.
```

## Closing an empty queue

```ocaml
# let q = Q.create () in Q.close q; Q.push q 1;;
Exception: Eio_utils__Lf_queue.Closed.
```

## Empty?

```ocaml
# let q : int Q.t = Q.create ();;
val q : int Q.t = <abstr>
# Q.is_empty q;;
- : bool = true
# Q.push q 1; Q.is_empty q;;
- : bool = false
# Q.pop q;;
- : int option = Some 1
# Q.is_empty q;;
- : bool = true
# Q.close q; Q.is_empty q;;
Exception: Eio_utils__Lf_queue.Closed.
```

## Pushing to the head

```ocaml
# let q : int Q.t = Q.create ();;
val q : int Q.t = <abstr>
# Q.push_head q 3; Q.push q 4; Q.push_head q 2; Q.push q 5; Q.push_head q 1;;
- : unit = ()
# Q.pop q;;
- : int option = Some 1
# Q.pop q;;
- : int option = Some 2
# Q.pop q;;
- : int option = Some 3
# Q.pop q;;
- : int option = Some 4
# Q.pop q;;
- : int option = Some 5
# Q.pop q;;
- : int option = None
```