00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArCondition.h"
00030 #include "ArLog.h"
00031
00032
00033 ArStrMap ArCondition::ourStrMap;
00034
00035
00036 ArCondition::ArCondition() :
00037 myFailedInit(false),
00038 myCond(),
00039 myCount(0)
00040 {
00041 myCond=CreateEvent(0, FALSE, FALSE, 0);
00042 if (myCond == NULL)
00043 {
00044 ArLog::log(ArLog::Terse, "ArCondition::ArCondition: Unknown error trying to create the condition.");
00045 myFailedInit=true;
00046 }
00047
00048 ourStrMap[STATUS_FAILED]="General failure";
00049 ourStrMap[STATUS_FAILED_DESTROY]=
00050 "Another thread is waiting on this condition so it can not be destroyed";
00051 ourStrMap[STATUS_FAILED_INIT] =
00052 "Failed to initialize thread. Requested action is imposesible";
00053 ourStrMap[STATUS_MUTEX_FAILED_INIT]="The underlying mutex failed to init";
00054 ourStrMap[STATUS_MUTEX_FAILED]="The underlying mutex failed in some fashion";
00055 }
00056
00057 ArCondition::~ArCondition()
00058 {
00059 if (!myFailedInit && !CloseHandle(myCond))
00060 ArLog::log(ArLog::Terse, "ArCondition::~ArCondition: Unknown error while trying to destroy the condition.");
00061 }
00062
00063 int ArCondition::signal()
00064 {
00065 if (myFailedInit)
00066 {
00067 ArLog::log(ArLog::Terse, "ArCondition::signal: Initialization of condition failed, failed to signal");
00068 return(STATUS_FAILED_INIT);
00069 }
00070
00071 if (!PulseEvent(myCond))
00072 {
00073 ArLog::log(ArLog::Terse, "ArCondition::signal: Unknown error while trying to signal the condition.");
00074 return(STATUS_FAILED);
00075 }
00076
00077 return(0);
00078 }
00079
00080 int ArCondition::broadcast()
00081 {
00082 int ret=0;
00083
00084 if (myFailedInit)
00085 {
00086 ArLog::log(ArLog::Terse, "ArCondition::broadcast: Initialization of condition failed, failed to broadcast");
00087 return(STATUS_FAILED_INIT);
00088 }
00089
00090 for (; myCount != 0; --myCount)
00091 {
00092 if (PulseEvent(myCond) != 0)
00093 {
00094 ArLog::log(ArLog::Terse, "ArCondition::broadcast: Unknown error while trying to broadcast the condition.");
00095 ret=STATUS_FAILED;
00096 }
00097 }
00098
00099 return(ret);
00100 }
00101
00102 int ArCondition::wait()
00103 {
00104 DWORD ret;
00105
00106 if (myFailedInit)
00107 {
00108 ArLog::log(ArLog::Terse, "ArCondition::wait: Initialization of condition failed, failed to wait");
00109 return(STATUS_FAILED_INIT);
00110 }
00111
00112 ++myCount;
00113 ret=WaitForSingleObject(myCond, INFINITE);
00114 if (ret == WAIT_OBJECT_0)
00115 return(0);
00116 else
00117 {
00118 ArLog::logNoLock(ArLog::Terse, "ArCondition::wait: Failed to lock due to an unknown error");
00119 return(STATUS_FAILED);
00120 }
00121 }
00122
00123 int ArCondition::timedWait(unsigned int msecs)
00124 {
00125 int ret;
00126
00127 if (myFailedInit)
00128 {
00129 ArLog::log(ArLog::Terse, "ArCondition::wait: Initialization of condition failed, failed to wait");
00130 return(STATUS_FAILED_INIT);
00131 }
00132
00133 ++myCount;
00134 ret=WaitForSingleObject(myCond, msecs);
00135 if (ret == WAIT_OBJECT_0)
00136 return(0);
00137 else if (ret == WAIT_TIMEOUT)
00138 return(STATUS_WAIT_TIMEDOUT);
00139 else
00140 {
00141 ArLog::logNoLock(ArLog::Terse, "ArCondition::timedWait: Failed to lock due to an unknown error");
00142 return(STATUS_FAILED);
00143 }
00144 }
00145
00146 const char *ArCondition::getError(int messageNumber) const
00147 {
00148 ArStrMap::const_iterator it;
00149 if ((it = ourStrMap.find(messageNumber)) != ourStrMap.end())
00150 return (*it).second.c_str();
00151 else
00152 return NULL;
00153 }