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