📄 kernel-counters.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>Counters</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="Exception handling"HREF="kernel-exceptions.html"><LINKREL="NEXT"TITLE="Clocks"HREF="kernel-clocks.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-exceptions.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="kernel-clocks.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="KERNEL-COUNTERS">Counters</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN854"></A><H2>Name</H2>cyg_counter_create, cyg_counter_delete, cyg_counter_current_value, cyg_counter_set_value, cyg_counter_tick -- Count event occurrences</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN861"><H2>Synopsis</H2><DIVCLASS="FUNCSYNOPSIS"><ANAME="AEN862"><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">void cyg_counter_create</CODE>(cyg_handle_t* handle, cyg_counter* counter);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_counter_delete</CODE>(cyg_handle_t counter);</CODE></P><P><CODE><CODECLASS="FUNCDEF">cyg_tick_count_t cyg_counter_current_value</CODE>(cyg_handle_t counter);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_counter_set_value</CODE>(cyg_handle_t counter, cyg_tick_count_t new_value);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_counter_tick</CODE>(cyg_handle_t counter);</CODE></P><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="KERNEL-COUNTERS-DESCRIPTION"></A><H2>Description</H2><P>Kernel counters can be used to keep track of how many times aparticular event has occurred. Usually this event is an externalsignal of some sort. The most common use of counters is in theimplementation of clocks, but they can be useful with other eventsources as well. Application code can attach <AHREF="kernel-alarms.html">alarms</A> to counters, causing a functionto be called when some number of events have occurred. </P><P>A new counter is initialized by a call to<TTCLASS="FUNCTION">cyg_counter_create</TT>. The first argument is used toreturn a handle to the new counter which can be used for subsequentoperations. The second argument allows the application to provide thememory needed for the object, thus eliminating any need for dynamicmemory allocation within the kernel. If a counter is no longerrequired and does not have any alarms attached then<TTCLASS="FUNCTION">cyg_counter_delete</TT> can be used to release theresources, allowing the <SPANCLASS="STRUCTNAME">cyg_counter</SPAN> datastructure to be re-used. </P><P>Initializing a counter does not automatically attach it to any sourceof events. Instead some other code needs to call<TTCLASS="FUNCTION">cyg_counter_tick</TT> whenever a suitable eventoccurs, which will cause the counter to be incremented and may causealarms to trigger. The current value associated with the counter canbe retrieved using <TTCLASS="FUNCTION">cyg_counter_current_value</TT> andmodified with <TTCLASS="FUNCTION">cyg_counter_set_value</TT>. Typicallythe latter function is only used during initialization, for example toset a clock to wallclock time, but it can be used to reset a counterif necessary. However <TTCLASS="FUNCTION">cyg_counter_set_value</TT> willnever trigger any alarms. A newly initialized counter has a startingvalue of 0. </P><P>The kernel provides two different implementations of counters. Thedefault is <TTCLASS="VARNAME">CYGIMP_KERNEL_COUNTERS_SINGLE_LIST</TT> whichstores all alarms attached to the counter on a single list. This issimple and usually efficient. However when a tick occurs the kernelcode has to traverse this list, typically at DSR level, so if thereare a significant number of alarms attached to a single counter thiswill affect the system's dispatch latency. The alternativeimplementation, <TTCLASS="VARNAME">CYGIMP_KERNEL_COUNTERS_MULTI_LIST</TT>,stores each alarm in one of an array of lists such that at most one ofthe lists needs to be searched per clock tick. This involves extracode and data, but can improve real-time responsiveness in somecircumstances. Another configuration option that is relevant hereis <TTCLASS="VARNAME">CYGIMP_KERNEL_COUNTERS_SORT_LIST</TT>, which isdisabled by default. This provides a trade off between doing workwhenever a new alarm is added to a counter and doing work whenever atick occurs. It is application-dependent which of these is moreappropriate. </P></DIV><DIVCLASS="REFSECT1"><ANAME="KERNEL-COUNTERS-CONTEXT"></A><H2>Valid contexts</H2><P><TTCLASS="FUNCTION">cyg_counter_create</TT> is typically called duringsystem initialization but may also be called in thread context.Similarly <TTCLASS="FUNCTION">cyg_counter_delete</TT> may be called duringinitialization or in thread context.<TTCLASS="FUNCTION">cyg_counter_current_value</TT>,<TTCLASS="FUNCTION">cyg_counter_set_value</TT> and<TTCLASS="FUNCTION">cyg_counter_tick</TT> may be called duringinitialization or from thread or DSR context. In fact,<TTCLASS="FUNCTION">cyg_counter_tick</TT> is usually called from inside aDSR in response to an external event of some sort. </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-exceptions.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-clocks.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Exception handling</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="kernel.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Clocks</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -