aboutsummaryrefslogtreecommitdiff
path: root/docs/doxygen/nel/a02516.html
blob: 41fdc21e504080b1b7ec7abd049d7ba8b3855ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>NeL: NLMISC::CFastMutex class Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.6 -->
<div class="qindex">  <form class="search" action="search.php" method="get">
<a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Data&nbsp;Structures</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Data&nbsp;Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a>  | <span class="search"><u>S</u>earch&nbsp;for&nbsp;<input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
<h1>NLMISC::CFastMutex Class Reference</h1><code>#include &lt;<a class="el" href="a06088.html">mutex.h</a>&gt;</code>
<p>
<hr><a name="_details"></a><h2>Detailed Description</h2>
Fast mutex implementation (not fairly) The mutex ARE NOT recursive (ie don't call <a class="el" href="a02516.html#NLMISC_1_1CFastMutexa1">enter()</a> several times on the same mutex from the same thread without having called <a class="el" href="a02516.html#NLMISC_1_1CFastMutexa3">leave()</a>) ; The threads ARE NOT woken-up in the same order as they were put to sleep. The threads ARE NOT woken-up using signals but using Sleep(). This mutex works but is not optimal for multiprocessors because if the mutex is locked, next enter will be sleeped without waiting a little.<p>
Implementation notes:<ul>
<li>Implementated under WIN32</li><li>Other OS use CMutex</li></ul>
<p>
Tested: OK on Windows and Linux single &amp; multi-processor<p>
<pre class="fragment"><div> <a class="code" href="a02516.html#NLMISC_1_1CFastMutexa0">CFastMutex</a> m;
 m.enter ();
 <span class="comment">// do critical stuffs</span>
 m.leave ();
 *
</div></pre><dl compact><dt><b>Author:</b></dt><dd>Cyril 'Hulud' Corvazier <p>
Olivier Cado <p>
Nevrax France </dd></dl>
<dl compact><dt><b>Date:</b></dt><dd>2002, 2003 </dd></dl>

<p>

<p>
Definition at line <a class="el" href="a06088.html#l00194">194</a> of file <a class="el" href="a06088.html">mutex.h</a>.<table border=0 cellpadding=0 cellspacing=0>
<tr><td></td></tr>
<tr><td colspan=2><br><h2>Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a02516.html#NLMISC_1_1CFastMutexa0">CFastMutex</a> ()</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Constructor.  <a href="#NLMISC_1_1CFastMutexa0"></a><br><br></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>__forceinline void&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a02516.html#NLMISC_1_1CFastMutexa1">enter</a> () volatile</td></tr>

<tr><td class="memItemLeft" nowrap align=right valign=top>void&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a02516.html#NLMISC_1_1CFastMutexa2">init</a> () volatile</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Same as constructor, useful for init in a shared memory block.  <a href="#NLMISC_1_1CFastMutexa2"></a><br><br></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>__forceinline void&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a02516.html#NLMISC_1_1CFastMutexa3">leave</a> () volatile</td></tr>

<tr><td colspan=2><br><h2>Static Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>__forceinline bool&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a02516.html#NLMISC_1_1CFastMutexe0">atomic_swap</a> (volatile <a class="el" href="a04558.html#a11">uint32</a> *lockPtr)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Atomic swap.  <a href="#NLMISC_1_1CFastMutexe0"></a><br><br></td></tr>
<tr><td colspan=2><br><h2>Private Attributes</h2></td></tr>
<tr><td class="memItemLeft" nowrap align=right valign=top>volatile <a class="el" href="a04558.html#a11">uint32</a>&nbsp;</td><td class="memItemRight" valign=bottom><a class="el" href="a02516.html#NLMISC_1_1CFastMutexr0">_Lock</a></td></tr>

</table>
<hr><h2>Constructor &amp; Destructor Documentation</h2>
<a class="anchor" name="NLMISC_1_1CFastMutexa0" doxytag="NLMISC::CFastMutex::CFastMutex" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> NLMISC::CFastMutex::CFastMutex </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [inline]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Constructor. 
<p>

<p>
Definition at line <a class="el" href="a06088.html#l00199">199</a> of file <a class="el" href="a06088.html">mutex.h</a>.
<p>
References <a class="el" href="a06088.html#l00280">_Lock</a>.
<p>
<div class="fragment"><pre>00199 : <a class="code" href="a02516.html#NLMISC_1_1CFastMutexr0">_Lock</a>(0) {}
</pre></div>    </td>
  </tr>
</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="NLMISC_1_1CFastMutexe0" doxytag="NLMISC::CFastMutex::atomic_swap" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> __forceinline bool NLMISC::CFastMutex::atomic_swap </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">volatile <a class="el" href="a04558.html#a11">uint32</a> *&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>lockPtr</em>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [inline, static]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Atomic swap. 
<p>

<p>
Definition at line <a class="el" href="a06088.html#l00205">205</a> of file <a class="el" href="a06088.html">mutex.h</a>.
<p>
References <a class="el" href="a06088.html#l00127">ASM_ASWAP_FOR_GCC_XCHG</a>, and <a class="el" href="a05981.html#l00100">uint32</a>.
<p>
Referenced by <a class="el" href="a06088.html#l00238">enter()</a>.
<p>
<div class="fragment"><pre>00206         {
00207                 <a class="code" href="a04558.html#a11">uint32</a> result;
00208 <span class="preprocessor">#ifdef NL_OS_WINDOWS</span>
00209 <span class="preprocessor"></span><span class="preprocessor">#ifdef NL_DEBUG_FAST</span>
00210 <span class="preprocessor"></span>                <span class="comment">// Workaround for dumb inlining bug (returning of function goes into the choux): push/pop registers</span>
00211                 __asm
00212                 {
00213                                 push eax
00214                                 push ecx
00215                         mov ecx,lockPtr
00216                         mov eax,1
00217                         xchg [ecx],eax
00218                         mov [result],eax
00219                                 pop ecx
00220                                 pop eax
00221                 }
00222 <span class="preprocessor">#else</span>
00223 <span class="preprocessor"></span>                __asm
00224                 {
00225                         mov ecx,lockPtr
00226                         mov eax,1
00227                         xchg [ecx],eax
00228                         mov [result],eax
00229                 }
00230 <span class="preprocessor">#endif</span>
00231 <span class="preprocessor"></span><span class="preprocessor">#else</span>
00232 <span class="preprocessor"></span>                <a class="code" href="a04665.html#a2">ASM_ASWAP_FOR_GCC_XCHG</a>
00233 <span class="preprocessor">#endif // NL_OS_WINDOWS</span>
00234 <span class="preprocessor"></span>                <span class="keywordflow">return</span> result != 0;
00235         }
</pre></div>    </td>
  </tr>
</table>
<a class="anchor" name="NLMISC_1_1CFastMutexa1" doxytag="NLMISC::CFastMutex::enter" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> __forceinline void NLMISC::CFastMutex::enter </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> volatile<code> [inline]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>

<p>
Definition at line <a class="el" href="a06088.html#l00238">238</a> of file <a class="el" href="a06088.html">mutex.h</a>.
<p>
References <a class="el" href="a06088.html#l00280">_Lock</a>, <a class="el" href="a06088.html#l00205">atomic_swap()</a>, and <a class="el" href="a05981.html#l00105">uint</a>.
<p>
Referenced by <a class="el" href="a05490.html#l00209">NLMISC::CBlockMemory&lt; CNode &gt;::__stl_alloc_changeEltSize()</a>, <a class="el" href="a05490.html#l00097">NLMISC::CBlockMemory&lt; CNode &gt;::allocate()</a>, <a class="el" href="a05490.html#l00147">NLMISC::CBlockMemory&lt; CNode &gt;::free()</a>, <a class="el" href="a05490.html#l00185">NLMISC::CBlockMemory&lt; CNode &gt;::purge()</a>, and <a class="el" href="a06768.html#l00444">RenderTriangle()</a>.
<p>
<div class="fragment"><pre>00239         {
00240           <span class="comment">//std::cout &lt;&lt; "Entering, Lock=" &lt;&lt; _Lock &lt;&lt; std::endl; </span>
00241                 <span class="keywordflow">if</span> (<a class="code" href="a02516.html#NLMISC_1_1CFastMutexe0">atomic_swap</a> (&amp;_Lock))
00242                 {
00243                         <span class="comment">// First test</span>
00244                         <a class="code" href="a04558.html#a15">uint</a> i;
00245                         <span class="keywordflow">for</span> (i = 0 ;; ++i)
00246                         {
00247                                 <a class="code" href="a04558.html#a15">uint</a> wait_time = i + 6;
00248 
00249                                 <span class="comment">// Increment wait time with a log function</span>
00250                                 <span class="keywordflow">if</span> (wait_time &gt; 27) 
00251                                         wait_time = 27;
00252 
00253                                 <span class="comment">// Sleep</span>
00254                                 <span class="keywordflow">if</span> (wait_time &lt;= 20) 
00255                                         wait_time = 0;
00256                                 <span class="keywordflow">else</span>
00257                                         wait_time = 1 &lt;&lt; (wait_time - 20);
00258 
00259                                 <span class="keywordflow">if</span> (!<a class="code" href="a02516.html#NLMISC_1_1CFastMutexe0">atomic_swap</a> (&amp;_Lock))
00260                                         <span class="keywordflow">break</span>;
00261 
00262 <span class="preprocessor">#ifdef NL_OS_WINDOWS</span>
00263 <span class="preprocessor"></span>                                Sleep (wait_time);
00264 <span class="preprocessor">#else</span>
00265 <span class="preprocessor"></span>                                <span class="comment">//std::cout &lt;&lt;  "Sleeping i=" &lt;&lt; i &lt;&lt; std::endl;</span>
00266                                 usleep( wait_time*1000 );
00267 <span class="preprocessor">#endif</span>
00268 <span class="preprocessor"></span>                        }
00269                 }
00270         }
</pre></div>    </td>
  </tr>
</table>
<a class="anchor" name="NLMISC_1_1CFastMutexa2" doxytag="NLMISC::CFastMutex::init" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> void NLMISC::CFastMutex::init </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">void&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> volatile<code> [inline]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Same as constructor, useful for init in a shared memory block. 
<p>

<p>
Definition at line <a class="el" href="a06088.html#l00202">202</a> of file <a class="el" href="a06088.html">mutex.h</a>.
<p>
References <a class="el" href="a06088.html#l00280">_Lock</a>.
<p>
<div class="fragment"><pre>00202 { <a class="code" href="a02516.html#NLMISC_1_1CFastMutexr0">_Lock</a> = 0; }
</pre></div>    </td>
  </tr>
</table>
<a class="anchor" name="NLMISC_1_1CFastMutexa3" doxytag="NLMISC::CFastMutex::leave" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> __forceinline void NLMISC::CFastMutex::leave </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> volatile<code> [inline]</code></td>
        </tr>

      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>

<p>
Definition at line <a class="el" href="a06088.html#l00273">273</a> of file <a class="el" href="a06088.html">mutex.h</a>.
<p>
References <a class="el" href="a06088.html#l00280">_Lock</a>.
<p>
Referenced by <a class="el" href="a05490.html#l00209">NLMISC::CBlockMemory&lt; CNode &gt;::__stl_alloc_changeEltSize()</a>, <a class="el" href="a05490.html#l00097">NLMISC::CBlockMemory&lt; CNode &gt;::allocate()</a>, <a class="el" href="a05490.html#l00147">NLMISC::CBlockMemory&lt; CNode &gt;::free()</a>, <a class="el" href="a05490.html#l00185">NLMISC::CBlockMemory&lt; CNode &gt;::purge()</a>, and <a class="el" href="a06768.html#l00444">RenderTriangle()</a>.
<p>
<div class="fragment"><pre>00274         {
00275                 <a class="code" href="a02516.html#NLMISC_1_1CFastMutexr0">_Lock</a> = 0;
00276                 <span class="comment">//std::cout &lt;&lt; "Leave" &lt;&lt; std::endl;            </span>
00277         }
</pre></div>    </td>
  </tr>
</table>
<hr><h2>Field Documentation</h2>
<a class="anchor" name="NLMISC_1_1CFastMutexr0" doxytag="NLMISC::CFastMutex::_Lock" ></a><p>
<table class="mdTable" width="100%" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top"> volatile <a class="el" href="a04558.html#a11">uint32</a> <a class="el" href="a02516.html#NLMISC_1_1CFastMutexr0">NLMISC::CFastMutex::_Lock</a><code> [private]</code>
      </table>
    </td>
  </tr>
</table>
<table cellspacing=5 cellpadding=0 border=0>
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>

<p>
Definition at line <a class="el" href="a06088.html#l00280">280</a> of file <a class="el" href="a06088.html">mutex.h</a>.
<p>
Referenced by <a class="el" href="a06088.html#l00199">CFastMutex()</a>, <a class="el" href="a06088.html#l00238">enter()</a>, <a class="el" href="a06088.html#l00202">init()</a>, and <a class="el" href="a06088.html#l00273">leave()</a>.    </td>
  </tr>
</table>
<hr>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="a06088.html">mutex.h</a></ul>
<hr size="1"><address style="align: right;"><small>Generated on Tue Mar 16 13:12:31 2004 for NeL by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 > 
</a>1.3.6 </small></address>
</body>
</html>