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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crateLockStatus;
use Future;
use ;
/// RAII structure used to release the shared read access of a `read-lock` when
/// dropped.
///
/// The protected data can be accessed through this guard via its [`Deref`] implementation.
///
/// This structure is created by the [`AsyncRWLock::read`], [`AsyncRWLock::try_read`]
/// and [`AsyncRWLock::read_unlock`] methods.
/// RAII structure used to release the unique write access of a `write-lock` when
/// dropped.
///
/// The protected data can be accessed through this guard via its [`Deref`] and
/// [`DerefMut`] implementations.
///
/// This structure is created by the [`AsyncRWLock::write`], [`AsyncRWLock::try_write`] and
/// [`AsyncRWLock::write_unlock`] methods.
/// An asynchronous version of a [`reader-writer lock`](std::sync::RwLock).
///
/// This type of lock allows a number of readers or at most one writer at any
/// point in time. The write portion of this lock typically allows modification
/// of the underlying data (exclusive access) and the read portion of this lock
/// typically allows for read-only access (shared access).
///
/// In comparison, a [`Mutex`](crate::sync::Mutex)
/// does not distinguish between readers or writers
/// that acquire the lock, therefore blocking any tasks waiting for the lock to
/// become available. An `RWLock` will allow any number of readers to acquire the
/// lock as long as a writer is not holding the lock.
///
/// The type parameter `T` represents the data that this lock protects. It is
/// required that `T` satisfies [`Sync`] to allow concurrent access through readers. The RAII guards
/// returned from the locking methods implement [`Deref`] (and [`DerefMut`]
/// for the `write` methods) to allow access to the content of the lock.
///
/// # Example
///
/// ```rust
/// use std::collections::HashMap;
/// use orengine::sync::AsyncRWLock;
///
/// # async fn write_to_the_dump_file(key: usize, value: usize) {}
///
/// async fn dump_storage<S: AsyncRWLock<HashMap<usize, usize>>>(storage: &S) {
/// let mut read_guard = storage.read().await;
///
/// for (key, value) in read_guard.iter() {
/// write_to_the_dump_file(*key, *value).await;
/// }
///
/// // read lock is released when `guard` goes out of scope
/// }
/// ```