00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcMutex.h: interface for the CdcMutex class. */ 00021 00022 /** 00023 * Wrapper for an OS Mutex. 00024 * <p> 00025 * @author Erez Bibi 00026 * @version 1.0 00027 */ 00028 00029 #ifndef __CDC_MUTEX 00030 #define __CDC_MUTEX 00031 00032 #include "../DcGlobals.h" 00033 #include "../DcString.h" 00034 #include "../DcException.h" 00035 00036 00037 class CdcMutex 00038 { 00039 private: 00040 00041 /* Handle to the mutex. */ 00042 HANDLE hMutex; 00043 00044 public: 00045 00046 /** 00047 * Constructor - Initialize the mutext. 00048 */ 00049 CdcMutex() 00050 { hMutex = CreateMutex (NULL, false, NULL); 00051 if (hMutex == NULL) 00052 throw CdcException ("Cannot create mutex."); 00053 } 00054 00055 /** 00056 * Destructor - Release the mutex. 00057 */ 00058 virtual ~CdcMutex() 00059 { ReleaseMutex (hMutex); CloseHandle (hMutex); } 00060 00061 /** 00062 * Takes the mutex. 00063 * <p> 00064 * @param wait The time to wait if the mutex blocks [mSec]. 00065 * @return (bool) False if timeout. 00066 */ 00067 bool Lock (int wait = INFINITE) 00068 { return WaitForSingleObject (hMutex, wait) == WAIT_OBJECT_0; } 00069 00070 /** 00071 * Returns the mutex. 00072 */ 00073 bool Unlock () 00074 { return ReleaseMutex (hMutex) != 0; } 00075 00076 }; 00077 00078 #endif /* __CDC_MUTEX */