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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright The Pit Project Owners. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Please see https://openpit.dev and the OWNERS file for details.
//! Engine-level synchronization configuration.
//!
//! A [`SyncMode`] selects every synchronization aspect of an
//! [`Engine`](crate::Engine): the strong and weak handle pointers, the
//! required trait-object shape for registered policies, and the storage
//! locking factory used by both user policies and engine-internal storage.
use Deref;
use crateAccountId;
use cratePreTradePolicy;
use crate;
/// Synchronization mode for an [`Engine`](crate::Engine).
///
/// Implement this trait to configure all engine synchronization choices at
/// once. Built-in modes cover the standard regimes:
///
/// - [`LocalSync`]: `!Send + !Sync` engine handle, no storage locking, and
/// `'static` policies.
/// - [`FullSync`]: `Send + Sync` engine handle, fully locked storage, and
/// `Send + Sync + 'static` policies.
/// - [`AccountSync`]: `Send + !Sync` engine handle, account-keyed storage,
/// and `Send + 'static` policies.
///
/// # Custom modes
///
/// Custom modes are open to downstream crates. If
/// [`PreTradePolicyObject`](Self::PreTradePolicyObject) resolves to one of
/// the three built-in policy-object shapes, the engine builder's
/// [`pre_trade`](crate::SyncedEngineBuilder::pre_trade) method works through
/// the blanket [`IntoPolicyObject`](crate::IntoPolicyObject) implementations.
/// A custom object shape must provide its own `IntoPolicyObject` conversion.
/// Single-thread synchronization mode.
///
/// The engine handle is `!Send + !Sync`; keep it on the OS thread that
/// created it. Registered policies require only `'static`, so non-Send state
/// is supported.
///
/// Storage tables use [`NoLocking`](crate::storage::NoLocking), with no
/// runtime synchronization overhead.
///
/// # Examples
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use openpit::{Engine, LocalEngine};
/// use openpit::pretrade::policies::OrderValidationPolicy;
/// use openpit::OrderOperation;
///
/// let engine: LocalEngine<OrderOperation> = Engine::builder()
/// .no_sync()
/// .pre_trade(OrderValidationPolicy::new())
/// .build()?;
/// # Ok(())
/// # }
/// ```
///
/// `LocalEngine<T>` is `!Send + !Sync`; attempting to send it across threads
/// does not compile:
///
/// ```compile_fail
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use openpit::{Engine, OrderOperation};
/// use openpit::pretrade::policies::OrderValidationPolicy;
///
/// fn require_send<T: Send>(_: T) {}
///
/// let engine = Engine::builder()
/// .sync(openpit::LocalSync)
/// .pre_trade(OrderValidationPolicy::new())
/// .build()?;
/// require_send(engine); // compile error: LocalSync engines are !Send
/// # Ok(())
/// # }
/// ```
;
/// Full thread-safety synchronization mode.
///
/// The engine handle inherits `Send + Sync` from its inner state and can be
/// shared across threads when registered policies are `Send + Sync`. Storage
/// tables use [`FullLocking`](crate::storage::FullLocking). Each individual
/// storage access is synchronized; a multi-step engine operation does not hold
/// one account-wide lock from entry to return. Calls for the same account may
/// therefore interleave, and rollback paths must preserve accepted concurrent
/// deltas rather than restoring mutable snapshots. Operations that require a
/// stronger invariant provide their own narrowly scoped synchronization.
///
/// # Examples
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use openpit::{Engine, FullSyncEngine};
/// use openpit::pretrade::policies::OrderValidationPolicy;
/// use openpit::OrderOperation;
/// use std::sync::Arc;
///
/// let engine: Arc<FullSyncEngine<OrderOperation>> = Arc::new(
/// Engine::builder()
/// .full_sync()
/// .pre_trade(OrderValidationPolicy::new())
/// .build()?,
/// );
/// # Ok(())
/// # }
/// ```
;
/// Engine handle handed out for account-sharded sequential cross-thread
/// invocation.
///
/// `Send`: ownership of the engine handle may move between OS threads
/// sequentially, with the caller serialising per-handle invocation (one
/// active public-method call per handle at a time). Concurrent invocation
/// on the same handle is forbidden by contract and not supported at the
/// type level (the handle is `!Sync`).
///
/// # Pure Rust vs binding-layer contract
///
/// This pure-Rust handle does **not** allow concurrent invocation from
/// multiple threads even when calls are partitioned by account. The `!Sync`
/// bound enforces this at compile time. Rust SDK clients who need
/// per-account concurrency must shard ownership: place each
/// `Arc<Mutex<AccountSyncEngine<...>>>` (or equivalent) behind a per-account
/// lock, or
/// move handles between threads sequentially with explicit serialisation.
>>,
);
// SAFETY:
// Sending an `Arc<T>` between threads requires `T: Send + Sync` for
// `std::sync::Arc<T>: Send`. We claim `Send` with only `T: Send`
// because the SDK's account-sync threading contract requires the
// caller to serialise per-handle invocation: ownership of the handle
// may move between OS threads sequentially, but only one thread
// observes `&EngineInner` at any moment. The `Arc` refcount is
// thread-safe; the inner state is observed by at most one thread at
// a time per the contract.
//
// The marker field deliberately keeps this handle `!Sync`: concurrent
// shared access is not supported under account-sharded synchronization,
// and the type system reflects that. Callers attempting
// `Arc<AccountSyncEngine<...>>` and concurrent invocation
// receive a compile error.
unsafe
/// Weak counterpart of [`AccountSyncHandle`].
>>,
);
// SAFETY: same sequential ownership-transfer contract as
// `AccountSyncHandle` above. The weak handle owns no engine state;
// it only carries the thread-safe weak reference count.
unsafe
/// Account-keyed synchronization mode.
///
/// The engine handle is `Send + !Sync`: ownership may move between OS
/// threads sequentially, but concurrent invocation on the same handle is not
/// supported. Registered policies must be `Send + 'static`.
///
/// The engine takes no lock for its exclusive operations (see the threading
/// section on [`Engine`](crate::Engine)): the caller runs each with no other
/// call on the engine in flight. This handle's sequential contract already
/// gives that; a binding layer that admits concurrent calls for distinct
/// accounts passes the obligation on to its own caller.
///
/// Storage tables use
/// [`IndexLocking<AccountKeyConstraint>`](crate::storage::IndexLocking),
/// which requires every storage key to identify an account.
;