Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArCondition_WIN.cpp

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
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 }

Generated on Wed Oct 19 12:56:34 2005 for Aria by  doxygen 1.4.0