Skip to content

Commit 4e44751

Browse files
committed
Deduplicate Storage and StorageHandle fields
1 parent 1887089 commit 4e44751

File tree

1 file changed

+23
-43
lines changed

1 file changed

+23
-43
lines changed

src/storage.rs

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

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

4052
impl<Db> StorageHandle<Db> {
4153
pub fn into_storage(self) -> Storage<Db> {
42-
let StorageHandle {
43-
zalsa_impl,
44-
coordinate,
45-
phantom,
46-
} = self;
4754
Storage {
48-
zalsa_impl,
49-
coordinate,
50-
phantom,
55+
handle: self,
5156
zalsa_local: ZalsaLocal::new(),
5257
}
5358
}
@@ -67,20 +72,10 @@ pub unsafe trait HasStorage: Database + Clone + Sized {
6772

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

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

8681
struct Coordinate {
@@ -97,13 +92,8 @@ impl RefUnwindSafe for Coordinate {}
9792
impl<Db: Database> Default for Storage<Db> {
9893
fn default() -> Self {
9994
Self {
100-
zalsa_impl: Arc::new(Zalsa::new::<Db>()),
101-
coordinate: CoordinateDrop(Arc::new(Coordinate {
102-
clones: Mutex::new(1),
103-
cvar: Default::default(),
104-
})),
95+
handle: StorageHandle::default(),
10596
zalsa_local: ZalsaLocal::new(),
106-
phantom: PhantomData,
10797
}
10898
}
10999
}
@@ -113,16 +103,10 @@ impl<Db: Database> Storage<Db> {
113103
/// and [`std::panic::UnwindSafe`].
114104
pub fn into_zalsa_handle(self) -> StorageHandle<Db> {
115105
let Storage {
116-
zalsa_impl,
117-
coordinate,
118-
phantom,
106+
handle,
119107
zalsa_local: _,
120108
} = self;
121-
StorageHandle {
122-
zalsa_impl,
123-
coordinate,
124-
phantom,
125-
}
109+
handle
126110
}
127111

128112
// ANCHOR: cancel_other_workers
@@ -132,29 +116,29 @@ impl<Db: Database> Storage<Db> {
132116
/// This could deadlock if there is a single worker with two handles to the
133117
/// same database!
134118
fn cancel_others(&self, db: &Db) {
135-
self.zalsa_impl.set_cancellation_flag();
119+
self.handle.zalsa_impl.set_cancellation_flag();
136120

137121
db.salsa_event(&|| Event::new(EventKind::DidSetCancellationFlag));
138122

139-
let mut clones = self.coordinate.clones.lock();
123+
let mut clones = self.handle.coordinate.clones.lock();
140124
while *clones != 1 {
141-
self.coordinate.cvar.wait(&mut clones);
125+
self.handle.coordinate.cvar.wait(&mut clones);
142126
}
143127
}
144128
// ANCHOR_END: cancel_other_workers
145129
}
146130

147131
unsafe impl<T: HasStorage> ZalsaDatabase for T {
148132
fn zalsa(&self) -> &Zalsa {
149-
&self.storage().zalsa_impl
133+
&self.storage().handle.zalsa_impl
150134
}
151135

152136
fn zalsa_mut(&mut self) -> &mut Zalsa {
153137
self.storage().cancel_others(self);
154138

155139
let storage = self.storage_mut();
156140
// The ref count on the `Arc` should now be 1
157-
let zalsa_mut = Arc::get_mut(&mut storage.zalsa_impl).unwrap();
141+
let zalsa_mut = Arc::get_mut(&mut storage.handle.zalsa_impl).unwrap();
158142
zalsa_mut.new_revision();
159143
zalsa_mut
160144
}
@@ -170,13 +154,9 @@ unsafe impl<T: HasStorage> ZalsaDatabase for T {
170154

171155
impl<Db: Database> Clone for Storage<Db> {
172156
fn clone(&self) -> Self {
173-
*self.coordinate.clones.lock() += 1;
174-
175157
Self {
176-
zalsa_impl: self.zalsa_impl.clone(),
177-
coordinate: CoordinateDrop(Arc::clone(&self.coordinate)),
158+
handle: self.handle.clone(),
178159
zalsa_local: ZalsaLocal::new(),
179-
phantom: PhantomData,
180160
}
181161
}
182162
}

0 commit comments

Comments
 (0)