Skip to content

Commit 880d88e

Browse files
committed
Simplify event processing loop exit condition somewhat
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.
1 parent 9dfe42c commit 880d88e

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

lightning/src/ln/channelmanager.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -1718,14 +1718,11 @@ macro_rules! handle_new_monitor_update {
17181718

17191719
macro_rules! process_events_body {
17201720
($self: expr, $event_to_handle: expr, $handle_event: expr) => {
1721-
let mut processed_all_events = false;
1722-
while !processed_all_events {
1723-
if $self.pending_events_processor.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
1724-
return;
1725-
}
1726-
1727-
let mut result = NotifyOption::SkipPersist;
1728-
1721+
if $self.pending_events_processor.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed).is_err() {
1722+
return;
1723+
}
1724+
let mut result = NotifyOption::SkipPersist;
1725+
loop {
17291726
{
17301727
// We'll acquire our total consistency lock so that we can be sure no other
17311728
// persists happen while processing monitor events.
@@ -1754,23 +1751,26 @@ macro_rules! process_events_body {
17541751
}
17551752
}
17561753

1757-
{
1758-
let mut pending_events = $self.pending_events.lock().unwrap();
1759-
pending_events.drain(..num_events);
1760-
processed_all_events = pending_events.is_empty();
1761-
$self.pending_events_processor.store(false, Ordering::Release);
1762-
}
1763-
1754+
let mut processed_all_events = true;
17641755
if !post_event_actions.is_empty() {
17651756
$self.handle_post_event_actions(post_event_actions);
17661757
// If we had some actions, go around again as we may have more events now
17671758
processed_all_events = false;
17681759
}
17691760

1770-
if result == NotifyOption::DoPersist {
1771-
$self.persistence_notifier.notify();
1761+
{
1762+
let mut pending_events = $self.pending_events.lock().unwrap();
1763+
pending_events.drain(..num_events);
1764+
processed_all_events = pending_events.is_empty();
1765+
if processed_all_events {
1766+
$self.pending_events_processor.store(false, Ordering::Release);
1767+
break;
1768+
}
17721769
}
17731770
}
1771+
if result == NotifyOption::DoPersist {
1772+
$self.persistence_notifier.notify();
1773+
}
17741774
}
17751775
}
17761776

0 commit comments

Comments
 (0)