00001 /// \file CATSignal_Win32.cpp 00002 /// \brief Win32 implementation of signal events 00003 /// \ingroup CAT 00004 /// 00005 /// 00006 /// Copyright (c) 2003-2008 by Michael Ellison. 00007 /// See COPYING.txt for the \ref gaslicense License (MIT License). 00008 /// 00009 // $Author: mikeellison $ 00010 // $Date: 2008-01-21 08:33:12 -0600 (Mon, 21 Jan 2008) $ 00011 // $Revision: $ 00012 // $NoKeywords: $ 00013 00014 #include "CATSignal.h" 00015 00016 CATSignal::CATSignal(bool autoReset) 00017 { 00018 fEvent = ::CreateEvent(0,autoReset?FALSE:TRUE,FALSE,0); 00019 if (fEvent == INVALID_HANDLE_VALUE) 00020 { 00021 CATASSERT(false,"Could not create event!"); 00022 fEvent = 0; 00023 } 00024 } 00025 00026 CATSignal::~CATSignal() 00027 { 00028 if (fEvent) 00029 { 00030 CloseHandle(fEvent); 00031 fEvent = 0; 00032 } 00033 } 00034 00035 // Wait() waits up to [milliseconds] milliseconds for the 00036 // signal to be fired. If the CATSignal is set to auto-reset, 00037 // the it will be reset when a caller successfully receive 00038 // the event through a wait. 00039 // 00040 // Check the result code here! It can very easily time out. 00041 // 00042 // The default, however, is an infinite wait. 00043 // 00044 // \param milliseconds - milliseconds to wait while trying to get synch 00045 // 00046 // \sa Release() 00047 CATResult CATSignal::Wait(CATUInt32 milliseconds) 00048 { 00049 if (!fEvent) 00050 { 00051 return CATRESULT(CAT_ERR_SIGNAL_INVALID_HANDLE); 00052 } 00053 00054 if (milliseconds == 0xFFFFFFFF) 00055 { 00056 milliseconds = INFINITE; 00057 } 00058 DWORD waitRes = ::WaitForSingleObject(fEvent,milliseconds); 00059 00060 switch (waitRes) 00061 { 00062 case WAIT_OBJECT_0: 00063 return CATRESULT(CAT_SUCCESS); 00064 case WAIT_TIMEOUT: 00065 return CATRESULT(CAT_ERR_SIGNAL_TIMEOUT); 00066 default: 00067 // Error occurred. Handle was invalid or something similar. 00068 return CATRESULT(CAT_ERR_SIGNAL_WAIT_ERROR); 00069 } 00070 } 00071 00072 00073 // Fire() fires the signal, which then allows caller(s) through. 00074 CATResult CATSignal::Fire() 00075 { 00076 if (!fEvent) 00077 { 00078 return CATRESULT(CAT_ERR_SIGNAL_INVALID_HANDLE); 00079 } 00080 ::SetEvent(fEvent); 00081 00082 return CAT_SUCCESS; 00083 } 00084 00085 /// Reset() resets the signal, making the signal block callers. 00086 CATResult CATSignal::Reset() 00087 { 00088 if (!fEvent) 00089 { 00090 return CATRESULT(CAT_ERR_SIGNAL_INVALID_HANDLE); 00091 } 00092 ::ResetEvent(fEvent); 00093 00094 return CAT_SUCCESS; 00095 } 00096 00097 HANDLE CATSignal::GetWin32Handle() 00098 { 00099 return fEvent; 00100 } 00101 00102