00001 /// \file CATMutex_Win32.cpp 00002 /// \brief Win32 implementation of CATMutex 00003 /// \ingroup CAT 00004 /// 00005 /// Copyright (c) 2003-2008 by Michael Ellison. 00006 /// See COPYING.txt for the \ref gaslicense License (MIT License). 00007 /// 00008 // $Author: mikeellison $ 00009 // $Date: 2008-01-21 08:33:12 -0600 (Mon, 21 Jan 2008) $ 00010 // $Revision: $ 00011 // $NoKeywords: $ 00012 00013 #include "CATMutex.h" 00014 #include "CATUtil.h" 00015 00016 //--------------------------------------------------------------------------- 00017 CATMutex::CATMutex() 00018 { 00019 fMutexHandle = CreateMutex(0,FALSE,0); 00020 CATASSERT(fMutexHandle != INVALID_HANDLE_VALUE, "Unable to create mutex."); 00021 00022 if (fMutexHandle == INVALID_HANDLE_VALUE) 00023 fMutexHandle = 0; 00024 } 00025 00026 //--------------------------------------------------------------------------- 00027 CATMutex::~CATMutex() 00028 { 00029 if (fMutexHandle) 00030 { 00031 CloseHandle(fMutexHandle); 00032 fMutexHandle = 0; 00033 } 00034 } 00035 00036 //--------------------------------------------------------------------------- 00037 CATResult CATMutex::Wait(CATUInt32 milliseconds) 00038 { 00039 if (fMutexHandle == 0) 00040 { 00041 return CATRESULT(CAT_ERR_MUTEX_INVALID_HANDLE); 00042 } 00043 00044 if (milliseconds == 0xFFFFFFFF) 00045 { 00046 milliseconds = INFINITE; 00047 } 00048 00049 DWORD result = WaitForSingleObject(fMutexHandle,milliseconds); 00050 00051 switch (result) 00052 { 00053 case WAIT_OBJECT_0: 00054 return CATRESULT(CAT_SUCCESS); 00055 case WAIT_TIMEOUT: 00056 return CATRESULT(CAT_ERR_MUTEX_TIMEOUT); 00057 default: 00058 // Error occurred. Handle was invalid or something similar. 00059 return CATRESULT(CAT_ERR_MUTEX_WAIT_ERROR); 00060 } 00061 } 00062 00063 //--------------------------------------------------------------------------- 00064 CATResult CATMutex::Release() 00065 { 00066 if (fMutexHandle == 0) 00067 { 00068 return CATRESULT(CAT_ERR_MUTEX_INVALID_HANDLE); 00069 } 00070 00071 ::ReleaseMutex(fMutexHandle); 00072 return CATRESULT(CAT_SUCCESS); 00073 } 00074