Struct zircon_object::task::Thread
source · [−]pub struct Thread { /* private fields */ }
Expand description
Runnable / computation entity
SYNOPSIS
TODO
DESCRIPTION
The thread object is the construct that represents a time-shared CPU execution context. Thread objects live associated to a particular Process Object which provides the memory and the handles to other objects necessary for I/O and computation.
Lifetime
Threads are created by calling Thread::create()
, but only start executing
when either Thread::start()
or Process::start()
are called. Both syscalls
take as an argument the entrypoint of the initial routine to execute.
The thread passed to Process::start()
should be the first thread to start execution
on a process.
A thread terminates execution:
- by calling
CurrentThread::exit()
- when the parent process terminates
- by calling
Task::kill()
- after generating an exception for which there is no handler or the handler decides to terminate the thread.
Returning from the entrypoint routine does not terminate execution. The last
action of the entrypoint should be to call CurrentThread::exit()
.
Closing the last handle to a thread does not terminate execution. In order to
forcefully kill a thread for which there is no available handle, use
KernelObject::get_child()
to obtain a handle to the thread. This method is strongly
discouraged. Killing a thread that is executing might leave the process in a
corrupt state.
Fuchsia native threads are always detached. That is, there is no join() operation needed to do a clean termination. However, some runtimes above the kernel, such as C11 or POSIX might require threads to be joined.
Signals
Threads provide the following signals:
When a thread is started THREAD_RUNNING
is asserted. When it is suspended
THREAD_RUNNING
is deasserted, and THREAD_SUSPENDED
is asserted. When
the thread is resumed THREAD_SUSPENDED
is deasserted and
THREAD_RUNNING
is asserted. When a thread terminates both
THREAD_RUNNING
and THREAD_SUSPENDED
are deasserted and
THREAD_TERMINATED
is asserted.
Note that signals are OR’d into the state maintained by the
KernelObject::wait_signal()
family of functions thus
you may see any combination of requested signals when they return.
Implementations
sourceimpl Thread
impl Thread
sourcepub fn create_with_ext(
proc: &Arc<Process>,
name: &str,
ext: impl Any + Send + Sync
) -> ZxResult<Arc<Self>>
pub fn create_with_ext(
proc: &Arc<Process>,
name: &str,
ext: impl Any + Send + Sync
) -> ZxResult<Arc<Self>>
Create a new thread with extension info.
Example
let job = Job::root();
let proc = Process::create(&job, "proc").unwrap();
// create a thread with extension info
let thread = Thread::create_with_ext(&proc, "thread", job.clone()).unwrap();
// get the extension info
let ext = thread.ext().downcast_ref::<Arc<Job>>().unwrap();
assert!(Arc::ptr_eq(ext, &job));
sourcepub fn context_cloned(&self) -> ZxResult<UserContext>
pub fn context_cloned(&self) -> ZxResult<UserContext>
Returns a copy of saved context of current thread, or Err(ZxError::BAD_STATE)
if the thread is running.
sourcepub fn with_context<T, F>(&self, f: F) -> ZxResult<T> where
F: FnOnce(&mut UserContext) -> T,
pub fn with_context<T, F>(&self, f: F) -> ZxResult<T> where
F: FnOnce(&mut UserContext) -> T,
Access saved context of current thread, or Err(ZxError::BAD_STATE)
if
the thread is running.
sourcepub fn backup_context(&self, context: UserContext, siginfo: usize, uctx: usize)
pub fn backup_context(&self, context: UserContext, siginfo: usize, uctx: usize)
Backup current user context before calling signal handler
sourcepub fn fetch_backup_context(&self) -> Option<(UserContext, usize, usize)>
pub fn fetch_backup_context(&self) -> Option<(UserContext, usize, usize)>
Fetch the context backup
sourcepub fn start_with_entry(
self: &Arc<Self>,
entry: usize,
stack: usize,
arg1: usize,
arg2: usize,
thread_fn: ThreadFn
) -> ZxResult
pub fn start_with_entry(
self: &Arc<Self>,
entry: usize,
stack: usize,
arg1: usize,
arg2: usize,
thread_fn: ThreadFn
) -> ZxResult
Setup the instruction and stack pointer, then tart execution on the thread
sourcepub fn read_state(&self, kind: ThreadStateKind, buf: &mut [u8]) -> ZxResult<usize>
pub fn read_state(&self, kind: ThreadStateKind, buf: &mut [u8]) -> ZxResult<usize>
Read one aspect of thread state.
sourcepub fn write_state(&self, kind: ThreadStateKind, buf: &[u8]) -> ZxResult
pub fn write_state(&self, kind: ThreadStateKind, buf: &[u8]) -> ZxResult
Write one aspect of thread state.
sourcepub fn get_thread_info(&self) -> ThreadInfo
pub fn get_thread_info(&self) -> ThreadInfo
Get the thread’s information.
sourcepub fn get_thread_exception_info(&self) -> ZxResult<ExceptionReport>
pub fn get_thread_exception_info(&self) -> ZxResult<ExceptionReport>
Get the thread’s exception report.
sourcepub fn state(&self) -> ThreadState
pub fn state(&self) -> ThreadState
Get the thread state.
sourcepub fn is_first_thread(&self) -> bool
pub fn is_first_thread(&self) -> bool
Whether this thread is the first thread of a process.
sourcepub fn flags(&self) -> ThreadFlag
pub fn flags(&self) -> ThreadFlag
Get the thread’s flags.
sourcepub fn update_flags(&self, f: impl FnOnce(&mut ThreadFlag))
pub fn update_flags(&self, f: impl FnOnce(&mut ThreadFlag))
Apply f
to the thread’s flags.
Trait Implementations
sourceimpl KernelObject for Thread
impl KernelObject for Thread
sourcefn signal_set(&self, signal: Signal)
fn signal_set(&self, signal: Signal)
Assert signal
.
sourcefn signal_clear(&self, signal: Signal)
fn signal_clear(&self, signal: Signal)
Deassert signal
.
sourcefn signal_change(&self, clear: Signal, set: Signal)
fn signal_change(&self, clear: Signal, set: Signal)
Change signal status: first clear
then set
indicated bits. Read more
sourcefn add_signal_callback(&self, callback: SignalHandler)
fn add_signal_callback(&self, callback: SignalHandler)
Add callback
for signal status changes. Read more
If the object is related to another (such as the other end of a channel, or the parent of a job), returns the KoID of that object, otherwise returns zero. Read more
sourcefn get_child(&self, _id: KoID) -> ZxResult<Arc<dyn KernelObject>>
fn get_child(&self, _id: KoID) -> ZxResult<Arc<dyn KernelObject>>
Attempt to find a child of the object with given KoID. Read more
sourcefn allowed_signals(&self) -> Signal
fn allowed_signals(&self) -> Signal
Get object’s allowed signals.
sourceimpl Task for Thread
impl Task for Thread
sourcefn kill(&self)
fn kill(&self)
Kill the task. The task do not terminate immediately when killed. It will terminate after all its children are terminated or some cleanups are finished. Read more
sourcefn exceptionate(&self) -> Arc<Exceptionate>
fn exceptionate(&self) -> Arc<Exceptionate>
Get the exceptionate.
sourcefn debug_exceptionate(&self) -> Arc<Exceptionate>
fn debug_exceptionate(&self) -> Arc<Exceptionate>
Get the debug exceptionate.
Auto Trait Implementations
impl !RefUnwindSafe for Thread
impl Send for Thread
impl Sync for Thread
impl Unpin for Thread
impl !UnwindSafe for Thread
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> CheckedAs for T
impl<T> CheckedAs for T
sourcefn checked_as<Dst>(self) -> Option<Dst> where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst> where
T: CheckedCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> CheckedCastFrom<Src> for Dst where
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dst where
Src: CheckedCast<Dst>,
sourcefn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
sourceimpl<T> OverflowingAs for T
impl<T> OverflowingAs for T
sourcefn overflowing_as<Dst>(self) -> (Dst, bool) where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool) where
T: OverflowingCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> OverflowingCastFrom<Src> for Dst where
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dst where
Src: OverflowingCast<Dst>,
sourcefn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
sourceimpl<T> SaturatingAs for T
impl<T> SaturatingAs for T
sourcefn saturating_as<Dst>(self) -> Dst where
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dst where
T: SaturatingCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> SaturatingCastFrom<Src> for Dst where
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dst where
Src: SaturatingCast<Dst>,
sourcefn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
sourceimpl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
sourcefn unwrapped_as<Dst>(self) -> Dst where
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dst where
T: UnwrappedCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> UnwrappedCastFrom<Src> for Dst where
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dst where
Src: UnwrappedCast<Dst>,
sourcefn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
sourceimpl<T> WrappingAs for T
impl<T> WrappingAs for T
sourcefn wrapping_as<Dst>(self) -> Dst where
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dst where
T: WrappingCast<Dst>,
Casts the value.
sourceimpl<Src, Dst> WrappingCastFrom<Src> for Dst where
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dst where
Src: WrappingCast<Dst>,
sourcefn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.