Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

win32_mutex.h

00001 #ifndef WIN32_MUTEX_H
00002 #define WIN32_MUTEX_H
00003 
00004 #include <windows.h>
00005 
00006 /* NOTE: In order to allow proper copying, I have to use pointers to
00007    CRITICAL_SECTION objects, so I decided I might as well use dynamic
00008    allocation.  I don't think we'll be creating that many mutexes
00009    anyway.  */
00010 
00011 class Mutex
00012 {
00013 public:
00014   /* default constructor (creates an unlocked mutex) */
00015   Mutex (void)
00016     {
00017       mutex = new CRITICAL_SECTION;
00018       InitializeCriticalSection (mutex);
00019       refs = new int;
00020       *refs = 1;
00021     }
00022 
00023   /* copy constructor */
00024   Mutex (const Mutex& m)
00025     {
00026       mutex = m.mutex;
00027       refs = m.refs;
00028       (*refs)++; /* <-- FIXME: race condition */
00029     }
00030 
00031   /* assignment operator */
00032   Mutex& operator = (const Mutex& m)
00033     {
00034       (*refs)--;
00035       if (*refs == 0)
00036         {
00037           delete refs;
00038           DeleteCriticalSection (mutex);
00039           delete mutex;
00040         }
00041 
00042       mutex = m.mutex;
00043       refs = m.refs;
00044       (*refs)++;
00045           return *(this);
00046     }
00047 
00048   /* lock the mutex */
00049   void Lock (void)
00050     {
00051       EnterCriticalSection (mutex);
00052     }
00053 
00054   /* unlock the mutex */
00055   void Unlock (void)
00056     {
00057       LeaveCriticalSection (mutex);
00058     }
00059 
00060   /* destructor */
00061   ~Mutex (void)
00062     {
00063       (*refs)--;
00064       if (*refs == 0)
00065         {
00066           delete refs;
00067           DeleteCriticalSection (mutex);
00068           delete mutex;
00069         }
00070     }
00071 
00072 private:
00073   /* reference count */
00074   int *refs;
00075 
00076   /* the mutex */
00077   CRITICAL_SECTION *mutex;
00078 };
00079 
00080 #endif

Generated on Fri Jan 12 14:15:02 2007 for Construct by  doxygen 1.4.3-20050530