Skip to content

Commit

Permalink
fn get_num_threads: Cleanup and make safe (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Oct 18, 2023
2 parents 90d6efe + 9b89fbe commit f8c94c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
6 changes: 2 additions & 4 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::src::const_fn::const_for;
use crate::src::internal::Rav1dContext;
use bitflags::bitflags;
use std::ffi::c_int;
use std::ffi::c_uint;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -208,6 +206,6 @@ pub extern "C" fn dav1d_set_cpu_flags_mask(mask: c_uint) {
}

#[cold]
pub(crate) fn rav1d_num_logical_processors(_c: *mut Rav1dContext) -> c_int {
num_cpus::get() as c_int
pub(crate) fn rav1d_num_logical_processors() -> usize {
num_cpus::get()
}
48 changes: 20 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::include::common::bitdepth::DynCoef;
use crate::include::common::intops::iclip;
use crate::include::common::validate::validate_input;
use crate::include::dav1d::common::Dav1dDataProps;
use crate::include::dav1d::common::Rav1dDataProps;
Expand Down Expand Up @@ -204,40 +203,31 @@ unsafe fn get_stack_size_internal(_thread_attr: *const pthread_attr_t) -> usize
return 0;
}

struct NumThreads {
n_tc: usize,
n_fc: usize,
}

#[cold]
unsafe fn get_num_threads(
c: *mut Rav1dContext,
s: *const Rav1dSettings,
n_tc: *mut c_uint,
n_fc: *mut c_uint,
) {
static fc_lut: [u8; 49] = [
1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
];
*n_tc = (if (*s).n_threads != 0 {
(*s).n_threads
fn get_num_threads(s: &Rav1dSettings) -> NumThreads {
let n_tc = if s.n_threads != 0 {
s.n_threads as usize
} else {
iclip(rav1d_num_logical_processors(c), 1 as c_int, 256 as c_int)
}) as c_uint;
*n_fc = if (*s).max_frame_delay != 0 {
cmp::min((*s).max_frame_delay as c_uint, *n_tc)
rav1d_num_logical_processors().clamp(1, 256)
};
let n_fc = if s.max_frame_delay != 0 {
cmp::min(s.max_frame_delay as usize, n_tc)
} else {
(if *n_tc < 50 as c_uint {
fc_lut[(*n_tc).wrapping_sub(1 as c_int as c_uint) as usize] as c_int
} else {
8 as c_int
}) as c_uint
cmp::min((n_tc as f64).sqrt().ceil() as usize, 8)
};
NumThreads { n_fc, n_tc }
}

#[cold]
pub(crate) unsafe fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult<c_uint> {
let mut n_tc: c_uint = 0;
let mut n_fc: c_uint = 0;
pub(crate) unsafe fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult<usize> {
validate_input!((s.n_threads >= 0 && s.n_threads <= 256, EINVAL))?;
validate_input!((s.max_frame_delay >= 0 && s.max_frame_delay <= 256, EINVAL))?;
get_num_threads(0 as *mut Rav1dContext, s, &mut n_tc, &mut n_fc);
let NumThreads { n_tc: _, n_fc } = get_num_threads(s);
Ok(n_fc)
}

Expand All @@ -246,7 +236,7 @@ pub(crate) unsafe fn rav1d_get_frame_delay(s: &Rav1dSettings) -> Rav1dResult<c_u
pub unsafe extern "C" fn dav1d_get_frame_delay(s: *const Dav1dSettings) -> Dav1dResult {
(|| {
validate_input!((!s.is_null(), EINVAL))?;
rav1d_get_frame_delay(&s.read().into())
rav1d_get_frame_delay(&s.read().into()).map(|frame_delay| frame_delay as c_uint)
})()
.into()
}
Expand Down Expand Up @@ -342,7 +332,9 @@ pub(crate) unsafe fn rav1d_open(c_out: &mut *mut Rav1dContext, s: &Rav1dSettings
}
(*c).flush = &mut (*c).flush_mem;
*(*c).flush = 0 as c_int;
get_num_threads(c, s, &mut (*c).n_tc, &mut (*c).n_fc);
let NumThreads { n_tc, n_fc } = get_num_threads(s);
(*c).n_tc = n_tc as c_uint;
(*c).n_fc = n_fc as c_uint;
(*c).fc = rav1d_alloc_aligned(
::core::mem::size_of::<Rav1dFrameContext>().wrapping_mul((*c).n_fc as usize),
32 as c_int as usize,
Expand Down

0 comments on commit f8c94c1

Please sign in to comment.