Migrate compare_and_swap to compare_exchange (#291)

This commit is contained in:
gakada 2021-03-06 19:30:52 +09:00 committed by GitHub
parent 0fcb102f03
commit dbc5f94241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 16 deletions

View File

@ -450,7 +450,15 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<RunningClient
let is_ready = Arc::new(atomic::AtomicBool::new(true)); let is_ready = Arc::new(atomic::AtomicBool::new(true));
miner.add_transactions_listener(Box::new(move |_hashes| { miner.add_transactions_listener(Box::new(move |_hashes| {
// we want to have only one PendingTransactions task in the queue. // we want to have only one PendingTransactions task in the queue.
if is_ready.compare_and_swap(true, false, atomic::Ordering::SeqCst) { if is_ready
.compare_exchange(
true,
false,
atomic::Ordering::SeqCst,
atomic::Ordering::SeqCst,
)
.is_ok()
{
let task = let task =
::sync::PriorityTask::PropagateTransactions(Instant::now(), is_ready.clone()); ::sync::PriorityTask::PropagateTransactions(Instant::now(), is_ready.clone());
// we ignore error cause it means that we are closing // we ignore error cause it means that we are closing

View File

@ -1187,7 +1187,7 @@ impl Engine<EthereumMachine> for AuthorityRound {
/// `Seal::None` will be returned. /// `Seal::None` will be returned.
fn generate_seal(&self, block: &ExecutedBlock, parent: &Header) -> Seal { fn generate_seal(&self, block: &ExecutedBlock, parent: &Header) -> Seal {
// first check to avoid generating signature most of the time // first check to avoid generating signature most of the time
// (but there's still a race to the `compare_and_swap`) // (but there's still a race to the `compare_exchange`)
if !self.step.can_propose.load(AtomicOrdering::SeqCst) { if !self.step.can_propose.load(AtomicOrdering::SeqCst) {
trace!(target: "engine", "Aborting seal generation. Can't propose."); trace!(target: "engine", "Aborting seal generation. Can't propose.");
return Seal::None; return Seal::None;
@ -1245,7 +1245,8 @@ impl Engine<EthereumMachine> for AuthorityRound {
if self if self
.step .step
.can_propose .can_propose
.compare_and_swap(true, false, AtomicOrdering::SeqCst) .compare_exchange(true, false, AtomicOrdering::SeqCst, AtomicOrdering::SeqCst)
.is_ok()
{ {
self.generate_empty_step(header.parent_hash()); self.generate_empty_step(header.parent_hash());
} }
@ -1266,11 +1267,12 @@ impl Engine<EthereumMachine> for AuthorityRound {
)) { )) {
trace!(target: "engine", "generate_seal: Issuing a block for step {}.", step); trace!(target: "engine", "generate_seal: Issuing a block for step {}.", step);
// only issue the seal if we were the first to reach the compare_and_swap. // only issue the seal if we were the first to reach the compare_exchange.
if self if self
.step .step
.can_propose .can_propose
.compare_and_swap(true, false, AtomicOrdering::SeqCst) .compare_exchange(true, false, AtomicOrdering::SeqCst, AtomicOrdering::SeqCst)
.is_ok()
{ {
// we can drop all accumulated empty step messages that are // we can drop all accumulated empty step messages that are
// older than the parent step since we're including them in // older than the parent step since we're including them in

View File

@ -81,12 +81,16 @@ impl<M: Machine> Engine<M> for InstantSeal<M> {
// Return a regular seal if the given block is _higher_ than // Return a regular seal if the given block is _higher_ than
// the last sealed one // the last sealed one
if block_number > last_sealed_block { if block_number > last_sealed_block {
let prev_last_sealed_block = self.last_sealed_block.compare_and_swap( if self
last_sealed_block, .last_sealed_block
block_number, .compare_exchange(
Ordering::SeqCst, last_sealed_block,
); block_number,
if prev_last_sealed_block == last_sealed_block { Ordering::SeqCst,
Ordering::SeqCst,
)
.is_ok()
{
return Seal::Regular(Vec::new()); return Seal::Regular(Vec::new());
} }
} }

View File

@ -516,7 +516,8 @@ impl Service {
pub fn take_snapshot(&self, client: &Client, num: u64) -> Result<(), Error> { pub fn take_snapshot(&self, client: &Client, num: u64) -> Result<(), Error> {
if self if self
.taking_snapshot .taking_snapshot
.compare_and_swap(false, true, Ordering::SeqCst) .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_err()
{ {
info!( info!(
"Skipping snapshot at #{} as another one is currently in-progress.", "Skipping snapshot at #{} as another one is currently in-progress.",

View File

@ -181,8 +181,13 @@ impl QueueSignal {
if self if self
.signalled .signalled
.compare_and_swap(false, true, AtomicOrdering::Relaxed) .compare_exchange(
== false false,
true,
AtomicOrdering::Relaxed,
AtomicOrdering::Relaxed,
)
.is_ok()
{ {
let channel = self.message_channel.lock().clone(); let channel = self.message_channel.lock().clone();
if let Err(e) = channel.send_sync(ClientIoMessage::BlockVerified) { if let Err(e) = channel.send_sync(ClientIoMessage::BlockVerified) {
@ -199,8 +204,13 @@ impl QueueSignal {
if self if self
.signalled .signalled
.compare_and_swap(false, true, AtomicOrdering::Relaxed) .compare_exchange(
== false false,
true,
AtomicOrdering::Relaxed,
AtomicOrdering::Relaxed,
)
.is_ok()
{ {
let channel = self.message_channel.lock().clone(); let channel = self.message_channel.lock().clone();
if let Err(e) = channel.send(ClientIoMessage::BlockVerified) { if let Err(e) = channel.send(ClientIoMessage::BlockVerified) {