Skip to content

Commit 18cff8e

Browse files
committed
Controller & Scheduler: use single Arc instead of 1 per field
1 parent 7632594 commit 18cff8e

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

src/control.rs

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,47 @@ use crate::AtomicF64;
88
/// Helper struct to start and stop audio streams
99
#[derive(Clone, Debug)]
1010
pub(crate) struct Scheduler {
11-
start: Arc<AtomicF64>,
12-
stop: Arc<AtomicF64>,
11+
inner: Arc<SchedulerInner>,
12+
}
13+
14+
#[derive(Debug)]
15+
struct SchedulerInner {
16+
start: AtomicF64,
17+
stop: AtomicF64,
1318
}
1419

1520
impl Scheduler {
1621
/// Create a new Scheduler. Initial playback state will be: inactive.
1722
pub fn new() -> Self {
23+
let inner = SchedulerInner {
24+
start: AtomicF64::new(f64::MAX),
25+
stop: AtomicF64::new(f64::MAX),
26+
};
1827
Self {
19-
start: Arc::new(AtomicF64::new(f64::MAX)),
20-
stop: Arc::new(AtomicF64::new(f64::MAX)),
28+
inner: Arc::new(inner),
2129
}
2230
}
2331

2432
/// Retrieve playback start value
2533
pub fn get_start_at(&self) -> f64 {
26-
self.start.load(Ordering::SeqCst)
34+
self.inner.start.load(Ordering::SeqCst)
2735
}
2836

2937
/// Schedule playback start at this timestamp
3038
pub fn start_at(&self, start: f64) {
3139
// todo panic on invalid values, or when already called
32-
self.start.store(start, Ordering::SeqCst);
40+
self.inner.start.store(start, Ordering::SeqCst);
3341
}
3442

3543
/// Retrieve playback stop value
3644
pub fn get_stop_at(&self) -> f64 {
37-
self.stop.load(Ordering::SeqCst)
45+
self.inner.stop.load(Ordering::SeqCst)
3846
}
3947

4048
/// Stop playback at this timestamp
4149
pub fn stop_at(&self, stop: f64) {
4250
// todo panic on invalid values, or when already called
43-
self.stop.store(stop, Ordering::SeqCst);
51+
self.inner.stop.store(stop, Ordering::SeqCst);
4452
}
4553
}
4654

@@ -53,69 +61,78 @@ impl Default for Scheduler {
5361
/// Helper struct to control audio streams
5462
#[derive(Clone, Debug)]
5563
pub(crate) struct Controller {
56-
scheduler: Arc<Scheduler>,
57-
loop_: Arc<AtomicBool>,
58-
loop_start: Arc<AtomicF64>,
59-
loop_end: Arc<AtomicF64>,
60-
offset: Arc<AtomicF64>,
61-
duration: Arc<AtomicF64>,
64+
inner: Arc<ControllerInner>,
65+
}
66+
67+
#[derive(Debug)]
68+
struct ControllerInner {
69+
scheduler: Scheduler,
70+
loop_: AtomicBool,
71+
loop_start: AtomicF64,
72+
loop_end: AtomicF64,
73+
offset: AtomicF64,
74+
duration: AtomicF64,
6275
}
6376

6477
impl Controller {
6578
/// Create a new Controller. It will not be active
6679
pub fn new() -> Self {
80+
let inner = ControllerInner {
81+
scheduler: Scheduler::new(),
82+
loop_: AtomicBool::new(false),
83+
loop_start: AtomicF64::new(0.),
84+
loop_end: AtomicF64::new(f64::MAX),
85+
offset: AtomicF64::new(f64::MAX),
86+
duration: AtomicF64::new(f64::MAX),
87+
};
88+
6789
Self {
68-
scheduler: Arc::new(Scheduler::new()),
69-
loop_: Arc::new(AtomicBool::new(false)),
70-
loop_start: Arc::new(AtomicF64::new(0.)),
71-
loop_end: Arc::new(AtomicF64::new(f64::MAX)),
72-
offset: Arc::new(AtomicF64::new(f64::MAX)),
73-
duration: Arc::new(AtomicF64::new(f64::MAX)),
90+
inner: Arc::new(inner),
7491
}
7592
}
7693

7794
pub fn scheduler(&self) -> &Scheduler {
78-
&self.scheduler
95+
&self.inner.scheduler
7996
}
8097

8198
pub fn loop_(&self) -> bool {
82-
self.loop_.load(Ordering::SeqCst)
99+
self.inner.loop_.load(Ordering::SeqCst)
83100
}
84101

85102
pub fn set_loop(&self, loop_: bool) {
86-
self.loop_.store(loop_, Ordering::SeqCst);
103+
self.inner.loop_.store(loop_, Ordering::SeqCst);
87104
}
88105

89106
pub fn loop_start(&self) -> f64 {
90-
self.loop_start.load(Ordering::SeqCst)
107+
self.inner.loop_start.load(Ordering::SeqCst)
91108
}
92109

93110
pub fn set_loop_start(&self, loop_start: f64) {
94-
self.loop_start.store(loop_start, Ordering::SeqCst);
111+
self.inner.loop_start.store(loop_start, Ordering::SeqCst);
95112
}
96113

97114
pub fn loop_end(&self) -> f64 {
98-
self.loop_end.load(Ordering::SeqCst)
115+
self.inner.loop_end.load(Ordering::SeqCst)
99116
}
100117

101118
pub fn set_loop_end(&self, loop_end: f64) {
102-
self.loop_end.store(loop_end, Ordering::SeqCst);
119+
self.inner.loop_end.store(loop_end, Ordering::SeqCst);
103120
}
104121

105122
pub fn offset(&self) -> f64 {
106-
self.offset.load(Ordering::SeqCst)
123+
self.inner.offset.load(Ordering::SeqCst)
107124
}
108125

109126
pub fn set_offset(&self, offset: f64) {
110-
self.offset.store(offset, Ordering::SeqCst);
127+
self.inner.offset.store(offset, Ordering::SeqCst);
111128
}
112129

113130
pub fn duration(&self) -> f64 {
114-
self.duration.load(Ordering::SeqCst)
131+
self.inner.duration.load(Ordering::SeqCst)
115132
}
116133

117134
pub fn set_duration(&self, duration: f64) {
118-
self.duration.store(duration, Ordering::SeqCst)
135+
self.inner.duration.store(duration, Ordering::SeqCst)
119136
}
120137
}
121138

0 commit comments

Comments
 (0)