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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>nevrax.org : docs</TITLE>
<LINK REL=stylesheet TYPE="text/css" HREF="http://www.nevrax.org/inc/css/nevrax.css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY MARGINHEIGHT="0" MARGINWIDTH="0">
<!-- uplinks -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR>
<TD WIDTH=16><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD WIDTH=140 BGCOLOR=#dddddd><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD WIDTH=16><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD><IMG width=6 height=14 SRC="http://www.nevrax.org/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle> <A CLASS=uplinks HREF=http://www.nevrax.org><b>Home</B></FONT></A> </TD>
<TD><IMG width=6 height=14 SRC="http://www.nevrax.org/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle> <A CLASS=uplinks HREF=http://www.nevrax.com><b>nevrax.com</B></FONT></A> </TD>
</TR>
</TABLE>
<!-- banner Nevrax -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=100%>
<TR><TD BGCOLOR="#000000" BACKGROUND="http://www.nevrax.org/inc/img/black_banner.jpg"><A HREF="http://www.nevrax.org"><IMG SRC="http://www.nevrax.org/inc/img/nevrax.gif" WIDTH="170" HEIGHT="45" BORDER=0 ALT="Nevrax" ></A></TD></TR>
</TABLE>
<!-- main table -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 height=100%>
<TR>
<TD WIDTH=16><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="10" BORDER=0 ALT=""></TD>
<TD WIDTH=140 BGCOLOR=#dddddd VALIGN=TOP ALIGN=middle><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">
<!------ Begin Box ------>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=black><TR><TD><TABLE border=0 cellspacing=2 cellpadding=0 width=120><tr><TD ALIGN=middle bgcolor=black>
<FONT COLOR=white FACE="sans-serif"><B>Nevrax.org</B></FONT></TD></TR><tr><td colspan=2 bgcolor=#FFFFFF>
<TABLE cellspacing=0 cellpadding=1 border=0>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/news/" TITLE="Rubrique news"><img width=13 height=15 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-news.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/news/" TITLE="News">News</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/mail/" TITLE="Rubrique mail"><img width=15 height=11 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-mail.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/mail/" TITLE="Mailing list archive">Mailing-list</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/docs/" TITLE="Rubrique docs"><img width=14 height=16 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-docs.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/docs/" TITLE="Documentation">Documentation</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/cvs/" TITLE="Rubrique cvs"><img width=13 height=17 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-cvs.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/cvs/" TITLE="CVS Web">CVS</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/bugs/" TITLE="Rubrique bugs"><img width=20 height=16 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-bugs.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/bugs/" TITLE="Bugtracking">Bugs</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="http://www.nevrax.org/GPL.php3" TITLE="Rubrique license"><img width=18 height=12 hspace=5 border=0 src=http://www.nevrax.org/inc/img/picto-gpl.gif ALT=#></A></td><td><a class='linkbox' href="http://www.nevrax.org/GPL.php3" TITLE="License">License</a></td></tr>
</TABLE>
</TD></TR></TABLE></TD></TR></TABLE>
<!------ End Box ------>
</TD>
<TD WIDTH=15><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD ALIGN=left valign=top><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">
<!-- title -->
<TABLE background="http://www.nevrax.org/inc/img/redline.gif" CELLSPACING=0 CELLPADDING=0 BORDER=0 width=100%><tr><td>
<A HREF="http://www.nevrax.org/docs/"><img src="http://www.nevrax.org/inc/img/t_docs.gif" ALT="Docs" HEIGHT=20 BORDER=0></A>
</td><td><IMG SRC="http://www.nevrax.org/inc/img/pixel.gif" WIDTH="1" HEIGHT="1" BORDER=0 ALT="">
</td></tr></table>
<!-- block -->
<TABLE bgcolor="#dddddd" CELLSPACING=0 CELLPADDING=0 BORDER=0 width=100%><tr><td width=1% valign=middle><img width=6 height=14 hspace=2 vspace=2 src="http://www.nevrax.org/inc/img/reddots.gif"></TD>
<TD><B>Documentation</B></TD>
<TD ALIGN=RIGHT> </td>
</tr></table>
<!-- Generated by Doxygen 1.2.14 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> <a class="qindexRef" doxygen="_cgi:http://www.nevrax.org/cgi-bin/nel-search.cgi" href="http://www.nevrax.org/cgi-bin/nel-search.cgi">Search</a> </center>
<hr><h1>mutex.cpp</h1><a href="mutex_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001
00007 <font class="comment">/* Copyright, 2000 Nevrax Ltd.</font>
00008 <font class="comment"> *</font>
00009 <font class="comment"> * This file is part of NEVRAX NEL.</font>
00010 <font class="comment"> * NEVRAX NEL is free software; you can redistribute it and/or modify</font>
00011 <font class="comment"> * it under the terms of the GNU General Public License as published by</font>
00012 <font class="comment"> * the Free Software Foundation; either version 2, or (at your option)</font>
00013 <font class="comment"> * any later version.</font>
00014 <font class="comment"></font>
00015 <font class="comment"> * NEVRAX NEL is distributed in the hope that it will be useful, but</font>
00016 <font class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00017 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font>
00018 <font class="comment"> * General Public License for more details.</font>
00019 <font class="comment"></font>
00020 <font class="comment"> * You should have received a copy of the GNU General Public License</font>
00021 <font class="comment"> * along with NEVRAX NEL; see the file COPYING. If not, write to the</font>
00022 <font class="comment"> * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,</font>
00023 <font class="comment"> * MA 02111-1307, USA.</font>
00024 <font class="comment"> */</font>
00025
00026 <font class="preprocessor">#include "<a class="code" href="stdmisc_8h.html">stdmisc.h</a>"</font>
00027
00028 <font class="preprocessor">#ifndef _GNU_SOURCE</font>
00029 <font class="preprocessor"></font><font class="preprocessor">#define _GNU_SOURCE</font>
00030 <font class="preprocessor"></font><font class="preprocessor">#endif // _GNU_SOURCE</font>
00031 <font class="preprocessor"></font>
00032 <font class="preprocessor">#include "<a class="code" href="mutex_8h.html">nel/misc/mutex.h</a>"</font>
00033 <font class="preprocessor">#include "<a class="code" href="time__nl_8h.html">nel/misc/time_nl.h</a>"</font>
00034
00035 <font class="preprocessor">#ifdef MUTEX_DEBUG</font>
00036 <font class="preprocessor"></font><font class="preprocessor">#include <iostream></font>
00037 <font class="preprocessor">#endif</font>
00038 <font class="preprocessor"></font>
00039 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00040
00041 <font class="preprocessor">#ifndef MUTEX_DEBUG</font>
<a name="l00042"></a><a class="code" href="mutex_8cpp.html#a0">00042</a> <font class="preprocessor"></font><font class="preprocessor">#define debugCreateMutex() ;</font>
<a name="l00043"></a><a class="code" href="mutex_8cpp.html#a1">00043</a> <font class="preprocessor"></font><font class="preprocessor">#define debugBeginEnter() ;</font>
<a name="l00044"></a><a class="code" href="mutex_8cpp.html#a2">00044</a> <font class="preprocessor"></font><font class="preprocessor">#define debugEndEnter() ;</font>
<a name="l00045"></a><a class="code" href="mutex_8cpp.html#a3">00045</a> <font class="preprocessor"></font><font class="preprocessor">#define debugLeave() ;</font>
<a name="l00046"></a><a class="code" href="mutex_8cpp.html#a4">00046</a> <font class="preprocessor"></font><font class="preprocessor">#define debugDeleteMutex() ;</font>
00047 <font class="preprocessor"></font><font class="preprocessor">#endif</font>
00048 <font class="preprocessor"></font>
00049
00050 <font class="comment">/****************</font>
00051 <font class="comment"> * Windows code *</font>
00052 <font class="comment"> ****************/</font>
00053
00054 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00055 <font class="preprocessor"></font>
00056 <font class="comment">// these defines are for IsDebuggerPresent(). It'll not compile on windows 95</font>
00057 <font class="comment">// just comment this and the IsDebuggerPresent to compile on windows 95</font>
00058 <font class="preprocessor">#define _WIN32_WINDOWS 0x0410</font>
00059 <font class="preprocessor"></font><font class="preprocessor">#define WINVER 0x0400</font>
00060 <font class="preprocessor"></font><font class="preprocessor">#include <windows.h></font>
00061
00062
00063 <font class="keyword">namespace </font>NLMISC {
00064
00065
00066 <font class="comment">/*</font>
00067 <font class="comment"> * Windows version</font>
00068 <font class="comment"> */</font>
00069
00070 CUnfairMutex::CUnfairMutex()
00071 {
00072 <font class="comment">// Create a mutex with no initial owner.</font>
00073 Mutex = (<font class="keywordtype">void</font> *) CreateMutex (NULL, FALSE, NULL);
00074 <a class="code" href="debug_8h.html#a6">nlassert</a> (Mutex != NULL);
00075 }
00076
00077
00078 CUnfairMutex::CUnfairMutex(<font class="keyword">const</font> std::string &name)
00079 {
00080 <font class="comment">// Create a mutex with no initial owner.</font>
00081 Mutex = (<font class="keywordtype">void</font> *) CreateMutex (NULL, FALSE, NULL);
00082 <a class="code" href="debug_8h.html#a6">nlassert</a> (Mutex != NULL);
00083 }
00084
00085
00086 <font class="comment">/*</font>
00087 <font class="comment"> * Windows version</font>
00088 <font class="comment"> */</font>
00089 CUnfairMutex::~CUnfairMutex()
00090 {
00091 CloseHandle( Mutex );
00092 }
00093
00094
00095 <font class="comment">/*</font>
00096 <font class="comment"> * Windows version</font>
00097 <font class="comment"> */</font>
00098 <font class="keywordtype">void</font> CUnfairMutex::enter()
00099 {
00100 <font class="preprocessor">#ifdef NL_DEBUG</font>
00101 <font class="preprocessor"></font> DWORD timeout;
00102 <font class="keywordflow">if</font> ( <a class="code" href="displayer_8cpp.html#a0">IsDebuggerPresent</a>() )
00103 timeout = INFINITE;
00104 <font class="keywordflow">else</font>
00105 timeout = 10000;
00106
00107 <font class="comment">// Request ownership of mutex</font>
00108 DWORD dwWaitResult = WaitForSingleObject (Mutex, timeout);
00109 <font class="preprocessor">#else</font>
00110 <font class="preprocessor"></font> <font class="comment">// Request ownership of mutex during 10s</font>
00111 DWORD dwWaitResult = WaitForSingleObject (Mutex, 10000);
00112 <font class="preprocessor">#endif // NL_DEBUG</font>
00113 <font class="preprocessor"></font> <font class="keywordflow">switch</font> (dwWaitResult)
00114 {
00115 <font class="comment">// The thread got mutex ownership.</font>
00116 <font class="keywordflow">case</font> WAIT_OBJECT_0: <font class="keywordflow">break</font>;
00117 <font class="comment">// Cannot get mutex ownership due to time-out.</font>
00118 <font class="keywordflow">case</font> WAIT_TIMEOUT: <a class="code" href="debug_8h.html#a3">nlerror</a> (<font class="stringliteral">"Dead lock in a mutex (or more that 10s for the critical section"</font>);
00119 <font class="comment">// Got ownership of the abandoned mutex object.</font>
00120 <font class="keywordflow">case</font> WAIT_ABANDONED: <a class="code" href="debug_8h.html#a3">nlerror</a> (<font class="stringliteral">"A thread forgot to release the mutex"</font>);
00121 }
00122 }
00123
00124
00125 <font class="comment">/*</font>
00126 <font class="comment"> * Windows version</font>
00127 <font class="comment"> */</font>
00128 <font class="keywordtype">void</font> CUnfairMutex::leave()
00129 {
00130 <font class="keywordflow">if</font> (ReleaseMutex(Mutex) == 0)
00131 {
00132 <a class="code" href="debug_8h.html#a3">nlerror</a> (<font class="stringliteral">"error while releasing the mutex (0x%x %d), %p"</font>, GetLastError(), GetLastError(), Mutex);
00133 }
00134 }
00135
00136
00137 <font class="comment">/*</font>
00138 <font class="comment"> * Windows version</font>
00139 <font class="comment"> */</font>
00140 CFairMutex::CFairMutex()
00141 {
00142 Name = <font class="stringliteral">"unset mutex name"</font>;
00143
00144 <a class="code" href="mutex_8cpp.html#a0">debugCreateMutex</a>();
00145
00146 <font class="comment">// Check that the CRITICAL_SECTION structure has not changed</font>
00147 <a class="code" href="debug_8h.html#a6">nlassert</a>( <font class="keyword">sizeof</font>(TNelRtlCriticalSection)==<font class="keyword">sizeof</font>(CRITICAL_SECTION) );
00148
00149 <font class="preprocessor">#if (_WIN32_WINNT >= 0x0500)</font>
00150 <font class="preprocessor"></font> DWORD dwSpinCount = 0x80000000; <font class="comment">// set high-order bit to use preallocation</font>
00151 <font class="keywordflow">if</font> ( ! InitializeCriticalSectionAndSpinCount( &_Cs, dwSpinCount ) )
00152 {
00153 <a class="code" href="debug_8h.html#a3">nlerror</a>( <font class="stringliteral">"Error entering critical section"</font> );
00154 }
00155 <font class="preprocessor">#else</font>
00156 <font class="preprocessor"></font> InitializeCriticalSection( (CRITICAL_SECTION*)&_Cs );
00157 <font class="preprocessor">#endif</font>
00158 <font class="preprocessor"></font>}
00159
00160
00161 CFairMutex::CFairMutex(<font class="keyword">const</font> string &name)
00162 {
00163 Name = name;
00164
00165 <font class="preprocessor">#ifdef MUTEX_DEBUG</font>
00166 <font class="preprocessor"></font> <a class="code" href="mutex_8cpp.html#a0">debugCreateMutex</a>();
00167 <font class="preprocessor">#endif</font>
00168 <font class="preprocessor"></font>
00169 <font class="comment">// Check that the CRITICAL_SECTION structure has not changed</font>
00170 <a class="code" href="debug_8h.html#a6">nlassert</a>( <font class="keyword">sizeof</font>(TNelRtlCriticalSection)==<font class="keyword">sizeof</font>(CRITICAL_SECTION) );
00171
00172 <font class="preprocessor">#if (_WIN32_WINNT >= 0x0500)</font>
00173 <font class="preprocessor"></font> DWORD dwSpinCount = 0x80000000; <font class="comment">// set high-order bit to use preallocation</font>
00174 <font class="keywordflow">if</font> ( ! InitializeCriticalSectionAndSpinCount( &_Cs, dwSpinCount ) )
00175 {
00176 <a class="code" href="debug_8h.html#a3">nlerror</a>( <font class="stringliteral">"Error entering critical section"</font> );
00177 }
00178 <font class="preprocessor">#else</font>
00179 <font class="preprocessor"></font> InitializeCriticalSection( (CRITICAL_SECTION*)&_Cs );
00180 <font class="preprocessor">#endif</font>
00181 <font class="preprocessor"></font>}
00182
00183
00184
00185 <font class="comment">/*</font>
00186 <font class="comment"> * Windows version</font>
00187 <font class="comment"> */</font>
00188 CFairMutex::~CFairMutex()
00189 {
00190 DeleteCriticalSection( (CRITICAL_SECTION*)&_Cs );
00191
00192 <a class="code" href="mutex_8cpp.html#a4">debugDeleteMutex</a>();
00193 }
00194
00195
00196 <font class="comment">/*</font>
00197 <font class="comment"> * Windows version</font>
00198 <font class="comment"> */</font>
00199 <font class="keywordtype">void</font> CFairMutex::enter()
00200 {
00201 <a class="code" href="mutex_8cpp.html#a1">debugBeginEnter</a>();
00202
00203 EnterCriticalSection( (CRITICAL_SECTION*)&_Cs );
00204
00205 <a class="code" href="mutex_8cpp.html#a2">debugEndEnter</a>();
00206 }
00207
00208
00209 <font class="comment">/*</font>
00210 <font class="comment"> * Windows version</font>
00211 <font class="comment"> */</font>
00212 <font class="keywordtype">void</font> CFairMutex::leave()
00213 {
00214 LeaveCriticalSection( (CRITICAL_SECTION*)&_Cs );
00215
00216 <a class="code" href="mutex_8cpp.html#a3">debugLeave</a>();
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226 <font class="comment">/*************</font>
00227 <font class="comment"> * Unix code *</font>
00228 <font class="comment"> *************/</font>
00229
00230 <font class="preprocessor">#elif defined NL_OS_UNIX</font>
00231 <font class="preprocessor"></font>
00232 <font class="preprocessor">#include <pthread.h></font>
00233 <font class="preprocessor">#include <errno.h></font>
00234
00235
00236 <font class="comment">/*</font>
00237 <font class="comment"> * Clanlib authors say: "We need to do this because the posix threads library</font>
00238 <font class="comment"> * under linux obviously suck:"</font>
00239 <font class="comment"> */</font>
00240 <font class="keyword">extern</font> <font class="stringliteral">"C"</font>
00241 {
00242 <font class="keywordtype">int</font> pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, <font class="keywordtype">int</font> kind );
00243 }
00244
00245
00246 <font class="keyword">namespace </font>NLMISC {
00247
00248
00249 <font class="comment">/*</font>
00250 <font class="comment"> * Unix version</font>
00251 <font class="comment"> */</font>
00252 CUnfairMutex::CUnfairMutex(<font class="keyword">const</font> std::string &name)
00253 {
00254 pthread_mutexattr_t attr;
00255 pthread_mutexattr_init( &attr );
00256 <font class="comment">// Fast mutex. Note: on Windows all mutexes are recursive</font>
00257 pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ADAPTIVE_NP ); <font class="comment">//PTHREAD_MUTEX_ERRORCHECK_NP );//PTHREAD_MUTEX_ADAPTIVE_NP );//PTHREAD_MUTEX_RECURSIVE_NP );</font>
00258 pthread_mutex_init( &mutex, &attr );
00259 pthread_mutexattr_destroy( &attr );
00260 }
00261
00262
00263 <font class="comment">/*</font>
00264 <font class="comment"> * Unix version</font>
00265 <font class="comment"> */</font>
00266 CUnfairMutex::~CUnfairMutex()
00267 {
00268 pthread_mutex_destroy( &mutex );
00269 }
00270
00271
00272 <font class="comment">/*</font>
00273 <font class="comment"> * Unix version</font>
00274 <font class="comment"> */</font>
00275 <font class="keywordtype">void</font> CUnfairMutex::enter()
00276 {
00277 <font class="comment">//cout << getpid() << ": Locking " << &mutex << endl;</font>
00278 <font class="keywordflow">if</font> ( pthread_mutex_lock( &mutex ) != 0 )
00279 {
00280 <font class="comment">//cout << "Error locking a mutex " << endl;</font>
00281 <a class="code" href="debug_8h.html#a3">nlerror</a>( <font class="stringliteral">"Error locking a mutex"</font> );
00282 }
00283 <font class="comment">/*else</font>
00284 <font class="comment"> {</font>
00285 <font class="comment"> cout << getpid() << ": Owning " << &mutex << endl;</font>
00286 <font class="comment"> }*/</font>
00287 }
00288
00289
00290 <font class="comment">/*</font>
00291 <font class="comment"> * Unix version</font>
00292 <font class="comment"> */</font>
00293 <font class="keywordtype">void</font> CUnfairMutex::leave()
00294 {
00295 <font class="comment">//int errcode;</font>
00296 <font class="comment">//cout << getpid() << ": Unlocking " << &mutex << endl;</font>
00297 <font class="keywordflow">if</font> ( (<font class="comment">/*errcode=*/</font>pthread_mutex_unlock( &mutex )) != 0 )
00298 {
00299 <font class="comment">/* switch ( errcode )</font>
00300 <font class="comment"> {</font>
00301 <font class="comment"> case EINVAL: cout << "INVAL" << endl; break;</font>
00302 <font class="comment"> case EPERM: cout << "PERM" << endl; break;</font>
00303 <font class="comment"> default: cout << "OTHER" << endl;</font>
00304 <font class="comment"> }</font>
00305 <font class="comment"> */</font>
00306 <font class="comment">//cout << "Error unlocking a mutex " /*<< &mutex*/ << endl;</font>
00307 <a class="code" href="debug_8h.html#a3">nlerror</a>( <font class="stringliteral">"Error unlocking a mutex"</font> );
00308 }
00309 <font class="comment">/*else</font>
00310 <font class="comment"> {</font>
00311 <font class="comment"> cout << getpid() << ": Released " << &mutex << endl;</font>
00312 <font class="comment"> }*/</font>
00313 }
00314
00315
00316 <font class="comment">/*</font>
00317 <font class="comment"> * Unix version</font>
00318 <font class="comment"> */</font>
00319 CFairMutex::CFairMutex()
00320 {
00321 sem_init( const_cast<sem_t*>(&_Sem), 0, 1 );
00322 }
00323
00324
00325 CFairMutex::CFairMutex( <font class="keyword">const</font> std::string &name )
00326 {
00327 sem_init( const_cast<sem_t*>(&_Sem), 0, 1 );
00328 }
00329
00330
00331 <font class="comment">/*</font>
00332 <font class="comment"> * Unix version</font>
00333 <font class="comment"> */</font>
00334 CFairMutex::~CFairMutex()
00335 {
00336 sem_destroy( const_cast<sem_t*>(&_Sem) ); <font class="comment">// needs that no thread is waiting on the semaphore</font>
00337 }
00338
00339
00340 <font class="comment">/*</font>
00341 <font class="comment"> * Unix version</font>
00342 <font class="comment"> */</font>
00343 <font class="keywordtype">void</font> CFairMutex::enter()
00344 {
00345 sem_wait( const_cast<sem_t*>(&_Sem) );
00346 }
00347
00348
00349 <font class="comment">/*</font>
00350 <font class="comment"> * Unix version</font>
00351 <font class="comment"> */</font>
00352 <font class="keywordtype">void</font> CFairMutex::leave()
00353 {
00354 sem_post( const_cast<sem_t*>(&_Sem) );
00355 }
00356
00357
00358 <font class="preprocessor">#endif // NL_OS_WINDOWS/NL_OS_LINUX</font>
00359 <font class="preprocessor"></font>
00360
00361
00362
00363
00364
00365
00366 <font class="comment">/******************</font>
00367 <font class="comment"> * Debugging code *</font>
00368 <font class="comment"> ******************/</font>
00369
00370 <font class="preprocessor">#ifdef MUTEX_DEBUG</font>
00371 <font class="preprocessor"></font>
00372 map<CFairMutex*,TMutexLocks> *AcquireTime = NULL;
00373 <a class="code" href="types__nl_8h.html#a10">uint32</a> NbMutexes = 0;
00374 CFairMutex *ATMutex = NULL;
00375 <font class="keywordtype">bool</font> InitAT = <font class="keyword">true</font>;
00376
00377
00379 <font class="keywordtype">void</font> initAcquireTimeMap()
00380 {
00381 <font class="keywordflow">if</font> ( InitAT )
00382 {
00383 ATMutex = <font class="keyword">new</font> CFairMutex(<font class="stringliteral">"ATMutex"</font>);
00384 AcquireTime = <font class="keyword">new</font> map<CFairMutex*,TMutexLocks>;
00385 InitAT = <font class="keyword">false</font>;
00386 }
00387 }
00388
00389
00391 map<CFairMutex*,TMutexLocks> getNewAcquireTimes()
00392 {
00393 map<CMutex*,TMutexLocks> m;
00394 ATMutex->enter();
00395
00396 <font class="comment">// Copy map</font>
00397 m = *AcquireTime;
00398
00399 <font class="comment">// Reset map</font>
00400 <font class="comment">/* map<CMutex*,TMutexLocks>::iterator im;</font>
00401 <font class="comment"> for ( im=AcquireTime->begin(); im!=AcquireTime->end(); ++im )</font>
00402 <font class="comment"> {</font>
00403 <font class="comment"> (*im).second.Time = 0;</font>
00404 <font class="comment"> (*im).second.Nb = 0;</font>
00405 <font class="comment"> (*im).second.Locked = false;</font>
00406 <font class="comment"> }</font>
00407 <font class="comment">*/</font>
00408 ATMutex->leave();
00409 <font class="keywordflow">return</font> m;
00410 }
00411
00412
00415
00416 <font class="keywordtype">void</font> <a class="code" href="mutex_8cpp.html#a0">CFairMutex::debugCreateMutex</a>()
00417 {
00418 <font class="comment">/* if ( ! InitAT )</font>
00419 <font class="comment"> {</font>
00420 <font class="comment"> ATMutex->enter();</font>
00421 <font class="comment"> AcquireTime->insert( make_pair( this, TMutexLocks(NbMutexes) ) );</font>
00422 <font class="comment"> NbMutexes++;</font>
00423 <font class="comment"> ATMutex->leave();</font>
00424 <font class="comment"> char dbgstr [256];</font>
00425 <font class="comment"> smprintf( dbgstr, 256, "MUTEX: Creating mutex %p %s (number %u)\n", this, Name.c_str(), NbMutexes-1 );</font>
00426 <font class="comment">#ifdef NL_OS_WINDOWS</font>
00427 <font class="comment"> if ( IsDebuggerPresent() )</font>
00428 <font class="comment"> OutputDebugString( dbgstr );</font>
00429 <font class="comment">#endif</font>
00430 <font class="comment"> cout << dbgstr << endl;</font>
00431 <font class="comment"> }</font>
00432 <font class="comment">*/</font>}
00433
00434 <font class="keywordtype">void</font> <a class="code" href="mutex_8cpp.html#a4">CFairMutex::debugDeleteMutex</a>()
00435 {
00436 <font class="keywordflow">if</font> ( (<font class="keyword">this</font>!=ATMutex ) && (ATMutex!=NULL) )
00437 {
00438 ATMutex->enter();
00439 (*AcquireTime)[<font class="keyword">this</font>].Dead = <font class="keyword">true</font>;
00440 ATMutex->leave();
00441 }
00442 }
00443
00444 <font class="keywordtype">void</font> <a class="code" href="mutex_8cpp.html#a1">CFairMutex::debugBeginEnter</a>()
00445 {
00446 <font class="keywordflow">if</font> ( (<font class="keyword">this</font>!=ATMutex ) && (ATMutex!=NULL) )
00447 {
00448 <a class="code" href="namespaceNLMISC.html#a183">TTicks</a> <a class="code" href="driver__opengl__extension__def_8h.html#a384">t</a> = CTime::getPerformanceTime();
00449
00450 ATMutex->enter();
00451 std::map<CMutex*,TMutexLocks>::iterator it = (*AcquireTime).find (<font class="keyword">this</font>);
00452 <font class="keywordflow">if</font> (it == (*AcquireTime).end())
00453 {
00454 AcquireTime->insert( make_pair( <font class="keyword">this</font>, TMutexLocks(NbMutexes++) ) );
00455 <font class="keywordtype">char</font> dbgstr [256];
00456 <a class="code" href="namespaceNLMISC.html#a203">smprintf</a>( dbgstr, 256, <font class="stringliteral">"MUTEX: Creating mutex %p %s (number %u)\n"</font>, <font class="keyword">this</font>, Name.c_str(), NbMutexes-1 );
00457 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00458 <font class="preprocessor"></font> <font class="keywordflow">if</font> ( <a class="code" href="displayer_8cpp.html#a0">IsDebuggerPresent</a>() ) OutputDebugString( dbgstr );
00459 <font class="preprocessor">#endif</font>
00460 <font class="preprocessor"></font> cout << dbgstr << endl;
00461
00462 it = (*AcquireTime).find (<font class="keyword">this</font>);
00463 (*it).second.MutexName = Name;
00464 }
00465 (*it).second.WaitingMutex++;
00466 (*it).second.BeginEnter = <a class="code" href="driver__opengl__extension__def_8h.html#a384">t</a>;
00467 ATMutex->leave();
00468 }
00469 }
00470
00471
00472 <font class="keywordtype">void</font> <a class="code" href="mutex_8cpp.html#a2">CFairMutex::debugEndEnter</a>()
00473 {
00474 <font class="comment">// printf("1");</font>
00475 <font class="comment">/* char str[1024];</font>
00476 <font class="comment"> sprintf(str, "enter %8p %8p %8p\n", this, Mutex, getThreadId ());</font>
00477 <font class="comment"> if (Mutex == (void*)0x88)</font>
00478 <font class="comment"> {</font>
00479 <font class="comment"> OutputDebugString (str);</font>
00480 <font class="comment"> if (entered) __asm int 3;</font>
00481 <font class="comment"> entered = true;</font>
00482 <font class="comment"> }</font>
00483 <font class="comment">*/</font>
00484 <font class="keywordflow">if</font> ( ( <font class="keyword">this</font> != ATMutex ) && ( ATMutex != NULL ) )
00485 {
00486 <a class="code" href="namespaceNLMISC.html#a183">TTicks</a> <a class="code" href="driver__opengl__extension__def_8h.html#a384">t</a> = CTime::getPerformanceTime();
00487 ATMutex->enter();
00488 <font class="keywordflow">if</font> ((uint32)(<a class="code" href="driver__opengl__extension__def_8h.html#a384">t</a>-(*AcquireTime)[<font class="keyword">this</font>].BeginEnter) > (*AcquireTime)[<font class="keyword">this</font>].TimeToEnter)
00489 (*AcquireTime)[<font class="keyword">this</font>].TimeToEnter = (uint32)(<a class="code" href="driver__opengl__extension__def_8h.html#a384">t</a>-(*AcquireTime)[<font class="keyword">this</font>].BeginEnter);
00490 (*AcquireTime)[<font class="keyword">this</font>].Nb += 1;
00491 (*AcquireTime)[<font class="keyword">this</font>].WaitingMutex--;
00492 (*AcquireTime)[<font class="keyword">this</font>].ThreadHavingTheMutex = <a class="code" href="namespaceNLMISC.html#a229">getThreadId</a>();
00493 (*AcquireTime)[<font class="keyword">this</font>].EndEnter = <a class="code" href="driver__opengl__extension__def_8h.html#a384">t</a>;
00494 ATMutex->leave();
00495 }
00496 }
00497
00498
00499 <font class="keywordtype">void</font> <a class="code" href="mutex_8cpp.html#a3">CFairMutex::debugLeave</a>()
00500 {
00501 <font class="comment">// printf( "0" );</font>
00502 <font class="comment">/* char str[1024];</font>
00503 <font class="comment"> sprintf(str, "leave %8p %8p %8p\n", this, Mutex, getThreadId ());</font>
00504 <font class="comment"> if (Mutex == (void*)0x88)</font>
00505 <font class="comment"> {</font>
00506 <font class="comment"> OutputDebugString (str);</font>
00507 <font class="comment"> if (!entered) __asm int 3;</font>
00508 <font class="comment"> entered = false;</font>
00509 <font class="comment"> }</font>
00510 <font class="comment">*/</font>
00511
00512 <font class="keywordflow">if</font> ( ( <font class="keyword">this</font> != ATMutex ) && ( ATMutex != NULL ) )
00513 {
00514 <a class="code" href="namespaceNLMISC.html#a183">TTicks</a> Leave = CTime::getPerformanceTime();
00515 ATMutex->enter();
00516 <font class="keywordflow">if</font> ((uint32)(Leave-(*AcquireTime)[<font class="keyword">this</font>].EndEnter) > (*AcquireTime)[<font class="keyword">this</font>].TimeInMutex)
00517 (*AcquireTime)[<font class="keyword">this</font>].TimeInMutex = (uint32)(Leave-(*AcquireTime)[<font class="keyword">this</font>].EndEnter);
00518 (*AcquireTime)[<font class="keyword">this</font>].Nb += 1;
00519 (*AcquireTime)[<font class="keyword">this</font>].WaitingMutex = <font class="keyword">false</font>;
00520 (*AcquireTime)[<font class="keyword">this</font>].ThreadHavingTheMutex = 0xFFFFFFFF;
00521 ATMutex->leave();
00522 }
00523
00524 }
00525
00526 <font class="preprocessor">#endif // MUTEX_DEBUG</font>
00527 <font class="preprocessor"></font>
00528 } <font class="comment">// NLMISC</font>
</pre></div>
<!-- footer -->
<BR><FONT Size=+5> </FONT>
</TD>
<TD WIDTH=15><IMG SRC=http://www.nevrax.org/inc/img/pixel.gif WIDTH=15 HEIGHT=15 BORDER=0 ALT=""></TD>
</TR>
</TABLE>
</BODY>
</HTML>
|