Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support drop guard for session #86

Merged
merged 1 commit into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion openxr/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,26 @@ impl Instance {
info: &G::SessionCreateInfo,
) -> Result<(Session<G>, FrameWaiter, FrameStream<G>)> {
let handle = G::create_session(self, system, info)?;
Ok(Session::from_raw(self.clone(), handle))
Ok(Session::from_raw(self.clone(), handle, Box::new(())))
}

/// Refer to [`Instance::create_session()`]. The extra `drop_guard` argument is dropped after
/// the session is destroyed and this can be used to ensure safety.
///
/// # Safety
///
/// The requirements documented by the graphics API extension must be respected. Among other
/// requirements, `info` must contain valid handles, and certain operations must be externally
/// synchronized.
#[inline]
pub unsafe fn create_session_with_guard<G: Graphics>(
&self,
system: SystemId,
info: &G::SessionCreateInfo,
drop_guard: DropGuard,
) -> Result<(Session<G>, FrameWaiter, FrameStream<G>)> {
let handle = G::create_session(self, system, info)?;
Ok(Session::from_raw(self.clone(), handle, drop_guard))
}

/// Get the next event, if available
Expand Down
10 changes: 9 additions & 1 deletion openxr/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{marker::PhantomData, ptr, sync::Arc};

use crate::*;

pub(crate) type DropGuard = Box<dyn std::any::Any + Send + Sync>;

/// A rendering session using a particular graphics API `G`
///
/// Convertible into an API-agnostic session using [`Session::into_any_graphics`].
Expand Down Expand Up @@ -382,9 +384,14 @@ impl<G: Graphics> Session<G> {
pub unsafe fn from_raw(
instance: Instance,
handle: sys::Session,
drop_guard: DropGuard,
) -> (Self, FrameWaiter, FrameStream<G>) {
let session = Self {
inner: Arc::new(SessionInner { instance, handle }),
inner: Arc::new(SessionInner {
instance,
handle,
_drop_guard: drop_guard,
}),
_marker: PhantomData,
};
(
Expand Down Expand Up @@ -448,6 +455,7 @@ impl<G> Clone for Session<G> {
pub(crate) struct SessionInner {
pub(crate) instance: Instance,
pub(crate) handle: sys::Session,
pub(crate) _drop_guard: DropGuard,
}

impl Drop for SessionInner {
Expand Down