random_utils/src/SleepyObject.cpp

Go to the documentation of this file.
00001     
00002 #include "random_utils/SleepyObject.h"
00003 #include <iostream>
00004 
00005 namespace random_utils
00006 {
00007 
00008 SleepyObject::SleepyObject()
00009 {
00010     m_sleepRequested        = false;
00011 
00012     m_finishedUpdateCondition = SDL_CreateCond();
00013     m_finishedUpdateMutex = SDL_CreateMutex();
00014     m_wakeUpCondition = SDL_CreateCond();
00015     m_requestMutex = SDL_CreateMutex();
00016 }
00017 
00018 SleepyObject::~SleepyObject()
00019 {
00020     SDL_DestroyCond(m_finishedUpdateCondition);
00021     SDL_DestroyMutex(m_finishedUpdateMutex);
00022     SDL_DestroyCond(m_wakeUpCondition);
00023     SDL_DestroyMutex(m_requestMutex);
00024 }
00025 
00026 void
00027 SleepyObject::sleep()
00028 {
00029     if(SDL_LockMutex(m_requestMutex) == -1)
00030         std::cerr << "ERROR: cannot lock request mutex" << std::endl;
00031 
00032     m_sleepRequested = true;
00033 
00034     if(SDL_CondWait(m_finishedUpdateCondition, m_requestMutex) == -1)
00035         std::cerr << "ERROR: cannot wait for update condition" << std::endl;
00036 
00037     if(SDL_UnlockMutex(m_requestMutex) == -1)
00038         std::cerr << "ERROR: cannot unlock request mutex" << std::endl;
00039 
00040     if(SDL_LockMutex(m_finishedUpdateMutex) == -1)
00041         std::cerr << "ERROR: cannot lock update mutex" << std::endl;
00042     if(SDL_UnlockMutex(m_finishedUpdateMutex) == -1)
00043         std::cerr << "ERROR: cannot unlock update mutex" << std::endl;
00044 }
00045 
00046 void
00047 SleepyObject::runUpdate()
00048 {
00049     update();
00050 
00051     if(SDL_LockMutex(m_requestMutex) == -1)
00052         std::cerr << "ERROR: cannot lock request mutex" << std::endl;
00053 
00054     if(m_sleepRequested)
00055     {
00056         m_sleepRequested = false;
00057 
00058         if(SDL_LockMutex(m_finishedUpdateMutex) == -1)
00059             std::cerr << "ERROR: cannot lock update mutex" << std::endl;;
00060         
00061         if(SDL_CondSignal(m_finishedUpdateCondition) == -1)
00062             std::cerr << "ERROR: cannot signal update condition" << std::endl;
00063 
00064         if(SDL_UnlockMutex(m_requestMutex) == -1)
00065             std::cerr << "ERROR: cannot unlock request mutex" << std::endl;;
00066 
00067         if(SDL_CondWait(m_wakeUpCondition, m_finishedUpdateMutex) == -1)
00068             std::cerr << "ERROR: cannot wait for wakeup" << std::endl;;
00069 
00070         if(SDL_UnlockMutex(m_finishedUpdateMutex) == -1)
00071             std::cerr << "ERROR: cannot unlock update mutex" << std::endl;;
00072     }
00073     else
00074     {
00075         if(SDL_UnlockMutex(m_requestMutex) == -1)
00076             std::cerr << "ERROR: cannot unlock request mutex" << std::endl;
00077     }
00078 }
00079 
00080 void
00081 SleepyObject::wakeUp()
00082 {
00083     if(SDL_CondSignal(m_wakeUpCondition) == -1)
00084         std::cerr << "ERROR: cannot signal wake up" << std::endl;
00085 }
00086 
00087 bool
00088 SleepyObject::sleepRequested()
00089 {
00090     bool ret;
00091     
00092     if(SDL_LockMutex(m_requestMutex) == -1)
00093         std::cerr << "ERROR: cannot lock request mutex" << std::endl;
00094 
00095     ret = m_sleepRequested;
00096     
00097     if(SDL_UnlockMutex(m_requestMutex) == -1)
00098         std::cerr << "ERROR: cannot unlock request mutex" << std::endl;
00099 
00100     return ret;
00101 }
00102 
00103 }

Generated on Fri May 11 21:01:58 2007 for Random Racer by  doxygen 1.5.1