#include <mutex.h>
Implementation notes:
Tested: OK on Windows and Linux single & multi-processor
CFastMutex m; m.enter (); // do critical stuffs m.leave (); *
Olivier Cado
Nevrax France
Definition at line 194 of file mutex.h.
Public Member Functions | |
CFastMutex () | |
Constructor. | |
__forceinline void | enter () volatile |
void | init () volatile |
Same as constructor, useful for init in a shared memory block. | |
__forceinline void | leave () volatile |
Static Public Member Functions | |
__forceinline bool | atomic_swap (volatile uint32 *lockPtr) |
Atomic swap. | |
Private Attributes | |
volatile uint32 | _Lock |
|
Constructor.
Definition at line 199 of file mutex.h. References _Lock.
00199 : _Lock(0) {} |
|
Atomic swap.
Definition at line 205 of file mutex.h. References ASM_ASWAP_FOR_GCC_XCHG, and uint32. Referenced by enter().
00206 { 00207 uint32 result; 00208 #ifdef NL_OS_WINDOWS 00209 #ifdef NL_DEBUG_FAST 00210 // Workaround for dumb inlining bug (returning of function goes into the choux): push/pop registers 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 #else 00223 __asm 00224 { 00225 mov ecx,lockPtr 00226 mov eax,1 00227 xchg [ecx],eax 00228 mov [result],eax 00229 } 00230 #endif 00231 #else 00232 ASM_ASWAP_FOR_GCC_XCHG 00233 #endif // NL_OS_WINDOWS 00234 return result != 0; 00235 } |
|
Definition at line 238 of file mutex.h. References _Lock, atomic_swap(), and uint. Referenced by NLMISC::CBlockMemory< CNode >::__stl_alloc_changeEltSize(), NLMISC::CBlockMemory< CNode >::allocate(), NLMISC::CBlockMemory< CNode >::free(), NLMISC::CBlockMemory< CNode >::purge(), and RenderTriangle().
00239 { 00240 //std::cout << "Entering, Lock=" << _Lock << std::endl; 00241 if (atomic_swap (&_Lock)) 00242 { 00243 // First test 00244 uint i; 00245 for (i = 0 ;; ++i) 00246 { 00247 uint wait_time = i + 6; 00248 00249 // Increment wait time with a log function 00250 if (wait_time > 27) 00251 wait_time = 27; 00252 00253 // Sleep 00254 if (wait_time <= 20) 00255 wait_time = 0; 00256 else 00257 wait_time = 1 << (wait_time - 20); 00258 00259 if (!atomic_swap (&_Lock)) 00260 break; 00261 00262 #ifdef NL_OS_WINDOWS 00263 Sleep (wait_time); 00264 #else 00265 //std::cout << "Sleeping i=" << i << std::endl; 00266 usleep( wait_time*1000 ); 00267 #endif 00268 } 00269 } 00270 } |
|
Same as constructor, useful for init in a shared memory block.
Definition at line 202 of file mutex.h. References _Lock.
00202 { _Lock = 0; } |
|
Definition at line 273 of file mutex.h. References _Lock. Referenced by NLMISC::CBlockMemory< CNode >::__stl_alloc_changeEltSize(), NLMISC::CBlockMemory< CNode >::allocate(), NLMISC::CBlockMemory< CNode >::free(), NLMISC::CBlockMemory< CNode >::purge(), and RenderTriangle().
00274 { 00275 _Lock = 0; 00276 //std::cout << "Leave" << std::endl; 00277 } |
|
Definition at line 280 of file mutex.h. Referenced by CFastMutex(), enter(), init(), and leave(). |