(&'static self, get_size: F)
| 285 | impl ThreadPool { |
| 286 | /// Resizes the thread pool to fill (almost) all available cores. After this |
| 287 | /// returns, the pool will have between 1 and 32 workers. Returns the new |
| 288 | /// size of the pool. |
| 289 | /// |
| 290 | /// This always leaves one core free, so that the main program loop can |
| 291 | /// continue executing on it. If you have 8 cores, calling this function |
| 292 | /// will add 7 workers to the pool (and then the main thread will become the |
| 293 | /// 8th worker if it makes a blocking call like `join`). |
| 294 | /// |
| 295 | /// See [`ThreadPool::resize`] for more information about resizing. |
| 296 | pub fn resize_to_available(&'static self) -> usize { |
| 297 | let mut available = |
| 298 | available_parallelism().map(NonZero::get).unwrap_or(1); |
| 299 | available = available.saturating_sub(1).max(1); |
| 300 | self.resize_to(available) |
| 301 | } |
| 302 | |
| 303 | /// Resizes the pool to the specified number of threads. Returns the new |
| 304 | /// size of the thread pool. The new size may be smaller than requested if |
| 305 | /// all the seats in the thread pool are occupied. |
| 306 | /// |
| 307 | /// See [`ThreadPool::resize`] for more information about resizing. |
| 308 | pub fn resize_to(&'static self, new_size: usize) -> usize { |
| 309 | self.resize(|_| new_size) |
| 310 | } |
| 311 | |
| 312 | /// Adds the given number of threads to the thread pool. Returns the new |
| 313 | /// size of the pool. The new size may be smaller than requested if all the |
| 314 | /// seats in the thread pool are occupied. |
| 315 | /// |
| 316 | /// See [`ThreadPool::resize`] for more information about resizing. |
| 317 | pub fn grow(&'static self, added_threads: usize) -> usize { |
| 318 | self.resize(|current_size| current_size.saturating_add(added_threads)) |
| 319 | } |
| 320 | |
| 321 | /// Removes the given number of threads from the thread pool. Returns the new |
| 322 | /// size of the pool. |
| 323 | /// |
| 324 | /// See [`ThreadPool::resize`] for more information about resizing. |
| 325 | pub fn shrink(&'static self, terminated_threads: usize) -> usize { |
| 326 | self.resize(|current_size| { |
| 327 | current_size.saturating_sub(terminated_threads) |
| 328 | }) |
| 329 | } |
| 330 | |
| 331 | /// Ensures that there is at least one worker thread attached to the thread |
| 332 | /// pool. This is mostly used to avoid deadlocks. This should be called |
| 333 | /// before blocking on a thread pool to ensure the block will eventually be |
| 334 | /// released. Returns the new size of the pool, which will be either the old |
| 335 | /// size or one. |
| 336 | /// |
| 337 | /// See [`ThreadPool::resize_to`] for more information about resizing. |
| 338 | pub fn populate(&'static self) -> usize { |
| 339 | self.resize( |
| 340 | |current_size| { |
| 341 | if current_size == 0 { 1 } else { current_size } |
| 342 | }, |
| 343 | ) |
| 344 | } |
no test coverage detected