📄 kernel.sgml
字号:
The exact stack size requirements for any given thread depend on anumber of factors. The most important is of course the code that willbe executed in the context of this code: if this involves significantnesting of function calls, recursion, or large local arrays, then thestack size needs to be set to a suitably high value. There are somearchitectural issues, for example the number of cpu registers and thecalling conventions will have some effect on stack usage. Also,depending on the configuration, it is possible that some other codesuch as interrupt handlers will occasionally run on the currentthread's stack. This depends in part on configuration options such as<varname>CYGIMP_HAL_COMMON_INTERRUPTS_USE_INTERRUPT_STACK</varname>and <varname>CYGSEM_HAL_COMMON_INTERRUPTS_ALLOW_NESTING</varname>. </para> <para>Determining an application's actual stack size requirements is theresponsibility of the application developer, since the kernel cannotknow in advance what code a given thread will run. However, the systemdoes provide some hints about reasonable stack sizes in the form oftwo constants: <varname>CYGNUM_HAL_STACK_SIZE_MINIMUM</varname> and<varname>CYGNUM_HAL_STACK_SIZE_TYPICAL</varname>. These are defined bythe appropriate HAL package. The <varname>MINIMUM</varname> value isappropriate for a thread that just runs a single function and makesvery simple system calls. Trying to create a thread with a smallerstack than this is illegal. The <varname>TYPICAL</varname> value isappropriate for applications where application calls are nested nomore than half a dozen or so levels, and there are no large arrays onthe stack. </para> <para>If the stack sizes are not estimated correctly and a stack overflowoccurs, the probably result is some form of memory corruption. Thiscan be very hard to track down. The kernel does contain some code tohelp detect stack overflows, controlled by the configuration option<varname>CYGFUN_KERNEL_THREADS_STACK_CHECKING</varname>: a smallamount of space is reserved at the stack limit and filled with aspecial signature: every time a thread context switch occurs thissignature is checked, and if invalid that is a good indication (butnot absolute proof) that a stack overflow has occurred. This form ofstack checking is enabled by default when the system is built withdebugging enabled. A related configuration option is<varname>CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT</varname>: enablingthis option means that a thread can call the function<function>cyg_thread_measure_stack_usage</function> to find out themaximum stack usage to date. Note that this is not necessarily thetrue maximum because, for example, it is possible that in the currentrun no interrupt occurred at the worst possible moment. </para> </refsect1> <refsect1 id="kernel-thread-create-context"><title>Valid contexts</title> <para><function>cyg_thread_create</function> may be called duringinitialization and from within thread context. It may not be calledfrom inside a DSR. </para> </refsect1> <refsect1 id="kernel-thread-create-example"><title>Example</title> <para>A simple example of thread creation is shown below. This involvescreating five threads, one producer and four consumers or workers. Thethreads are created in the system's<function>cyg_user_start</function>: depending on the configuration itmight be more appropriate to do this elsewhere, for example inside<function>main</function>. </para> <programlisting width=72>#include <cyg/hal/hal_arch.h>#include <cyg/kernel/kapi.h>// These numbers depend entirely on your application#define NUMBER_OF_WORKERS 4#define PRODUCER_PRIORITY 10#define WORKER_PRIORITY 11#define PRODUCER_STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL#define WORKER_STACKSIZE (CYGNUM_HAL_STACK_SIZE_MINIMUM + 1024)static unsigned char producer_stack[PRODUCER_STACKSIZE];static unsigned char worker_stacks[NUMBER_OF_WORKERS][WORKER_STACKSIZE];static cyg_handle_t producer_handle, worker_handles[NUMBER_OF_WORKERS];static cyg_thread_t producer_thread, worker_threads[NUMBER_OF_WORKERS];static voidproducer(cyg_addrword_t data){ …}static voidworker(cyg_addrword_t data){ …}voidcyg_user_start(void){ int i; cyg_thread_create(PRODUCER_PRIORITY, &producer, 0, "producer", producer_stack, PRODUCER_STACKSIZE, &producer_handle, &producer_thread); cyg_thread_resume(producer_handle); for (i = 0; i < NUMBER_OF_WORKERS; i++) { cyg_thread_create(WORKER_PRIORITY, &worker, i, "worker", worker_stacks[i], WORKER_STACKSIZE, &(worker_handles[i]), &(worker_threads[i])); cyg_thread_resume(worker_handles[i]); }} </programlisting> </refsect1> <refsect1 id="kernel-thread-create-cxx"><title>Thread Entry Points and C++</title> <para>For code written in C++ the thread entry function must be either astatic member function of a class or an ordinary function outside anyclass. It cannot be a normal member function of a class because suchmember functions take an implicit additional argument<varname>this</varname>, and the kernel has no way of knowing whatvalue to use for this argument. One way around this problem is to makeuse of a special static member function, for example: </para> <programlisting width=72>class fred { public: void thread_function(); static void static_thread_aux(cyg_addrword_t);};voidfred::static_thread_aux(cyg_addrword_t objptr){ fred* object = static_cast<fred*>(objptr); object->thread_function();}static fred instance;extern "C" voidcyg_start( void ){ … cyg_thread_create( …, &fred::static_thread_aux, static_cast<cyg_addrword_t>(&instance), …); …} </programlisting> <para>Effectively this uses the <parameterclass="function">entry_data</parameter> argument to<function>cyg_thread_create</function> to hold the<varname>this</varname> pointer. Unfortunately this approach doesrequire the use of some C++ casts, so some of the type safety that canbe achieved when programming in C++ is lost. </para> </refsect1> </refentry><!-- }}} --><!-- {{{ Thread info --> <refentry id="kernel-thread-info"> <refmeta> <refentrytitle>Thread information</refentrytitle> </refmeta> <refnamediv> <refname>cyg_thread_self</refname> <refname>cyg_thread_idle_thread</refname> <refname>cyg_thread_get_stack_base</refname> <refname>cyg_thread_get_stack_size</refname> <refname>cyg_thread_measure_stack_usage</refname> <refname>cyg_thread_get_next</refname> <refname>cyg_thread_get_info</refname> <refname>cyg_thread_find</refname> <refpurpose>Get basic thread information</refpurpose> </refnamediv> <refsynopsisdiv> <funcsynopsis> <funcsynopsisinfo>#include <cyg/kernel/kapi.h> </funcsynopsisinfo> <funcprototype> <funcdef>cyg_handle_t <function>cyg_thread_self</function></funcdef> <void> </funcprototype> <funcprototype> <funcdef>cyg_handle_t <function>cyg_thread_idle_thread</function></funcdef> <void> </funcprototype> <funcprototype> <funcdef>cyg_addrword_t <function>cyg_thread_get_stack_base</function></funcdef> <paramdef>cyg_handle_t <parameter>thread</parameter></paramdef> </funcprototype> <funcprototype> <funcdef>cyg_uint32 <function>cyg_thread_get_stack_size</function></funcdef> <paramdef>cyg_handle_t <parameter>thread</parameter></paramdef> </funcprototype> <funcprototype> <funcdef>cyg_uint32 <function>cyg_thread_measure_stack_usage</function></funcdef> <paramdef>cyg_handle_t <parameter>thread</parameter></paramdef> </funcprototype> <funcprototype> <funcdef>cyg_bool <function>cyg_thread_get_next</function></funcdef> <paramdef>cyg_handle_t *<parameter>thread</parameter></paramdef> <paramdef>cyg_uint16 *<parameter>id</parameter></paramdef> </funcprototype> <funcprototype> <funcdef>cyg_bool <function>cyg_thread_get_info</function></funcdef> <paramdef>cyg_handle_t <parameter>thread</parameter></paramdef> <paramdef>cyg_uint16 <parameter>id</parameter></paramdef> <paramdef>cyg_thread_info *<parameter>info</parameter></paramdef> </funcprototype> <funcprototype> <funcdef>cyg_handle_t <function>cyg_thread_find</function></funcdef> <paramdef>cyg_uint16 <parameter>id</parameter></paramdef> </funcprototype> </funcsynopsis> </refsynopsisdiv> <refsect1 id="kernel-thread-info-description"><title>Description</title> <para>These functions can be used to obtain some basic information aboutvarious threads in the system. Typically they serve little or nopurpose in real applications, but they can be useful during debugging. </para> <para><function>cyg_thread_self</function> returns a handle correspondingto the current thread. It will be the same as the value filled in by<function>cyg_thread_create</function> when the current thread wascreated. This handle can then be passed to other functions such as<function>cyg_thread_get_priority</function>. </para> <para><function>cyg_thread_idle_thread</function> returns the handlecorresponding to the idle thread. This thread is created automaticallyby the kernel, so application-code has no other way of getting hold ofthis information. </para> <para><function>cyg_thread_get_stack_base</function> and<function>cyg_thread_get_stack_size</function> return informationabout a specific thread's stack. The values returned will match thevalues passed to <function>cyg_thread_create</function> when thisthread was created. </para> <para><function>cyg_thread_measure_stack_usage</function> is only availableif the configuration option<varname>CYGFUN_KERNEL_THREADS_STACK_MEASUREMENT</varname> is enabled.The return value is the maximum number of bytes of stack space used sofar by the specified thread. Note that this should not be considered atrue upper bound, for example it is possible that in the current testrun the specified thread has not yet been interrupted at the deepestpoint in the function call graph. Never the less the value returnedcan give some useful indication of the thread's stack requirements. </para> <para><function>cyg_thread_get_next</function> is used to enumerate all thecurrent threads in the system. It should be called initially with thelocations pointed to by <parameter>thread</parameter> and<parameter>id</parameter> set to zero. On return these will be set tothe handle and ID of the first thread. On subsequent calls, theseparameters should be left set to the values returned by the previouscall. The handle and ID of the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -