about summary refs log tree commit diff
path: root/src/librustdoc/docfs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/librustdoc/docfs.rs')
-rw-r--r--src/librustdoc/docfs.rs27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/librustdoc/docfs.rs b/src/librustdoc/docfs.rs
index be066bdafa1..59393697dfe 100644
--- a/src/librustdoc/docfs.rs
+++ b/src/librustdoc/docfs.rs
@@ -9,11 +9,14 @@
 //! needs to read-after-write from a file, then it would be added to this
 //! abstraction.
 
+use std::cmp::max;
 use std::fs;
 use std::io;
 use std::path::{Path, PathBuf};
 use std::string::ToString;
 use std::sync::mpsc::Sender;
+use std::thread::available_parallelism;
+use threadpool::ThreadPool;
 
 pub(crate) trait PathError {
     fn new<S, P: AsRef<Path>>(e: S, path: P) -> Self
@@ -24,11 +27,21 @@ pub(crate) trait PathError {
 pub(crate) struct DocFS {
     sync_only: bool,
     errors: Option<Sender<String>>,
+    pool: ThreadPool,
 }
 
 impl DocFS {
     pub(crate) fn new(errors: Sender<String>) -> DocFS {
-        DocFS { sync_only: false, errors: Some(errors) }
+        const MINIMUM_NB_THREADS: usize = 2;
+        DocFS {
+            sync_only: false,
+            errors: Some(errors),
+            pool: ThreadPool::new(
+                available_parallelism()
+                    .map(|nb| max(nb.get(), MINIMUM_NB_THREADS))
+                    .unwrap_or(MINIMUM_NB_THREADS),
+            ),
+        }
     }
 
     pub(crate) fn set_sync_only(&mut self, sync_only: bool) {
@@ -54,12 +67,11 @@ impl DocFS {
     where
         E: PathError,
     {
-        #[cfg(windows)]
         if !self.sync_only {
             // A possible future enhancement after more detailed profiling would
             // be to create the file sync so errors are reported eagerly.
             let sender = self.errors.clone().expect("can't write after closing");
-            rayon::spawn(move || {
+            self.pool.execute(move || {
                 fs::write(&path, contents).unwrap_or_else(|e| {
                     sender.send(format!("\"{}\": {}", path.display(), e)).unwrap_or_else(|_| {
                         panic!("failed to send error on \"{}\"", path.display())
@@ -70,9 +82,12 @@ impl DocFS {
             fs::write(&path, contents).map_err(|e| E::new(e, path))?;
         }
 
-        #[cfg(not(windows))]
-        fs::write(&path, contents).map_err(|e| E::new(e, path))?;
-
         Ok(())
     }
 }
+
+impl Drop for DocFS {
+    fn drop(&mut self) {
+        self.pool.join();
+    }
+}