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
// 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.
//! Locking policy traits.
//!
//! [`LockingPolicy`] abstracts over the synchronization scheme used by a
//! [`Storage`](super::Storage) instance. A storage owns one policy instance,
//! created by a [`LockingPolicyFactory`]. The factory is the type-level
//! configuration on a [`StorageBuilder`](super::StorageBuilder); each call to
//! [`StorageBuilder::create_for_bound_key`](super::StorageBuilder::create_for_bound_key) produces a fresh
//! policy via [`LockingPolicyFactory::create_policy`], so every storage has
//! its own private locks.
//!
//! # Lock domains
//!
//! A policy controls two independent lock domains:
//!
//! * **Index** — the set of keys present in the storage. Acquired
//! exclusively when keys are added or removed; acquired shared when an
//! existing key is looked up.
//! * **Values** — the bag of all values stored under those keys. Acquired
//! exclusively when any value is mutated; acquired shared when any value
//! is read. The key of the entry being accessed is passed as an argument
//! so that future implementations can apply per-key granularity; the
//! built-in policies ignore it.
//!
//! # Acquisition order
//!
//! Storage operations always acquire **index before values**. Implementations
//! must not introduce any other ordering, otherwise deadlocks are possible.
use PhantomData;
/// Synchronization strategy used by a [`Storage`](super::Storage).
///
/// A policy provides four lock acquisitions, one per `(domain, mode)` pair
/// where the domain is index or values and the mode is shared (multiple
/// concurrent holders, no mutators) or exclusive (single holder, no other
/// readers or writers).
///
/// The `read_values` and `write_values` methods receive the key of the
/// entry being accessed. The built-in implementations ignore this argument;
/// it is reserved for custom implementations that want to apply per-key
/// granularity. The argument is generic so that no `Key` type-parameter
/// propagates into the trait itself, keeping the trait object-capable in
/// principle and straightforward to implement for all key types at once.
///
/// # Safety
///
/// `Storage` relies on the policy to uphold the following invariants:
///
/// * While any [`Self::IndexSharedGuard`] is alive, no thread modifies the
/// set of keys present in the owning storage.
/// * While any [`Self::IndexExclusiveGuard`] is alive, no other thread holds
/// any index guard for the same storage.
/// * While any [`Self::ValuesSharedGuard`] is alive, no thread mutates any
/// value in the owning storage.
/// * While any [`Self::ValuesExclusiveGuard`] is alive, no other thread
/// holds any values guard for the same storage.
///
/// The built-in implementations supplied with this crate
/// ([`NoLocking`](super::NoLocking), [`IndexLocking`](super::IndexLocking)
/// and [`FullLocking`](super::FullLocking)) honour these invariants. Custom
/// implementations are responsible for their own correctness; getting the
/// invariants wrong leads to undefined behaviour.
///
/// `NoLocking` honours the invariants vacuously by making the storage
/// `!Sync`: the Rust type system guarantees that no two threads can ever
/// observe the storage at the same time. The caller is then responsible
/// for not creating overlapping mutable borrows within that single thread,
/// the same contract as for direct use of [`std::cell::UnsafeCell`].
pub unsafe
/// Marker that asserts a [`LockingPolicy`] synchronizes BOTH the index
/// domain AND the values domain.
///
/// A [`Storage`](super::Storage) is [`Sync`] only when its policy
/// implements `FullySynchronized`. Policies that synchronize only the
/// index domain (or nothing at all) leave the storage `Send` but
/// `!Sync`: clients may move ownership between threads or guard the
/// storage with their own [`Mutex`](std::sync::Mutex), but cannot share
/// `&Storage` concurrently from multiple threads.
///
/// # Safety
///
/// Implementor guarantees that, in addition to the [`LockingPolicy`]
/// invariants:
///
/// * [`LockingPolicy::read_values`] returns a guard that prevents
/// concurrent mutation of any value in the owning storage for the
/// guard's lifetime.
/// * [`LockingPolicy::write_values`] returns a guard that prevents
/// concurrent access (read or write) of any value in the owning
/// storage for the guard's lifetime.
///
/// Index-only synchronization (where `read_values` / `write_values`
/// are no-ops) does **not** satisfy this contract.
pub unsafe
/// Factory of [`LockingPolicy`] instances.
///
/// A factory is the type-level configuration parameter of
/// [`StorageBuilder`](super::StorageBuilder). Every call to
/// [`StorageBuilder::create_for_bound_key`](super::StorageBuilder::create_for_bound_key) invokes
/// [`Self::create_policy`] and hands the freshly-built policy to the new
/// storage. Storages built from the same builder therefore have completely
/// independent locks.
/// Marker that opts a type out of [`Send`] and [`Sync`].
///
/// Used by [`NoLocking`](super::NoLocking) to make the resulting storage
/// thread-local at compile time, since it provides no synchronization at
/// runtime.
pub type NotThreadSafe = ;