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
// 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://github.com/openpitkit and the OWNERS file for details.
//! Locking policy that synchronizes only the index domain.
use PhantomData;
use ;
use crateAnyKey;
use crate;
use crate;
/// Sync-mode-aware shared handle for [`IndexLocking`]-based engines.
///
/// Mirrors `AccountSyncHandle`: wraps `Arc<T>` for thread-safe
/// reference-counting while being deliberately `!Sync` (via
/// `PhantomData<Cell<()>>`) to match the `AccountSync` engine contract
/// that guarantees sequential per-handle invocation.
>>,
);
// SAFETY: `IndexLocking` engines guarantee sequential per-handle
// invocation: only one logical thread holds a reference at a time, so
// shared `&T` never races with mutation. The `Arc` refcount is
// thread-safe. `!Sync` (enforced by `PhantomData<Cell<()>>`) prevents
// sharing `&IndexShared<T>` across threads, matching the `AccountSync`
// engine contract.
unsafe
/// Locking policy factory that synchronizes the **index** but leaves
/// values unsynchronized.
///
/// A [`Storage`](crate::storage::Storage) configured with `IndexLocking`
/// allows concurrent inserts, removals, and lookups of keys (the index
/// is protected by a reader-writer lock), but reads and writes of the
/// values themselves are not synchronized. The caller must ensure
/// values are either thread-safe internally or accessed under their own
/// external discipline (for example, only one thread mutates values).
///
/// # Aliasing safety
///
/// The closure-based access methods on
/// [`Storage`](crate::storage::Storage) confine references to the call
/// that produced them. Read-from-read re-entry is allowed because it
/// creates only shared references. Any re-entry path involving a write
/// hold would alias `&mut Value` while another values borrow is still
/// live; it is detected by `Storage` in debug builds and panics with a
/// clear message. Release builds skip the check.
///
/// # Compile-time key bound
///
/// The `KeyBound` type parameter selects which keys this factory will
/// produce storages for. The default,
/// [`AnyKey`](crate::storage::AnyKey), accepts any
/// [`Hash`](std::hash::Hash) + [`Eq`] key. Embeddings can substitute a
/// stricter marker that implements
/// [`IndexKeyBound`](crate::storage::IndexKeyBound) for only a subset
/// of keys; attempting to construct a storage with a key the marker
/// does not admit then becomes a compile-time error.
///
/// `KeyBound` carries no runtime data (it lives behind a
/// `PhantomData<fn() -> KeyBound>` so it does not affect [`Send`] /
/// [`Sync`] inheritance) and exists purely to gate
/// [`StorageBuilder::create_for_bound_key`](crate::storage::StorageBuilder::create_for_bound_key).
///
/// # Thread safety
///
/// Storages built with `IndexLocking` are [`Send`] (provided
/// `Key: Send` and `Value: Send`) but **not** [`Sync`]. The values
/// domain is left unsynchronized, so sharing `&Storage` across threads
/// would race on values; the type system rejects that at compile time.
/// To use the storage from multiple threads, transfer ownership (move
/// it across a channel, wrap in a `Mutex<Storage<..>>`, etc.) or
/// switch to [`FullLocking`](crate::storage::FullLocking), which
/// implements
/// [`FullySynchronized`](crate::storage::FullySynchronized).
///
/// The following must therefore fail to compile:
///
/// ```compile_fail
/// use std::sync::Arc;
/// use std::thread;
/// use openpit::Engine;
///
/// let builder = Engine::builder::<(), ()>().account_sync();
/// let storage = Arc::new(builder.storage_builder().create_for_bound_key::<u64, u64>());
/// let s2 = Arc::clone(&storage);
/// thread::spawn(move || {
/// let _ = s2.with(&1, |v| *v);
/// });
/// ```
/// Concrete index-only policy installed in storages built from
/// [`IndexLocking`].
///
/// Not re-exported from the crate; name it via the associated type if
/// needed: `<IndexLocking as LockingPolicyFactory>::Policy`.
// SAFETY: `read_index`/`write_index` delegate to a
// `parking_lot::RwLock`, which upholds the standard reader-writer
// invariants. The values domain has no synchronization, but the
// storage-level public API confines value references to closure calls.
// Values guards are no-ops because this policy deliberately leaves the
// values domain unsynchronized.
unsafe