Skip to content

Commit 25154f7

Browse files
committed
Deduplicate Storage and StorageHandle fields
1 parent db30255 commit 25154f7

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

src/storage.rs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ pub struct StorageHandle<Db> {
2525
phantom: PhantomData<fn() -> Db>,
2626
}
2727

28+
impl<Db> Clone for StorageHandle<Db> {
29+
fn clone(&self) -> Self {
30+
*self.coordinate.clones.lock() += 1;
31+
32+
Self {
33+
zalsa_impl: self.zalsa_impl.clone(),
34+
coordinate: CoordinateDrop(Arc::clone(&self.coordinate)),
35+
phantom: PhantomData,
36+
}
37+
}
38+
}
39+
2840
impl<Db: Database> Default for StorageHandle<Db> {
2941
fn default() -> Self {
3042
Self {
@@ -40,15 +52,8 @@ impl<Db: Database> Default for StorageHandle<Db> {
4052

4153
impl<Db> StorageHandle<Db> {
4254
pub fn into_storage(self) -> Storage<Db> {
43-
let StorageHandle {
44-
zalsa_impl,
45-
coordinate,
46-
phantom,
47-
} = self;
4855
Storage {
49-
zalsa_impl,
50-
coordinate,
51-
phantom,
56+
handle: self,
5257
zalsa_local: ZalsaLocal::new(),
5358
}
5459
}
@@ -68,20 +73,10 @@ pub unsafe trait HasStorage: Database + Clone + Sized {
6873

6974
/// Concrete implementation of the [`Database`] trait with local state that can be used to drive computations.
7075
pub struct Storage<Db> {
71-
// Note: Drop order is important, zalsa_impl needs to drop before coordinate
72-
/// Reference to the database.
73-
zalsa_impl: Arc<Zalsa>,
74-
75-
// Note: Drop order is important, coordinate needs to drop after zalsa_impl
76-
/// Coordination data for cancellation of other handles when `zalsa_mut` is called.
77-
/// This could be stored in Zalsa but it makes things marginally cleaner to keep it separate.
78-
coordinate: CoordinateDrop,
76+
handle: StorageHandle<Db>,
7977

8078
/// Per-thread state
8179
zalsa_local: zalsa_local::ZalsaLocal,
82-
83-
/// We store references to `Db`
84-
phantom: PhantomData<fn() -> Db>,
8580
}
8681

8782
struct Coordinate {
@@ -98,13 +93,8 @@ impl RefUnwindSafe for Coordinate {}
9893
impl<Db: Database> Default for Storage<Db> {
9994
fn default() -> Self {
10095
Self {
101-
zalsa_impl: Arc::new(Zalsa::new::<Db>()),
102-
coordinate: CoordinateDrop(Arc::new(Coordinate {
103-
clones: Mutex::new(1),
104-
cvar: Default::default(),
105-
})),
96+
handle: StorageHandle::default(),
10697
zalsa_local: ZalsaLocal::new(),
107-
phantom: PhantomData,
10898
}
10999
}
110100
}
@@ -114,16 +104,10 @@ impl<Db: Database> Storage<Db> {
114104
/// and [`std::panic::UnwindSafe`].
115105
pub fn into_zalsa_handle(self) -> StorageHandle<Db> {
116106
let Storage {
117-
zalsa_impl,
118-
coordinate,
119-
phantom,
107+
handle,
120108
zalsa_local: _,
121109
} = self;
122-
StorageHandle {
123-
zalsa_impl,
124-
coordinate,
125-
phantom,
126-
}
110+
handle
127111
}
128112

129113
pub fn debug_input_entries<T>(&self) -> impl Iterator<Item = &input::Value<T>>
@@ -171,7 +155,7 @@ impl<Db: Database> Storage<Db> {
171155
/// possible as `zalsa_impl` only becomes
172156
/// `None` once we are in the `Drop` impl.
173157
fn zalsa_impl(&self) -> &Arc<Zalsa> {
174-
&self.zalsa_impl
158+
&self.handle.zalsa_impl
175159
}
176160

177161
// ANCHOR: cancel_other_workers
@@ -181,29 +165,29 @@ impl<Db: Database> Storage<Db> {
181165
/// This could deadlock if there is a single worker with two handles to the
182166
/// same database!
183167
fn cancel_others(&self, db: &Db) {
184-
self.zalsa_impl.set_cancellation_flag();
168+
self.handle.zalsa_impl.set_cancellation_flag();
185169

186170
db.salsa_event(&|| Event::new(EventKind::DidSetCancellationFlag));
187171

188-
let mut clones = self.coordinate.clones.lock();
172+
let mut clones = self.handle.coordinate.clones.lock();
189173
while *clones != 1 {
190-
self.coordinate.cvar.wait(&mut clones);
174+
self.handle.coordinate.cvar.wait(&mut clones);
191175
}
192176
}
193177
// ANCHOR_END: cancel_other_workers
194178
}
195179

196180
unsafe impl<T: HasStorage> ZalsaDatabase for T {
197181
fn zalsa(&self) -> &Zalsa {
198-
&self.storage().zalsa_impl
182+
&self.storage().handle.zalsa_impl
199183
}
200184

201185
fn zalsa_mut(&mut self) -> &mut Zalsa {
202186
self.storage().cancel_others(self);
203187

204188
let storage = self.storage_mut();
205189
// The ref count on the `Arc` should now be 1
206-
let zalsa_mut = Arc::get_mut(&mut storage.zalsa_impl).unwrap();
190+
let zalsa_mut = Arc::get_mut(&mut storage.handle.zalsa_impl).unwrap();
207191
zalsa_mut.new_revision();
208192
zalsa_mut
209193
}
@@ -219,13 +203,9 @@ unsafe impl<T: HasStorage> ZalsaDatabase for T {
219203

220204
impl<Db: Database> Clone for Storage<Db> {
221205
fn clone(&self) -> Self {
222-
*self.coordinate.clones.lock() += 1;
223-
224206
Self {
225-
zalsa_impl: self.zalsa_impl.clone(),
226-
coordinate: CoordinateDrop(Arc::clone(&self.coordinate)),
207+
handle: self.handle.clone(),
227208
zalsa_local: ZalsaLocal::new(),
228-
phantom: PhantomData,
229209
}
230210
}
231211
}

0 commit comments

Comments
 (0)