Skip to main content

WorkerPool

Struct WorkerPool 

Source
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

§Examples

ⓘ
let _ = std::mem::size_of::<pharos_runtime_core::worker_pool::WorkerPool>();

Fields§

§sender: Mutex<Option<SyncSender<T>>>§shutdown: Arc<AtomicBool>§workers: Mutex<Vec<JoinHandle<()>>>

Implementations§

Source§

impl<T: Send + 'static> WorkerPool<T>

Source

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!());
Source

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!());
Source

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!());
Source

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();
Source

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();
Source

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();

Trait Implementations§

Source§

impl<T: Send + 'static> Drop for WorkerPool<T>

Source§

fn drop(&mut self)

Performs drop.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for WorkerPool<T>

§

impl<T> RefUnwindSafe for WorkerPool<T>

§

impl<T> Send for WorkerPool<T>

§

impl<T> Sync for WorkerPool<T>

§

impl<T> Unpin for WorkerPool<T>

§

impl<T> UnsafeUnpin for WorkerPool<T>

§

impl<T> UnwindSafe for WorkerPool<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.