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