📄 kernel-thread-destructors.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/). --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission is obtained from the copyright holder. --><HTML><HEAD><TITLE>Thread destructors</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos Reference Manual"HREF="ecos-ref.html"><LINKREL="UP"TITLE="The eCos Kernel"HREF="kernel.html"><LINKREL="PREVIOUS"TITLE="Per-thread data"HREF="kernel-thread-data.html"><LINKREL="NEXT"TITLE="Exception handling"HREF="kernel-exceptions.html"></HEAD><BODYCLASS="REFENTRY"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos Reference Manual</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="kernel-thread-data.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="kernel-exceptions.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="KERNEL-THREAD-DESTRUCTORS">Thread destructors</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN730"></A><H2>Name</H2>cyg_thread_add_destructor, cyg_thread_rem_destructor -- Call functions on thread termination</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN734"><H2>Synopsis</H2><DIVCLASS="FUNCSYNOPSIS"><ANAME="AEN735"><P></P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="FUNCSYNOPSISINFO">#include <cyg/kernel/kapi.h>typedef void (*<SPANCLASS="TYPE">cyg_thread_destructor_fn</SPAN>)(<SPANCLASS="TYPE">cyg_addrword_t</SPAN>); </PRE></TD></TR></TABLE><P><CODE><CODECLASS="FUNCDEF">cyg_bool_t cyg_thread_add_destructor</CODE>(cyg_thread_destructor_fn fn, cyg_addrword_t data);</CODE></P><P><CODE><CODECLASS="FUNCDEF">cyg_bool_t cyg_thread_rem_destructor</CODE>(cyg_thread_destructor_fn fn, cyg_addrword_t data);</CODE></P><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="AEN753"></A><H2>Description</H2><P>These functions are provided for cases when an application requires afunction to be automatically called when a thread exits. This is oftenuseful when, for example, freeing up resources allocated by the thread. </P><P>This support must be enabled with the configuration option<TTCLASS="VARNAME">CYGPKG_KERNEL_THREADS_DESTRUCTORS</TT>. When enabled,you may register a function of type<SPANCLASS="TYPE">cyg_thread_destructor_fn</SPAN> to be called on threadtermination using <TTCLASS="FUNCTION">cyg_thread_add_destructor</TT>. Youmay also provide it with a piece of arbitrary information in the<TTCLASS="PARAMETER"><I>data</I></TT> argument which will be passed to thedestructor function <TTCLASS="PARAMETER"><I>fn</I></TT> when the threadterminates. If you no longer wish to call a function previousregistered with <TTCLASS="FUNCTION">cyg_thread_add_destructor</TT>, youmay call <TTCLASS="FUNCTION">cyg_thread_rem_destructor</TT> with the sameparameters used to register the destructor function. Both thesefunctions return <TTCLASS="LITERAL">true</TT> on success and<TTCLASS="LITERAL">false</TT> on failure. </P><P>By default, thread destructors are per-thread, which means that registeringa destructor function only registers that function for the current thread.In other words, each thread has its own list of destructors.Alternatively you may disable the configuration option<TTCLASS="VARNAME">CYGSEM_KERNEL_THREADS_DESTRUCTORS_PER_THREAD</TT> in whichcase any registered destructors will be run when <SPANCLASS="emphasis"><ICLASS="EMPHASIS">any</I></SPAN>threads exit. In other words, the thread destructor list is global and allthreads have the same destructors. </P><P>There is a limit to the number of destructors which may be registered,which can be controlled with the<TTCLASS="VARNAME">CYGNUM_KERNEL_THREADS_DESTRUCTORS</TT> configurationoption. Increasing this value will very slightly increase the amountof memory in use, and when<TTCLASS="VARNAME">CYGSEM_KERNEL_THREADS_DESTRUCTORS_PER_THREAD</TT> isenabled, the amount of memory used per thread will increase. When thelimit has been reached, <TTCLASS="FUNCTION">cyg_thread_add_destructor</TT>will return <TTCLASS="LITERAL">false</TT>. </P></DIV><DIVCLASS="REFSECT1"><ANAME="KERNEL-THREAD-DESTRUCTORS-CONTEXT"></A><H2>Valid contexts</H2><P>When <TTCLASS="VARNAME">CYGSEM_KERNEL_THREADS_DESTRUCTORS_PER_THREAD</TT>is enabled, these functions must only be called from a thread contextas they implicitly operate on the current thread. When<TTCLASS="VARNAME">CYGSEM_KERNEL_THREADS_DESTRUCTORS_PER_THREAD</TT> isdisabled, these functions may be called from thread or DSR context,or at initialization time. </P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="kernel-thread-data.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-ref.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="kernel-exceptions.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Per-thread data</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="kernel.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Exception handling</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -