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

📄 kernel-schedcontrol.html

📁 有关ecos2。0介绍了实时嵌入式的结构以及线程调度的实现和内存的管理等
💻 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>Scheduler Control</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="Spinlocks"HREF="kernel-spinlocks.html"><LINKREL="NEXT"TITLE="Interrupt Handling"HREF="kernel-interrupts.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-spinlocks.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="kernel-interrupts.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="KERNEL-SCHEDCONTROL">Scheduler Control</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN1784"></A><H2>Name</H2>cyg_scheduler_start, cyg_scheduler_lock, cyg_scheduler_unlock, cyg_scheduler_safe_lock, cyg_scheduler_read_lock&nbsp;--&nbsp;Control the state of the scheduler</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN1791"><H2>Synopsis</H2><DIVCLASS="FUNCSYNOPSIS"><ANAME="AEN1792"><P></P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="FUNCSYNOPSISINFO">#include &lt;cyg/kernel/kapi.h&gt;        </PRE></TD></TR></TABLE><P><CODE><CODECLASS="FUNCDEF">void cyg_scheduler_start</CODE>(void);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_scheduler_lock</CODE>(void);</CODE></P><P><CODE><CODECLASS="FUNCDEF">void cyg_scheduler_unlock</CODE>(void);</CODE></P><P><CODE><CODECLASS="FUNCDEF">cyg_ucount32 cyg_scheduler_read_lock</CODE>(void);</CODE></P><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="KERNEL-SCHEDCONTROL-DESCRIPTION"></A><H2>Description</H2><P><TTCLASS="FUNCTION">cyg_scheduler_start</TT> should only be called once,to mark the end of system initialization. In typical configurations itis called automatically by the system startup, but some applicationsmay bypass the standard startup in which case<TTCLASS="FUNCTION">cyg_scheduler_start</TT> will have to be calledexplicitly. The call will enable system interrupts, allowing I/Ooperations to commence. Then the scheduler will be invoked and controlwill be transferred to the highest priority runnable thread. The callwill never return.      </P><P>The various data structures inside the eCos kernel must be protectedagainst concurrent updates. Consider a call to<TTCLASS="FUNCTION">cyg_semaphore_post</TT> which causes a thread to bewoken up: the semaphore data structure must be updated to remove thethread from its queue; the scheduler data structure must also beupdated to mark the thread as runnable; it is possible that the newlyrunnable thread has a higher priority than the current one, in whichcase preemption is required. If in the middle of the semaphore postcall an interrupt occurred and the interrupt handler tried tomanipulate the same data structures, for example by making anotherthread runnable, then it is likely that the structures will be left inan inconsistent state and the system will fail.      </P><P>To prevent such problems the kernel contains a special lock known asthe scheduler lock. A typical kernel function such as<TTCLASS="FUNCTION">cyg_semaphore_post</TT> will claim the scheduler lock,do all its manipulation of kernel data structures, and then releasethe scheduler lock. The current thread cannot be preempted while itholds the scheduler lock. If an interrupt occurs and a DSR is supposedto run to signal that some event has occurred, that DSR is postponeduntil the scheduler unlock operation. This prevents concurrent updatesof kernel data structures.      </P><P>The kernel exports three routines for manipulating the scheduler lock.<TTCLASS="FUNCTION">cyg_scheduler_lock</TT> can be called to claim thelock. On return it is guaranteed that the current thread will not bepreempted, and that no other code is manipulating any kernel datastructures. <TTCLASS="FUNCTION">cyg_scheduler_unlock</TT> can be used torelease the lock, which may cause the current thread to be preempted.<TTCLASS="FUNCTION">cyg_scheduler_read_lock</TT> can be used to query thecurrent state of the scheduler lock. This function should never beneeded because well-written code should always know whether or not thescheduler is currently locked, but may prove useful during debugging.      </P><P>The implementation of the scheduler lock involves a simple counter.Code can call <TTCLASS="FUNCTION">cyg_scheduler_lock</TT> multiple times,causing the counter to be incremented each time, as long as<TTCLASS="FUNCTION">cyg_scheduler_unlock</TT> is called the same number oftimes. This behaviour is different from mutexes where an attempt by athread to lock a mutex multiple times will result in deadlock or anassertion failure.      </P><P>Typical application code should not use the scheduler lock. Insteadother synchronization primitives such as mutexes and semaphores shouldbe used. While the scheduler is locked the current thread cannot bepreempted, so any higher priority threads will not be able to run.Also no DSRs can run, so device drivers may not be able to serviceI/O requests. However there is one situation where locking thescheduler is appropriate: if some data structure needs to be sharedbetween an application thread and a DSR associated with some interruptsource, the thread can use the scheduler lock to prevent concurrentinvocations of the DSR and then safely manipulate the structure. It isdesirable that the scheduler lock is held for only a short period oftime, typically some tens of instructions. In exceptional cases theremay also be some performance-critical code where it is moreappropriate to use the scheduler lock rather than a mutex, because theformer is more efficient.      </P></DIV><DIVCLASS="REFSECT1"><ANAME="KERNEL-SCHEDCONTROL-CONTEXT"></A><H2>Valid contexts</H2><P><TTCLASS="FUNCTION">cyg_scheduler_start</TT> can only be called duringsystem initialization, since it marks the end of that phase. Theremaining functions may be called from thread or DSR context. Lockingthe scheduler from inside the DSR has no practical effect because thelock is claimed automatically by the interrupt subsystem beforerunning DSRs, but allows functions to be shared between normal threadcode and DSRs.      </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-spinlocks.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-interrupts.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Spinlocks</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="kernel.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Interrupt Handling</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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