Fixes to tasks

This commit is contained in:
Luke D. Jones
2022-06-12 15:31:09 +12:00
parent 578d5fd541
commit 9519a35e32
4 changed files with 83 additions and 63 deletions

View File

@@ -171,17 +171,19 @@ impl CtrlAnime {
loop { loop {
// wait for other threads to set not running so we know they exited // wait for other threads to set not running so we know they exited
if !thread_running.load(Ordering::SeqCst) { if !thread_running.load(Ordering::SeqCst) {
thread_exit.store(false, Ordering::SeqCst);
info!("AniMe forced a thread to exit"); info!("AniMe forced a thread to exit");
break; break;
} }
} }
thread_exit.store(false, Ordering::SeqCst);
thread_running.store(true, Ordering::SeqCst);
'main: loop { 'main: loop {
if thread_exit.load(Ordering::SeqCst) {
break 'main;
}
for action in actions.iter() { for action in actions.iter() {
if thread_exit.load(Ordering::SeqCst) {
break 'main;
}
match action { match action {
ActionData::Animation(frames) => { ActionData::Animation(frames) => {
if let Err(err) = rog_anime::run_animation( if let Err(err) = rog_anime::run_animation(
@@ -192,18 +194,14 @@ impl CtrlAnime {
.try_lock() .try_lock()
.map(|lock| lock.write_data_buffer(frame)) .map(|lock| lock.write_data_buffer(frame))
.map_err(|err| { .map_err(|err| {
warn!("rog_anime::run_animation: {}", err); warn!("rog_anime::run_animation:callback {}", err);
AnimeError::NoFrames AnimeError::NoFrames
}) })
}, },
) { ) {
warn!("rog_anime::run_animation: {}", err); warn!("rog_anime::run_animation:Animation {}", err);
break 'main; break 'main;
}; };
if thread_exit.load(Ordering::SeqCst) {
break 'main;
}
} }
ActionData::Image(image) => { ActionData::Image(image) => {
once = false; once = false;
@@ -228,7 +226,6 @@ impl CtrlAnime {
lock.write_data_buffer(data); lock.write_data_buffer(data);
} }
// Loop ended, set the atmonics // Loop ended, set the atmonics
thread_exit.store(false, Ordering::SeqCst);
thread_running.store(false, Ordering::SeqCst); thread_running.store(false, Ordering::SeqCst);
info!("AniMe system thread exited"); info!("AniMe system thread exited");
}) })
@@ -328,24 +325,30 @@ impl crate::CtrlTask for CtrlAnimeTask {
.for_each(|event| { .for_each(|event| {
if let Ok(args) = event.args() { if let Ok(args) = event.args() {
if args.start { if args.start {
if let Ok(lock) = inner.clone().try_lock() { loop {
info!("CtrlAnimeTask running sleep animation"); // Loop is required to try an attempt to get the mutex *without* blocking
lock.thread_exit.store(true, Ordering::Relaxed); // other threads - it is possible to end up with deadlocks otherwise.
CtrlAnime::run_thread( if let Ok(lock) = inner.clone().try_lock() {
inner.clone(), info!("CtrlAnimeTask running sleep animation");
lock.cache.shutdown.clone(), CtrlAnime::run_thread(
true, inner.clone(),
); lock.cache.shutdown.clone(),
true,
);
break;
}
} }
} else { } else {
if let Ok(lock) = inner.clone().try_lock() { loop {
info!("CtrlAnimeTask running wake animation"); if let Ok(lock) = inner.clone().try_lock() {
lock.thread_exit.store(true, Ordering::Relaxed); info!("CtrlAnimeTask running wake animation");
CtrlAnime::run_thread( CtrlAnime::run_thread(
inner.clone(), inner.clone(),
lock.cache.wake.clone(), lock.cache.wake.clone(),
true, true,
); );
break;
}
} }
} }
} }
@@ -367,25 +370,29 @@ impl crate::CtrlTask for CtrlAnimeTask {
.for_each(|event| { .for_each(|event| {
if let Ok(args) = event.args() { if let Ok(args) = event.args() {
if args.start { if args.start {
if let Ok(lock) = inner.clone().try_lock() { loop {
info!("CtrlAnimeTask running sleep animation"); if let Ok(lock) = inner.clone().try_lock() {
lock.thread_exit.store(true, Ordering::Relaxed); info!("CtrlAnimeTask running sleep animation");
CtrlAnime::run_thread( CtrlAnime::run_thread(
inner.clone(), inner.clone(),
lock.cache.shutdown.clone(), lock.cache.shutdown.clone(),
true, true,
); );
break;
}
} }
} else { } else {
// If waking up - intention is to catch hibernation event // If waking up - intention is to catch hibernation event
if let Ok(lock) = inner.clone().try_lock() { loop {
info!("CtrlAnimeTask running wake animation"); if let Ok(lock) = inner.clone().lock() {
lock.thread_exit.store(true, Ordering::Relaxed); info!("CtrlAnimeTask running wake animation");
CtrlAnime::run_thread( CtrlAnime::run_thread(
inner.clone(), inner.clone(),
lock.cache.wake.clone(), lock.cache.wake.clone(),
true, true,
); );
break;
}
} }
} }
} }

View File

@@ -111,19 +111,28 @@ impl CtrlTask for CtrlKbdLedTask {
// If waking up // If waking up
if !args.start { if !args.start {
info!("CtrlKbdLedTask reloading brightness and modes"); info!("CtrlKbdLedTask reloading brightness and modes");
if let Ok(lock) = inner.clone().try_lock() { loop {
lock.set_brightness(lock.config.brightness) // Loop so that we do aquire the lock but also don't block other
// threads (prevents potential deadlocks)
if let Ok(lock) = inner.clone().try_lock() {
// Can't reload brightness due to system setting the brightness on sleep/wake
// and the config update task saving that change.
// lock.set_brightness(lock.config.brightness)
// .map_err(|e| error!("CtrlKbdLedTask: {e}"))
// .ok();
lock.set_side_leds_states(
lock.config.side_leds_enabled,
)
.map_err(|e| error!("CtrlKbdLedTask: {e}")) .map_err(|e| error!("CtrlKbdLedTask: {e}"))
.ok(); .ok();
lock.set_side_leds_states(lock.config.side_leds_enabled) if let Some(mode) =
.map_err(|e| error!("CtrlKbdLedTask: {e}")) lock.config.builtins.get(&lock.config.current_mode)
.ok(); {
if let Some(mode) = lock.write_mode(mode)
lock.config.builtins.get(&lock.config.current_mode) .map_err(|e| error!("CtrlKbdLedTask: {e}"))
{ .ok();
lock.write_mode(mode) }
.map_err(|e| error!("CtrlKbdLedTask: {e}")) break;
.ok();
} }
} }
} }
@@ -135,9 +144,10 @@ impl CtrlTask for CtrlKbdLedTask {
.detach(); .detach();
let inner = self.inner.clone(); let inner = self.inner.clone();
self.repeating_task(500, executor, move || { self.repeating_task(500, executor, move || loop {
if let Ok(ref mut lock) = inner.try_lock() { if let Ok(ref mut lock) = inner.try_lock() {
Self::update_config(lock).unwrap(); Self::update_config(lock).unwrap();
break;
} }
}) })
.await; .await;

View File

@@ -178,13 +178,16 @@ impl CtrlTask for CtrlCharge {
// If waking up - intention is to catch hibernation event // If waking up - intention is to catch hibernation event
if !args.start { if !args.start {
info!("CtrlCharge reloading charge limit"); info!("CtrlCharge reloading charge limit");
if let Ok(mut lock) = config.clone().try_lock() { loop {
Self::set(lock.bat_charge_limit, &mut lock) if let Ok(mut lock) = config.clone().try_lock() {
.map_err(|err| { Self::set(lock.bat_charge_limit, &mut lock)
warn!("CtrlCharge: set_limit {}", err); .map_err(|err| {
err warn!("CtrlCharge: set_limit {}", err);
}) err
.ok(); })
.ok();
break;
}
} }
} }
} }

View File

@@ -164,7 +164,7 @@ pub fn run_animation(
} }
} }
callback(output)?; callback(output).ok();
if timed && Instant::now().duration_since(start) > run_time { if timed && Instant::now().duration_since(start) > run_time {
break 'animation; break 'animation;