-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc0c556
commit f919d96
Showing
4 changed files
with
108 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::sync::mpsc::{Receiver, RecvError, RecvTimeoutError, TryRecvError}; | ||
|
||
const TIMEOUT_DURATION: std::time::Duration = std::time::Duration::from_millis(10); | ||
|
||
pub(crate) fn receive_or_yield<R>(receiver: &Receiver<R>) -> std::result::Result<R, RecvError> { | ||
loop { | ||
match receiver.try_recv() { | ||
Ok(t) => return Ok(t), | ||
Err(TryRecvError::Empty) => match rayon::yield_now() { | ||
None => return receiver.recv(), | ||
Some(rayon::Yield::Executed) => continue, | ||
Some(rayon::Yield::Idle) => match receiver.recv_timeout(TIMEOUT_DURATION) { | ||
Ok(t) => return Ok(t), | ||
Err(RecvTimeoutError::Timeout) => { | ||
//dbg!("receive idle"); | ||
continue; | ||
} | ||
Err(RecvTimeoutError::Disconnected) => return Err(RecvError), | ||
}, | ||
}, | ||
Err(TryRecvError::Disconnected) => return Err(RecvError), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters