<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- permission is obtained from the copyright holder. -->
|
<!-- permission is obtained from the copyright holder. -->
|
<HTML
|
<HTML
|
><HEAD
|
><HEAD
|
><TITLE
|
><TITLE
|
>Thread termination</TITLE
|
>Thread termination</TITLE
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
<META
|
<META
|
NAME="GENERATOR"
|
NAME="GENERATOR"
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
"><LINK
|
"><LINK
|
REL="HOME"
|
REL="HOME"
|
TITLE="eCos Reference Manual"
|
TITLE="eCos Reference Manual"
|
HREF="ecos-ref.html"><LINK
|
HREF="ecos-ref.html"><LINK
|
REL="UP"
|
REL="UP"
|
TITLE="The eCos Kernel"
|
TITLE="The eCos Kernel"
|
HREF="kernel.html"><LINK
|
HREF="kernel.html"><LINK
|
REL="PREVIOUS"
|
REL="PREVIOUS"
|
TITLE="Thread control"
|
TITLE="Thread control"
|
HREF="kernel-thread-control.html"><LINK
|
HREF="kernel-thread-control.html"><LINK
|
REL="NEXT"
|
REL="NEXT"
|
TITLE="Thread priorities"
|
TITLE="Thread priorities"
|
HREF="kernel-thread-priorities.html"></HEAD
|
HREF="kernel-thread-priorities.html"></HEAD
|
><BODY
|
><BODY
|
CLASS="REFENTRY"
|
CLASS="REFENTRY"
|
BGCOLOR="#FFFFFF"
|
BGCOLOR="#FFFFFF"
|
TEXT="#000000"
|
TEXT="#000000"
|
LINK="#0000FF"
|
LINK="#0000FF"
|
VLINK="#840084"
|
VLINK="#840084"
|
ALINK="#0000FF"
|
ALINK="#0000FF"
|
><DIV
|
><DIV
|
CLASS="NAVHEADER"
|
CLASS="NAVHEADER"
|
><TABLE
|
><TABLE
|
SUMMARY="Header navigation table"
|
SUMMARY="Header navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TH
|
><TH
|
COLSPAN="3"
|
COLSPAN="3"
|
ALIGN="center"
|
ALIGN="center"
|
>eCos Reference Manual</TH
|
>eCos Reference Manual</TH
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="kernel-thread-control.html"
|
HREF="kernel-thread-control.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="80%"
|
WIDTH="80%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="kernel-thread-priorities.html"
|
HREF="kernel-thread-priorities.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"></DIV
|
WIDTH="100%"></DIV
|
><H1
|
><H1
|
><A
|
><A
|
NAME="KERNEL-THREAD-TERMINATION">Thread termination</H1
|
NAME="KERNEL-THREAD-TERMINATION">Thread termination</H1
|
><DIV
|
><DIV
|
CLASS="REFNAMEDIV"
|
CLASS="REFNAMEDIV"
|
><A
|
><A
|
NAME="AEN560"
|
NAME="AEN560"
|
></A
|
></A
|
><H2
|
><H2
|
>Name</H2
|
>Name</H2
|
>cyg_thread_exit, cyg_thread_kill, cyg_thread_delete -- Allow threads to terminate</DIV
|
>cyg_thread_exit, cyg_thread_kill, cyg_thread_delete -- Allow threads to terminate</DIV
|
><DIV
|
><DIV
|
CLASS="REFSYNOPSISDIV"
|
CLASS="REFSYNOPSISDIV"
|
><A
|
><A
|
NAME="AEN565"><H2
|
NAME="AEN565"><H2
|
>Synopsis</H2
|
>Synopsis</H2
|
><DIV
|
><DIV
|
CLASS="FUNCSYNOPSIS"
|
CLASS="FUNCSYNOPSIS"
|
><A
|
><A
|
NAME="AEN566"><P
|
NAME="AEN566"><P
|
></P
|
></P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="FUNCSYNOPSISINFO"
|
CLASS="FUNCSYNOPSISINFO"
|
>#include <cyg/kernel/kapi.h>
|
>#include <cyg/kernel/kapi.h>
|
</PRE
|
</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_thread_exit</CODE
|
>void cyg_thread_exit</CODE
|
>(void);</CODE
|
>(void);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_thread_kill</CODE
|
>void cyg_thread_kill</CODE
|
>(cyg_handle_t thread);</CODE
|
>(cyg_handle_t thread);</CODE
|
></P
|
></P
|
><P
|
><P
|
><CODE
|
><CODE
|
><CODE
|
><CODE
|
CLASS="FUNCDEF"
|
CLASS="FUNCDEF"
|
>void cyg_thread_delete</CODE
|
>void cyg_thread_delete</CODE
|
>(cyg_handle_t thread);</CODE
|
>(cyg_handle_t thread);</CODE
|
></P
|
></P
|
><P
|
><P
|
></P
|
></P
|
></DIV
|
></DIV
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="REFSECT1"
|
CLASS="REFSECT1"
|
><A
|
><A
|
NAME="AEN582"
|
NAME="AEN582"
|
></A
|
></A
|
><H2
|
><H2
|
>Description</H2
|
>Description</H2
|
><P
|
><P
|
>In many embedded systems the various threads are allocated statically,
|
>In many embedded systems the various threads are allocated statically,
|
created during initialization, and never need to terminate. This
|
created during initialization, and never need to terminate. This
|
avoids any need for dynamic memory allocation or other resource
|
avoids any need for dynamic memory allocation or other resource
|
management facilities. However if a given application does have a
|
management facilities. However if a given application does have a
|
requirement that some threads be created dynamically, must terminate,
|
requirement that some threads be created dynamically, must terminate,
|
and their resources such as the stack be reclaimed, then the kernel
|
and their resources such as the stack be reclaimed, then the kernel
|
provides the functions <TT
|
provides the functions <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_exit</TT
|
>cyg_thread_exit</TT
|
>,
|
>,
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_kill</TT
|
>cyg_thread_kill</TT
|
>, and
|
>, and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_delete</TT
|
>cyg_thread_delete</TT
|
>.
|
>.
|
</P
|
</P
|
><P
|
><P
|
><TT
|
><TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_exit</TT
|
>cyg_thread_exit</TT
|
> allows a thread to terminate
|
> allows a thread to terminate
|
itself, thus ensuring that it will not be run again by the scheduler.
|
itself, thus ensuring that it will not be run again by the scheduler.
|
However the <SPAN
|
However the <SPAN
|
CLASS="STRUCTNAME"
|
CLASS="STRUCTNAME"
|
>cyg_thread</SPAN
|
>cyg_thread</SPAN
|
> data structure passed
|
> data structure passed
|
to <TT
|
to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_create</TT
|
>cyg_thread_create</TT
|
> remains in use, and the
|
> remains in use, and the
|
handle returned by <TT
|
handle returned by <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_create</TT
|
>cyg_thread_create</TT
|
> remains
|
> remains
|
valid. This allows other threads to perform certain operations on the
|
valid. This allows other threads to perform certain operations on the
|
terminated thread, for example to determine its stack usage via
|
terminated thread, for example to determine its stack usage via
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_measure_stack_usage</TT
|
>cyg_thread_measure_stack_usage</TT
|
>. When the handle
|
>. When the handle
|
and <SPAN
|
and <SPAN
|
CLASS="STRUCTNAME"
|
CLASS="STRUCTNAME"
|
>cyg_thread</SPAN
|
>cyg_thread</SPAN
|
> structure are no longer
|
> structure are no longer
|
required, <TT
|
required, <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_delete</TT
|
>cyg_thread_delete</TT
|
> should be called to
|
> should be called to
|
release these resources. If the stack was dynamically allocated then
|
release these resources. If the stack was dynamically allocated then
|
this should not be freed until after the call to
|
this should not be freed until after the call to
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_delete</TT
|
>cyg_thread_delete</TT
|
>.
|
>.
|
</P
|
</P
|
><P
|
><P
|
>Alternatively, one thread may use <TT
|
>Alternatively, one thread may use <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_kill</TT
|
>cyg_thread_kill</TT
|
>
|
>
|
on another This has much the same effect as the affected thread
|
on another This has much the same effect as the affected thread
|
calling <TT
|
calling <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_exit</TT
|
>cyg_thread_exit</TT
|
>. However killing a thread
|
>. However killing a thread
|
is generally rather dangerous because no attempt is made to unlock any
|
is generally rather dangerous because no attempt is made to unlock any
|
synchronization primitives currently owned by that thread or release
|
synchronization primitives currently owned by that thread or release
|
any other resources that thread may have claimed. Therefore use of
|
any other resources that thread may have claimed. Therefore use of
|
this function should be avoided, and
|
this function should be avoided, and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_exit</TT
|
>cyg_thread_exit</TT
|
> is preferred.
|
> is preferred.
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_kill</TT
|
>cyg_thread_kill</TT
|
> cannot be used by a thread to
|
> cannot be used by a thread to
|
kill itself.
|
kill itself.
|
</P
|
</P
|
><P
|
><P
|
><TT
|
><TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_delete</TT
|
>cyg_thread_delete</TT
|
> should be used on a thread
|
> should be used on a thread
|
after it has exited and is no longer required. After this call the
|
after it has exited and is no longer required. After this call the
|
thread handle is no longer valid, and both the
|
thread handle is no longer valid, and both the
|
<SPAN
|
<SPAN
|
CLASS="STRUCTNAME"
|
CLASS="STRUCTNAME"
|
>cyg_thread</SPAN
|
>cyg_thread</SPAN
|
> structure and the thread stack can
|
> structure and the thread stack can
|
be re-used or freed. If <TT
|
be re-used or freed. If <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_delete</TT
|
>cyg_thread_delete</TT
|
> is
|
> is
|
invoked on a thread that is still running then there is an implicit
|
invoked on a thread that is still running then there is an implicit
|
call to <TT
|
call to <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_kill</TT
|
>cyg_thread_kill</TT
|
>.
|
>.
|
</P
|
</P
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="REFSECT1"
|
CLASS="REFSECT1"
|
><A
|
><A
|
NAME="KERNEL-THREAD-TERMINATION-CONTEXT"
|
NAME="KERNEL-THREAD-TERMINATION-CONTEXT"
|
></A
|
></A
|
><H2
|
><H2
|
>Valid contexts</H2
|
>Valid contexts</H2
|
><P
|
><P
|
><TT
|
><TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_exit</TT
|
>cyg_thread_exit</TT
|
>,
|
>,
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_kill</TT
|
>cyg_thread_kill</TT
|
> and
|
> and
|
<TT
|
<TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_thread_delete</TT
|
>cyg_thread_delete</TT
|
> can only be called from thread
|
> can only be called from thread
|
context.
|
context.
|
</P
|
</P
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="NAVFOOTER"
|
CLASS="NAVFOOTER"
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"><TABLE
|
WIDTH="100%"><TABLE
|
SUMMARY="Footer navigation table"
|
SUMMARY="Footer navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel-thread-control.html"
|
HREF="kernel-thread-control.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="ecos-ref.html"
|
HREF="ecos-ref.html"
|
ACCESSKEY="H"
|
ACCESSKEY="H"
|
>Home</A
|
>Home</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel-thread-priorities.html"
|
HREF="kernel-thread-priorities.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
>Thread control</TD
|
>Thread control</TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="kernel.html"
|
HREF="kernel.html"
|
ACCESSKEY="U"
|
ACCESSKEY="U"
|
>Up</A
|
>Up</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
>Thread priorities</TD
|
>Thread priorities</TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
></DIV
|
></DIV
|
></BODY
|
></BODY
|
></HTML
|
></HTML
|
|
|