Skip to content

Commit

Permalink
Simplify event processing loop exit condition somewhat
Browse files Browse the repository at this point in the history
Instead of having potentially two event processors running at once,
but only at different parts of the loop, we simplify the event
processing loop a bit by ensuring only one event processor can run
at once until the first is ready to exit.

Note that we still have to ensure we unset the mutual exclusion
flag inside the `pending_events` lock as otherwise we can race and
end up with two event processors called and both return without
every pending event having been processed.
  • Loading branch information
TheBlueMatt committed May 4, 2023
1 parent 9dfe42c commit 9c36735
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,14 +1718,11 @@ macro_rules! handle_new_monitor_update {

macro_rules! process_events_body {
($self: expr, $event_to_handle: expr, $handle_event: expr) => {
let mut processed_all_events = false;
while !processed_all_events {
if $self.pending_events_processor.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
return;
}

let mut result = NotifyOption::SkipPersist;

if $self.pending_events_processor.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
return;
}
let mut result = NotifyOption::SkipPersist;
loop {
{
// We'll acquire our total consistency lock so that we can be sure no other
// persists happen while processing monitor events.
Expand Down Expand Up @@ -1754,23 +1751,26 @@ macro_rules! process_events_body {
}
}

{
let mut pending_events = $self.pending_events.lock().unwrap();
pending_events.drain(..num_events);
processed_all_events = pending_events.is_empty();
$self.pending_events_processor.store(false, Ordering::Release);
}

let mut processed_all_events = true;
if !post_event_actions.is_empty() {
$self.handle_post_event_actions(post_event_actions);
// If we had some actions, go around again as we may have more events now
processed_all_events = false;
}

if result == NotifyOption::DoPersist {
$self.persistence_notifier.notify();
{
let mut pending_events = $self.pending_events.lock().unwrap();
pending_events.drain(..num_events);
processed_all_events &= pending_events.is_empty();
if processed_all_events {
$self.pending_events_processor.store(false, Ordering::Release);
break;
}
}
}
if result == NotifyOption::DoPersist {
$self.persistence_notifier.notify();
}
}
}

Expand Down

0 comments on commit 9c36735

Please sign in to comment.