#![doc(html_root_url = "https://docs.rs/rayon-core/1.4")]
use std::any::Any;
use std::env;
use std::error::Error;
use std::fmt;
use std::io;
use std::marker::PhantomData;
use std::str::FromStr;
extern crate crossbeam_deque;
#[macro_use]
extern crate lazy_static;
extern crate libc;
extern crate num_cpus;
#[cfg(test)]
extern crate rand;
#[macro_use]
mod log;
mod job;
mod join;
mod latch;
mod registry;
mod scope;
mod sleep;
mod spawn;
mod worker_local;
mod thread_pool;
mod unwind;
mod util;
mod compile_fail;
mod test;
pub mod tlv;
#[cfg(rayon_unstable)]
pub mod internal;
pub use join::{join, join_context};
pub use scope::{scope, Scope};
pub use registry::{Registry, mark_blocked, mark_unblocked};
pub use spawn::spawn;
pub use worker_local::WorkerLocal;
pub use thread_pool::current_thread_has_pending_tasks;
pub use thread_pool::current_thread_index;
pub use thread_pool::ThreadPool;
pub fn current_num_threads() -> usize {
::registry::Registry::current_num_threads()
}
#[derive(Debug)]
pub struct ThreadPoolBuildError {
kind: ErrorKind,
}
#[derive(Debug)]
enum ErrorKind {
GlobalPoolAlreadyInitialized,
IOError(io::Error),
}
#[derive(Default)]
pub struct ThreadPoolBuilder {
num_threads: usize,
panic_handler: Option<Box<PanicHandler>>,
get_thread_name: Option<Box<FnMut(usize) -> String>>,
stack_size: Option<usize>,
deadlock_handler: Option<Box<DeadlockHandler>>,
start_handler: Option<Box<StartHandler>>,
exit_handler: Option<Box<ExitHandler>>,
main_handler: Option<Box<MainHandler>>,
acquire_thread_handler: Option<Box<AcquireThreadHandler>>,
release_thread_handler: Option<Box<ReleaseThreadHandler>>,
breadth_first: bool,
}
#[deprecated(note = "Use `ThreadPoolBuilder`")]
#[derive(Default)]
pub struct Configuration {
builder: ThreadPoolBuilder,
}
type PanicHandler = Fn(Box<Any + Send>) + Send + Sync;
type DeadlockHandler = Fn() + Send + Sync;
type StartHandler = Fn(usize) + Send + Sync;
type ExitHandler = Fn(usize) + Send + Sync;
type MainHandler = Fn(usize, &mut FnMut()) + Send + Sync;
type AcquireThreadHandler = Fn() + Send + Sync;
type ReleaseThreadHandler = Fn() + Send + Sync;
impl ThreadPoolBuilder {
pub fn new() -> ThreadPoolBuilder {
ThreadPoolBuilder::default()
}
pub fn build(self) -> Result<ThreadPool, ThreadPoolBuildError> {
thread_pool::build(self)
}
pub fn build_global(self) -> Result<(), ThreadPoolBuildError> {
let registry = try!(registry::init_global_registry(self));
registry.wait_until_primed();
Ok(())
}
fn get_num_threads(&self) -> usize {
if self.num_threads > 0 {
self.num_threads
} else {
match env::var("RAYON_NUM_THREADS")
.ok()
.and_then(|s| usize::from_str(&s).ok())
{
Some(x) if x > 0 => return x,
Some(x) if x == 0 => return num_cpus::get(),
_ => {}
}
match env::var("RAYON_RS_NUM_CPUS")
.ok()
.and_then(|s| usize::from_str(&s).ok())
{
Some(x) if x > 0 => x,
_ => num_cpus::get(),
}
}
}
fn get_thread_name(&mut self, index: usize) -> Option<String> {
self.get_thread_name.as_mut().map(|c| c(index))
}
pub fn thread_name<F>(mut self, closure: F) -> Self
where
F: FnMut(usize) -> String + 'static,
{
self.get_thread_name = Some(Box::new(closure));
self
}
pub fn num_threads(mut self, num_threads: usize) -> ThreadPoolBuilder {
self.num_threads = num_threads;
self
}
fn take_panic_handler(&mut self) -> Option<Box<PanicHandler>> {
self.panic_handler.take()
}
pub fn panic_handler<H>(mut self, panic_handler: H) -> ThreadPoolBuilder
where
H: Fn(Box<Any + Send>) + Send + Sync + 'static,
{
self.panic_handler = Some(Box::new(panic_handler));
self
}
fn get_stack_size(&self) -> Option<usize> {
self.stack_size
}
pub fn stack_size(mut self, stack_size: usize) -> Self {
self.stack_size = Some(stack_size);
self
}
pub fn breadth_first(mut self) -> Self {
self.breadth_first = true;
self
}
fn get_breadth_first(&self) -> bool {
self.breadth_first
}
fn take_acquire_thread_handler(&mut self) -> Option<Box<AcquireThreadHandler>> {
self.acquire_thread_handler.take()
}
pub fn acquire_thread_handler<H>(mut self, acquire_thread_handler: H) -> ThreadPoolBuilder
where H: Fn() + Send + Sync + 'static
{
self.acquire_thread_handler = Some(Box::new(acquire_thread_handler));
self
}
fn take_release_thread_handler(&mut self) -> Option<Box<ReleaseThreadHandler>> {
self.release_thread_handler.take()
}
pub fn release_thread_handler<H>(mut self, release_thread_handler: H) -> ThreadPoolBuilder
where H: Fn() + Send + Sync + 'static
{
self.release_thread_handler = Some(Box::new(release_thread_handler));
self
}
fn take_deadlock_handler(&mut self) -> Option<Box<DeadlockHandler>> {
self.deadlock_handler.take()
}
pub fn deadlock_handler<H>(mut self, deadlock_handler: H) -> ThreadPoolBuilder
where H: Fn() + Send + Sync + 'static
{
self.deadlock_handler = Some(Box::new(deadlock_handler));
self
}
fn take_start_handler(&mut self) -> Option<Box<StartHandler>> {
self.start_handler.take()
}
pub fn start_handler<H>(mut self, start_handler: H) -> ThreadPoolBuilder
where
H: Fn(usize) + Send + Sync + 'static,
{
self.start_handler = Some(Box::new(start_handler));
self
}
fn take_exit_handler(&mut self) -> Option<Box<ExitHandler>> {
self.exit_handler.take()
}
pub fn exit_handler<H>(mut self, exit_handler: H) -> ThreadPoolBuilder
where
H: Fn(usize) + Send + Sync + 'static,
{
self.exit_handler = Some(Box::new(exit_handler));
self
}
fn take_main_handler(&mut self) -> Option<Box<MainHandler>> {
self.main_handler.take()
}
pub fn main_handler<H>(mut self, main_handler: H) -> ThreadPoolBuilder
where H: Fn(usize, &mut FnMut()) + Send + Sync + 'static
{
self.main_handler = Some(Box::new(main_handler));
self
}
}
#[allow(deprecated)]
impl Configuration {
pub fn new() -> Configuration {
Configuration {
builder: ThreadPoolBuilder::new(),
}
}
pub fn build(self) -> Result<ThreadPool, Box<Error + 'static>> {
self.builder.build().map_err(|e| e.into())
}
pub fn thread_name<F>(mut self, closure: F) -> Self
where
F: FnMut(usize) -> String + 'static,
{
self.builder = self.builder.thread_name(closure);
self
}
pub fn num_threads(mut self, num_threads: usize) -> Configuration {
self.builder = self.builder.num_threads(num_threads);
self
}
pub fn panic_handler<H>(mut self, panic_handler: H) -> Configuration
where
H: Fn(Box<Any + Send>) + Send + Sync + 'static,
{
self.builder = self.builder.panic_handler(panic_handler);
self
}
pub fn stack_size(mut self, stack_size: usize) -> Self {
self.builder = self.builder.stack_size(stack_size);
self
}
pub fn breadth_first(mut self) -> Self {
self.builder = self.builder.breadth_first();
self
}
pub fn start_handler<H>(mut self, start_handler: H) -> Configuration
where
H: Fn(usize) + Send + Sync + 'static,
{
self.builder = self.builder.start_handler(start_handler);
self
}
pub fn exit_handler<H>(mut self, exit_handler: H) -> Configuration
where
H: Fn(usize) + Send + Sync + 'static,
{
self.builder = self.builder.exit_handler(exit_handler);
self
}
fn into_builder(self) -> ThreadPoolBuilder {
self.builder
}
}
impl ThreadPoolBuildError {
fn new(kind: ErrorKind) -> ThreadPoolBuildError {
ThreadPoolBuildError { kind: kind }
}
}
impl Error for ThreadPoolBuildError {
fn description(&self) -> &str {
match self.kind {
ErrorKind::GlobalPoolAlreadyInitialized => {
"The global thread pool has already been initialized."
}
ErrorKind::IOError(ref e) => e.description(),
}
}
}
impl fmt::Display for ThreadPoolBuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ErrorKind::IOError(ref e) => e.fmt(f),
_ => self.description().fmt(f),
}
}
}
#[deprecated(note = "use `ThreadPoolBuilder::build_global`")]
#[allow(deprecated)]
pub fn initialize(config: Configuration) -> Result<(), Box<Error>> {
config.into_builder().build_global().map_err(|e| e.into())
}
impl fmt::Debug for ThreadPoolBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ThreadPoolBuilder {
ref num_threads,
ref get_thread_name,
ref panic_handler,
ref stack_size,
ref deadlock_handler,
ref start_handler,
ref main_handler,
ref exit_handler,
ref acquire_thread_handler,
ref release_thread_handler,
ref breadth_first,
} = *self;
struct ClosurePlaceholder;
impl fmt::Debug for ClosurePlaceholder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("<closure>")
}
}
let get_thread_name = get_thread_name.as_ref().map(|_| ClosurePlaceholder);
let panic_handler = panic_handler.as_ref().map(|_| ClosurePlaceholder);
let deadlock_handler = deadlock_handler.as_ref().map(|_| ClosurePlaceholder);
let start_handler = start_handler.as_ref().map(|_| ClosurePlaceholder);
let exit_handler = exit_handler.as_ref().map(|_| ClosurePlaceholder);
let main_handler = main_handler.as_ref().map(|_| ClosurePlaceholder);
let acquire_thread_handler = acquire_thread_handler.as_ref().map(|_| ClosurePlaceholder);
let release_thread_handler = release_thread_handler.as_ref().map(|_| ClosurePlaceholder);
f.debug_struct("ThreadPoolBuilder")
.field("num_threads", num_threads)
.field("get_thread_name", &get_thread_name)
.field("panic_handler", &panic_handler)
.field("stack_size", &stack_size)
.field("deadlock_handler", &deadlock_handler)
.field("start_handler", &start_handler)
.field("exit_handler", &exit_handler)
.field("main_handler", &main_handler)
.field("acquire_thread_handler", &acquire_thread_handler)
.field("release_thread_handler", &release_thread_handler)
.field("breadth_first", &breadth_first)
.finish()
}
}
#[allow(deprecated)]
impl fmt::Debug for Configuration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.builder.fmt(f)
}
}
#[derive(Debug)]
pub struct FnContext {
migrated: bool,
_marker: PhantomData<*mut ()>,
}
impl FnContext {
#[inline]
fn new(migrated: bool) -> Self {
FnContext {
migrated: migrated,
_marker: PhantomData,
}
}
}
impl FnContext {
#[inline]
pub fn migrated(&self) -> bool {
self.migrated
}
}