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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use crate::{Doc, Origin, Store, Transaction, TransactionMut};
use async_lock::futures::{Read, Write};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use thiserror::Error;
/// Trait implemented by [Doc] and shared types, used for carrying over the responsibilities of
/// creating new transactions, used as a unit of work in Yrs.
pub trait Transact {
/// Creates and returns a lightweight read-only transaction.
///
/// # Errors
///
/// While it's possible to have multiple read-only transactions active at the same time,
/// this method will return a [TransactionAcqError::SharedAcqFailed] error whenever called
/// while a read-write transaction (see: [Self::try_transact_mut]) is active at the same time.
fn try_transact(&self) -> Result<Transaction, TransactionAcqError>;
/// Creates and returns a read-write capable transaction. This transaction can be used to
/// mutate the contents of underlying document store and upon dropping or committing it may
/// subscription callbacks.
///
/// # Errors
///
/// Only one read-write transaction can be active at the same time. If any other transaction -
/// be it a read-write or read-only one - is active at the same time, this method will return
/// a [TransactionAcqError::ExclusiveAcqFailed] error.
fn try_transact_mut(&self) -> Result<TransactionMut, TransactionAcqError>;
/// Creates and returns a read-write capable transaction with an `origin` classifier attached.
/// This transaction can be used to mutate the contents of underlying document store and upon
/// dropping or committing it may subscription callbacks.
///
/// An `origin` may be used to identify context of operations made (example updates performed
/// locally vs. incoming from remote replicas) and it's used i.e. by [`UndoManager`][crate::undo::UndoManager].
///
/// # Errors
///
/// Only one read-write transaction can be active at the same time. If any other transaction -
/// be it a read-write or read-only one - is active at the same time, this method will return
/// a [TransactionAcqError::ExclusiveAcqFailed] error.
fn try_transact_mut_with<T>(&self, origin: T) -> Result<TransactionMut, TransactionAcqError>
where
T: Into<Origin>;
/// Creates and returns a read-write capable transaction with an `origin` classifier attached.
/// This transaction can be used to mutate the contents of underlying document store and upon
/// dropping or committing it may subscription callbacks.
///
/// An `origin` may be used to identify context of operations made (example updates performed
/// locally vs. incoming from remote replicas) and it's used i.e. by [`UndoManager`][crate::undo::UndoManager].
///
/// # Errors
///
/// Only one read-write transaction can be active at the same time. If any other transaction -
/// be it a read-write or read-only one - is active at the same time, this method will
/// block the thread until exclusive access can be acquired, unless building for wasm
/// which panics. If blocking is undesirable, use `try_transact_mut_with(origin).unwrap()` instead`.
fn transact_mut_with<T>(&self, origin: T) -> TransactionMut
where
T: Into<Origin>;
/// Creates and returns a lightweight read-only transaction.
///
/// # Errors
///
/// While it's possible to have multiple read-only transactions active at the same time,
/// this method will block the thread until exclusive access can be acquired, unless building for wasm
/// which panics. If blocking is undesirable, use `try_transact(origin).unwrap()` instead`.
fn transact(&self) -> Transaction;
/// Creates and returns a read-write capable transaction. This transaction can be used to
/// mutate the contents of underlying document store and upon dropping or committing it may
/// subscription callbacks.
///
/// # Errors
///
/// Only one read-write transaction can be active at the same time. If any other transaction -
/// be it a read-write or read-only one - is active at the same time, this method will block
/// the thread until exclusive access can be acquired, unless building for wasm
/// which panics. If blocking is undesirable, use `try_transact_mut(origin).unwrap()` instead`.
fn transact_mut(&self) -> TransactionMut;
}
impl Transact for Doc {
fn try_transact(&self) -> Result<Transaction, TransactionAcqError> {
match self.store.try_read() {
Some(store) => Ok(Transaction::new(store)),
None => Err(TransactionAcqError::SharedAcqFailed),
}
}
fn try_transact_mut(&self) -> Result<TransactionMut, TransactionAcqError> {
match self.store.try_write() {
Some(store) => Ok(TransactionMut::new(self.clone(), store, None)),
None => Err(TransactionAcqError::ExclusiveAcqFailed),
}
}
fn try_transact_mut_with<T>(&self, origin: T) -> Result<TransactionMut, TransactionAcqError>
where
T: Into<Origin>,
{
match self.store.try_write() {
Some(store) => Ok(TransactionMut::new(
self.clone(),
store,
Some(origin.into()),
)),
None => Err(TransactionAcqError::ExclusiveAcqFailed),
}
}
fn transact_mut_with<T>(&self, origin: T) -> TransactionMut
where
T: Into<Origin>,
{
let lock = self.store.write_blocking();
TransactionMut::new(self.clone(), lock, Some(origin.into()))
}
fn transact(&self) -> Transaction {
let lock = self.store.read_blocking();
Transaction::new(lock)
}
fn transact_mut(&self) -> TransactionMut {
let lock = self.store.write_blocking();
TransactionMut::new(self.clone(), lock, None)
}
}
/// Trait implemented by [Doc] and shared types, used for carrying over the responsibilities of
/// creating new transactions, used as a unit of work in Yrs.
pub trait AsyncTransact<'doc> {
type Read: Future<Output = Transaction<'doc>>;
type Write: Future<Output = TransactionMut<'doc>>;
fn transact(&'doc self) -> Self::Read;
fn transact_mut(&'doc self) -> Self::Write;
/// Creates and returns a read-write capable transaction with an `origin` classifier attached.
/// This transaction can be used to mutate the contents of underlying document store and upon
/// dropping or committing it may subscription callbacks.
///
/// An `origin` may be used to identify context of operations made (example updates performed
/// locally vs. incoming from remote replicas) and it's used i.e. by [`UndoManager`][crate::undo::UndoManager].
fn transact_mut_with<T>(&'doc self, origin: T) -> Self::Write
where
T: Into<Origin>;
}
impl<'doc> AsyncTransact<'doc> for Doc {
type Read = AcquireTransaction<'doc>;
type Write = AcquireTransactionMut<'doc>;
fn transact(&'doc self) -> Self::Read {
let fut = self.store.read_async();
AcquireTransaction { fut }
}
fn transact_mut(&'doc self) -> Self::Write {
let fut = self.store.write_async();
AcquireTransactionMut {
doc: self.clone(),
origin: None,
fut,
}
}
fn transact_mut_with<T>(&'doc self, origin: T) -> Self::Write
where
T: Into<Origin>,
{
let fut = self.store.write_async();
AcquireTransactionMut {
doc: self.clone(),
origin: Some(origin.into()),
fut,
}
}
}
pub struct AcquireTransaction<'doc> {
fut: Read<'doc, Store>,
}
impl<'doc> Unpin for AcquireTransaction<'doc> {}
impl<'doc> Future for AcquireTransaction<'doc> {
type Output = Transaction<'doc>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let pinned = unsafe { Pin::new_unchecked(&mut self.fut) };
pinned.poll(cx).map(Transaction::new)
}
}
pub struct AcquireTransactionMut<'doc> {
doc: Doc,
origin: Option<Origin>,
fut: Write<'doc, Store>,
}
impl<'doc> Unpin for AcquireTransactionMut<'doc> {}
impl<'doc> Future for AcquireTransactionMut<'doc> {
type Output = TransactionMut<'doc>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let pinned = unsafe { Pin::new_unchecked(&mut self.fut) };
match pinned.poll(cx) {
Poll::Ready(store) => {
let doc = self.doc.clone();
let origin = self.origin.take();
Poll::Ready(TransactionMut::new(doc, store, origin))
}
Poll::Pending => Poll::Pending,
}
}
}
#[derive(Error, Debug)]
pub enum TransactionAcqError {
#[error("Failed to acquire read-only transaction. Drop read-write transaction and retry.")]
SharedAcqFailed,
#[error("Failed to acquire read-write transaction. Drop other transactions and retry.")]
ExclusiveAcqFailed,
#[error("All references to a parent document containing this structure has been dropped.")]
DocumentDropped,
}
#[cfg(test)]
mod test {
use crate::{Doc, GetString, Text, Transact};
use rand::random;
use std::sync::{Arc, Barrier};
use std::time::{Duration, Instant};
#[test]
fn multi_thread_transact_mut() {
let doc = Doc::new();
let txt = doc.get_or_insert_text("text");
const N: usize = 3;
let barrier = Arc::new(Barrier::new(N + 1));
let start = Instant::now();
for _ in 0..N {
let d = doc.clone();
let t = txt.clone();
let b = barrier.clone();
std::thread::spawn(move || {
// let mut txn = d.try_transact_mut().unwrap(); // this will hang forever
let mut txn = d.transact_mut();
let n = random::<u64>() % 5;
std::thread::sleep(Duration::from_millis(n * 100));
t.insert(&mut txn, 0, "a");
drop(txn);
b.wait();
});
}
barrier.wait();
println!("{} threads executed in {:?}", N, Instant::now() - start);
let expected: String = (0..N).map(|_| 'a').collect();
let txn = doc.transact();
let str = txt.get_string(&txn);
assert_eq!(str, expected);
}
}