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