Skip to content

Commit e34722a

Browse files
authored
refactor: Remove unnecessary query stack acess in block_on (#771)
Thanks to the fixpoint changes this has become obsolete actually
1 parent ea99739 commit e34722a

File tree

3 files changed

+20
-60
lines changed

3 files changed

+20
-60
lines changed

Diff for: src/runtime.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::{
2-
panic::AssertUnwindSafe,
32
sync::atomic::{AtomicBool, Ordering},
43
thread::ThreadId,
54
};
65

76
use parking_lot::Mutex;
87

98
use crate::{
10-
durability::Durability, key::DatabaseKeyIndex, table::Table, zalsa_local::ZalsaLocal,
11-
Cancelled, Database, Event, EventKind, Revision,
9+
durability::Durability, key::DatabaseKeyIndex, table::Table, Cancelled, Database, Event,
10+
EventKind, Revision,
1211
};
1312

1413
use self::dependency_graph::DependencyGraph;
@@ -169,8 +168,7 @@ impl Runtime {
169168
/// cancelled, so this function will panic with a `Cancelled` value.
170169
pub(crate) fn block_on<QueryMutexGuard>(
171170
&self,
172-
db: &dyn Database,
173-
local_state: &ZalsaLocal,
171+
db: &(impl Database + ?Sized),
174172
database_key: DatabaseKeyIndex,
175173
other_id: ThreadId,
176174
query_mutex_guard: QueryMutexGuard,
@@ -189,21 +187,8 @@ impl Runtime {
189187
})
190188
});
191189

192-
// `DependencyGraph::block_on` does not panic, so we cannot enter an inconsistent state.
193-
let dg = AssertUnwindSafe(dg);
194-
// `DependencyGraph::block_on` does not panic, nor does it read from query_mutex_guard, so
195-
// we cannot enter an inconsistent state for this parameter.
196-
let query_mutex_guard = AssertUnwindSafe(query_mutex_guard);
197-
let result = local_state.with_query_stack(|stack| {
198-
DependencyGraph::block_on(
199-
{ dg }.0,
200-
thread_id,
201-
database_key,
202-
other_id,
203-
stack,
204-
{ query_mutex_guard }.0,
205-
)
206-
});
190+
let result =
191+
DependencyGraph::block_on(dg, thread_id, database_key, other_id, query_mutex_guard);
207192

208193
match result {
209194
WaitResult::Completed => BlockResult::Completed,

Diff for: src/runtime/dependency_graph.rs

+11-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::thread::ThreadId;
22

3-
use crate::active_query::ActiveQuery;
43
use crate::key::DatabaseKeyIndex;
54
use crate::runtime::WaitResult;
65
use parking_lot::MutexGuard;
@@ -59,12 +58,9 @@ impl DependencyGraph {
5958
from_id: ThreadId,
6059
database_key: DatabaseKeyIndex,
6160
to_id: ThreadId,
62-
from_stack: &mut [ActiveQuery],
6361
query_mutex_guard: QueryMutexGuard,
6462
) -> WaitResult {
65-
// SAFETY: We are blocking until the result is removed from `DependencyGraph::wait_results`
66-
// and as such we are keeping `from_stack` alive.
67-
let condvar = unsafe { me.add_edge(from_id, database_key, to_id, from_stack) };
63+
let edge = me.add_edge(from_id, database_key, to_id);
6864

6965
// Release the mutex that prevents `database_key`
7066
// from completing, now that the edge has been added.
@@ -75,30 +71,23 @@ impl DependencyGraph {
7571
debug_assert!(!me.edges.contains_key(&from_id));
7672
return result;
7773
}
78-
condvar.wait(&mut me);
74+
edge.wait(&mut me);
7975
}
8076
}
8177

8278
/// Helper for `block_on`: performs actual graph modification
8379
/// to add a dependency edge from `from_id` to `to_id`, which is
8480
/// computing `database_key`.
85-
///
86-
/// # Safety
87-
///
88-
/// The caller needs to keep `from_stack`/`'aq`` alive until `from_id` has been removed from the `wait_results`.
89-
// This safety invariant is consumed by the `Edge` struct
90-
unsafe fn add_edge<'aq>(
81+
fn add_edge(
9182
&mut self,
9283
from_id: ThreadId,
9384
database_key: DatabaseKeyIndex,
9485
to_id: ThreadId,
95-
from_stack: &'aq mut [ActiveQuery],
96-
) -> edge::EdgeGuard<'aq> {
86+
) -> edge::EdgeGuard {
9787
assert_ne!(from_id, to_id);
9888
debug_assert!(!self.edges.contains_key(&from_id));
9989
debug_assert!(!self.depends_on(to_id, from_id));
100-
// SAFETY: The caller is responsible for ensuring that the `EdgeGuard` outlives the `Edge`.
101-
let (edge, guard) = unsafe { edge::Edge::new(to_id, from_stack) };
90+
let (edge, guard) = edge::Edge::new(to_id);
10291
self.edges.insert(from_id, edge);
10392
self.query_dependents
10493
.entry(database_key)
@@ -138,11 +127,11 @@ impl DependencyGraph {
138127
}
139128

140129
mod edge {
141-
use std::{marker::PhantomData, sync::Arc, thread::ThreadId};
130+
use std::{sync::Arc, thread::ThreadId};
142131

143132
use parking_lot::MutexGuard;
144133

145-
use crate::{active_query::ActiveQuery, runtime::dependency_graph::DependencyGraph};
134+
use crate::runtime::dependency_graph::DependencyGraph;
146135

147136
#[derive(Debug)]
148137
pub(super) struct Edge {
@@ -153,35 +142,24 @@ mod edge {
153142
condvar: Arc<parking_lot::Condvar>,
154143
}
155144

156-
pub struct EdgeGuard<'aq> {
145+
pub struct EdgeGuard {
157146
condvar: Arc<parking_lot::Condvar>,
158-
// Inform the borrow checker that the edge stack is borrowed until the guard is released.
159-
// This is necessary to ensure that the stack is not modified by the caller of
160-
// `DependencyGraph::add_edge` after the call returns.
161-
_pd: PhantomData<&'aq mut [ActiveQuery]>,
162147
}
163148

164-
impl EdgeGuard<'_> {
149+
impl EdgeGuard {
165150
pub fn wait(&self, mutex_guard: &mut MutexGuard<'_, DependencyGraph>) {
166151
self.condvar.wait(mutex_guard)
167152
}
168153
}
169154

170155
impl Edge {
171-
pub(super) unsafe fn new(
172-
blocked_on_id: ThreadId,
173-
stack: &mut [ActiveQuery],
174-
) -> (Self, EdgeGuard<'_>) {
175-
_ = stack;
156+
pub(super) fn new(blocked_on_id: ThreadId) -> (Self, EdgeGuard) {
176157
let condvar = Arc::new(parking_lot::Condvar::new());
177158
let edge = Self {
178159
blocked_on_id,
179160
condvar: condvar.clone(),
180161
};
181-
let edge_guard = EdgeGuard {
182-
condvar,
183-
_pd: PhantomData,
184-
};
162+
let edge_guard = EdgeGuard { condvar };
185163
(edge, edge_guard)
186164
}
187165

Diff for: src/table/sync.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,10 @@ impl SyncTable {
7171
// boolean is to decide *whether* to acquire the lock,
7272
// not to gate future atomic reads.
7373
*anyone_waiting = true;
74-
match zalsa.runtime().block_on(
75-
db.as_dyn_database(),
76-
db.zalsa_local(),
77-
database_key_index,
78-
*other_id,
79-
syncs,
80-
) {
74+
match zalsa
75+
.runtime()
76+
.block_on(db, database_key_index, *other_id, syncs)
77+
{
8178
BlockResult::Completed => ClaimResult::Retry,
8279
BlockResult::Cycle => ClaimResult::Cycle,
8380
}

0 commit comments

Comments
 (0)