#include <mutex.h>
Implementation notes:
CFastMutexMP m; m.enter (); // do critical stuffs m.leave (); *
Olivier Cado
Nevrax France
Definition at line 309 of file mutex.h.
Public Member Functions | |
CFastMutexMP () | |
Constructor. | |
__forceinline void | enter () volatile |
void | init () volatile |
Same as constructor, useful for init in a shared memory block. | |
__forceinline void | leave () volatile |
Private Attributes | |
volatile uint32 | _Lock |
|
Constructor.
Definition at line 314 of file mutex.h.
00314 : _Lock(0) {} |
|
Definition at line 320 of file mutex.h. References uint.
00321 { 00322 //std::cout << "Entering, Lock=" << _Lock << std::endl; 00323 while (CFastMutex::atomic_swap (&_Lock)) 00324 { 00325 static uint last = 0; 00326 static uint _max = 30; 00327 uint spinMax = _max; 00328 uint lastSpins = last; 00329 volatile uint temp = 17; 00330 uint i; 00331 for (i = 0; i < spinMax; ++i) 00332 { 00333 if (i < lastSpins/2 || _Lock) 00334 { 00335 temp *= temp; 00336 temp *= temp; 00337 temp *= temp; 00338 temp *= temp; 00339 } 00340 else 00341 { 00342 if (!CFastMutex::atomic_swap(&_Lock)) 00343 { 00344 last = i; 00345 _max = 1000; 00346 return; 00347 } 00348 } 00349 } 00350 00351 _max = 30; 00352 00353 // First test 00354 for (i = 0 ;; ++i) 00355 { 00356 uint wait_time = i + 6; 00357 00358 // Increment wait time with a log function 00359 if (wait_time > 27) 00360 wait_time = 27; 00361 00362 // Sleep 00363 if (wait_time <= 20) 00364 wait_time = 0; 00365 else 00366 wait_time = 1 << (wait_time - 20); 00367 00368 if (!CFastMutex::atomic_swap (&_Lock)) 00369 break; 00370 00371 #ifdef NL_OS_WINDOWS 00372 Sleep (wait_time); 00373 #else 00374 //std::cout << "Sleeping i=" << i << std::endl; 00375 usleep( wait_time*1000 ); 00376 #endif 00377 } 00378 } 00379 } |
|
Same as constructor, useful for init in a shared memory block.
Definition at line 317 of file mutex.h.
00317 { _Lock = 0; } |
|
Definition at line 382 of file mutex.h.
00383 { 00384 _Lock = 0; 00385 //std::cout << "Leave" << std::endl; 00386 } |
|
|