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

📄 modules and the tinyos execution model - tinyos documentation wiki.htm

📁 从官方网站上下载tinyos2.0的学习指南
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  }
  else {
    post computeTask();
  }
}
</PRE><A name=Internal_Functions></A>
<H1><SPAN class=mw-headline>Internal Functions</SPAN></H1>
<P>Commands and events are the only way that a function in a component can be 
made callable by another component. There are situations when a component wants 
private functions for its own internal use. A component can define standard C 
functions, which other components cannot name and therefore cannot invoke 
directly. While these functions do not have the <TT>command</TT> or 
<TT>event</TT> modifier, they can freely call commands or signal events. For 
example, this is perfectly reasonable nesC code: </P><PRE>module BlinkC {
  uses interface Timer&lt;TMilli&gt; as Timer0;
  uses interface Timer&lt;TMilli&gt; as Timer1;
  uses interface Timer&lt;TMilli&gt; as Timer2;
  uses interface Leds;
  uses interface Boot;
}
implementation
{

  void startTimers() {
    call Timer0.startPeriodic( 250 );
    call Timer1.startPeriodic( 500 );
    call Timer2.startPeriodic( 1000 );
  }

  event void Boot.booted()
  {
    startTimers();
  }

  event void Timer0.fired()
  {
    call Leds.led0Toggle();
  }

  event void Timer1.fired()
  {
    call Leds.led1Toggle();
  }

  event void Timer2.fired()
  {
    call Leds.led2Toggle();
  }
}
</PRE>
<P>Internal functions act just like C functions: they don't need the 
<TT>call</TT> or <TT>signal</TT> keywords. </P><A 
name=Split-Phase_Operations></A>
<H1><SPAN class=mw-headline>Split-Phase Operations</SPAN></H1>
<P>Because nesC interfaces are wired at compile time, callbacks (events) in 
TinyOS are very efficient. In most C-like languages, callbacks have to be 
registered at run-time with a function pointer. This can prevent the compiler 
from being able to optimize code across callback call paths. Since they are 
wired statically in nesC, the compiler knows exactly what functions are called 
where and can optimize heavily. </P>
<P>The ability to optimize across component boundaries is very important in 
TinyOS, because it has no blocking operations. Instead, every long-running 
operation is <B>split-phase</B>. In a blocking system, when a program calls a 
long-running operation, the call does not return until the operation is 
complete: the program blocks. In a split-phase system, when a program calls a 
long-running operation, the call returns immediately, and the called abstraction 
issues a callback when it completes. This approach is called split-phase because 
it splits invocation and completion into two separate phases of execution. Here 
is a simple example of the difference between the two: </P>
<CENTER>
<TABLE>
  <TBODY>
  <TR>
    <TD align=middle>Blocking </TD>
    <TD align=middle>Split-Phase </TD></TR>
  <TR>
    <TD vAlign=top><PRE>if (send() == SUCCESS) {
  sendCount++;
}
</PRE></TD>
    <TD vAlign=top><PRE>// start phase
send();

//completion phase
void sendDone(error_t err) {
  if (err == SUCCESS) {
    sendCount++;
  }
}
</PRE></TD></TR></TBODY></TABLE></CENTER>
<P>Split-phase code is often a bit more verbose and complex than sequential 
code. But it has several advantages. First, split-phase calls do not tie up 
stack memory while they are executing. Second, they keep the system reponsive: 
there is never a situation when an application needs to take an action but all 
of its threads are tied up in blocking calls. Third, it tends to reduce stack 
utilization, as creating large variables on the stack is rarely necessary. </P>
<P>Split-phase interfaces enable a TinyOS component to easily start several 
operations at once and have them execute in parallel. Also, split-phase 
operations can save memory. This is because when a program calls a blocking 
operation, all of the state it has stored on the call stack (e.g., variables 
declared in functions) have to be saved. As determining the exact size of the 
stack is difficult, operating systems often choose a very conservative and 
therefore large size. Of course, if there is data that has to be kept across the 
call, split-phase operations still need to save it. </P>
<P>The command <TT>Timer.startOneShot</TT> is an example of a split-phase call. 
The user of the Timer inteface calls the command, which returns immediately. 
Some time later (specified by the argument), the component providing Timer 
signals <TT>Timer.fired</TT>. In a system with blocking calls, a program might 
use <TT>sleep()</TT>: </P>
<CENTER>
<TABLE>
  <TBODY>
  <TR>
    <TD align=middle>Blocking </TD>
    <TD align=middle>Split-phase </TD></TR>
  <TR>
    <TD vAlign=top><PRE>state = WAITING;
op1();
sleep(500);
op2();
state = RUNNING

</PRE></TD>
    <TD vAlign=top><PRE>state = WAITING;
op1();
call Timer.startOneShot(500);

event void Timer.fired() {
  op2();
  state = RUNNING;
}

</PRE></TD></TR></TBODY></TABLE></CENTER>
<P>In the next lesson, we'll look at one of the most basic split-phase 
operations: sending packets. </P><A name=Related_Documentation></A>
<H1><SPAN class=mw-headline>Related Documentation</SPAN></H1>
<UL>
  <LI><A class="external text" 
  title=http://www.tinyos.net/tinyos-2.x/doc/html/tep102.html 
  href="http://www.tinyos.net/tinyos-2.x/doc/html/tep102.html" rel=nofollow>TEP 
  102: Timers</A> 
  <LI><A class="external text" 
  title=http://www.tinyos.net/tinyos-2.x/doc/html/tep106.html 
  href="http://www.tinyos.net/tinyos-2.x/doc/html/tep106.html" rel=nofollow>TEP 
  106: Schedulers and Tasks</A> </LI></UL>
<HR>

<OL class=references>
  <LI id=_note-task><A title="" 
  href="http://docs.tinyos.net/index.php/Modules_and_the_TinyOS_Execution_Model#_ref-task_0">↑</A> 
  The task semantics have changed significantly from tinyos-2.x. In 1.x, a task 
  could be posted more than once and a post could fail if the task queue were 
  full. In 2.x, a basic post will only fail if that task has already been posted 
  and has not started execution. So a task can always run, but can only have one 
  outstanding post at any time. If a component needs to post task several times, 
  then the end of the task logic can repost itself as need be.</LI></OL>
<HR>

<CENTER>
<P>&lt; <A title="Getting Started with TinyOS" 
href="http://docs.tinyos.net/index.php/Getting_Started_with_TinyOS">Previous 
Lesson</A> | <A title="Modules and the TinyOS Execution Model" 
href="http://docs.tinyos.net/index.php/Modules_and_the_TinyOS_Execution_Model#Modules_and_State">Top</A> 
| <A title="Mote-mote radio communication" 
href="http://docs.tinyos.net/index.php/Mote-mote_radio_communication">Next 
Lesson</A> &gt; </P></CENTER><!-- Saved in parser cache with key tinyosdocs:pcache:idhash:8-0!1!0!!en!2!edit=0 and timestamp 20080401131337 -->
<DIV class=printfooter>Retrieved from "<A 
href="http://docs.tinyos.net/index.php/Modules_and_the_TinyOS_Execution_Model">http://docs.tinyos.net/index.php/Modules_and_the_TinyOS_Execution_Model</A>"</DIV><!-- end content -->
<DIV class=visualClear></DIV></DIV></DIV></DIV>
<DIV id=column-one>
<DIV class=portlet id=p-cactions>
<H5>Views</H5>
<DIV class=pBody>
<UL>
  <LI class=selected id=ca-nstab-main><A title="View the content page [c]" 
  accessKey=c 
  href="http://docs.tinyos.net/index.php/Modules_and_the_TinyOS_Execution_Model">Article</A> 

  <LI id=ca-talk><A title="Discussion about the content page [t]" accessKey=t 
  href="http://docs.tinyos.net/index.php/Talk:Modules_and_the_TinyOS_Execution_Model">Discussion</A> 

  <LI id=ca-viewsource><A 
  title="This page is protected. You can view its source. [e]" accessKey=e 
  href="http://docs.tinyos.net/index.php?title=Modules_and_the_TinyOS_Execution_Model&amp;action=edit">View 
  source</A> 
  <LI id=ca-history><A title="Past versions of this page. [h]" accessKey=h 
  href="http://docs.tinyos.net/index.php?title=Modules_and_the_TinyOS_Execution_Model&amp;action=history">History</A> 
  </LI></UL></DIV></DIV>
<DIV class=portlet id=p-personal>
<H5>Personal tools</H5>
<DIV class=pBody>
<UL>
  <LI id=pt-login><A 
  title="You are encouraged to log in, it is not mandatory however. [o]" 
  accessKey=o 
  href="http://docs.tinyos.net/index.php?title=Special:Userlogin&amp;returnto=Modules_and_the_TinyOS_Execution_Model">Log 
  in / create account</A> </LI></UL></DIV></DIV>
<DIV class=portlet id=p-logo><A title="Visit the Main Page [z]" 
style="BACKGROUND-IMAGE: url(/images/tos-jwall-small.jpg)" accessKey=z 
href="http://docs.tinyos.net/index.php/Main_Page"></A></DIV>
<SCRIPT type=text/javascript> if (window.isMSIE55) fixalpha(); </SCRIPT>

<DIV class=portlet id=p-navigation>
<H5>Navigation</H5>
<DIV class=pBody>
<UL>
  <LI id=n-mainpage><A title="Visit the Main Page [z]" accessKey=z 
  href="http://docs.tinyos.net/index.php/Main_Page">Main Page</A> 
  <LI id=n-portal><A 
  title="About the project, what you can do, where to find things" 
  href="http://docs.tinyos.net/index.php/TinyOS_Documentation_Wiki:Community_Portal">Community 
  portal</A> 
  <LI id=n-currentevents><A 
  title="Find background information on current events" 
  href="http://docs.tinyos.net/index.php/Current_events">Current events</A> 
  <LI id=n-recentchanges><A title="The list of recent changes in the wiki. [r]" 
  accessKey=r 
  href="http://docs.tinyos.net/index.php/Special:Recentchanges">Recent 
  changes</A> 
  <LI id=n-randompage><A title="Load a random page [x]" accessKey=x 
  href="http://docs.tinyos.net/index.php/Special:Random">Random page</A> 
  <LI id=n-help><A title="The place to find out." 
  href="http://docs.tinyos.net/index.php/Help:Contents">Help</A> 
  <LI id=n-sitesupport><A title="Support us" 
  href="http://docs.tinyos.net/index.php/TinyOS_Documentation_Wiki:Site_support">Donations</A> 
  </LI></UL></DIV></DIV>
<DIV class=portlet id=p-search>
<H5><LABEL for=searchInput>Search</LABEL></H5>
<DIV class=pBody id=searchBody>
<FORM id=searchform action=/index.php/Special:Search>
<DIV><INPUT id=searchInput title="Search TinyOS Documentation Wiki [f]" 
accessKey=f name=search> <INPUT class=searchButton id=searchGoButton type=submit value=Go name=go>&nbsp; <INPUT class=searchButton id=mw-searchButton type=submit value=Search name=fulltext> 
</DIV></FORM></DIV></DIV>
<DIV class=portlet id=p-tb>
<H5>Toolbox</H5>
<DIV class=pBody>
<UL>
  <LI id=t-whatlinkshere><A title="List of all wiki pages that link here [j]" 
  accessKey=j 
  href="http://docs.tinyos.net/index.php/Special:Whatlinkshere/Modules_and_the_TinyOS_Execution_Model">What 
  links here</A> 
  <LI id=t-recentchangeslinked><A 
  title="Recent changes in pages linked from this page [k]" accessKey=k 
  href="http://docs.tinyos.net/index.php/Special:Recentchangeslinked/Modules_and_the_TinyOS_Execution_Model">Related 
  changes</A> 
  <LI id=t-upload><A title="Upload images or media files [u]" accessKey=u 
  href="http://docs.tinyos.net/index.php/Special:Upload">Upload file</A> 
  <LI id=t-specialpages><A title="List of all special pages [q]" accessKey=q 
  href="http://docs.tinyos.net/index.php/Special:Specialpages">Special pages</A> 

  <LI id=t-print><A title="Printable version of this page [p]" accessKey=p 
  href="http://docs.tinyos.net/index.php?title=Modules_and_the_TinyOS_Execution_Model&amp;printable=yes">Printable 
  version</A> 
  <LI id=t-permalink><A title="Permanent link to this version of the page" 
  href="http://docs.tinyos.net/index.php?title=Modules_and_the_TinyOS_Execution_Model&amp;oldid=671">Permanent 
  link</A> </LI></UL></DIV></DIV></DIV><!-- end of the left (by default at least) column -->
<DIV class=visualClear></DIV>
<DIV id=footer>
<DIV id=f-poweredbyico><A href="http://www.mediawiki.org/"><IMG 
alt="Powered by MediaWiki" 
src="Modules and the TinyOS Execution Model - TinyOS Documentation Wiki.files/poweredby_mediawiki_88x31.png"></A></DIV>
<UL id=f-list>
  <LI id=lastmod>This page was last modified 00:32, 23 January 2008. 
  <LI id=viewcount>This page has been accessed 1,403 times. 
  <LI id=privacy><A title="TinyOS Documentation Wiki:Privacy policy" 
  href="http://docs.tinyos.net/index.php/TinyOS_Documentation_Wiki:Privacy_policy">Privacy 
  policy</A> 
  <LI id=about><A title="TinyOS Documentation Wiki:About" 
  href="http://docs.tinyos.net/index.php/TinyOS_Documentation_Wiki:About">About 
  TinyOS Documentation Wiki</A> 
  <LI id=disclaimer><A title="TinyOS Documentation Wiki:General disclaimer" 
  href="http://docs.tinyos.net/index.php/TinyOS_Documentation_Wiki:General_disclaimer">Disclaimers</A> 
  </LI></UL></DIV>
<SCRIPT type=text/javascript>if (window.runOnloadHook) runOnloadHook();</SCRIPT>
</DIV><!-- Served in 0.276 secs. --></BODY></HTML>

⌨️ 快捷键说明

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