about summary refs log tree commit diff
path: root/src/libstd/sync/mpsc/shared.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-11-30 12:42:44 +0000
committerbors <bors@rust-lang.org>2019-11-30 12:42:44 +0000
commitd8bdb3fdcbd88eb16e1a6669236122c41ed2aed3 (patch)
tree33974ee0e3d5976f284b056e03e6ef529d15e563 /src/libstd/sync/mpsc/shared.rs
parent8f1bbd69e13c9e04a4c2b75612bc0c31af972439 (diff)
parentb14d9c21203ea79035bf4a8a8a68ad34658a265f (diff)
downloadrust-d8bdb3fdcbd88eb16e1a6669236122c41ed2aed3.tar.gz
rust-d8bdb3fdcbd88eb16e1a6669236122c41ed2aed3.zip
Auto merge of #66887 - dtolnay:rollup-uxowp8d, r=Centril
Rollup of 4 pull requests

Successful merges:

 - #66818 (Format libstd/os with rustfmt)
 - #66819 (Format libstd/sys with rustfmt)
 - #66820 (Format libstd with rustfmt)
 - #66847 (Allow any identifier as format arg name)

Failed merges:

r? @ghost
Diffstat (limited to 'src/libstd/sync/mpsc/shared.rs')
-rw-r--r--src/libstd/sync/mpsc/shared.rs57
1 files changed, 37 insertions, 20 deletions
diff --git a/src/libstd/sync/mpsc/shared.rs b/src/libstd/sync/mpsc/shared.rs
index dbcdcdac932..2b0393573fd 100644
--- a/src/libstd/sync/mpsc/shared.rs
+++ b/src/libstd/sync/mpsc/shared.rs
@@ -7,7 +7,6 @@
 /// High level implementation details can be found in the comment of the parent
 /// module. You'll also note that the implementation of the shared and stream
 /// channels are quite similar, and this is no coincidence!
-
 pub use self::Failure::*;
 use self::StartResult::*;
 
@@ -17,7 +16,7 @@ use core::isize;
 
 use crate::cell::UnsafeCell;
 use crate::ptr;
-use crate::sync::atomic::{AtomicUsize, AtomicIsize, AtomicBool, Ordering};
+use crate::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
 use crate::sync::mpsc::blocking::{self, SignalToken};
 use crate::sync::mpsc::mpsc_queue as mpsc;
 use crate::sync::{Mutex, MutexGuard};
@@ -34,9 +33,9 @@ const MAX_STEALS: isize = 1 << 20;
 
 pub struct Packet<T> {
     queue: mpsc::Queue<T>,
-    cnt: AtomicIsize, // How many items are on this channel
+    cnt: AtomicIsize,          // How many items are on this channel
     steals: UnsafeCell<isize>, // How many times has a port received without blocking?
-    to_wake: AtomicUsize, // SignalToken for wake up
+    to_wake: AtomicUsize,      // SignalToken for wake up
 
     // The number of channels which are currently using this packet.
     channels: AtomicUsize,
@@ -92,9 +91,7 @@ impl<T> Packet<T> {
     // threads in select().
     //
     // This can only be called at channel-creation time
-    pub fn inherit_blocker(&self,
-                           token: Option<SignalToken>,
-                           guard: MutexGuard<'_, ()>) {
+    pub fn inherit_blocker(&self, token: Option<SignalToken>, guard: MutexGuard<'_, ()>) {
         token.map(|token| {
             assert_eq!(self.cnt.load(Ordering::SeqCst), 0);
             assert_eq!(self.to_wake.load(Ordering::SeqCst), 0);
@@ -119,7 +116,9 @@ impl<T> Packet<T> {
             // To offset this bad increment, we initially set the steal count to
             // -1. You'll find some special code in abort_selection() as well to
             // ensure that this -1 steal count doesn't escape too far.
-            unsafe { *self.steals.get() = -1; }
+            unsafe {
+                *self.steals.get() = -1;
+            }
         });
 
         // When the shared packet is constructed, we grabbed this lock. The
@@ -132,7 +131,9 @@ impl<T> Packet<T> {
 
     pub fn send(&self, t: T) -> Result<(), T> {
         // See Port::drop for what's going on
-        if self.port_dropped.load(Ordering::SeqCst) { return Err(t) }
+        if self.port_dropped.load(Ordering::SeqCst) {
+            return Err(t);
+        }
 
         // Note that the multiple sender case is a little trickier
         // semantically than the single sender case. The logic for
@@ -160,7 +161,7 @@ impl<T> Packet<T> {
         // received". Once we get beyond this check, we have permanently
         // entered the realm of "this may be received"
         if self.cnt.load(Ordering::SeqCst) < DISCONNECTED + FUDGE {
-            return Err(t)
+            return Err(t);
         }
 
         self.queue.push(t);
@@ -197,7 +198,7 @@ impl<T> Packet<T> {
                         // maybe we're done, if we're not the last ones
                         // here, then we need to go try again.
                         if self.sender_drain.fetch_sub(1, Ordering::SeqCst) == 1 {
-                            break
+                            break;
                         }
                     }
 
@@ -236,7 +237,10 @@ impl<T> Packet<T> {
         }
 
         match self.try_recv() {
-            data @ Ok(..) => unsafe { *self.steals.get() -= 1; data },
+            data @ Ok(..) => unsafe {
+                *self.steals.get() -= 1;
+                data
+            },
             data => data,
         }
     }
@@ -252,12 +256,16 @@ impl<T> Packet<T> {
             let steals = ptr::replace(self.steals.get(), 0);
 
             match self.cnt.fetch_sub(1 + steals, Ordering::SeqCst) {
-                DISCONNECTED => { self.cnt.store(DISCONNECTED, Ordering::SeqCst); }
+                DISCONNECTED => {
+                    self.cnt.store(DISCONNECTED, Ordering::SeqCst);
+                }
                 // If we factor in our steals and notice that the channel has no
                 // data, we successfully sleep
                 n => {
                     assert!(n >= 0);
-                    if n - steals <= 0 { return Installed }
+                    if n - steals <= 0 {
+                        return Installed;
+                    }
                 }
             }
 
@@ -290,7 +298,10 @@ impl<T> Packet<T> {
                 loop {
                     thread::yield_now();
                     match self.queue.pop() {
-                        mpsc::Data(t) => { data = t; break }
+                        mpsc::Data(t) => {
+                            data = t;
+                            break;
+                        }
                         mpsc::Empty => panic!("inconsistent => empty"),
                         mpsc::Inconsistent => {}
                     }
@@ -361,9 +372,13 @@ impl<T> Packet<T> {
         }
 
         match self.cnt.swap(DISCONNECTED, Ordering::SeqCst) {
-            -1 => { self.take_to_wake().signal(); }
+            -1 => {
+                self.take_to_wake().signal();
+            }
             DISCONNECTED => {}
-            n => { assert!(n >= 0); }
+            n => {
+                assert!(n >= 0);
+            }
         }
     }
 
@@ -380,7 +395,9 @@ impl<T> Packet<T> {
             // control of this thread.
             loop {
                 match self.queue.pop() {
-                    mpsc::Data(..) => { steals += 1; }
+                    mpsc::Data(..) => {
+                        steals += 1;
+                    }
                     mpsc::Empty | mpsc::Inconsistent => break,
                 }
             }
@@ -406,7 +423,7 @@ impl<T> Packet<T> {
                 self.cnt.store(DISCONNECTED, Ordering::SeqCst);
                 DISCONNECTED
             }
-            n => n
+            n => n,
         }
     }
 
@@ -432,7 +449,7 @@ impl<T> Packet<T> {
         // positive.
         let steals = {
             let cnt = self.cnt.load(Ordering::SeqCst);
-            if cnt < 0 && cnt != DISCONNECTED {-cnt} else {0}
+            if cnt < 0 && cnt != DISCONNECTED { -cnt } else { 0 }
         };
         let prev = self.bump(steals + 1);