pub struct JobScope { /* private fields */ }
Expand description
Manages the creation and scheduling of jobs. This type can either be created from a JobSystem
instance or retrieved from a worker thread.
If you tried to use this with a thread that is not being tracked by the job system, all
the functions will fail with Error::InvalidThread
.
Implementations§
Source§impl JobScope
impl JobScope
Sourcepub fn new_from_system(job_system: &JobSystem) -> Self
pub fn new_from_system(job_system: &JobSystem) -> Self
Create a new instance from a job system.
Sourcepub fn new_from_thread() -> Result<Self, Error>
pub fn new_from_thread() -> Result<Self, Error>
Create a new instance on a thread managed by a job system. If this calling thread is not managed by the job system, this will fail
Sourcepub fn create_noop(&self) -> Result<ScopedJobHandle<'_, fn()>, Error>
pub fn create_noop(&self) -> Result<ScopedJobHandle<'_, fn()>, Error>
Allocates a new Job that does nothing.
This can be useful in cases where you need a parent job for grouping control.
Sourcepub unsafe fn create<T>(&self, job: T) -> Result<ScopedJobHandle<'_, T>, Error>
pub unsafe fn create<T>(&self, job: T) -> Result<ScopedJobHandle<'_, T>, Error>
Allocate a new Job
This function will check whether the closure fits in the job storage space and return an error if that’s the case.
§Safety
This function is unsafe since it’s not possible to accurately determine whether the provided closure’s capture will be captured multiple times or be alive until this closure has finished executing. This method is safe to call provided that you guarantee:
- The closure captures are valid during the execution of the job
- The closure doesn’t capture a writeable reference more than once
Sourcepub unsafe fn create_with_parent<'a, T, Y>(
&'a self,
parent: &ScopedJobHandle<'a, Y>,
job: T,
) -> Result<ScopedJobHandle<'a, T>, Error>
pub unsafe fn create_with_parent<'a, T, Y>( &'a self, parent: &ScopedJobHandle<'a, Y>, job: T, ) -> Result<ScopedJobHandle<'a, T>, Error>
Allocate a new Job as a child of another job.
The newly allocated will be created as a child of a parent job. What this means in practice is that the new job (child) can run parallel with the parent job and the parent job will not reach completion status until all of it’s children have finished.
§Safety
This function is unsafe since it’s not possible to accurately determine whether the provided closure’s capture will be captured multiple times or be alive until this closure has finished executing. This method is safe to call provided that you guarantee:
- The closure captures are valid during the execution of the job
- The closure doesn’t capture a writeable reference more than once
Sourcepub fn chain<T, Y>(
&self,
parent: &mut ScopedJobHandle<'_, T>,
child: &ScopedJobHandle<'_, Y>,
) -> Result<(), Error>
pub fn chain<T, Y>( &self, parent: &mut ScopedJobHandle<'_, T>, child: &ScopedJobHandle<'_, Y>, ) -> Result<(), Error>
Register the child job as a follow up to the parent job.
Every Job has the ability to chain follow up jobs on completion. This ensures the child job only runs when the parent is finished.
There is a limited capacity for the number of jobs you can chain with one job. This function will return error if we are unable to register the child job.
Sourcepub unsafe fn run<T>(
&self,
handle: &ScopedJobHandle<'_, T>,
) -> Result<(), Error>
pub unsafe fn run<T>( &self, handle: &ScopedJobHandle<'_, T>, ) -> Result<(), Error>
Execute a job.
§Safety
This is currently marked unsafe due to the reasons described in JobScope::create() and JobScope::create_with_parent().
Sourcepub fn wait<T>(&self, handle: &ScopedJobHandle<'_, T>) -> Result<(), Error>
pub fn wait<T>(&self, handle: &ScopedJobHandle<'_, T>) -> Result<(), Error>
Block and wait until the job has finished.
While we wait, the system will try to execute other jobs.
Sourcepub fn is_finished<T>(
&self,
handle: &ScopedJobHandle<'_, T>,
) -> Result<bool, Error>
pub fn is_finished<T>( &self, handle: &ScopedJobHandle<'_, T>, ) -> Result<bool, Error>
Check if a job has finished execution. Does not block.
Sourcepub fn for_each<'env, T, Y>(
&self,
slice: &'env mut [Y],
cb: T,
) -> Result<(), Error>
pub fn for_each<'env, T, Y>( &self, slice: &'env mut [Y], cb: T, ) -> Result<(), Error>
Given a slice of data and read-only closure, divide the slice into unique sub-slices which are distributed to the worker threads.
The closure will receive the following parameters in order:
- unique sub-slice over which the the current thread is operating on
- start index of the full slice
- end index of the the full slice Note use for_each_with_result() if you wish to produce output.