⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kernel-thread-create.html

📁 ecos 很好的源代码
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!-- 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 creation</TITLE
><meta name="MSSmartTagsPreventParsing" content="TRUE">
<META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="eCos Reference Manual"
HREF="ecos-ref.html"><LINK
REL="UP"
TITLE="The eCos Kernel"
HREF="kernel.html"><LINK
REL="PREVIOUS"
TITLE="SMP Support"
HREF="kernel-smp.html"><LINK
REL="NEXT"
TITLE="Thread information"
HREF="kernel-thread-info.html"></HEAD
><BODY
CLASS="REFENTRY"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>eCos Reference Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="kernel-smp.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="kernel-thread-info.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="KERNEL-THREAD-CREATE">Thread creation</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN256"
></A
><H2
>Name</H2
>cyg_thread_create&nbsp;--&nbsp;Create a new thread</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN259"><H2
>Synopsis</H2
><DIV
CLASS="FUNCSYNOPSIS"
><A
NAME="AEN260"><P
></P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="FUNCSYNOPSISINFO"
>#include &lt;cyg/kernel/kapi.h&gt;    
	</PRE
></TD
></TR
></TABLE
><P
><CODE
><CODE
CLASS="FUNCDEF"
>void cyg_thread_create</CODE
>(cyg_addrword_t sched_info, cyg_thread_entry_t* entry, cyg_addrword_t entry_data, char* name, void* stack_base, cyg_ucount32 stack_size, cyg_handle_t* handle, cyg_thread* thread);</CODE
></P
><P
></P
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="KERNEL-THREAD-CREATE-DESCRIPTION"
></A
><H2
>Description</H2
><P
>The <TT
CLASS="FUNCTION"
>cyg_thread_create</TT
> function allows application
code and eCos packages to create new threads. In many applications
this only happens during system initialization and all required data
is allocated statically.  However additional threads can be created at
any time, if necessary. A newly created thread is always in suspended
state and will not start running until it has been resumed via a call
to <TT
CLASS="FUNCTION"
>cyg_thread_resume</TT
>. Also, if threads are
created during system initialization then they will not start running
until the eCos scheduler has been started.
      </P
><P
>The <TT
CLASS="PARAMETER"
><I
>name</I
></TT
> argument is used
primarily for debugging purposes, making it easier to keep track of
which <SPAN
CLASS="STRUCTNAME"
>cyg_thread</SPAN
> structure is associated with
which application-level thread. The kernel configuration option
<TT
CLASS="VARNAME"
>CYGVAR_KERNEL_THREADS_NAME</TT
> controls whether or not
this name is actually used.
      </P
><P
>On creation each thread is assigned a unique handle, and this will be
stored in the location pointed at by the <TT
CLASS="PARAMETER"
><I
>handle</I
></TT
> argument. Subsequent operations on
this thread including the required
<TT
CLASS="FUNCTION"
>cyg_thread_resume</TT
> should use this handle to
identify the thread.
      </P
><P
>The kernel requires a small amount of space for each thread, in the
form of a <SPAN
CLASS="STRUCTNAME"
>cyg_thread</SPAN
> data structure, to hold
information such as the current state of that thread. To avoid any
need for dynamic memory allocation within the kernel this space has to
be provided by higher-level code, typically in the form of a static
variable. The <TT
CLASS="PARAMETER"
><I
>thread</I
></TT
> argument
provides this space.
      </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="KERNEL-THREAD-CREATE-ENTRY"
></A
><H2
>Thread Entry Point</H2
><P
>The entry point for a thread takes the form:
      </P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>void
thread_entry_function(cyg_addrword_t data)
{
    &#8230;
}
      </PRE
></TD
></TR
></TABLE
><P
>The second argument to <TT
CLASS="FUNCTION"
>cyg_thread_create</TT
> is a
pointer to such a function. The third argument <TT
CLASS="PARAMETER"
><I
>entry_data</I
></TT
> is used to pass additional
data to the function. Typically this takes the form of a pointer to
some static data, or a small integer, or <TT
CLASS="LITERAL"
>0</TT
> if the
thread does not require any additional data.
      </P
><P
>If the thread entry function ever returns then this is equivalent to
the thread calling <TT
CLASS="FUNCTION"
>cyg_thread_exit</TT
>. Even though
the thread will no longer run again, it remains registered with the
scheduler. If the application needs to re-use the
<SPAN
CLASS="STRUCTNAME"
>cyg_thread</SPAN
> data structure then a call to
<TT
CLASS="FUNCTION"
>cyg_thread_delete</TT
> is required first.
      </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="KERNEL-THREAD-CREATE-PRIORITIES"
></A
><H2
>Thread Priorities</H2
><P
>The <TT
CLASS="PARAMETER"
><I
>sched_info</I
></TT
> argument
provides additional information to the scheduler. The exact details
depend on the scheduler being used. For the bitmap and mlqueue
schedulers it is a small integer, typically in the range 0 to 31, with
0 being the highest priority. The lowest priority is normally used
only by the system's idle thread. The exact number of priorities is
controlled by the kernel configuration option
<TT
CLASS="VARNAME"
>CYGNUM_KERNEL_SCHED_PRIORITIES</TT
>. 
      </P
><P
>It is the responsibility of the application developer to be aware of
the various threads in the system, including those created by eCos
packages, and to ensure that all threads run at suitable priorities.
For threads created by other packages the documentation provided by
those packages should indicate any requirements.
      </P
><P
>The functions <TT
CLASS="FUNCTION"
>cyg_thread_set_priority</TT
>,
<TT
CLASS="FUNCTION"
>cyg_thread_get_priority</TT
>, and
<TT
CLASS="FUNCTION"
>cyg_thread_get_current_priority</TT
> can be used to
manipulate a thread's priority.
      </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="KERNEL-THREAD-CREATE-STACK"
></A
><H2
>Stacks and Stack Sizes</H2
><P
>Each thread needs its own stack for local variables and to keep track
of function calls and returns. Again it is expected that this stack is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -