Skip to content

Commit

Permalink
Merge pull request #192 from mahkoh/jorth/shutdown-clear
Browse files Browse the repository at this point in the history
all: clear more reference cycles at shutdown
  • Loading branch information
mahkoh authored May 2, 2024
2 parents 4d33dbf + 3473b89 commit 06fe5b1
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ linear_ids!(DrmDeviceIds, DrmDeviceId);

pub trait Backend {
fn run(self: Rc<Self>) -> SpawnedFuture<Result<(), Box<dyn Error>>>;
fn clear(&self) {
// nothing
}
#[cfg_attr(not(feature = "it"), allow(dead_code))]
fn into_any(self: Rc<Self>) -> Rc<dyn Any>;

Expand Down
51 changes: 50 additions & 1 deletion src/backends/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use {
Backend, InputDevice, InputDeviceAccelProfile, InputDeviceCapability, InputDeviceId,
InputEvent, KeyState, TransformMatrix,
},
backends::metal::video::{MetalDrmDeviceData, MetalRenderContext, PendingDrmDevice},
backends::metal::video::{
MetalDrmDeviceData, MetalLeaseData, MetalRenderContext, PendingDrmDevice,
},
dbus::{DbusError, SignalHandler},
drm_feedback::DrmFeedback,
gfx_api::GfxError,
Expand Down Expand Up @@ -165,6 +167,53 @@ impl Backend for MetalBackend {
})
}

fn clear(&self) {
self.pause_handler.take();
self.resume_handler.take();
self.ctx.take();
self.device_holder.devices.clear();
for dev in self.device_holder.input_devices.take() {
if let Some(dev) = dev {
dev.inputdev.take();
dev.events.take();
dev.cb.take();
}
}
for (_, dev) in self.device_holder.drm_devices.lock().drain() {
dev.futures.clear();
for crtc in dev.dev.crtcs.values() {
crtc.connector.take();
}
dev.dev.handle_events.handle_events.take();
dev.dev.on_change.clear();
let clear_lease = |lease: &mut MetalLeaseData| {
lease.connectors.clear();
lease.crtcs.clear();
lease.planes.clear();
};
for (_, mut lease) in dev.dev.leases.lock().drain() {
clear_lease(&mut lease);
}
for (_, mut lease) in dev.dev.leases_to_break.lock().drain() {
clear_lease(&mut lease);
}
for (_, connector) in dev.connectors.lock().drain() {
{
let d = &mut *connector.display.borrow_mut();
d.crtcs.clear();
}
connector.primary_plane.take();
connector.cursor_plane.take();
connector.crtc.take();
connector.on_change.clear();
connector.present_trigger.clear();
connector.render_result.take();
connector.active_framebuffer.take();
connector.next_framebuffer.take();
}
}
}

fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
self
}
Expand Down
12 changes: 6 additions & 6 deletions src/backends/metal/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ impl ConnectorDisplayData {
linear_ids!(MetalLeaseIds, MetalLeaseId, u64);

pub struct MetalLeaseData {
lease: DrmLease,
_lessee: Rc<dyn BackendDrmLessee>,
connectors: Vec<Rc<MetalConnector>>,
crtcs: Vec<Rc<MetalCrtc>>,
planes: Vec<Rc<MetalPlane>>,
revoked: Cell<bool>,
pub lease: DrmLease,
pub _lessee: Rc<dyn BackendDrmLessee>,
pub connectors: Vec<Rc<MetalConnector>>,
pub crtcs: Vec<Rc<MetalCrtc>>,
pub planes: Vec<Rc<MetalPlane>>,
pub revoked: Cell<bool>,
}

impl MetalLeaseData {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
clippy::uninlined_format_args,
clippy::manual_is_ascii_check,
clippy::needless_borrow,
clippy::unnecessary_cast
clippy::unnecessary_cast,
clippy::manual_flatten
)]

#[macro_use]
Expand Down
3 changes: 2 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,15 @@ impl State {
forker.clear();
}
self.acceptor.set(None);
self.backend.set(Rc::new(DummyBackend));
self.backend.set(Rc::new(DummyBackend)).clear();
self.run_toplevel.clear();
self.xwayland.handler.borrow_mut().take();
self.xwayland.queue.clear();
self.idle.inhibitors.clear();
self.idle.change.clear();
for (_, drm_dev) in self.drm_devs.lock().drain() {
drm_dev.handler.take();
drm_dev.connectors.clear();
}
for (_, connector) in self.connectors.lock().drain() {
connector.handler.take();
Expand Down
5 changes: 5 additions & 0 deletions src/utils/on_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ pub struct OnChange<T> {
}

impl<T> OnChange<T> {
pub fn clear(&self) {
self.on_change.take();
self.events.take();
}

pub fn send_event(&self, event: T) {
self.events.push(event);
if let Some(cb) = self.on_change.get() {
Expand Down
10 changes: 9 additions & 1 deletion src/xwayland/xwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,15 @@ impl Drop for Wm {
if let Some(window) = window.window.take() {
window.break_loops();
}
}
window.children.clear();
window.parent.take();
window.stack_link.take();
window.map_link.take();
}
self.windows_by_surface_id.clear();
self.windows_by_surface_serial.clear();
self.focus_window.take();
self.known_seats.clear();
}
}

Expand Down

0 comments on commit 06fe5b1

Please sign in to comment.