@@ -24,6 +24,18 @@ pub struct StorageHandle<Db> {
24
24
phantom : PhantomData < fn ( ) -> Db > ,
25
25
}
26
26
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
+
27
39
impl < Db : Database > Default for StorageHandle < Db > {
28
40
fn default ( ) -> Self {
29
41
Self {
@@ -39,15 +51,8 @@ impl<Db: Database> Default for StorageHandle<Db> {
39
51
40
52
impl < Db > StorageHandle < Db > {
41
53
pub fn into_storage ( self ) -> Storage < Db > {
42
- let StorageHandle {
43
- zalsa_impl,
44
- coordinate,
45
- phantom,
46
- } = self ;
47
54
Storage {
48
- zalsa_impl,
49
- coordinate,
50
- phantom,
55
+ handle : self ,
51
56
zalsa_local : ZalsaLocal :: new ( ) ,
52
57
}
53
58
}
@@ -67,20 +72,10 @@ pub unsafe trait HasStorage: Database + Clone + Sized {
67
72
68
73
/// Concrete implementation of the [`Database`] trait with local state that can be used to drive computations.
69
74
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 > ,
78
76
79
77
/// Per-thread state
80
78
zalsa_local : zalsa_local:: ZalsaLocal ,
81
-
82
- /// We store references to `Db`
83
- phantom : PhantomData < fn ( ) -> Db > ,
84
79
}
85
80
86
81
struct Coordinate {
@@ -97,13 +92,8 @@ impl RefUnwindSafe for Coordinate {}
97
92
impl < Db : Database > Default for Storage < Db > {
98
93
fn default ( ) -> Self {
99
94
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 ( ) ,
105
96
zalsa_local : ZalsaLocal :: new ( ) ,
106
- phantom : PhantomData ,
107
97
}
108
98
}
109
99
}
@@ -113,16 +103,10 @@ impl<Db: Database> Storage<Db> {
113
103
/// and [`std::panic::UnwindSafe`].
114
104
pub fn into_zalsa_handle ( self ) -> StorageHandle < Db > {
115
105
let Storage {
116
- zalsa_impl,
117
- coordinate,
118
- phantom,
106
+ handle,
119
107
zalsa_local : _,
120
108
} = self ;
121
- StorageHandle {
122
- zalsa_impl,
123
- coordinate,
124
- phantom,
125
- }
109
+ handle
126
110
}
127
111
128
112
// ANCHOR: cancel_other_workers
@@ -132,29 +116,29 @@ impl<Db: Database> Storage<Db> {
132
116
/// This could deadlock if there is a single worker with two handles to the
133
117
/// same database!
134
118
fn cancel_others ( & self , db : & Db ) {
135
- self . zalsa_impl . set_cancellation_flag ( ) ;
119
+ self . handle . zalsa_impl . set_cancellation_flag ( ) ;
136
120
137
121
db. salsa_event ( & || Event :: new ( EventKind :: DidSetCancellationFlag ) ) ;
138
122
139
- let mut clones = self . coordinate . clones . lock ( ) ;
123
+ let mut clones = self . handle . coordinate . clones . lock ( ) ;
140
124
while * clones != 1 {
141
- self . coordinate . cvar . wait ( & mut clones) ;
125
+ self . handle . coordinate . cvar . wait ( & mut clones) ;
142
126
}
143
127
}
144
128
// ANCHOR_END: cancel_other_workers
145
129
}
146
130
147
131
unsafe impl < T : HasStorage > ZalsaDatabase for T {
148
132
fn zalsa ( & self ) -> & Zalsa {
149
- & self . storage ( ) . zalsa_impl
133
+ & self . storage ( ) . handle . zalsa_impl
150
134
}
151
135
152
136
fn zalsa_mut ( & mut self ) -> & mut Zalsa {
153
137
self . storage ( ) . cancel_others ( self ) ;
154
138
155
139
let storage = self . storage_mut ( ) ;
156
140
// 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 ( ) ;
158
142
zalsa_mut. new_revision ( ) ;
159
143
zalsa_mut
160
144
}
@@ -170,13 +154,9 @@ unsafe impl<T: HasStorage> ZalsaDatabase for T {
170
154
171
155
impl < Db : Database > Clone for Storage < Db > {
172
156
fn clone ( & self ) -> Self {
173
- * self . coordinate . clones . lock ( ) += 1 ;
174
-
175
157
Self {
176
- zalsa_impl : self . zalsa_impl . clone ( ) ,
177
- coordinate : CoordinateDrop ( Arc :: clone ( & self . coordinate ) ) ,
158
+ handle : self . handle . clone ( ) ,
178
159
zalsa_local : ZalsaLocal :: new ( ) ,
179
- phantom : PhantomData ,
180
160
}
181
161
}
182
162
}
0 commit comments