pub struct WorkerPool<T: Send + 'static> {
sender: Mutex<Option<SyncSender<T>>>,
shutdown: Arc<AtomicBool>,
workers: Mutex<Vec<JoinHandle<()>>>,
}Expand description
A bounded worker pool that executes an owned callback for each item.
§Fields
- This type stores module-owned state for
crate::worker_pool::WorkerPool.
§Examples
let _ = std::mem::size_of::<pharos_runtime_core::worker_pool::WorkerPool>();§Related
crate::worker_poolfor constructors, helpers, and neighboring runtime types.
Fields§
§sender: Mutex<Option<SyncSender<T>>>§shutdown: Arc<AtomicBool>§workers: Mutex<Vec<JoinHandle<()>>>Implementations§
Source§impl<T: Send + 'static> WorkerPool<T>
impl<T: Send + 'static> WorkerPool<T>
Sourcepub fn new(
worker_count: usize,
queue_capacity: usize,
handler: Arc<dyn Fn(T) + Send + Sync + 'static>,
) -> Result<Self, WorkerPoolError>
pub fn new( worker_count: usize, queue_capacity: usize, handler: Arc<dyn Fn(T) + Send + Sync + 'static>, ) -> Result<Self, WorkerPoolError>
Creates a pool with a bounded queue and a shared item handler.
§Parameters
worker_count: Input consumed by [crate::worker_pool::new].queue_capacity: Input consumed by [crate::worker_pool::new].handler: Input consumed by [crate::worker_pool::new].
§Returns
Returns the successful value produced by [crate::worker_pool::new] or an error when the operation fails.
§Errors
Returns an error value when [crate::worker_pool::new] cannot complete successfully.
§Performance
Performance is proportional to the size of the supplied inputs and any parsed or loaded data consumed by [crate::worker_pool::new].
§Examples
let _result = pharos_runtime_core::worker_pool::new(todo!(), todo!(), todo!());§Related
crate::worker_poolfor adjacent helpers that share the same runtime surface.
Sourcepub fn enqueue(&self, item: T) -> Result<(), WorkerPoolError>
pub fn enqueue(&self, item: T) -> Result<(), WorkerPoolError>
Enqueues one item without allowing the queue to grow without bound.
§Parameters
item: Input consumed by [crate::worker_pool::enqueue].
§Returns
Returns the successful value produced by [crate::worker_pool::enqueue] or an error when the operation fails.
§Errors
Returns an error value when [crate::worker_pool::enqueue] cannot complete successfully.
§Examples
let _result = pharos_runtime_core::worker_pool::enqueue(todo!());§Related
crate::worker_poolfor adjacent helpers that share the same runtime surface.
Sourcepub fn enqueue_with_rejection<F>(
&self,
item: T,
reject: F,
) -> Result<(), WorkerPoolError>where
F: FnOnce(T),
pub fn enqueue_with_rejection<F>(
&self,
item: T,
reject: F,
) -> Result<(), WorkerPoolError>where
F: FnOnce(T),
Enqueues one item and returns ownership to a bounded overload handler when full.
§Parameters
item: Input consumed by [crate::worker_pool::enqueue_with_rejection].reject: Input consumed by [crate::worker_pool::enqueue_with_rejection].
§Returns
Returns the successful value produced by [crate::worker_pool::enqueue_with_rejection] or an error when the operation fails.
§Errors
Returns an error value when [crate::worker_pool::enqueue_with_rejection] cannot complete successfully.
§Examples
let _result = pharos_runtime_core::worker_pool::enqueue_with_rejection(todo!(), todo!());§Related
crate::worker_poolfor adjacent helpers that share the same runtime surface.
Sourcepub fn request_shutdown(&self) -> Result<(), WorkerPoolError>
pub fn request_shutdown(&self) -> Result<(), WorkerPoolError>
Requests shutdown and closes the queue to new work.
§Returns
Returns the successful value produced by [crate::worker_pool::request_shutdown] or an error when the operation fails.
§Errors
Returns an error value when [crate::worker_pool::request_shutdown] cannot complete successfully.
§Examples
let _result = pharos_runtime_core::worker_pool::request_shutdown();§Related
crate::worker_poolfor adjacent helpers that share the same runtime surface.
Sourcepub fn join(&self) -> Result<(), WorkerPoolError>
pub fn join(&self) -> Result<(), WorkerPoolError>
Joins all workers after shutdown has been requested.
§Returns
Returns the successful value produced by [crate::worker_pool::join] or an error when the operation fails.
§Errors
Returns an error value when [crate::worker_pool::join] cannot complete successfully.
§Performance
Performance is proportional to the size of the supplied inputs and any parsed or loaded data consumed by [crate::worker_pool::join].
§Examples
let _result = pharos_runtime_core::worker_pool::join();§Related
crate::worker_poolfor adjacent helpers that share the same runtime surface.
Sourcepub fn destroy(self) -> Result<(), WorkerPoolError>
pub fn destroy(self) -> Result<(), WorkerPoolError>
Shuts down and joins the pool, releasing all worker handles.
§Returns
Returns the successful value produced by [crate::worker_pool::destroy] or an error when the operation fails.
§Errors
Returns an error value when [crate::worker_pool::destroy] cannot complete successfully.
§Examples
let _result = pharos_runtime_core::worker_pool::destroy();§Related
crate::worker_poolfor adjacent helpers that share the same runtime surface.