Alex Bikfalvi
SimWorker Documentation
SimSignal.cpp
00001 #include "Headers.h" 00002 #include "SimSignal.h" 00003 #include "ExceptionSignal.h" 00004 00005 CSimSignal::CSimSignal() 00006 { 00007 #ifdef WIN32 00008 // Initialize the event 00009 if(NULL == (this->evt = CreateEvent( 00010 NULL, // default security attributes 00011 false, // manual reset 00012 false, // initial state (true is signaled) 00013 NULL // event name 00014 ))) throw CExceptionSignal("create signal event failed", __FILE__, __LINE__); 00015 #elif POSIX 00016 // Initialize the mutex 00017 if(pthread_mutex_init(&this->mutex, NULL)) throw CExceptionSignal("create signal mutex failed", __FILE__, __LINE__); 00018 // Initialize the condition 00019 if(pthread_cond_init(&this->cond, NULL)) throw CExceptionSignal("create signal condition failed", __FILE__, __LINE__); 00020 // Lock the mutex 00021 if(pthread_mutex_lock(&this->mutex)) throw CExceptionSignal("locking signal mutex failed", __FILE__, __LINE__); 00022 #endif 00023 } 00024 00025 CSimSignal::~CSimSignal() 00026 { 00027 #ifdef WIN32 00028 // Delete the event 00029 if(!CloseHandle(this->evt)) throw CExceptionSignal("close signal event failed", __FILE__, __LINE__); 00030 #elif POSIX 00031 // Unlock the mutex 00032 if(pthread_mutex_unlock(&this->mutex)) throw CExceptionSignal("unlocking signal mutex failed", __FILE__, __LINE__); 00033 // Delete the condition 00034 if(pthread_cond_destroy(&this->cond)) throw CExceptionSignal("close signal condition failed", __FILE__, __LINE__); 00035 // Delete the mutex 00036 if(pthread_mutex_destroy(&this->mutex)) throw CExceptionSignal("close signal mutex failed", __FILE__, __LINE__); 00037 #endif 00038 } 00039 00040 void CSimSignal::Raise() 00041 { 00042 #ifdef WIN32 00043 // Signal the event 00044 if(!SetEvent(this->evt)) throw CExceptionSignal("set signal to signaled state failed", __FILE__, __LINE__); 00045 #elif POSIX 00046 // Lock the mutex 00047 if(pthread_mutex_lock(&this->mutex)) throw CExceptionSignal("locking signal mutex failed", __FILE__, __LINE__); 00048 // Signal the condition 00049 if(pthread_cond_signal(&this->cond)) throw CExceptionSignal("set signal to signaled state failed", __FILE__, __LINE__); 00050 // Unlock the mutex 00051 if(pthread_mutex_unlock(&this->mutex)) throw CExceptionSignal("unlocking signal mutex failed", __FILE__, __LINE__); 00052 #endif 00053 } 00054 00055 void CSimSignal::Wait() 00056 { 00057 #ifdef WIN32 00058 if(WAIT_FAILED == WaitForSingleObject(this->evt, INFINITE)) throw CExceptionSignal("waiting for signal failed", __FILE__, __LINE__); 00059 #elif POSIX 00060 if(pthread_cond_wait(&this->cond, &this->mutex)) throw CExceptionSignal("waiting for signal failed", __FILE__, __LINE__); 00061 #endif 00062 } 00063 00064 void CSimSignal::Lock() 00065 { 00066 // Used only in POSIX to lock the signal mutex 00067 #ifdef POSIX 00068 if(pthread_mutex_lock(&this->mutex)) throw CExceptionSignal("locking signal mutex failed", __FILE__, __LINE__); 00069 #endif 00070 } 00071 00072 void CSimSignal::Unlock() 00073 { 00074 // Used only in POSIX to unlock the signal mutex 00075 #ifdef POSIX 00076 if(pthread_mutex_unlock(&this->mutex)) throw CExceptionSignal("unlocking signal mutex failed", __FILE__, __LINE__); 00077 #endif 00078 }
Last updated: February 8, 2011