Skip to content

Commit 3888b7c

Browse files
committed
remove priority mailbox; fix stdout deadlock
1 parent a146da1 commit 3888b7c

File tree

1 file changed

+7
-25
lines changed

1 file changed

+7
-25
lines changed

src/progress.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ use std::{
1010
Arc,
1111
},
1212
thread::{self, JoinHandle},
13-
time::Duration,
1413
};
1514

16-
/// Timeout used for [`IndicatorHandle`]'s `priority_mailbox`.
17-
const PRIORITY_MAIL_TIMEOUT: Duration = Duration::from_nanos(1);
18-
1915
/// Responsible for displying the progress indicator. This struct will be owned by a separate
2016
/// thread that is responsible for displaying the progress text whereas the [`IndicatorHandle`]
2117
/// is how the outside world will interact with it.
@@ -28,13 +24,9 @@ pub struct Indicator<'a> {
2824
/// This struct is how the outside world will inform the [`Indicator`] about the progress of the
2925
/// program. The `join_handle` returns the handle to the thread that owns the [`Indicator`] and the
3026
/// `mailbox` is the [`SyncSender`] channel that allows [`Message`]s to be sent to [`Indicator`].
31-
///
32-
/// The `priority_mailbox` is used to prematurely terminate the [`Indicator`] in the case of say a
33-
/// `SIGINT` signal.
3427
pub struct IndicatorHandle {
3528
pub join_handle: Option<JoinHandle<Result<(), Error>>>,
3629
mailbox: SyncSender<Message>,
37-
priority_mailbox: SyncSender<()>,
3830
}
3931

4032
/// The different messages that could be sent to the thread that owns the [`Indicator`].
@@ -49,6 +41,9 @@ pub enum Message {
4941
/// Message that indicates that the output is ready to be flushed and that we should cleanup
5042
/// the [`Indicator`] as well as the screen.
5143
RenderReady,
44+
45+
/// Tear down the progress indicator for whatever reason.
46+
Finish,
5247
}
5348

5449
/// All of the different states the [`Indicator`] can be in during its life cycle.
@@ -73,7 +68,7 @@ pub enum Error {
7368
Io(#[from] io::Error),
7469

7570
#[error("#{0}")]
76-
Send(#[from] SendError<()>),
71+
Send(#[from] SendError<Message>),
7772
}
7873

7974
impl Default for Indicator<'_> {
@@ -92,12 +87,10 @@ impl IndicatorHandle {
9287
pub fn new(
9388
join_handle: Option<JoinHandle<Result<(), Error>>>,
9489
mailbox: SyncSender<Message>,
95-
priority_mailbox: SyncSender<()>,
9690
) -> Self {
9791
Self {
9892
join_handle,
9993
mailbox,
100-
priority_mailbox,
10194
}
10295
}
10396

@@ -106,15 +99,10 @@ impl IndicatorHandle {
10699
self.mailbox.clone()
107100
}
108101

109-
/// Getter for a cloned `priority_mailbox` wherewith to send [`Message`]s to the [`Indicator`].
110-
pub fn priority_mailbox(&self) -> SyncSender<()> {
111-
self.priority_mailbox.clone()
112-
}
113-
114102
/// Send a message through to the `priority_mailbox` tear down the [`Indicator`].
115103
pub fn terminate(this: Option<Arc<Self>>) -> Result<(), Error> {
116104
if let Some(mut handle) = this {
117-
handle.priority_mailbox().send(())?;
105+
handle.mailbox().send(Message::Finish)?;
118106

119107
if let Some(hand) = Arc::get_mut(&mut handle) {
120108
hand.join_handle
@@ -134,7 +122,6 @@ impl<'a> Indicator<'a> {
134122
/// outside world to send messages to the worker thread and ultimately to the [`Indicator`].
135123
pub fn measure() -> IndicatorHandle {
136124
let (tx, rx) = mpsc::sync_channel(1024);
137-
let (ptx, prx) = mpsc::sync_channel(1);
138125

139126
let join_handle = thread::spawn(move || {
140127
let mut indicator = Self::default();
@@ -143,17 +130,12 @@ impl<'a> Indicator<'a> {
143130
indicator.stdout.execute(cursor::Hide)?;
144131

145132
while let Ok(msg) = rx.recv() {
146-
if prx.recv_timeout(PRIORITY_MAIL_TIMEOUT).is_ok() {
147-
indicator.update_state(IndicatorState::Done)?;
148-
return Ok(());
149-
}
150-
151133
match msg {
152134
Message::Index => indicator.index()?,
153135
Message::DoneIndexing => {
154136
indicator.update_state(IndicatorState::Rendering)?;
155137
},
156-
Message::RenderReady => {
138+
Message::RenderReady | Message::Finish => {
157139
indicator.update_state(IndicatorState::Done)?;
158140
return Ok(());
159141
},
@@ -165,7 +147,7 @@ impl<'a> Indicator<'a> {
165147
Ok(())
166148
});
167149

168-
IndicatorHandle::new(Some(join_handle), tx, ptx)
150+
IndicatorHandle::new(Some(join_handle), tx)
169151
}
170152

171153
/// Updates the `state` of the [`Indicator`] to `new_state`, immediately running an associated

0 commit comments

Comments
 (0)