Project

General

Profile

Statistics
| Revision:

root / branches / wireless / docs / libdragonfly / html / group__util__atomic.html @ 1578

History | View | Annotate | Download (13.1 KB)

1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3
<title>Colony: &lt;util/atomic.h&gt; Atomically and Non-Atomically Executed Code Blocks</title>
4
<link href="tabs.css" rel="stylesheet" type="text/css">
5
<link href="doxygen.css" rel="stylesheet" type="text/css">
6
</head><body>
7
<!-- Generated by Doxygen 1.5.8 -->
8
<div class="navigation" id="top">
9
  <div class="tabs">
10
    <ul>
11
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
12
      <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
13
      <li><a href="modules.html"><span>Modules</span></a></li>
14
      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
15
      <li><a href="files.html"><span>Files</span></a></li>
16
    </ul>
17
  </div>
18
</div>
19
<div class="contents">
20
<h1>&lt;util/atomic.h&gt; Atomically and Non-Atomically Executed Code Blocks</h1><table border="0" cellpadding="0" cellspacing="0">
21
<tr><td></td></tr>
22
<tr><td colspan="2"><br><h2>Defines</h2></td></tr>
23
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__atomic.html#gaaea265b31dabcfb3098bec7685c39e4">ATOMIC_BLOCK</a>(type)</td></tr>
24

    
25
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__atomic.html#g6e195ee2117559a25f77fbba9054674a">NONATOMIC_BLOCK</a>(type)</td></tr>
26

    
27
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__atomic.html#g362c18b15a09703e42e1c246c47420ef">ATOMIC_RESTORESTATE</a></td></tr>
28

    
29
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__atomic.html#g92b11103b4b3b000a3190f0d26ba7062">ATOMIC_FORCEON</a></td></tr>
30

    
31
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__atomic.html#gb075653bf638fae9db049575741d3152">NONATOMIC_RESTORESTATE</a></td></tr>
32

    
33
<tr><td class="memItemLeft" nowrap align="right" valign="top">#define&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__util__atomic.html#gfb959d7d00d2d790b58d0e9880ea255a">NONATOMIC_FORCEOFF</a></td></tr>
34

    
35
</table>
36
<hr><a name="_details"></a><h2>Detailed Description</h2>
37
<div class="fragment"><pre class="fragment"><span class="preprocessor">    #include &lt;util/atomic.h&gt;</span>
38
</pre></div><p>
39
<dl class="note" compact><dt><b>Note:</b></dt><dd>The macros in this header file require the ISO/IEC 9899:1999 ("ISO C99") feature of for loop variables that are declared inside the for loop itself. For that reason, this header file can only be used if the standard level of the compiler (option --std=) is set to either <code>c99</code> or <code>gnu99</code>.</dd></dl>
40
The macros in this header file deal with code blocks that are guaranteed to be excuted Atomically or Non-Atmomically. The term "Atomic" in this context refers to the unability of the respective code to be interrupted.<p>
41
These macros operate via automatic manipulation of the Global Interrupt Status (I) bit of the SREG register. Exit paths from both block types are all managed automatically without the need for special considerations, i. e. the interrupt status will be restored to the same value it has been when entering the respective block.<p>
42
A typical example that requires atomic access is a 16 (or more) bit variable that is shared between the main execution path and an ISR. While declaring such a variable as volatile ensures that the compiler will not optimize accesses to it away, it does not guarantee atomic access to it. Assuming the following example:<p>
43
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;inttypes.h&gt;</span>
44
<span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>
45
<span class="preprocessor">#include &lt;avr/io.h&gt;</span>
46

    
47
<span class="keyword">volatile</span> uint16_t ctr;
48

    
49
<a class="code" href="i2c_8c.html#474f42eedbdc093683fdb4b88be3c48f" title="Interrupt to handle I2C interrupts from the I2C hardware.">ISR</a>(TIMER1_OVF_vect)
50
{
51
  ctr--;
52
}
53

    
54
...
55
int
56
main(<span class="keywordtype">void</span>)
57
{
58
   ...
59
   ctr = 0x200;
60
   start_timer();
61
   <span class="keywordflow">while</span> (ctr != 0)
62
     <span class="comment">// wait</span>
63
       ;
64
   ...
65
}
66
</pre></div><p>
67
There is a chance where the main context will exit its wait loop when the variable <code>ctr</code> just reached the value 0xFF. This happens because the compiler cannot natively access a 16-bit variable atomically in an 8-bit CPU. So the variable is for example at 0x100, the compiler then tests the low byte for 0, which succeeds. It then proceeds to test the high byte, but that moment the ISR triggers, and the main context is interrupted. The ISR will decrement the variable from 0x100 to 0xFF, and the main context proceeds. It now tests the high byte of the variable which is (now) also 0, so it concludes the variable has reached 0, and terminates the loop.<p>
68
Using the macros from this header file, the above code can be rewritten like:<p>
69
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;inttypes.h&gt;</span>
70
<span class="preprocessor">#include &lt;avr/interrupt.h&gt;</span>
71
<span class="preprocessor">#include &lt;avr/io.h&gt;</span>
72
<span class="preprocessor">#include &lt;util/atomic.h&gt;</span>
73

    
74
<span class="keyword">volatile</span> uint16_t ctr;
75

    
76
<a class="code" href="i2c_8c.html#474f42eedbdc093683fdb4b88be3c48f" title="Interrupt to handle I2C interrupts from the I2C hardware.">ISR</a>(TIMER1_OVF_vect)
77
{
78
  ctr--;
79
}
80

    
81
...
82
int
83
main(<span class="keywordtype">void</span>)
84
{
85
   ...
86
   ctr = 0x200;
87
   start_timer();
88
   sei();
89
   uint16_t ctr_copy;
90
   <span class="keywordflow">do</span>
91
   {
92
     <a class="code" href="group__util__atomic.html#gaaea265b31dabcfb3098bec7685c39e4">ATOMIC_BLOCK</a>(<a class="code" href="group__util__atomic.html#g92b11103b4b3b000a3190f0d26ba7062">ATOMIC_FORCEON</a>)
93
     {
94
       ctr_copy = ctr;
95
     }
96
   }
97
   <span class="keywordflow">while</span> (ctr_copy != 0);
98
   ...
99
}
100
</pre></div><p>
101
This will install the appropriate interrupt protection before accessing variable <code>ctr</code>, so it is guaranteed to be consistently tested. If the global interrupt state were uncertain before entering the ATOMIC_BLOCK, it should be executed with the parameter ATOMIC_RESTORESTATE rather than ATOMIC_FORCEON. <hr><h2>Define Documentation</h2>
102
<a class="anchor" name="gaaea265b31dabcfb3098bec7685c39e4"></a><!-- doxytag: member="atomic.h::ATOMIC_BLOCK" ref="gaaea265b31dabcfb3098bec7685c39e4" args="(type)" -->
103
<div class="memitem">
104
<div class="memproto">
105
      <table class="memname">
106
        <tr>
107
          <td class="memname">#define ATOMIC_BLOCK          </td>
108
          <td>(</td>
109
          <td class="paramtype">type&nbsp;</td>
110
          <td class="paramname">          </td>
111
          <td>&nbsp;)&nbsp;</td>
112
          <td></td>
113
        </tr>
114
      </table>
115
</div>
116
<div class="memdoc">
117

    
118
<p>
119
<b>Value:</b><div class="fragment"><pre class="fragment"><span class="keywordflow">for</span> ( type, __ToDo = __iCliRetVal(); \
120
                               __ToDo ; __ToDo = 0 )
121
</pre></div>Creates a block of code that is guaranteed to be executed atomically. Upon entering the block the Global Interrupt Status flag in SREG is disabled, and re-enabled upon exiting the block from any exit path.<p>
122
Two possible macro parameters are permitted, ATOMIC_RESTORESTATE and ATOMIC_FORCEON. 
123
</div>
124
</div><p>
125
<a class="anchor" name="g92b11103b4b3b000a3190f0d26ba7062"></a><!-- doxytag: member="atomic.h::ATOMIC_FORCEON" ref="g92b11103b4b3b000a3190f0d26ba7062" args="" -->
126
<div class="memitem">
127
<div class="memproto">
128
      <table class="memname">
129
        <tr>
130
          <td class="memname">#define ATOMIC_FORCEON          </td>
131
        </tr>
132
      </table>
133
</div>
134
<div class="memdoc">
135

    
136
<p>
137
<b>Value:</b><div class="fragment"><pre class="fragment">uint8_t sreg_save \
138
        __attribute__((__cleanup__(__iSeiParam))) = 0
139
</pre></div>This is a possible parameter for ATOMIC_BLOCK. When used, it will cause the ATOMIC_BLOCK to force the state of the SREG register on exit, enabling the Global Interrupt Status flag bit. This saves on flash space as the previous value of the SREG register does not need to be saved at the start of the block.<p>
140
Care should be taken that ATOMIC_FORCEON is only used when it is known that interrupts are enabled before the block's execution or when the side effects of enabling global interrupts at the block's completion are known and understood. 
141
</div>
142
</div><p>
143
<a class="anchor" name="g362c18b15a09703e42e1c246c47420ef"></a><!-- doxytag: member="atomic.h::ATOMIC_RESTORESTATE" ref="g362c18b15a09703e42e1c246c47420ef" args="" -->
144
<div class="memitem">
145
<div class="memproto">
146
      <table class="memname">
147
        <tr>
148
          <td class="memname">#define ATOMIC_RESTORESTATE          </td>
149
        </tr>
150
      </table>
151
</div>
152
<div class="memdoc">
153

    
154
<p>
155
<b>Value:</b><div class="fragment"><pre class="fragment">uint8_t sreg_save \
156
        __attribute__((__cleanup__(__iRestore))) = SREG
157
</pre></div>This is a possible parameter for ATOMIC_BLOCK. When used, it will cause the ATOMIC_BLOCK to restore the previous state of the SREG register, saved before the Global Interrupt Status flag bit was disabled. The net effect of this is to make the ATOMIC_BLOCK's contents guaranteed atomic, without changing the state of the Global Interrupt Status flag when execution of the block completes. 
158
</div>
159
</div><p>
160
<a class="anchor" name="g6e195ee2117559a25f77fbba9054674a"></a><!-- doxytag: member="atomic.h::NONATOMIC_BLOCK" ref="g6e195ee2117559a25f77fbba9054674a" args="(type)" -->
161
<div class="memitem">
162
<div class="memproto">
163
      <table class="memname">
164
        <tr>
165
          <td class="memname">#define NONATOMIC_BLOCK          </td>
166
          <td>(</td>
167
          <td class="paramtype">type&nbsp;</td>
168
          <td class="paramname">          </td>
169
          <td>&nbsp;)&nbsp;</td>
170
          <td></td>
171
        </tr>
172
      </table>
173
</div>
174
<div class="memdoc">
175

    
176
<p>
177
<b>Value:</b><div class="fragment"><pre class="fragment"><span class="keywordflow">for</span> ( type, __ToDo = __iSeiRetVal(); \
178
                                  __ToDo ;  __ToDo = 0 )
179
</pre></div>Creates a block of code that is executed non-atomically. Upon entering the block the Global Interrupt Status flag in SREG is enabled, and disabled upon exiting the block from any exit path. This is useful when nested inside ATOMIC_BLOCK sections, allowing for non-atomic execution of small blocks of code while maintaining the atomic access of the other sections of the parent ATOMIC_BLOCK.<p>
180
Two possible macro parameters are permitted, NONATOMIC_RESTORESTATE and NONATOMIC_FORCEOFF. 
181
</div>
182
</div><p>
183
<a class="anchor" name="gfb959d7d00d2d790b58d0e9880ea255a"></a><!-- doxytag: member="atomic.h::NONATOMIC_FORCEOFF" ref="gfb959d7d00d2d790b58d0e9880ea255a" args="" -->
184
<div class="memitem">
185
<div class="memproto">
186
      <table class="memname">
187
        <tr>
188
          <td class="memname">#define NONATOMIC_FORCEOFF          </td>
189
        </tr>
190
      </table>
191
</div>
192
<div class="memdoc">
193

    
194
<p>
195
<b>Value:</b><div class="fragment"><pre class="fragment">uint8_t sreg_save \
196
        __attribute__((__cleanup__(__iCliParam))) = 0
197
</pre></div>This is a possible parameter for NONATOMIC_BLOCK. When used, it will cause the NONATOMIC_BLOCK to force the state of the SREG register on exit, disabling the Global Interrupt Status flag bit. This saves on flash space as the previous value of the SREG register does not need to be saved at the start of the block.<p>
198
Care should be taken that NONATOMIC_FORCEOFF is only used when it is known that interrupts are disabled before the block's execution or when the side effects of disabling global interrupts at the block's completion are known and understood. 
199
</div>
200
</div><p>
201
<a class="anchor" name="gb075653bf638fae9db049575741d3152"></a><!-- doxytag: member="atomic.h::NONATOMIC_RESTORESTATE" ref="gb075653bf638fae9db049575741d3152" args="" -->
202
<div class="memitem">
203
<div class="memproto">
204
      <table class="memname">
205
        <tr>
206
          <td class="memname">#define NONATOMIC_RESTORESTATE          </td>
207
        </tr>
208
      </table>
209
</div>
210
<div class="memdoc">
211

    
212
<p>
213
<b>Value:</b><div class="fragment"><pre class="fragment">uint8_t sreg_save \
214
        __attribute__((__cleanup__(__iRestore))) = SREG
215
</pre></div>This is a possible parameter for NONATOMIC_BLOCK. When used, it will cause the NONATOMIC_BLOCK to restore the previous state of the SREG register, saved before the Global Interrupt Status flag bit was enabled. The net effect of this is to make the NONATOMIC_BLOCK's contents guaranteed non-atomic, without changing the state of the Global Interrupt Status flag when execution of the block completes. 
216
</div>
217
</div><p>
218
</div>
219
<hr size="1"><address style="text-align: right;"><small>Generated on Fri Nov 20 21:51:59 2009 for Colony by&nbsp;
220
<a href="http://www.doxygen.org/index.html">
221
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.8 </small></address>
222
</body>
223
</html>