job_pool/
lib.rs

1/*  Copyright (C) 2025 Saúl Valdelvira
2 *
3 *  This program is free software: you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation, version 3.
6 *
7 *  This program is distributed in the hope that it will be useful,
8 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *  GNU General Public License for more details.
11 *
12 *  You should have received a copy of the GNU General Public License
13 *  along with this program.  If not, see <https://www.gnu.org/licenses/>. */
14
15//! Thread Pool
16//!
17//! This crate contains code to run a Job pool.
18//!
19//! # Example
20//! ```rust,no_run
21//! use job_pool::*;
22//! use std::thread;
23//! use std::time::Duration;
24//!
25//! let conf = PoolConfig::default();
26//! let pool = ThreadPool::new(conf).unwrap();
27//! for _ in 0..10 {
28//!     pool.execute(|| {
29//!         thread::sleep(Duration::from_secs(5));
30//!     });
31//! }
32//! pool.join();
33//! ```
34
35#![cfg_attr(feature = "use-nightly-mpmc", feature(mpmc_channel))]
36
37#[cfg(feature = "bindings")]
38mod ffi;
39
40mod pool;
41mod worker;
42mod config;
43
44/* Switch between mpsc and mpmc until
45 * std::sync::mpmc is stabilized */
46
47#[cfg(feature = "use-nightly-mpmc")]
48#[path ="channel/mpmc.rs"]
49mod channel;
50
51#[cfg(not(feature = "use-nightly-mpmc"))]
52#[path ="channel/mpsc.rs"]
53mod channel;
54
55use std::{borrow::Cow, sync::{Arc, Condvar, Mutex}};
56
57pub use pool::ThreadPool;
58pub use config::PoolConfig;
59
60type Semaphore = Arc<(Mutex<u16>,Condvar)>;
61
62pub type Result<T> = std::result::Result<T,Cow<'static,str>>;