mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-06 09:28:07 +02:00
rusticl/event: use Weak refs for dependencies
This fixes a potential stack overflow when the dep chain of events gets
too long and droped all at the same time.
Cc: mesa-stable
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/29089>
(cherry picked from commit a45f199086)
This commit is contained in:
parent
33d6e6f9a2
commit
38485a49f4
3 changed files with 19 additions and 11 deletions
|
|
@ -894,7 +894,7 @@
|
|||
"description": "rusticl/event: use Weak refs for dependencies",
|
||||
"nominated": true,
|
||||
"nomination_type": 0,
|
||||
"resolution": 0,
|
||||
"resolution": 1,
|
||||
"main_sha": null,
|
||||
"because_sha": null,
|
||||
"notes": null
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use std::sync::Arc;
|
|||
use std::sync::Condvar;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::MutexGuard;
|
||||
use std::sync::Weak;
|
||||
use std::time::Duration;
|
||||
|
||||
// we assert that those are a continous range of numbers so we won't have to use HashMaps
|
||||
|
|
@ -48,7 +49,8 @@ pub struct Event {
|
|||
pub context: Arc<Context>,
|
||||
pub queue: Option<Arc<Queue>>,
|
||||
pub cmd_type: cl_command_type,
|
||||
pub deps: Vec<Arc<Event>>,
|
||||
// using a Weak ref so we don't cause stack overflows in the `drop` impl
|
||||
pub deps: Vec<Weak<Event>>,
|
||||
state: Mutex<EventMutState>,
|
||||
cv: Condvar,
|
||||
}
|
||||
|
|
@ -66,6 +68,7 @@ impl Event {
|
|||
deps: Vec<Arc<Event>>,
|
||||
work: EventSig,
|
||||
) -> Arc<Event> {
|
||||
let deps = deps.iter().map(Arc::downgrade).collect();
|
||||
Arc::new(Self {
|
||||
base: CLObjectBase::new(),
|
||||
context: queue.context.clone(),
|
||||
|
|
@ -238,14 +241,18 @@ impl Event {
|
|||
}
|
||||
}
|
||||
|
||||
fn deep_unflushed_deps_impl<'a>(&'a self, result: &mut HashSet<&'a Event>) {
|
||||
pub fn deps(&self) -> impl Iterator<Item = Arc<Self>> + Clone + '_ {
|
||||
self.deps.iter().filter_map(Weak::upgrade)
|
||||
}
|
||||
|
||||
fn deep_unflushed_deps_impl(self: &Arc<Self>, result: &mut HashSet<Arc<Event>>) {
|
||||
if self.status() <= CL_SUBMITTED as i32 {
|
||||
return;
|
||||
}
|
||||
|
||||
// only scan dependencies if it's a new one
|
||||
if result.insert(self) {
|
||||
for e in &self.deps {
|
||||
if result.insert(Arc::clone(self)) {
|
||||
for e in self.deps() {
|
||||
e.deep_unflushed_deps_impl(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -253,7 +260,7 @@ impl Event {
|
|||
|
||||
/// does a deep search and returns a list of all dependencies including `events` which haven't
|
||||
/// been flushed out yet
|
||||
pub fn deep_unflushed_deps(events: &[Arc<Event>]) -> HashSet<&Event> {
|
||||
pub fn deep_unflushed_deps(events: &[Arc<Event>]) -> HashSet<Arc<Event>> {
|
||||
let mut result = HashSet::new();
|
||||
|
||||
for e in events {
|
||||
|
|
|
|||
|
|
@ -127,16 +127,17 @@ impl Queue {
|
|||
let mut flushed = Vec::new();
|
||||
|
||||
for e in new_events {
|
||||
let deps_iter = e.deps();
|
||||
|
||||
// If we hit any deps from another queue, flush so we don't risk a dead
|
||||
// lock.
|
||||
if e.deps.iter().any(|ev| ev.queue != e.queue) {
|
||||
// lock. Also clone the iter here as we'll iterate again later
|
||||
if deps_iter.clone().any(|ev| ev.queue != e.queue) {
|
||||
// this flush _has_ to happen before we wait on any of the deps
|
||||
flush_events(&mut flushed, &ctx);
|
||||
}
|
||||
|
||||
// We have to wait on user events or events from other queues.
|
||||
let err = e
|
||||
.deps
|
||||
.iter()
|
||||
let err = deps_iter
|
||||
.filter(|ev| ev.is_user() || ev.queue != e.queue)
|
||||
.map(|e| e.wait())
|
||||
.find(|s| *s < 0);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue