]>
git.proxmox.com Git - rustc.git/blob - vendor/rayon-core/src/job.rs
1 use crate::latch
::Latch
;
3 use crossbeam_deque
::{Injector, Steal}
;
5 use std
::cell
::UnsafeCell
;
8 pub(super) enum JobResult
<T
> {
11 Panic(Box
<dyn Any
+ Send
>),
14 /// A `Job` is used to advertise work for other threads that they may
15 /// want to steal. In accordance with time honored tradition, jobs are
16 /// arranged in a deque, so that thieves can take from the top of the
17 /// deque while the main worker manages the bottom of the deque. This
18 /// deque is managed by the `thread_pool` module.
19 pub(super) trait Job
{
20 /// Unsafe: this may be called from a different thread than the one
21 /// which scheduled the job, so the implementer must ensure the
22 /// appropriate traits are met, whether `Send`, `Sync`, or both.
23 unsafe fn execute(this
: *const Self);
26 /// Effectively a Job trait object. Each JobRef **must** be executed
27 /// exactly once, or else data may leak.
29 /// Internally, we store the job's data in a `*const ()` pointer. The
30 /// true type is something like `*const StackJob<...>`, but we hide
31 /// it. We also carry the "execute fn" from the `Job` trait.
32 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
33 pub(super) struct JobRef
{
35 execute_fn
: unsafe fn(*const ()),
38 unsafe impl Send
for JobRef {}
39 unsafe impl Sync
for JobRef {}
42 /// Unsafe: caller asserts that `data` will remain valid until the
44 pub(super) unsafe fn new
<T
>(data
: *const T
) -> JobRef
48 let fn_ptr
: unsafe fn(*const T
) = <T
as Job
>::execute
;
52 pointer
: data
as *const (),
53 execute_fn
: mem
::transmute(fn_ptr
),
58 pub(super) unsafe fn execute(&self) {
59 (self.execute_fn
)(self.pointer
)
63 /// A job that will be owned by a stack slot. This means that when it
64 /// executes it need not free any heap data, the cleanup occurs when
65 /// the stack frame is later popped. The function parameter indicates
66 /// `true` if the job was stolen -- executed on a different thread.
67 pub(super) struct StackJob
<L
, F
, R
>
70 F
: FnOnce(bool
) -> R
+ Send
,
74 func
: UnsafeCell
<Option
<F
>>,
75 result
: UnsafeCell
<JobResult
<R
>>,
78 impl<L
, F
, R
> StackJob
<L
, F
, R
>
81 F
: FnOnce(bool
) -> R
+ Send
,
84 pub(super) fn new(func
: F
, latch
: L
) -> StackJob
<L
, F
, R
> {
87 func
: UnsafeCell
::new(Some(func
)),
88 result
: UnsafeCell
::new(JobResult
::None
),
92 pub(super) unsafe fn as_job_ref(&self) -> JobRef
{
96 pub(super) unsafe fn run_inline(self, stolen
: bool
) -> R
{
97 self.func
.into_inner().unwrap()(stolen
)
100 pub(super) unsafe fn into_result(self) -> R
{
101 self.result
.into_inner().into_return_value()
105 impl<L
, F
, R
> Job
for StackJob
<L
, F
, R
>
108 F
: FnOnce(bool
) -> R
+ Send
,
111 unsafe fn execute(this
: *const Self) {
112 fn call
<R
>(func
: impl FnOnce(bool
) -> R
) -> impl FnOnce() -> R
{
117 let abort
= unwind
::AbortIfPanic
;
118 let func
= (*this
.func
.get()).take().unwrap();
119 (*this
.result
.get()) = match unwind
::halt_unwinding(call(func
)) {
120 Ok(x
) => JobResult
::Ok(x
),
121 Err(x
) => JobResult
::Panic(x
),
128 /// Represents a job stored in the heap. Used to implement
129 /// `scope`. Unlike `StackJob`, when executed, `HeapJob` simply
130 /// invokes a closure, which then triggers the appropriate logic to
131 /// signal that the job executed.
133 /// (Probably `StackJob` should be refactored in a similar fashion.)
134 pub(super) struct HeapJob
<BODY
>
136 BODY
: FnOnce() + Send
,
138 job
: UnsafeCell
<Option
<BODY
>>,
141 impl<BODY
> HeapJob
<BODY
>
143 BODY
: FnOnce() + Send
,
145 pub(super) fn new(func
: BODY
) -> Self {
147 job
: UnsafeCell
::new(Some(func
)),
151 /// Creates a `JobRef` from this job -- note that this hides all
152 /// lifetimes, so it is up to you to ensure that this JobRef
153 /// doesn't outlive any data that it closes over.
154 pub(super) unsafe fn as_job_ref(self: Box
<Self>) -> JobRef
{
155 let this
: *const Self = mem
::transmute(self);
160 impl<BODY
> Job
for HeapJob
<BODY
>
162 BODY
: FnOnce() + Send
,
164 unsafe fn execute(this
: *const Self) {
165 let this
: Box
<Self> = mem
::transmute(this
);
166 let job
= (*this
.job
.get()).take().unwrap();
171 impl<T
> JobResult
<T
> {
172 /// Convert the `JobResult` for a job that has finished (and hence
173 /// its JobResult is populated) into its return value.
175 /// NB. This will panic if the job panicked.
176 pub(super) fn into_return_value(self) -> T
{
178 JobResult
::None
=> unreachable
!(),
179 JobResult
::Ok(x
) => x
,
180 JobResult
::Panic(x
) => unwind
::resume_unwinding(x
),
185 /// Indirect queue to provide FIFO job priority.
186 pub(super) struct JobFifo
{
187 inner
: Injector
<JobRef
>,
191 pub(super) fn new() -> Self {
193 inner
: Injector
::new(),
197 pub(super) unsafe fn push(&self, job_ref
: JobRef
) -> JobRef
{
198 // A little indirection ensures that spawns are always prioritized in FIFO order. The
199 // jobs in a thread's deque may be popped from the back (LIFO) or stolen from the front
200 // (FIFO), but either way they will end up popping from the front of this queue.
201 self.inner
.push(job_ref
);
206 impl Job
for JobFifo
{
207 unsafe fn execute(this
: *const Self) {
208 // We "execute" a queue by executing its first job, FIFO.
210 match (*this
).inner
.steal() {
211 Steal
::Success(job_ref
) => break job_ref
.execute(),
212 Steal
::Empty
=> panic
!("FIFO is empty"),