Skip to content

Commit

Permalink
struct Rav1dTaskContext: Remove *const Rav1dContext field (#721)
Browse files Browse the repository at this point in the history
`Rav1dTaskContext` doesn't need to hold a pointer to its parent if we
pass this borrow into the thread start function explicitly.
  • Loading branch information
rinon authored Feb 2, 2024
2 parents dc25a72 + 6fc48ff commit 45afa46
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ pub(crate) struct Rav1dTaskContext_task_thread {

#[repr(C)]
pub(crate) struct Rav1dTaskContext {
pub c: *const Rav1dContext,
pub f: *const Rav1dFrameContext,
pub ts: *mut Rav1dTileState,
pub bx: c_int,
Expand All @@ -692,3 +691,12 @@ pub(crate) struct Rav1dTaskContext {
pub frame_thread: Rav1dTaskContext_frame_thread,
pub task_thread: Rav1dTaskContext_task_thread,
}

// TODO(SJC): This is a temporary struct to pass a single pointer that holds
// both a Rav1dContext and Rav1dTaskContext to the start routine in
// pthread_create. We need to pass the Rav1dTaskContext into the thread by value
// and remove it from the context structure.
pub(crate) struct Rav1dTaskContext_borrow<'c> {
pub c: &'c Rav1dContext,
pub tc: &'c mut Rav1dTaskContext,
}
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::src::internal::Rav1dContext;
use crate::src::internal::Rav1dFrameContext;
use crate::src::internal::Rav1dTask;
use crate::src::internal::Rav1dTaskContext;
use crate::src::internal::Rav1dTaskContext_borrow;
use crate::src::internal::TaskThreadData;
use crate::src::intra_edge::rav1d_init_mode_tree;
use crate::src::levels::Av1Block;
Expand Down Expand Up @@ -375,15 +376,18 @@ pub(crate) unsafe fn rav1d_open(c_out: &mut *mut Rav1dContext, s: &Rav1dSettings
(*t).f = &mut *((*c).fc).offset(0) as *mut Rav1dFrameContext;
(&mut (*t).task_thread.ttd as *mut Arc<TaskThreadData>)
.write(Arc::clone(&(*c).task_thread));
(*t).c = c;
*BitDepth16::select_mut(&mut (*t).cf) = Align64([0; 32 * 32]);
if (*c).n_tc > 1 as c_uint {
(*t).task_thread.td.cond = Condvar::new();
let thread_args = Box::new(Rav1dTaskContext_borrow {
c: &*c,
tc: &mut *t,
});
if pthread_create(
&mut (*t).task_thread.td.thread,
&mut thread_attr,
Some(rav1d_worker_task),
t as *mut c_void,
Box::into_raw(thread_args).cast(),
) != 0
{
return error(c, c_out, &mut thread_attr);
Expand Down
4 changes: 2 additions & 2 deletions src/thread_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::src::internal::Rav1dContext;
use crate::src::internal::Rav1dFrameContext;
use crate::src::internal::Rav1dTask;
use crate::src::internal::Rav1dTaskContext;
use crate::src::internal::Rav1dTaskContext_borrow;
use crate::src::internal::Rav1dTileState;
use crate::src::internal::TaskThreadData;
use crate::src::internal::TaskThreadData_delayed_fg;
Expand Down Expand Up @@ -790,8 +791,7 @@ unsafe fn delayed_fg_task<'l, 'ttd: 'l>(
}

pub unsafe extern "C" fn rav1d_worker_task(data: *mut c_void) -> *mut c_void {
let tc: &mut Rav1dTaskContext = &mut *(data as *mut Rav1dTaskContext);
let c: &Rav1dContext = &*tc.c;
let Rav1dTaskContext_borrow { c, tc } = *Box::from_raw(data as *mut Rav1dTaskContext_borrow);

// We clone the Arc here for the lifetime of this function to avoid an
// immutable borrow of tc across the call to park
Expand Down

0 comments on commit 45afa46

Please sign in to comment.