Skip to content

Commit cb03e7d

Browse files
committed
Rav1dFrameContext_frame_thread_progress: Inner mutability for frame and copy_lpf
The `frame` and `copy_lpf` vectors need inner mutability so we can make `Rav1dFrameContext` immutable.
1 parent b27620a commit cb03e7d

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/internal.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ use std::sync::Arc;
106106
use std::sync::Condvar;
107107
use std::sync::Mutex;
108108
use std::sync::OnceLock;
109+
use std::sync::RwLock;
109110
use std::thread::JoinHandle;
110111

111112
#[repr(C)]
@@ -789,8 +790,8 @@ impl Rav1dFrameContext_task_thread {
789790
pub(crate) struct Rav1dFrameContext_frame_thread_progress {
790791
pub entropy: AtomicI32,
791792
pub deblock: AtomicI32, // in sby units
792-
pub frame: Vec<AtomicU32>,
793-
pub copy_lpf: Vec<AtomicU32>,
793+
pub frame: RwLock<Vec<AtomicU32>>,
794+
pub copy_lpf: RwLock<Vec<AtomicU32>>,
794795
}
795796

796797
#[repr(C)]

src/thread_task.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,15 @@ unsafe fn create_filter_sbrow(
317317
tasks.grow_tasks(num_tasks);
318318
let task_idx = Rav1dTaskIndex::Task((f.sbh * (pass & 1)) as usize);
319319
if pass & 1 != 0 {
320-
f.frame_thread_progress.entropy = AtomicI32::new(0);
320+
f.frame_thread_progress.entropy.store(0, Ordering::Relaxed);
321321
} else {
322322
let prog_sz = ((f.sbh + 31 & !(31 as c_int)) >> 5) as usize;
323-
f.frame_thread_progress.frame.clear();
324-
f.frame_thread_progress
325-
.frame
326-
.resize_with(prog_sz, || AtomicU32::new(0));
327-
f.frame_thread_progress.copy_lpf.clear();
328-
f.frame_thread_progress
329-
.copy_lpf
330-
.resize_with(prog_sz, || AtomicU32::new(0));
323+
let mut frame = f.frame_thread_progress.frame.try_write().unwrap();
324+
frame.clear();
325+
frame.resize_with(prog_sz, || AtomicU32::new(0));
326+
let mut copy_lpf = f.frame_thread_progress.copy_lpf.try_write().unwrap();
327+
copy_lpf.clear();
328+
copy_lpf.resize_with(prog_sz, || AtomicU32::new(0));
331329
f.frame_thread_progress.deblock.store(0, Ordering::SeqCst);
332330
}
333331
f.frame_thread.next_tile_row[(pass & 1) as usize].store(0, Ordering::Relaxed);
@@ -559,15 +557,16 @@ unsafe fn get_frame_progress(f: &Rav1dFrameData) -> c_int {
559557
}
560558
let mut idx = (frame_prog >> f.sb_shift + 7) as c_int;
561559
let mut prog;
560+
let frame = f.frame_thread_progress.frame.try_read().unwrap();
562561
loop {
563-
let val: c_uint = !(f.frame_thread_progress.frame)[idx as usize].load(Ordering::SeqCst);
562+
let val: c_uint = !frame[idx as usize].load(Ordering::SeqCst);
564563
prog = if val != 0 { ctz(val) } else { 32 as c_int };
565564
if prog != 32 as c_int {
566565
break;
567566
}
568567
prog = 0 as c_int;
569568
idx += 1;
570-
if !((idx as usize) < f.frame_thread_progress.frame.len()) {
569+
if !((idx as usize) < frame.len()) {
571570
break;
572571
}
573572
}
@@ -871,7 +870,8 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
871870
}
872871
break 'found (f, t_idx, prev_t);
873872
} else if t.type_0 == TaskType::Cdef {
874-
let p1_1 = f.frame_thread_progress.copy_lpf[(t.sby - 1 >> 5) as usize]
873+
let p1_1 = f.frame_thread_progress.copy_lpf.try_read().unwrap()
874+
[(t.sby - 1 >> 5) as usize]
875875
.load(Ordering::SeqCst);
876876
if p1_1 as c_uint & (1 as c_uint) << (t.sby - 1 & 31) != 0 {
877877
break 'found (f, t_idx, prev_t);
@@ -1174,14 +1174,14 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
11741174
ttd.cond.notify_one();
11751175
}
11761176
} else if seq_hdr.cdef != 0 || f.lf.restore_planes != 0 {
1177-
f.frame_thread_progress.copy_lpf[(sby >> 5) as usize]
1177+
let copy_lpf = f.frame_thread_progress.copy_lpf.try_read().unwrap();
1178+
copy_lpf[(sby >> 5) as usize]
11781179
.fetch_or((1 as c_uint) << (sby & 31), Ordering::SeqCst);
11791180
// CDEF needs the top buffer to be saved by lr_copy_lpf of the
11801181
// previous sbrow
11811182
if sby != 0 {
1182-
let prog_1 = f.frame_thread_progress.copy_lpf
1183-
[(sby - 1 >> 5) as usize]
1184-
.load(Ordering::SeqCst);
1183+
let prog_1 =
1184+
copy_lpf[(sby - 1 >> 5) as usize].load(Ordering::SeqCst);
11851185
if !prog_1 as c_uint & (1 as c_uint) << (sby - 1 & 31) != 0 {
11861186
t.type_0 = TaskType::Cdef;
11871187
t.deblock_progress = 0 as c_int;
@@ -1296,7 +1296,7 @@ pub unsafe fn rav1d_worker_task(c: &Rav1dContext, task_thread: Arc<Rav1dTaskCont
12961296
continue 'outer;
12971297
}
12981298
// t->type != DAV1D_TASK_TYPE_ENTROPY_PROGRESS
1299-
f.frame_thread_progress.frame[(sby >> 5) as usize]
1299+
f.frame_thread_progress.frame.try_read().unwrap()[(sby >> 5) as usize]
13001300
.fetch_or((1 as c_uint) << (sby & 31), Ordering::SeqCst);
13011301
{
13021302
let _task_thread_lock = f.task_thread.lock.lock().unwrap();

0 commit comments

Comments
 (0)