📄 kernel-thread-data.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>Per-thread data</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="Thread priorities"HREF="kernel-thread-priorities.html"><LINKREL="NEXT"TITLE="Thread destructors"HREF="kernel-thread-destructors.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-priorities.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="kernel-thread-destructors.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="KERNEL-THREAD-DATA">Per-thread data</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN660"></A><H2>Name</H2>cyg_thread_new_data_index, cyg_thread_free_data_index, cyg_thread_get_data, cyg_thread_get_data_ptr, cyg_thread_set_data -- Manipulate per-thread data</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN667"><H2>Synopsis</H2><DIVCLASS="FUNCSYNOPSIS"><ANAME="AEN668"><P></P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="FUNCSYNOPSISINFO">#include <cyg/kernel/kapi.h> </PRE></TD></TR></TABLE><P><CODE><CODECLASS="FUNCDEF">cyg_ucount32 cyg_thread_new_data_index</CODE>(void);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_thread_free_data_index</CODE>(cyg_ucount32 index);</CODE></P><P><CODE><CODECLASS="FUNCDEF">cyg_addrword_t cyg_thread_get_data</CODE>(cyg_ucount32 index);</CODE></P><P><CODE><CODECLASS="FUNCDEF">cyg_addrword_t* cyg_thread_get_data_ptr</CODE>(cyg_ucount32 index);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_thread_set_data</CODE>(cyg_ucount32 index, cyg_addrword_t data);</CODE></P><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="AEN696"></A><H2>Description</H2><P>In some applications and libraries it is useful to have some data thatis specific to each thread. For example, many of the functions in thePOSIX compatibility package return -1 to indicate an error and storeadditional information in what appears to be a global variable<TTCLASS="VARNAME">errno</TT>. However, if multiple threads make concurrentcalls into the POSIX library and if <TTCLASS="VARNAME">errno</TT> werereally a global variable then a thread would have no way of knowingwhether the current <TTCLASS="VARNAME">errno</TT> value really correspondedto the last POSIX call it made, or whether some other thread had runin the meantime and made a different POSIX call which updated thevariable. To avoid such confusion <TTCLASS="VARNAME">errno</TT> is insteadimplemented as a per-thread variable, and each thread has its owninstance. </P><P>The support for per-thread data can be disabled via the configurationoption <TTCLASS="VARNAME">CYGVAR_KERNEL_THREADS_DATA</TT>. If enabled, each<SPANCLASS="STRUCTNAME">cyg_thread</SPAN> data structure holds a small arrayof words. The size of this array is determined by the configurationoption <TTCLASS="VARNAME">CYGNUM_KERNEL_THREADS_DATA_MAX</TT>. When athread is created the array is filled with zeroes. </P><P>If an application needs to use per-thread data then it needs an indexinto this array which has not yet been allocated to other code. Thisindex can be obtained by calling<TTCLASS="FUNCTION">cyg_thread_new_data_index</TT>, and then used insubsequent calls to <TTCLASS="FUNCTION">cyg_thread_get_data</TT>.Typically indices are allocated during system initialization andstored in static variables. If for some reason a slot in the array isno longer required and can be re-used then it can be released by calling<TTCLASS="FUNCTION">cyg_thread_free_data_index</TT>, </P><P>The current per-thread data in a given slot can be obtained using<TTCLASS="FUNCTION">cyg_thread_get_data</TT>. This implicitly operates onthe current thread, and its single argument should be an index asreturned by <TTCLASS="FUNCTION">cyg_thread_new_data_index</TT>. Theper-thread data can be updated using<TTCLASS="FUNCTION">cyg_thread_set_data</TT>. If a particular item ofper-thread data is needed repeatedly then<TTCLASS="FUNCTION">cyg_thread_get_data_ptr</TT> can be used to obtain theaddress of the data, and indirecting through this pointer allows thedata to be examined and updated efficiently. </P><P>Some packages, for example the error and POSIX packages, havepre-allocated slots in the array of per-thread data. These slotsshould not normally be used by application code, and instead slotsshould be allocated during initialization by a call to<TTCLASS="FUNCTION">cyg_thread_new_data_index</TT>. If it is known that,for example, the configuration will never include the POSIXcompatibility package then application code may instead decide tore-use the slot allocated to that package,<TTCLASS="VARNAME">CYGNUM_KERNEL_THREADS_DATA_POSIX</TT>, but obviouslythis does involve a risk of strange and subtle bugs if theapplication's requirements ever change. </P></DIV><DIVCLASS="REFSECT1"><ANAME="KERNEL-THREAD-DATA-CONTEXT"></A><H2>Valid contexts</H2><P>Typically <TTCLASS="FUNCTION">cyg_thread_new_data_index</TT> is onlycalled during initialization, but may also be called at any time inthread context. <TTCLASS="FUNCTION">cyg_thread_free_data_index</TT>, ifused at all, can also be called during initialization or from threadcontext. <TTCLASS="FUNCTION">cyg_thread_get_data</TT>,<TTCLASS="FUNCTION">cyg_thread_get_data_ptr</TT>, and<TTCLASS="FUNCTION">cyg_thread_set_data</TT> may only be called fromthread context because they implicitly operate on the current thread. </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-priorities.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-thread-destructors.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Thread priorities</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="kernel.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Thread destructors</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -