Skip to content

Commit

Permalink
Replace deprecated compare_and_swap with compare_exchange (#514)
Browse files Browse the repository at this point in the history
The `compare_and_swap` method on atomics is now deprecated in favor
of `compare_exchange`.

Since the author of #510 closed that PR, this is just #510 with rustfmt run.

I also removed an unnecessary trailing semicolon that the latest rust
compiler now complains about.

Signed-off-by: Eliza Weisman <[email protected]>

Co-authored-by: Kornel <[email protected]>
  • Loading branch information
hawkw and kornelski authored Feb 5, 2021
1 parent eec547d commit 2c8c847
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
45 changes: 30 additions & 15 deletions src/proto/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ impl ReceivedPing {

impl UserPings {
pub(crate) fn send_ping(&self) -> Result<(), Option<proto::Error>> {
let prev = self.0.state.compare_and_swap(
USER_STATE_EMPTY, // current
USER_STATE_PENDING_PING, // new
Ordering::AcqRel,
);
let prev = self
.0
.state
.compare_exchange(
USER_STATE_EMPTY, // current
USER_STATE_PENDING_PING, // new
Ordering::AcqRel,
Ordering::Acquire,
)
.unwrap_or_else(|v| v);

match prev {
USER_STATE_EMPTY => {
Expand All @@ -234,11 +239,16 @@ impl UserPings {
// Must register before checking state, in case state were to change
// before we could register, and then the ping would just be lost.
self.0.pong_task.register(cx.waker());
let prev = self.0.state.compare_and_swap(
USER_STATE_RECEIVED_PONG, // current
USER_STATE_EMPTY, // new
Ordering::AcqRel,
);
let prev = self
.0
.state
.compare_exchange(
USER_STATE_RECEIVED_PONG, // current
USER_STATE_EMPTY, // new
Ordering::AcqRel,
Ordering::Acquire,
)
.unwrap_or_else(|v| v);

match prev {
USER_STATE_RECEIVED_PONG => Poll::Ready(Ok(())),
Expand All @@ -252,11 +262,16 @@ impl UserPings {

impl UserPingsRx {
fn receive_pong(&self) -> bool {
let prev = self.0.state.compare_and_swap(
USER_STATE_PENDING_PONG, // current
USER_STATE_RECEIVED_PONG, // new
Ordering::AcqRel,
);
let prev = self
.0
.state
.compare_exchange(
USER_STATE_PENDING_PONG, // current
USER_STATE_RECEIVED_PONG, // new
Ordering::AcqRel,
Ordering::Acquire,
)
.unwrap_or_else(|v| v);

if prev == USER_STATE_PENDING_PONG {
self.0.pong_task.wake();
Expand Down
2 changes: 1 addition & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@ impl proto::Peer for Peer {
reason: Reason::PROTOCOL_ERROR,
});
}}
};
}

b = b.version(Version::HTTP_2);

Expand Down

0 comments on commit 2c8c847

Please sign in to comment.