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

ArCondition_WIN.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
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 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
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 }

Generated on Tue Feb 20 10:51:39 2007 for Aria by  doxygen 1.4.0