NLMISC::CFastMutex Class Reference

#include <mutex.h>


Detailed Description

Fast mutex implementation (not fairly) The mutex ARE NOT recursive (ie don't call enter() several times on the same mutex from the same thread without having called leave()) ; 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.

Implementation notes:

Tested: OK on Windows and Linux single & multi-processor

CFastMutex m; m.enter (); // do critical stuffs m.leave (); *
Author:
Cyril 'Hulud' Corvazier

Olivier Cado

Nevrax France

Date:
2002, 2003

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 & Destructor Documentation

NLMISC::CFastMutex::CFastMutex  )  [inline]
 

Constructor.

Definition at line 199 of file mutex.h.

References _Lock.

00199 : _Lock(0) {}


Member Function Documentation

__forceinline bool NLMISC::CFastMutex::atomic_swap volatile uint32 lockPtr  )  [inline, static]
 

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         }

__forceinline void NLMISC::CFastMutex::enter  )  volatile [inline]
 

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         }

void NLMISC::CFastMutex::init void   )  volatile [inline]
 

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

Definition at line 202 of file mutex.h.

References _Lock.

00202 { _Lock = 0; }

__forceinline void NLMISC::CFastMutex::leave  )  volatile [inline]
 

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         }


Field Documentation

volatile uint32 NLMISC::CFastMutex::_Lock [private]
 

Definition at line 280 of file mutex.h.

Referenced by CFastMutex(), enter(), init(), and leave().


The documentation for this class was generated from the following file:
Generated on Tue Mar 16 13:12:31 2004 for NeL by doxygen 1.3.6